Criando a cadeia Graph RAG

Retrieval Augmented Generation (RAG) com LangChain

Meri Nova

Machine Learning Engineer

Construindo a arquitetura Graph RAG

Um banco de dados de grafos.

Retrieval Augmented Generation (RAG) com LangChain

Construindo a arquitetura Graph RAG

Documentos sendo transformados em documentos de grafo e armazenados no banco de dados de grafos.

Retrieval Augmented Generation (RAG) com LangChain

Construindo a arquitetura Graph RAG

Uma consulta Cypher é usada para recuperar documentos de grafo do banco de dados. Também há uma entrada do usuário e uma resposta em linguagem natural de volta ao usuário.

Retrieval Augmented Generation (RAG) com LangChain

Construindo a arquitetura Graph RAG

Um tradutor de linguagem natural para Cypher converte a entrada em linguagem natural em uma consulta Cypher e os documentos de grafo recuperados de volta para linguagem natural.

Retrieval Augmented Generation (RAG) com LangChain

De entradas do usuário a consultas Cypher

Um grafo simples com três nós e três relacionamentos. Dois nós representam pessoas e outro representa um local que James visitou.

Retrieval Augmented Generation (RAG) com LangChain

De entradas do usuário a consultas Cypher

Uma entrada do usuário perguntando "Onde o James visitou?".

Retrieval Augmented Generation (RAG) com LangChain

De entradas do usuário a consultas Cypher

A consulta Cypher é gerada usando o esquema do grafo e a entrada do usuário.

Retrieval Augmented Generation (RAG) com LangChain

GraphCypherQAChain

A arquitetura Graph RAG traduz entradas do usuário em consultas Cypher e retorna uma resposta em linguagem natural.

Retrieval Augmented Generation (RAG) com LangChain

GraphCypherQAChain

A arquitetura Graph RAG com a cadeia de geração de Cypher e a cadeia de resumo de resultados destacadas.

Retrieval Augmented Generation (RAG) com LangChain

Atualizar esquema

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) com LangChain

Consultando o grafo

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) com LangChain

Consultando o grafo

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) com LangChain

Personalização

chain = GraphCypherQAChain.from_llm(
    llm=ChatOpenAI(api_key="...", temperature=0), graph=graph, verbose=True
)
  • qa_prompt: Template de prompt para gerar respostas
  • cypher_prompt: Template de prompt para gerar Cypher
  • cypher_llm: LLM para gerar Cypher
  • qa_llm: LLM para gerar respostas
Retrieval Augmented Generation (RAG) com LangChain

Vamos praticar!

Retrieval Augmented Generation (RAG) com LangChain

Preparing Video For Download...