使用Azure Cosmos DB和Gremlin进行图数据库查询

64 阅读3分钟

使用Azure Cosmos DB和Gremlin进行图数据库查询

引言

Azure Cosmos DB for Apache Gremlin 是一种图数据库服务,可存储包含数十亿个顶点和边的巨型图。您可以使用Gremlin查询语言以毫秒级延迟查询图,并轻松演化图结构。本文将介绍如何使用LLMs(大型语言模型)为图数据库提供自然语言接口,并通过Gremlin查询语言进行查询。

主要内容

设置

首先,安装所需的库:

!pip3 install gremlinpython

您需要一个Azure Cosmos DB图数据库实例。可以选择在Azure上创建一个免费的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

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

初始化数据库

假设您的数据库是空的,可以使用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"})
rel4 = Relationship(id=8, type="Starring", source=movie, target=actor1, properties={"label": "Starring"})
rel5 = Relationship(id=9, type="Starring", source=movie, target=actor2, properties={"label": "Starring"})
rel6 = Relationship(id=10, type="Starring", source=movie, target=actor3, properties={"label": "Starring"})

graph_doc = GraphDocument(
    nodes=[movie, actor1, actor2, actor3],
    relationships=[rel1, rel2, rel3, rel4, rel5, rel6],
    source=source_doc,
)

nest_asyncio.apply()  # 解决python-gremlin在notebook中运行的问题

graph.add_graph_documents([graph_doc])

刷新图架构信息

如果数据库架构更改,可以刷新架构信息:

graph.refresh_schema()
print(graph.schema)

查询图数据库

我们现在可以使用Gremlin QA链来查询图数据库:

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

print(chain.invoke("Who played in The Matrix?"))
print(chain.run("How many people played in The Matrix?"))

常见问题和解决方案

  1. 连接问题:由于网络限制,部分地区的开发者可能会遇到连接问题。建议使用API代理服务以提高访问稳定性。
  2. 数据格式问题:确保节点和关系数据的格式正确,特别是节点的label属性和关系的属性。
  3. 查询性能问题:在大型图数据库中进行查询时,可能会遇到性能问题。可以通过优化查询和索引来解决。

总结和进一步学习资源

本文介绍了如何使用Azure Cosmos DB和Gremlin进行图数据库查询,并提供了一个完整的代码示例来演示这个过程。进一步学习资源:

参考资料

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

---END---