使用自然语言接口与Memgraph图数据库进行交互

214 阅读3分钟

引言

在现代数据管理中,图数据库以其卓越的连接与关系管理能力而被广泛使用。Memgraph作为一个开源的图数据库,兼容Neo4j,并使用Cypher作为查询语言,为用户提供了一种高效管理和查询数据的方式。今天,我们将探讨如何结合大语言模型(LLM)为Memgraph数据库提供一个自然语言接口,使用户能够以自然语言查询数据。

主要内容

Memgraph的安装与设置

要开始我们的旅程,首先需要确保计算机上安装了Docker和Python 3.x。以下提供了在不同操作系统上快速运行Memgraph的平台安装命令:

  • Linux/MacOS:

    curl https://install.memgraph.com | sh
    
  • Windows:

    iwr https://windows.memgraph.com | iex
    

这些命令将下载Docker Compose文件,并在两个独立的容器中启动memgraph-mage和memgraph-lab服务。

环境准备

使用pip安装所需的Python包:

pip install langchain langchain-openai neo4j gqlalchemy --user

然后,使用GQLAlchemy库连接Memgraph数据库,并准备数据:

from gqlalchemy import Memgraph

memgraph = Memgraph(host="127.0.0.1", port=7687)

# 创建并执行数据填充查询
query = """
    MERGE (g:Game {name: "Baldur's Gate 3"})
    WITH g, ["PlayStation 5", "Mac OS", "Windows", "Xbox Series X/S"] AS platforms,
            ["Adventure", "Role-Playing Game", "Strategy"] AS genres
    FOREACH (platform IN platforms |
        MERGE (p:Platform {name: platform})
        MERGE (g)-[:AVAILABLE_ON]->(p)
    )
    FOREACH (genre IN genres |
        MERGE (gn:Genre {name: genre})
        MERGE (g)-[:HAS_GENRE]->(gn)
    )
    MERGE (p:Publisher {name: "Larian Studios"})
    MERGE (g)-[:PUBLISHED_BY]->(p);
"""

memgraph.execute(query)

使用LangChain进行查询

在与OpenAI接口交互之前,确保已经设置API密钥:

import os
os.environ["OPENAI_API_KEY"] = "your-key-here"

通过LangChain创建图链并执行查询:

from langchain.chains import GraphCypherQAChain
from langchain_openai import ChatOpenAI
from langchain_community.graphs import MemgraphGraph

graph = MemgraphGraph(url="bolt://localhost:7687", username="", password="")

chain = GraphCypherQAChain.from_llm(
    ChatOpenAI(temperature=0), graph=graph, verbose=True, model_name="gpt-3.5-turbo"
)

response = chain.run("Which platforms is Baldur's Gate 3 available on?")
print(response)

代码示例

此代码示例展示了如何通过自然语言查询Memgraph数据库:

response = chain.run("Is Baldur's Gate 3 available on Windows?")
print(response)

常见问题和解决方案

查询对齐问题

当用户的查询方式与数据存储方式不一致时,可能会导致查询结果不准确。通过提示优化(Prompt Refinement),可以有效提升模型理解能力:

from langchain_core.prompts import PromptTemplate

CYPHER_GENERATION_TEMPLATE = """
Task:Generate Cypher statement to query a graph database.
...
"""

CYPHER_GENERATION_PROMPT = PromptTemplate(
    input_variables=["schema", "question"], template=CYPHER_GENERATION_TEMPLATE
)

chain = GraphCypherQAChain.from_llm(
    ChatOpenAI(temperature=0), cypher_prompt=CYPHER_GENERATION_PROMPT, graph=graph, verbose=True, model_name="gpt-3.5-turbo"
)

response = chain.run("Is Baldur's Gate 3 available on PS5?")
print(response)

总结和进一步学习资源

通过本文,我们了解了如何使用自然语言接口操控Memgraph数据库,提升数据交互的便捷性和效率。对于那些希望深入了解Cypher查询语言及其强大功能的开发者,建议参考Memgraph及Neo4j的官方文档。此外,也可以探索LangChain和大语言模型的集成应用,以增强应用程序的智能性。

参考资料

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

---END---