Retrieval Augmented Generation (RAG) avec LangChain
Meri Nova
Machine Learning Engineer
Limite principale : fiabilité de la traduction utilisateur → Cypher
Stratégies pour améliorer la recherche dans le graphe :
from langchain_community.chains.graph_qa.cypher import GraphCypherQAChain llm = ChatOpenAI(api_key="...", model="gpt-4o-mini", temperature=0)chain = GraphCypherQAChain.from_llm(graph=graph, llm=llm, exclude_types=["Concept"], verbose=True)print(graph.get_schema)
Propriétés de nœud :
Document {title: STRING, id: STRING, text: STRING, summary: STRING, source: STRING}
Organization {id: STRING}
chain = GraphCypherQAChain.from_llm(
graph=graph, llm=llm, verbose=True, validate_cypher=True
)
examples = [
{
"question": "How many notable large language models are mentioned in the article?",
"query": "MATCH (m:Concept {id: 'Large Language Model'}) RETURN count(DISTINCT m)",
},
{
"question": "Which companies or organizations have developed the large language models mentioned?",
"query": "MATCH (o:Organization)-[:DEVELOPS]->(m:Concept {id: 'Large Language Model'}) RETURN DISTINCT o.id",
},
{
"question": "What is the largest model size mentioned in the article, in terms of number of parameters?",
"query": "MATCH (m:Concept {id: 'Large Language Model'}) RETURN max(m.parameters) AS largest_model",
},
]
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplateexample_prompt = PromptTemplate.from_template( "User input: {question}\nCypher query: {query}" )cypher_prompt = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, prefix="Vous êtes un expert Neo4j. À partir d’une question en entrée, créez une requête Cypher syntaxiquement correcte à exécuter.\n\nVoici les informations de schéma\n{schema}.\n\n Ci-dessous, des exemples de questions et leurs requêtes Cypher correspondantes.", suffix="User input: {question}\nCypher query: ", input_variables=["question"], )
Vous êtes un expert Neo4j. À partir d’une question en entrée, créez une requête Cypher syntaxiquement correcte à exécuter.
Ci-dessous, des exemples de questions et leurs requêtes Cypher correspondantes.
Saisie utilisateur : How many notable large language models are mentioned in the article?
Requête Cypher : MATCH (p:Paper) RETURN count(DISTINCT p)
Saisie utilisateur : Which companies or organizations have developed the large language models?
Requête Cypher : MATCH (o:Organization)-[:DEVELOPS]->(m:Concept {id: 'Large Language Model'}) RETURN DISTINCT o.id
Saisie utilisateur : What is the largest model size mentioned in the article, in terms of number of parameters?
Requête Cypher : MATCH (m:Concept {id: 'Large Language Model'}) RETURN max(m.parameters) AS largest_model
Saisie utilisateur : How many papers were published in 2016?
Requête Cypher :
chain = GraphCypherQAChain.from_llm(
graph=graph, llm=llm, cypher_prompt=cypher_prompt,
verbose=True, validate_cypher=True
)
Retrieval Augmented Generation (RAG) avec LangChain