Text-to-Cypher 検索チェーン
自然言語を Cypher ステートメントに変換できるチェーンが用意できました。これをより大きなチェーンに組み込み、LLM が生成した Cypher を実行して、取得した情報でユーザーの質問に答えましょう。
この演習はコースの一部です
Graph RAG with LangChain and Neo4j
演習の手順
text_to_cypher_chainが生成した Cypher ステートメントを実行し、その結果をQA_PROMPTで使用する"context"変数に代入するようにコードを更新してください。- 入力は変更せずに、そのまま
"question"変数へ渡してください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
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)