텍스트를 Cypher로 변환하는 체인 만들기
직접 Cypher 쿼리를 작성해 보셨으니, 이제 LLM으로 생성해 보겠습니다! 자연어 사용자 질의로부터 Cypher 쿼리를 만드는 것은 text-to-Cypher Graph RAG 워크플로의 핵심 단계예요.
이전 연습 문제와 같은 데이터베이스를 사용하며, 사람, 회사 노드와 WORKS_FOR 관계가 있고, graph로 제공돼요.
이 연습은 강의의 일부입니다
LangChain과 Neo4j로 배우는 Graph RAG
연습 안내
partial_variables딕셔너리를 업데이트해 그래프 스키마를 프롬프트에 삽입하세요.prompt,llm을 사용하고 문자열로 파싱하도록 설정해 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)