Створення гібридного ланцюга пошуку

Graph RAG з LangChain і Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Поєднання методів пошуку

поєднаний_пошук1.jpg

Graph RAG з LangChain і Neo4j

Поєднання методів пошуку

поєднаний_пошук2.jpg

Graph RAG з LangChain і Neo4j

Робимо функції виконуваними

from langchain_core.runnables import RunnableLambda


double_chain = RunnableLambda( lambda input: input["input"] * 2 )
double_chain.invoke({"input": 2})
4
Graph RAG з LangChain і Neo4j

Проміжні ланцюги-перенаправники (passthrough)

from langchain_core.runnables import RunnablePassthrough


double_passthrough = RunnablePassthrough.assign( doubled=RunnableLambda(lambda x: x["input"] * 2) )
double_passthrough.invoke({"input": 2})
{"input": 2, "doubled": 4}
Graph RAG з LangChain і Neo4j

Створення підказки для Q&A

graphrag_qa_prompt = ChatPromptTemplate.from_messages([

SystemMessagePromptTemplate.from_template(""" You are a helpful assistant answering questions about the play Romeo and Juliet. You are given a question and a context. Question: {input} """),
Graph RAG з LangChain і Neo4j

Створення підказки для Q&A

    ...
    SystemMessagePromptTemplate.from_template("""
    The following context has been retrieved from the database using vector search to help
    you answer the question: 

    {vectors}
    """),
Graph RAG з LangChain і Neo4j

Створення підказки для Q&A

    ...
    SystemMessagePromptTemplate.from_template("""
    The following data has been retrieved from the knowledge graph using Cypher to 
    answer the question.  

    {records}

    You can treat any information contained from the knowledge graph as authoritative.

    If the information does not exist in this answer, fall back to vector search results.

    If the answer is not included in either just say that you don't know and don't rely
    on pre-existing knowledge."""),
Graph RAG з LangChain і Neo4j

Створення ланцюга Q&A

# line_retriever = Neo4jVector().as_retriever()
# text_to_cypher_chain = text_to_cypher_prompt | llm | StrOutputParser()
# graph = Neo4jGraph()


graphrag_qa_chain = RunnablePassthrough.assign(
vectors=RunnableLambda(lambda x: line_retriever.invoke(x["input"])),
records=text_to_cypher_chain | graph.query
)
| graphrag_qa_prompt
| llm
| StrOutputParser()
Graph RAG з LangChain і Neo4j

Виклик ланцюга Q&A

graphrag_qa_chain.invoke({"input": "Who is Romeo's best friend?"})
Romeo's best friends are Mercutio and Benvolio.
Graph RAG з LangChain і Neo4j

Давайте потренуємось!

Graph RAG з LangChain і Neo4j

Preparing Video For Download...