深入Azure Cosmos DB与Apache Gremlin:如何使用自然语言接口查询图数据库

54 阅读3分钟

深入Azure Cosmos DB与Apache Gremlin:如何使用自然语言接口查询图数据库

在现代数据密集型应用程序中,图数据库正逐渐成为处理复杂连接数据的理想选择。Azure Cosmos DB 提供了一种集成Apache Gremlin的图数据库服务,支持存储拥有数十亿个顶点和边的海量图数据。这篇文章将介绍如何使用自然语言模型(LLM)来与图数据库进行交互,运用Gremlin查询语言来执行查询。

引言

图数据库近年来因其优异的关联数据处理能力和查询速度而受到欢迎。Azure Cosmos DB与Apache Gremlin的结合提供了强大的图数据存储和查询功能。我们将探讨如何利用LLM为图数据库提供自然语言接口,从而简化复杂查询。本文将指导您设置环境、初始化图数据库并执行查询。

主要内容

1. 环境设置

首先,需要安装gremlinpython库:

!pip3 install gremlinpython

接下来,您需要一个Azure Cosmos DB图数据库实例。您可以选择在Azure中创建一个免费的Cosmos DB图数据库实例。在创建您的Cosmos DB账户和图时,请使用/type作为分区键。

cosmosdb_name = "mycosmosdb"
cosmosdb_db_id = "graphtesting"
cosmosdb_db_graph_id = "mygraph"
cosmosdb_access_Key = "longstring=="

import nest_asyncio
from langchain.chains.graph_qa.gremlin import GremlinQAChain
from langchain_community.graphs import GremlinGraph
from langchain_community.graphs.graph_document import GraphDocument, Node, Relationship
from langchain_core.documents import Document
from langchain_openai import AzureChatOpenAI

# 使用API代理服务提高访问稳定性
graph = GremlinGraph(
    url=f"=wss://{cosmosdb_name}.gremlin.cosmos.azure.com:443/",
    username=f"/dbs/{cosmosdb_db_id}/colls/{cosmosdb_db_graph_id}",
    password=cosmosdb_access_Key,
)

2. 数据库初始化

假设数据库为空,我们可以使用GraphDocuments填充数据。

source_doc = Document(
    page_content="Matrix is a movie where Keanu Reeves, Laurence Fishburne and Carrie-Anne Moss acted."
)
movie = Node(id="The Matrix", properties={"label": "movie", "title": "The Matrix"})
actor1 = Node(id="Keanu Reeves", properties={"label": "actor", "name": "Keanu Reeves"})
actor2 = Node(
    id="Laurence Fishburne", properties={"label": "actor", "name": "Laurence Fishburne"}
)
actor3 = Node(
    id="Carrie-Anne Moss", properties={"label": "actor", "name": "Carrie-Anne Moss"}
)
rel1 = Relationship(
    id=5, type="ActedIn", source=actor1, target=movie, properties={"label": "ActedIn"}
)
rel2 = Relationship(
    id=6, type="ActedIn", source=actor2, target=movie, properties={"label": "ActedIn"}
)
rel3 = Relationship(
    id=7, type="ActedIn", source=actor3, target=movie, properties={"label": "ActedIn"}
)
graph_doc = GraphDocument(
    nodes=[movie, actor1, actor2, actor3],
    relationships=[rel1, rel2, rel3],
    source=source_doc,
)

# 修复在notebook中运行时的潜在问题
nest_asyncio.apply()

# 添加文档到CosmosDB图数据库。
graph.add_graph_documents([graph_doc])

3. 查询图数据库

在填充数据后,可以使用Gremlin QA链来查询图。

chain = GremlinQAChain.from_llm(
    AzureChatOpenAI(
        temperature=0,
        azure_deployment="gpt-4-turbo",
    ),
    graph=graph,
    verbose=True,
)

# 查询"谁出演了《黑客帝国》?"
chain.invoke("Who played in The Matrix?")

# 查询"有多少人出演了《黑客帝国》?"
chain.run("How many people played in The Matrix?")

常见问题和解决方案

  • 数据同步问题:当数据库模式变更后,可通过graph.refresh_schema()更新模式信息,以维持同步。
  • 网络访问问题:在某些地区,访问Azure Cosmos DB的API可能需要使用API代理服务,以提高访问稳定性。

总结和进一步学习资源

Azure Cosmos DB与Apache Gremlin的组合提供了强大的图数据管理和查询功能。通过集成LLM,用户能够以自然语言方式与复杂的图数据进行交互,显著简化了操作难度。对于有兴趣深入了解的读者,可以参考以下资源:

参考资料

  • Apache TinkerPop官方文档
  • Azure Cosmos DB文档
  • Langchain库文档

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

---END---