探索Azure Cosmos DB中的Apache Gremlin:从存储到查询图数据库

110 阅读3分钟

引言

在当今的数据驱动世界中,图数据库因其在处理复杂连接数据上的优势而备受关注。Azure Cosmos DB的Apache Gremlin服务是一个强大的工具,能够以毫秒级的延迟查询海量图数据。在这篇文章中,我们将探讨如何在Azure Cosmos DB中使用Gremlin,并结合LLM(大语言模型)提供一个自然语言接口进行图数据库查询。

主要内容

设置环境

首先,我们需要安装必要的库:

!pip3 install gremlinpython

接下来,创建一个Azure Cosmos DB图形数据库实例。在创建时,使用/type作为分区键。

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

连接数据库

以下是如何连接到Cosmos DB的示例代码:

from langchain_community.graphs import GremlinGraph

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,
)

由于某些地区的网络限制,开发者可能需要考虑使用API代理服务来提高访问稳定性。

数据库初始化

初始化数据库并添加示例图数据:

from langchain_community.graphs.graph_document import GraphDocument, Node, Relationship
from langchain_core.documents import Document
import nest_asyncio

# 用于修复异步问题
nest_asyncio.apply()

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"})

relations = [
    Relationship(id=5, type="ActedIn", source=actor1, target=movie, properties={"label": "ActedIn"}),
    Relationship(id=6, type="ActedIn", source=actor2, target=movie, properties={"label": "ActedIn"}),
    Relationship(id=7, type="ActedIn", source=actor3, target=movie, properties={"label": "ActedIn"}),
    Relationship(id=8, type="Starring", source=movie, target=actor1, properties={"label": "Starring"}),
    Relationship(id=9, type="Starring", source=movie, target=actor2, properties={"label": "Starring"}),
    Relationship(id=10, type="Starring", source=movie, target=actor3, properties={"label": "Starring"}),
]

graph_doc = GraphDocument(
    nodes=[movie, actor1, actor2, actor3],
    relationships=relations,
    source=source_doc,
)

graph.add_graph_documents([graph_doc])

刷新图模式

在数据库模式修改后,可以刷新模式信息:

graph.refresh_schema()
print(graph.schema)

图数据库查询

使用Gremlin QA链进行自然语言查询:

from langchain.chains.graph_qa.gremlin import GremlinQAChain
from langchain_openai import AzureChatOpenAI

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

# 查询示例
response1 = chain.invoke("Who played in The Matrix?")
response2 = chain.run("How many people played in The Matrix?")

print(response1)
print(response2)

常见问题和解决方案

  • 连接问题:考虑使用API代理服务来解决可能的地理限制。
  • 性能优化:确保适当地使用分区键来优化查询性能。

总结和进一步学习资源

通过本文,我们学习了如何使用Azure Cosmos DB中的Apache Gremlin服务,结合LLM进行自然语言查询。这为图数据库的使用提供了一种更直观和高效的方式。

进一步学习资源

参考资料

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

---END---