ComeçarComece de graça

Text-to-Cypher retrieval chain

You now have a chain capable of converting natural language into a Cypher statement. Let's use it in a larger chain to execute this LLM-generated Cypher and answer user questions with the retrieved information.

Este exercício faz parte do curso

Graph RAG with LangChain and Neo4j

Ver curso

Instruções do exercício

  • Update the code to execute the Cypher statement generated by the text_to_cypher_chain and assign it to the "context" variable used in QA_PROMPT.
  • Pass through the input to the "question" variable unchanged.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

qa_prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template(QA_PROMPT),
    HumanMessagePromptTemplate.from_template("Question: {question}")
])

qa_chain = {
  	# Use output of text_to_cypher to get database results
    "context": text_to_cypher_chain | ____(lambda cypher: graph.____(cypher)),
    # Pass the user input through to the "question" variable
    "question": ____
} | qa_prompt | llm | StrOutputParser()

res = qa_chain.invoke({"question": "What companies is Harrison Chase connected to?"})
print(res)
Editar e executar o código