Get startedGet started for free

Improving graph retrieval

1. Improving graph retrieval

Let's now look at a few techniques to improve our Graph RAG architecture.

2. Techniques

Since large language models are non-deterministic by nature, the main limitation of our approach is the reliability of the user query to Cypher query translation. We will take a look at a few techniques to improve this process, including: filtering the graph, validating the Cypher query, and few-shot prompting.

3. Filtering

One way to improve the reliability of our Cypher queries is to filter down the search space. Within the .from_llm() method, we can use the exclude_types argument to specify nodes with given types to exclude from the database. Here, we remove the nodes with the Concept property. Removing unneeded node properties from the schema so the model can focus on a specific subset often improves the quality of the generated Cypher statements.

4. Validating the Cypher query

One thing LLMs have particular difficulty with is inferring the direction of relationships when generating Cypher statements. Because the graph schema is predefined when instantiating the chain, after the Cypher is generated, we can validate the query against the graph's schema. The validate_cypher argument inside the GraphCypherQAChain can perform this check, and if necessary, correct the relationship directions in these generated statements. The cypher validation detects the nodes, and their properties and relationships, determines the direction of a given relationship in the Cypher query, verifies if the node labels and relationship types match the provided schema, and finally, updates the direction of relationships based on the schema.

5. Few-shot prompting

Another great technique to improve the Cypher query generation is to provide the model with a few examples of user questions and their corresponding Cypher query. Few-shot prompting improves model performance on more complex queries, and it allows us to include more relevant examples for our use case. To set these examples up, create a list of dictionaries with "question" and "query" key:value pairs.

6. Implementing few-shot prompting

We can turn these examples into a prompt template using LangChain's prompt template classes. We create an example prompt to dictate the structure of each example, and pass this example prompt to the example_prompt argument of FewShotPromptTemplate, along with the examples list of dictionaries. We also include a prefix message to guide the model on the task at-hand, and a suffix message to clarity the desired output structure.

7. Complete prompt

Here is how the complete prompt looks before an input is inserted and it gets sent to the LLM.

8. Adding the few-shot examples

This prompt template can be passed to the cypher_prompt argument of the graph chain to overwrite the default template used by the class in the generate Cypher chain.

9. Let's practice!

Now that you have a set of tools to improve your Graph RAG systems, it's time to try them out!