如何高效映射值到图数据库:完整指南

# 如何高效映射值到图数据库:完整指南

在这篇文章中,我们将探讨如何通过映射用户输入的值到数据库来改进图数据库查询生成。当使用内置图表链时,LLM了解图的模式,但对存储在数据库中的属性值没有信息。因此,我们可以在图数据库问答系统中引入一个新步骤,来准确地映射这些值。

## 引言

图数据库在处理复杂关系和大型连接数据集方面提供了显著的优势。然而,为了充分利用其潜力,我们需要能够从用户输入中精确地提取并映射值到数据库。这篇指南将为你提供实用的知识和工具,使您能够成功完成这项任务。

## 主要内容

### 环境设置

首先,让我们获取必要的包并设置环境变量:

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

我们将在本文中默认使用OpenAI的模型,但你可以根据需要替换为其他供应商的模型。

import getpass
import os

os.environ["OPENAI_API_KEY"] = getpass.getpass()

# 使用LangSmith时可以取消下面的注释。不需要时可以忽略。
# 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提示,以将实体映射信息与模式和用户问题结合,构建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语句的链,我们需要在数据库中执行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()
)

result = chain.invoke({"question": "Who played in Casino movie?"})
# 输出:'Robert De Niro, James Woods, Joe Pesci, and Sharon Stone played in the movie "Casino".'

常见问题和解决方案

  1. 访问API时遇到网络限制:由于某些地区的网络限制,开发者可能需要考虑使用API代理服务来提高访问稳定性。例如,可以使用{AI_URL}作为API端点。

  2. 实体识别不准确:确保你的语言模型能够识别文本中的所有相关实体。你可以调整模型的温度或使用更为复杂的提示提高精确度。

  3. 图数据库性能问题:在处理大型数据集时,可能需要优化Cypher查询,使用索引或者分片以提高查询性能。

总结与进一步学习资源

通过阅读本文,你应该对如何将用户输入的值映射到图数据库有了更深的理解。正确地执行这一过程可以显著提高查询的准确性和效率,提升用户体验。

对于进一步学习,你可以参考以下资源:

参考资料

  1. Neo4j 官方文档: Neo4j Documentation
  2. LangChain GitHub: LangChain
  3. OpenAI API 文档: OpenAI API

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

---END---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值