HugeGraph
HugeGraph 是一個方便、高效且適應性強的圖形資料庫,與
Apache TinkerPop3
框架和Gremlin
查詢語言相容。Gremlin 是一種圖形遍歷語言和虛擬機器,由
Apache Software Foundation
的Apache TinkerPop
開發。
本筆記本展示如何使用 LLM 為 HugeGraph 資料庫提供自然語言介面。
設定
您需要有一個正在運行的 HugeGraph 實例。您可以通過運行以下腳本來運行本地 Docker 容器
docker run \
--name=graph \
-itd \
-p 8080:8080 \
hugegraph/hugegraph
如果我們想在應用程式中連接 HugeGraph,我們需要安裝 Python SDK
pip3 install hugegraph-python
如果您正在使用 Docker 容器,您需要等待幾秒鐘讓資料庫啟動,然後我們需要為資料庫建立結構描述並寫入圖形資料。
from hugegraph.connection import PyHugeGraph
client = PyHugeGraph("localhost", "8080", user="admin", pwd="admin", graph="hugegraph")
首先,我們為一個簡單的電影資料庫建立結構描述
"""schema"""
schema = client.schema()
schema.propertyKey("name").asText().ifNotExist().create()
schema.propertyKey("birthDate").asText().ifNotExist().create()
schema.vertexLabel("Person").properties(
"name", "birthDate"
).usePrimaryKeyId().primaryKeys("name").ifNotExist().create()
schema.vertexLabel("Movie").properties("name").usePrimaryKeyId().primaryKeys(
"name"
).ifNotExist().create()
schema.edgeLabel("ActedIn").sourceLabel("Person").targetLabel(
"Movie"
).ifNotExist().create()
'create EdgeLabel success, Detail: "b\'{"id":1,"name":"ActedIn","source_label":"Person","target_label":"Movie","frequency":"SINGLE","sort_keys":[],"nullable_keys":[],"index_labels":[],"properties":[],"status":"CREATED","ttl":0,"enable_label_index":true,"user_data":{"~create_time":"2023-07-04 10:48:47.908"}}\'"'
然後我們可以插入一些資料。
"""graph"""
g = client.graph()
g.addVertex("Person", {"name": "Al Pacino", "birthDate": "1940-04-25"})
g.addVertex("Person", {"name": "Robert De Niro", "birthDate": "1943-08-17"})
g.addVertex("Movie", {"name": "The Godfather"})
g.addVertex("Movie", {"name": "The Godfather Part II"})
g.addVertex("Movie", {"name": "The Godfather Coda The Death of Michael Corleone"})
g.addEdge("ActedIn", "1:Al Pacino", "2:The Godfather", {})
g.addEdge("ActedIn", "1:Al Pacino", "2:The Godfather Part II", {})
g.addEdge(
"ActedIn", "1:Al Pacino", "2:The Godfather Coda The Death of Michael Corleone", {}
)
g.addEdge("ActedIn", "1:Robert De Niro", "2:The Godfather Part II", {})
1:Robert De Niro--ActedIn-->2:The Godfather Part II
建立 HugeGraphQAChain
我們現在可以建立 HugeGraph
和 HugeGraphQAChain
。要建立 HugeGraph
,我們只需將資料庫物件傳遞給 HugeGraph
建構函數。
from langchain.chains import HugeGraphQAChain
from langchain_community.graphs import HugeGraph
from langchain_openai import ChatOpenAI
graph = HugeGraph(
username="admin",
password="admin",
address="localhost",
port=8080,
graph="hugegraph",
)
刷新圖表結構描述資訊
如果資料庫的結構描述發生更改,您可以刷新生成 Gremlin 語句所需的結構描述資訊。
# graph.refresh_schema()
print(graph.get_schema)
Node properties: [name: Person, primary_keys: ['name'], properties: ['name', 'birthDate'], name: Movie, primary_keys: ['name'], properties: ['name']]
Edge properties: [name: ActedIn, properties: []]
Relationships: ['Person--ActedIn-->Movie']
查詢圖表
我們現在可以使用圖形 Gremlin QA 鏈來詢問有關圖表的問題
chain = HugeGraphQAChain.from_llm(ChatOpenAI(temperature=0), graph=graph, verbose=True)
chain.run("Who played in The Godfather?")
[1m> Entering new chain...[0m
Generated gremlin:
[32;1m[1;3mg.V().has('Movie', 'name', 'The Godfather').in('ActedIn').valueMap(true)[0m
Full Context:
[32;1m[1;3m[{'id': '1:Al Pacino', 'label': 'Person', 'name': ['Al Pacino'], 'birthDate': ['1940-04-25']}][0m
[1m> Finished chain.[0m
'Al Pacino played in The Godfather.'