Graf alma iyileştirme

LangChain ile Retrieval Augmented Generation (RAG)

Meri Nova

Machine Learning Engineer

Teknikler

Temel sınırlama: kullanıcıdan Cypher’a çevirinin güvenilirliği

Graf alma sistemini iyileştirme stratejileri:

  • Grafik Şemasını filtreleme
  • Cypher sorgusunu doğrulama
  • Az örnekli istemleme
LangChain ile Retrieval Augmented Generation (RAG)

Filtreleme

from langchain_community.chains.graph_qa.cypher import GraphCypherQAChain

llm = ChatOpenAI(api_key="...", model="gpt-4o-mini", temperature=0)

chain = GraphCypherQAChain.from_llm(
graph=graph, llm=llm, exclude_types=["Concept"], verbose=True
)
print(graph.get_schema)
Düğüm özellikleri:
Document {title: STRING, id: STRING, text: STRING, summary: STRING, source: STRING}
Organization {id: STRING}
LangChain ile Retrieval Augmented Generation (RAG)

Cypher sorgusunu doğrulama

  • İlişki yönünü yorumlama zorluğu
chain = GraphCypherQAChain.from_llm(
    graph=graph, llm=llm, verbose=True, validate_cypher=True
)
  1. Düğümleri ve ilişkileri saptar
  2. İlişki yönlerini belirler
  3. Grafik şemasını kontrol eder
  4. İlişki yönlerini günceller
LangChain ile Retrieval Augmented Generation (RAG)

Az örnekli istemleme

examples = [
    {
        "question": "How many notable large language models are mentioned in the article?",
        "query": "MATCH (m:Concept {id: 'Large Language Model'}) RETURN count(DISTINCT m)",
    },
    {
        "question": "Which companies or organizations have developed the large language models mentioned?",
        "query": "MATCH (o:Organization)-[:DEVELOPS]->(m:Concept {id: 'Large Language Model'}) RETURN DISTINCT o.id",
    },
    {
        "question": "What is the largest model size mentioned in the article, in terms of number of parameters?",
        "query": "MATCH (m:Concept {id: 'Large Language Model'}) RETURN max(m.parameters) AS largest_model",
    },
]
LangChain ile Retrieval Augmented Generation (RAG)

Az örnekli istemlemeyi uygulama

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

example_prompt = PromptTemplate.from_template( "User input: {question}\nCypher query: {query}" )
cypher_prompt = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, prefix="You are a Neo4j expert. Given an input question, create a syntactically correct Cypher query to run.\n\nHere is the schema information\n{schema}.\n\n Below are a number of examples of questions and their corresponding Cypher queries.", suffix="User input: {question}\nCypher query: ", input_variables=["question"], )
LangChain ile Retrieval Augmented Generation (RAG)

Tam istem

Bir Neo4j uzmanısınız. Verilen bir soruya göre, çalıştırılacak sözdizimi olarak doğru bir Cypher sorgusu oluşturun.

Aşağıda sorular ve bunlara karşılık gelen Cypher sorguları yer almaktadır.

Kullanıcı girişi: Makalede kaç kayda değer büyük dil modeli anılıyor?
Cypher sorgusu: MATCH (p:Paper) RETURN count(DISTINCT p)

Kullanıcı girişi: Büyük dil modellerini hangi şirket veya kuruluşlar geliştirdi?
Cypher sorgusu: MATCH (o:Organization)-[:DEVELOPS]->(m:Concept {id: 'Large Language Model'}) RETURN DISTINCT o.id

Kullanıcı girişi: Makalede geçen en büyük model boyutu (parametre sayısı olarak) nedir?
Cypher sorgusu: MATCH (m:Concept {id: 'Large Language Model'}) RETURN max(m.parameters) AS largest_model

Kullanıcı girişi: 2016'da kaç makale yayımlandı?
Cypher sorgusu:
LangChain ile Retrieval Augmented Generation (RAG)

Az örnekli örnekleri ekleme

chain = GraphCypherQAChain.from_llm(
    graph=graph, llm=llm, cypher_prompt=cypher_prompt,
    verbose=True, validate_cypher=True
)
LangChain ile Retrieval Augmented Generation (RAG)

Hadi pratik yapalım!

LangChain ile Retrieval Augmented Generation (RAG)

Preparing Video For Download...