The Good Tech Companies - Unlocking Precision in RAG Applications: Harnessing Knowledge Graphs with Neo4j and LangChain

Episode Date: October 21, 2024

This story was originally published on HackerNoon at: https://hackernoon.com/unlocking-precision-in-rag-applications-harnessing-knowledge-graphs-with-neo4j-and-langchain. ... Explore a practical guide for constructing and retrieving information from knowledge graphs in RAG applications with Neo4j and LangChain. Check more stories related to machine-learning at: https://hackernoon.com/c/machine-learning. You can also check exclusive content about #retrieval-augmented-generation, #knowledge-graph, #graphrag, #rag-applications, #neo4j, #llmgraphtransformer, #langchain, #good-company, and more. This story was written by: @neo4j. Learn more about this writer by checking @neo4j's about page, and for more stories, please visit hackernoon.com. This blog post shows how to create a knowledge graph using LangChain. The code is available on GitHub. You need to set up a Neo4j instance. For this demonstration, we will use [Elizabeth I’s] Wikipedia page. We can use [LangChain loaders] to fetch and split the documents from Wikipedia.

Transcript
Discussion (0)
Starting point is 00:00:00 This audio is presented by Hacker Noon, where anyone can learn anything about any technology. Unlocking precision in RAG applications, harnessing knowledge graphs with Neo4j and Langchain by Neo4j. Graph Retrieval Augmented Generation, GraphRAG, is gaining momentum and becoming a powerful addition to traditional vector search retrieval methods. This approach leverages the structured nature of graph databases, which organize data's nodes and relationships to enhance the depth and contextuality of retrieved information. Graphs are great at representing and storing heterogeneous and interconnected information in a structured manner, effortlessly capturing complex relationships and attributes across diverse data types. In contrast, vector databases often struggle with such structured
Starting point is 00:00:45 information, as their strength lies in handling unstructured data through high-dimensional vectors. In your RAG application, you can combine structured graph data with vector search through unstructured texto achieve the best of both worlds. That's what we will demonstrate in this blog post. Knowledge graphs are great, but how do you create one? Constructing a knowledge graph is typically the most challenging step. IT involves gathering and structuring the data, which requires a deep understanding of both the domain and graph modeling. To simplify this process, we have been experimenting with LLMs. With their profound understanding of language and context, LLMs can automate significant parts of the knowledge graph creation process. By analyzing text data, these models can identify entities, understand their relationships, and suggest how they might be best represented in
Starting point is 00:01:34 a graph structure. As a result of these experiments, we have added the first version of the graph construction module to Langchain, which we will demonstrate in this blog post. The code is available on GitHub. Neo4j environment setup you need to set up a Neo4j instance. Follow along with the examples in this blog post. The easiest way is to start a free instance on Neo4j Aura, which offers cloud instances of the Neo4j database. Alternatively, you can also set up a local instance of the Neo4j database by downloading the Neo4j desktop application and creating a local database instance. Additionally, you must provide an OpenAI key, as we will use their models in this blog post. Data ingestion for this
Starting point is 00:02:18 demonstration, we will use Elizabeth I's Wikipedia page. We can use langchain loaders to fetch and split the documents from Wikipedia seamlessly. It's use lang chain loaders to fetch and split the documents from Wikipedia seamlessly. It's time to construct a graph based on the retrieved documents. For this purpose, we have implemented an LLM graph transformer module that significantly simplifies constructing and storing a knowledge graph in a graph database. You can define which LLM you want the knowledge graph generation chain to use. Currently, we support only function-calling models from OpenAI and Mistral. However, we plan to expand the LLM selection in the future.
Starting point is 00:02:53 In this example, we're using the latest GPT-4. Note that the quality of the generated graph significantly depends on the model you are using. In theory, you always want to house the most capable one. The LLM Graph Transformers return graph documents, which can be imported to Neo4j via the add underscore graph underscore documents method. The base entity label parameter assigns an additional entity label to each node, enhancing indexing and query performance. The include underscore source parameter links nodes to their originating documents, facilitating data traceability and context performance. The include underscore source parameter links nodes to their originating
Starting point is 00:03:25 documents, facilitating data traceability and context understanding. You can inspect the generated graph in the Neo4j browser. Note that this image represents only a part of the generated graph. Hybrid retrieval for RAG after the graph generation. We will use a hybrid retrieval approach that combines vector and keyword indexes with graph retrieval for RAG applications. The diagram illustrates a retrieval process beginning with a user posing a question, which is then directed to an RAG retriever. This retriever employs keyword and vector searches to search through unstructured text data and combines it with the information it collects from the knowledge graph. Since Neo4j
Starting point is 00:04:05 features both keyword and vector indexes, you can implement all three retrieval options with a single database system. The collected data from these sources is fed into an LLM to generate and deliver the final answer. Unstructured data retriever you can use the Neo4j vector from underscore existing underscore graph method to add both keyword and vector retrieval to documents. This method configures keyword and vector search indexes for a hybrid search approach targeting nodes labeled document. Additionally, it calculates text embedding values if they are missing. The vector index can then be called with the similarity underscore search method. Graph retriever on the other hand, configuring a graph
Starting point is 00:04:45 retrieval is more involved but offers more freedom. This example will use a full text index to identify relevant nodes and return their direct neighborhood. The graph retriever starts by identifying relevant entities in the input. For simplicity, we instruct the LLM to identify people, organizations, and locations. To achieve this, we will use LCL with the newly added with underscore structured underscore output method to achieve this. Let's test it out great. Now that we can detect entities in the question, let's use a full text index to map them to the knowledge graph. First, we need to define a full text index and a function that will generate full text queries that allow a bit of misspelling, which we won't go into much detail here. Let's put it all together
Starting point is 00:05:29 now. The structured underscore retriever function starts by detecting entities in the user question. Next, it iterates over the detected entities and uses a cipher template to retrieve the neighborhood of relevant nodes. Let's test it out. Final RETRIVERAs mentioned at the start, we'll combine the unstructured and graph retriever to create the final context passed to an LLM. As we are dealing with Python, we can simply concatenate the outputs using theft string. Defining the RAG chain we have successfully implemented the retrieval component of the RAG. Next, we introduce a prompt that leverages the context provided by the integrated hybrid retriever to produce the response, completing the implementation of the RAG chain.
Starting point is 00:06:12 Finally, we can go ahead and test our hybrid RAG implementation. I've also incorporated a query rewriting feature, enabling the RAG chain to adapt to conversational settings that allow follow-up questions. Given that WUS vector and keyword search methods, we that allow follow-up questions. Given that Woos vector and keyword search methods, we must rewrite follow-up questions to optimize our search process. You can observe that when was she born? Was first rewritten to when was Elizabeth I born? The rewritten query was then used to retrieve relevant context and answer the question. Knowledge graphs made easy with the introduction of the LLM Graph Transformer.
Starting point is 00:06:45 The process of generating knowledge graphs should now be smoother and more accessible, making it easier for anyone looking to enhance their RAG applications with the depth and context that knowledge graphs provide. This is just a start as we have a lot of improvements planned. If you have insights, suggestions, or questions about our generating graphs with LLMs, please don't hesitate to reach out. The code is available on GitHub. Thank you for listening to this HackerNoon story, read by Artificial Intelligence. Visit HackerNoon.com to read, write, learn and publish.

There aren't comments yet for this episode. Click on any sentence in the transcript to leave a comment.