始める無料で始める

text-to-Cypher チェーンを構築する

これまでに自分で Cypher クエリを書いてきましたが、今度は LLM を使って自動生成してみましょう。自然言語のユーザークエリから Cypher クエリを生成することは、text-to-Cypher の Graph RAG ワークフローの重要なステップです。

前の演習と同じデータベースを使います。人、企業を表すノードと、WORKS_FOR リレーションシップがあり、graph として利用できます。

この演習はコースの一部です

Graph RAG with LangChain and Neo4j

コースを見る

演習の手順

  • partial_variables 辞書を更新して、グラフのスキーマをプロンプトに挿入します。
  • promptllm を使い、文字列にパースして text-to-Cypher のチェーンを作成します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template(""" 
		You are an expert Neo4j developer. Use the following database schema to write a Cypher statement to answer the user's question. Only generate the Cypher statement, no pre-amble. Do not return any Markdown.
	Schema: 
    {schema}
    
	Question: {question}""", 
	# Update the partial_variables dictionary
    partial_variables={"schema": graph.____})
])

# Create the text-to-Cypher chain
text_to_cypher_chain = ____ | ____ | StrOutputParser()
cypher = text_to_cypher_chain.invoke({"question": "Where does Jo Cornelissen work?"})
print(cypher)
コードを編集して実行