Retrieval Augmented Generation (RAG) with LangChain
Meri Nova
Machine Learning Engineer
graph.refresh_schema()
print(graph.get_schema)
Node properties:
Document {title: STRING, id: STRING, text: STRING, summary: STRING, source: STRING}
Concept {id: STRING}
Organization {id: STRING}
Relationship properties:
The relationships:
(:Document)-[:MENTIONS]->(:Organization)
(:Concept)-[:DEVELOPED_BY]->(:Person)
from langchain_community.chains.graph_qa.cypher import GraphCypherQAChain
chain = GraphCypherQAChain.from_llm( llm=ChatOpenAI(api_key="...", temperature=0), graph=graph, verbose=True )
result = chain.invoke({"query": "What is the most accurate model?"})
print(f"Final answer: {result['result']}")
> Entering new GraphCypherQAChain chain... Generated Cypher: MATCH (m:Model) RETURN m ORDER BY m.accuracy DESC LIMIT 1; Full Context: [{'m': {'id': 'Artificial Neural Networks'}}] > Finished chain.
Final answer: Artificial Neural Networks
chain = GraphCypherQAChain.from_llm(
llm=ChatOpenAI(api_key="...", temperature=0), graph=graph, verbose=True
)
qa_prompt
: Prompt template for result generationcypher_prompt
: Prompt template for Cypher generationcypher_llm
: LLM for Cypher generationqa_llm
: LLM for result generationRetrieval Augmented Generation (RAG) with LangChain