グラフ検索の改善

LangChain による検索拡張生成(RAG)

Meri Nova

Machine Learning Engineer

手法

主な課題: ユーザー入力 → Cypher変換の信頼性

グラフ検索システムの改善手法:

  • グラフスキーマのフィルタリング
  • Cypherクエリの検証
  • Few-shotプロンプティング
LangChain による検索拡張生成(RAG)

フィルタリング

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)
Node properties:
Document {title: STRING, id: STRING, text: STRING, summary: STRING, source: STRING}
Organization {id: STRING}
LangChain による検索拡張生成(RAG)

Cypherクエリの検証

  • 関係の_方向_の解釈が困難
chain = GraphCypherQAChain.from_llm(
    graph=graph, llm=llm, verbose=True, validate_cypher=True
)
  1. ノードと関係を検出する
  2. 関係の方向を判定する
  3. グラフスキーマを確認する
  4. 関係の方向を修正する
LangChain による検索拡張生成(RAG)

Few-shotプロンプティング

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 による検索拡張生成(RAG)

Few-shotプロンプティングの実装

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 による検索拡張生成(RAG)

完全なプロンプト

You are a Neo4j expert. Given an input question, create a syntactically correct Cypher query to run.

Below are a number of examples of questions and their corresponding Cypher queries.

User input: How many notable large language models are mentioned in the article?
Cypher query: MATCH (p:Paper) RETURN count(DISTINCT p)

User input: Which companies or organizations have developed the large language models?
Cypher query: MATCH (o:Organization)-[:DEVELOPS]->(m:Concept {id: 'Large Language Model'}) RETURN DISTINCT o.id

User input: What is the largest model size mentioned in the article, in terms of number of parameters?
Cypher query: MATCH (m:Concept {id: 'Large Language Model'}) RETURN max(m.parameters) AS largest_model

User input: How many papers were published in 2016?
Cypher query:
LangChain による検索拡張生成(RAG)

Few-shotの例の追加

chain = GraphCypherQAChain.from_llm(
    graph=graph, llm=llm, cypher_prompt=cypher_prompt,
    verbose=True, validate_cypher=True
)
LangChain による検索拡張生成(RAG)

さあ、練習しましょう!

LangChain による検索拡張生成(RAG)

Preparing Video For Download...