利用LLM自然语言查询Memgraph数据库的完整指南

26 阅读2分钟
# 引言

在现代应用中,图数据库越来越受欢迎。在众多图数据库中,Memgraph作为开源的选择,与Neo4j兼容,且使用Cypher语言进行查询。本文将探讨如何利用大语言模型(LLM)提供Memgraph数据库的自然语言接口。

# 主要内容

## 设置环境

要完成本教程,您需要安装Docker和Python 3.x。确保您有正在运行的Memgraph实例。快速启动Memgraph平台,您可以使用以下命令:

### 在Linux/MacOS上:

```bash
curl https://install.memgraph.com | sh

在Windows上:

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

这些命令将在您的系统上下载Docker Compose文件,并建立和启动memgraph-mage和memgraph-lab Docker服务。

安装必要的Python包

使用pip安装必要的包:

pip install langchain langchain-openai neo4j gqlalchemy --user

连接数据库

使用GQLAlchemy库建立与Memgraph的连接:

from gqlalchemy import Memgraph

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

数据库填充

通过Cypher语言填充数据库:

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)

查询数据库

配置OpenAI API密钥:

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

创建GraphCypherQAChain以执行自然语言查询:

from langchain.chains import GraphCypherQAChain
from langchain_openai import ChatOpenAI

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)

代码示例

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

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

常见问题和解决方案

挑战:语句与数据不匹配

用户查询与数据库存储方式不一致,会导致无信息返回。通过提示优化,指导模型理解用户意图,生成精确查询。

解决方案:提示优化

from langchain_core.prompts import PromptTemplate

CYPHER_GENERATION_TEMPLATE = """
Task: Generate Cypher statement to query a graph database.
...
If the user asks about PS5, Play Station 5 or PS 5, that is the platform called PlayStation 5.
"""

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)

总结和进一步学习资源

通过利用LLM与Memgraph数据库结合,可以实现强大且灵活的自然语言查询接口。建议进一步阅读以下资源:

参考资料

  1. Memgraph Documentation: memgraph.com/docs
  2. Cypher Query Language: neo4j.com/developer/c…

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

---END---