Vytvoření Graph RAG řetězce

Retrieval Augmented Generation (RAG) with LangChain

Meri Nova

Machine Learning Engineer

Sestavení architektury Graph RAG

Grafová databáze.

Retrieval Augmented Generation (RAG) with LangChain

Sestavení architektury Graph RAG

Dokumenty převedené na grafové dokumenty a uložené v grafové databázi.

Retrieval Augmented Generation (RAG) with LangChain

Sestavení architektury Graph RAG

Cypher dotaz se používá k načtení grafových dokumentů z databáze. Je zobrazen také uživatelský vstup a odpověď uživateli v přirozeném jazyce.

Retrieval Augmented Generation (RAG) with LangChain

Sestavení architektury Graph RAG

Překladač z přirozeného jazyka na Cypher převádí vstup v přirozeném jazyce na Cypher dotaz a načtené grafové dokumenty zpět do přirozeného jazyka.

Retrieval Augmented Generation (RAG) with LangChain

Od uživatelského vstupu k Cypher dotazům

Jednoduchý graf obsahující tři uzly a tři vztahy. Dva uzly představují osoby a další představuje místo, které James navštívil.

Retrieval Augmented Generation (RAG) with LangChain

Od uživatelského vstupu k Cypher dotazům

Uživatelský vstup s dotazem „Where has James visited?“.

Retrieval Augmented Generation (RAG) with LangChain

Od uživatelského vstupu k Cypher dotazům

Cypher dotaz se generuje z grafového schématu a uživatelského vstupu.

Retrieval Augmented Generation (RAG) with LangChain

GraphCypherQAChain

Architektura Graph RAG pro překlad uživatelských vstupů na Cypher dotazy a vrácení odpovědi v přirozeném jazyce.

Retrieval Augmented Generation (RAG) with LangChain

GraphCypherQAChain

Architektura Graph RAG se zvýrazněným řetězcem pro generování cypher a řetězcem pro sumarizaci výsledků.

Retrieval Augmented Generation (RAG) with LangChain

Aktualizace schématu

graph.refresh_schema()
print(graph.get_schema)
Node properties:
Document {title: STRING, id: STRING, text: STRING, summary: STRING, source: STRING}
Concept {id: STRING}
Organization {id: STRING}
Relationship properties:

The relationships:
(:Document)-[:MENTIONS]->(:Organization)
(:Concept)-[:DEVELOPED_BY]->(:Person)
Retrieval Augmented Generation (RAG) with LangChain

Dotazování grafu

from langchain_community.chains.graph_qa.cypher import GraphCypherQAChain

chain = GraphCypherQAChain.from_llm( llm=ChatOpenAI(api_key="...", temperature=0), graph=graph, verbose=True )
result = chain.invoke({"query": "What is the most accurate model?"})
1 https://api.python.langchain.com/en/latest/chains/langchain_community.chains.graph_qa.cypher. GraphCypherQAChain.html
Retrieval Augmented Generation (RAG) with LangChain

Dotazování grafu

print(f"Final answer: {result['result']}")
> Entering new GraphCypherQAChain chain...
Generated Cypher:
MATCH (m:Model)
RETURN m
ORDER BY m.accuracy DESC
LIMIT 1;
Full Context:
[{'m': {'id': 'Artificial Neural Networks'}}]

> Finished chain.


Final answer: Artificial Neural Networks
Retrieval Augmented Generation (RAG) with LangChain

Přizpůsobení

chain = GraphCypherQAChain.from_llm(
    llm=ChatOpenAI(api_key="...", temperature=0), graph=graph, verbose=True
)
  • qa_prompt: Šablona promptu pro generování výsledku
  • cypher_prompt: Šablona promptu pro generování Cypher
  • cypher_llm: LLM pro generování Cypher
  • qa_llm: LLM pro generování výsledku
Retrieval Augmented Generation (RAG) with LangChain

Pojďme cvičit!

Retrieval Augmented Generation (RAG) with LangChain

Preparing Video For Download...