Methods for detection and filtering of contradictory context in RAG systems based on large language models
DOI: 10.31673/2412-9070.2025.044016
DOI:
https://doi.org/10.31673/2412-9070.2025.044016Abstract
Optimizing queries for large language models is a crucial component of modern generative artificial intelligence, which is widely applied in tasks such as text analysis, knowledge generation, decision support, machine translation, advisory systems, and other natural language processing domains where precision and logical consistency of results are vital. The quality of LLM responses depends not only on the model’s architecture but also on how the input query is formulated, whether it follows the expected structure, and whether it is provided with adequate and accurate context. To enrich model input with external data, the Retrieval-Augmented Generation (RAG) approach is employed, allowing the extraction of the most relevant fragments from external sources to supplement the user query and submit them as part of the LLM prompt. This significantly improves the effectiveness of answer generation, especially when the model lacks prior knowledge of a specific domain or time-sensitive data. However, it also introduces new challenges related to inconsistent, outdated, or logically contradictory information within the retrieved fragments. These issues can degrade output quality, lead to logical incoherence, or even introduce misinformation. To address these challenges, a methodology is proposed for post-retrieval context filtering before the generation stage, based on ensuring logical consistency between each fragment and the initial query. The filtering process involves identifying implicit or explicit contradictions between the query and external data, excluding incompatible segments from the final prompt. This significantly enhances logical coherence and reduces hallucination risk without retraining the base LLM. Practically, this requires evaluating not just semantic similarity but also logical compatibility, which implies adding a layer of internal logical analysis that can be implemented using secondary LLMs or heuristic rules. Another important aspect is adapting the query to the specific model architecture, taking into account its sensitivity to context length, instruction format, syntax, style, and internal understanding mechanisms. It is especially crucial for instruction-tuned models like GPT, Claude, or Mistral, which show significant differences in behavior depending on how the prompt is phrased. Also essential are multi-level verification strategies, such as incorporating chain-of-thought reasoning, meta-instructions, and leveraging external knowledge bases or knowledge graphs. These mechanisms boost the reliability of RAG systems in complex information environments. Overall, the proposed approach improves both the quality and trustworthiness of generated responses and their consistency across repeated usage, which is critical in professional, medical, legal, and scientific applications. Further research should focus on integrating logical semantic filtering into RAG pipelines and advancing dynamic query formation strategies tailored to domain, task, and user context to maximize response alignment with expectations while preserving high performance and explainability.
Keywords: generative artificial intelligence; RAG; logical filtering; context consistency; semantic search.