그래프 검색 개선

LangChain을 활용한 RAG(검색 증강 생성)

Meri Nova

Machine Learning Engineer

기법

주요 한계: 사용자 → Cypher 변환의 신뢰성

그래프 검색 시스템 개선 전략:

  • 그래프 스키마 필터링
  • Cypher 쿼리 유효성 검사
  • 퓨샷 프롬프팅
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(검색 증강 생성)

퓨샷 프롬프팅

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(검색 증강 생성)

퓨샷 프롬프팅 구현

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(검색 증강 생성)

퓨샷 예시 추가

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...