Enhance Precision with RAG: Hybrid Retrieval & Knowledge Graphs

In the rapidly evolving landscape of artificial intelligence and natural language processing, the integration of Retrieval-Augmented Generation (RAG) with hybrid dense retrieval techniques and knowledge graphs (KG) is proving to be a game changer. This innovative approach not only boosts the accuracy of responses in chat applications but also enhances the overall user experience. In 2024, as AI technologies continue to mature, understanding these advancements is crucial for developers and researchers alike.

The Role of HuixiangDou: A Knowledge Assistant for Group Chats

HuixiangDou operates as a sophisticated knowledge assistant designed specifically for group chat environments. In these dynamic settings, where multiple conversations occur simultaneously, it is vital for the assistant to provide relevant and accurate responses without overwhelming users. The guiding principles of HuixiangDou are as follows:

  • Silence on Irrelevant Content: The assistant refrains from responding to unrelated messages, ensuring that users are not distracted by unnecessary information.
  • Direct Responses to Relevant Queries: When a question is clear and pertinent, HuixiangDou retrieves and delivers the necessary information promptly.
  • Adherence to Core Values: Responses must align with fundamental principles of reliability and trustworthiness, reinforcing user confidence in the assistant’s capabilities.

In previous evaluations, HuixiangDou achieved a rejection F1 score of 75.88 by leveraging real group chat data. This article explores how the integration of knowledge graphs with dense retrieval methods can elevate this score to an impressive 77.57, showcasing the potential for even greater accuracy in AI-driven responses.

Comparative Analysis of Methods: A Closer Look

The effectiveness of various methods in enhancing retrieval accuracy can be summarized in the following table:

MethodF1 ScoreNotes
BCE+KG Hybrid (This Article)77.57KG weight approximately 20%
BCE75.88Requires specific splitter
BGE72.23Utilizes bge-large-zh-v1.5
BGE-M370.62Token limit of 8192 restricts evaluation
M3 Dense + Sparse Hybrid63.85Performance declines with higher sparse ratio

The hybrid method proposed in this article enhances dense retrieval by assigning weights to high-frequency terms during the retrieval process. This approach is not only straightforward but also highly effective, as it requires only a few hundred lines of code and is fully compatible with previous versions of the system.

Key Benefits of the Hybrid Approach

  • Simplicity: The implementation is streamlined, allowing for easy integration and updates.
  • Reliability: Extensive testing has shown that with appropriate parameters, consistent improvements in accuracy can be achieved, making this method robust for real-world applications.
  • Cost-Effectiveness: Even without multiple rounds of LLM processing, accuracy can be significantly enhanced. The current implementation employs two rounds of Named Entity Recognition (NER) to extract entities from the knowledge base, optimizing both performance and cost.

Understanding Key Terminology

To fully grasp the implications of this hybrid approach, it is essential to understand some key terms:

  • Knowledge Graph (KG): A structured repository of knowledge that organizes entities, attributes, relationships, and types in a graph format. KGs enhance the interpretability of AI systems by providing a clear representation of data relationships.
  • Named Entity Recognition (NER): The process of identifying meaningful entities in natural language, such as names, dates, and locations. This technique is crucial for extracting relevant information from unstructured data.
  • Dense Retrieval: An unstructured method that uses models to extract features from texts, images, or audio, calculating distances between features to match targets. This approach is commonly utilized in applications like facial recognition and document retrieval.
  • NetworkX: An open-source library in Python for graph theory and complex network analysis, providing data structures and algorithms to create and analyze complex networks.
  • Neo4j: A mature graph database management system that uses graphs to store and query data, contrasting with traditional relational databases that utilize tables and columns.
  • Milvus: An open-source vector database designed for storing, searching, and analyzing large volumes of vector data, particularly useful in AI applications.

Methodology Explanation: The Integration of Knowledge Graphs

Why Knowledge Graphs Are Essential for RAG

The integration of knowledge graphs into Retrieval-Augmented Generation (RAG) is pivotal for several reasons:

  • Enhancing System Interpretability: The high-dimensional space used in dense retrieval can often be opaque. KGs provide a structured way to interpret relationships between data points, making the system more transparent.
  • Ensuring Hierarchical Relationships Among Terms: In specialized fields, such as hybrid rice research, it is crucial for both dense and sparse methods to accurately represent parent-child relationships among terms. KGs facilitate this representation, improving the quality of responses.
  • Non-Intrusive Integration: The incorporation of KGs should not significantly disrupt existing services or compromise accuracy. This hybrid approach allows for seamless integration with existing systems.

Building the Knowledge Base

To establish the knowledge base, this article employs the qwen1.5-110B model for NER, utilizing the Silicon Cloud API to minimize costs. The knowledge base comprises nine algorithm libraries related to OpenMMLab, a leading open-source project in the field.

Building the knowledge base requires approximately 14 million tokens, with a single-threaded process taking over 12 hours and costing around 50 yuan. The command to initiate this process is as follows:

python3 -m huixiangdou.service.kg --build

Once the knowledge base is established, users can test the retrieval functionality by querying specific questions, such as how to install MMPose:

python3 -m huixiangdou.service.kg --query 如何安装mmpose?

To accommodate potential issues like API outages or network disruptions, the system records completed files, allowing for resumption of the process.

Visualizing the Knowledge Graph

In HuixiangDou, the knowledge graph is stored in JSONL format, with calculations performed using NetworkX. To leverage Neo4j’s visualization tools, we support converting JSONL to Neo4j format, which allows for comprehensive analysis of the relationships within the data.

python3 -m huixiangdou.service.kg --dump-neo4j --neo4j-uri ${URI} --neo4j-user ${USER} --neo4j-passwd ${PWD}

With approximately 300,000 nodes and relationships, remote communication is expected to take around 4 hours, providing a rich visual representation of the knowledge graph’s structure.

Direct Retrieval Testing: Enhancing Accuracy

The retrieval process mirrors the knowledge base construction, beginning with LLM extraction of entity terms to obtain matching candidate documents. The scoring mechanism is designed to ensure that only relevant documents are considered:

$$
{score} = min(100, {count(docs)}) / 100
$$

This scoring system allows for dynamic adjustments based on the number of candidate documents retrieved. For instance, if a user query retrieves more than five candidate documents, the assistant continues processing the input rather than declining.

Hybrid Retrieval Testing: Balancing Precision and Recall

While conservatism in retrieval may seem limiting, it enhances reliability. This conservative trait is suitable for computing positive values in the range [0, +1], amplifying the variance in previous distributions.

The hybrid retrieval method proposed here can be summarized as a simple “bonus” system:

$$
{final_score} = {dense_score} + 0.2 times {kg_score}
$$

This approach allows for a reset of the query threshold without altering the dense retrieval code, enabling KGs to function as a toggle option, fully compatible with older feature sets.

Conclusion: The Future of AI-Driven Responses

The integration of knowledge graphs with hybrid dense retrieval represents a significant advancement in the field of AI and natural language processing. By weighting high-frequency terms during retrieval, this approach enhances precision and reliability, making AI-driven responses more accurate and contextually relevant.

As we move further into 2024, the potential for these technologies to transform user interactions in chat applications and beyond is immense. The current implementation, while effective, still has room for improvement, particularly in supporting various formats and optimizing speed. The full potential of KG-LLM integration remains to be fully realized, promising exciting developments in the future of AI-driven communication.

Categories: AI Tools Guide
X