KuzuQAChain
这个笔记本展示了如何使用LLMs为Kùzu数据库提供自然语言接口。
Kùzu是一个进程内属性图数据库管理系统。您可以使用pip
轻松安装它:
pip install kuzu
安装完成后,您可以简单地导入它并开始在本地机器上创建数据库并连接到它:
import kuzu
db = kuzu.Database("test_db")
conn = kuzu.Connection(db)
首先,我们为一个简单的电影数据库创建模式:
conn.execute("CREATE NODE TABLE Movie (name STRING, PRIMARY KEY(name))")
conn.execute(
"CREATE NODE TABLE Person (name STRING, birthDate STRING, PRIMARY KEY(name))"
)
conn.execute("CREATE REL TABLE ActedIn (FROM Person TO Movie)")
<kuzu.query_result.QueryResult at 0x1066ff410>
然后我们可以插入一些数据。
conn.execute("CREATE (:Person {name: 'Al Pacino', birthDate: '1940-04-25'})")
conn.execute("CREATE (:Person {name: 'Robert De Niro', birthDate: '1943-08-17'})")
conn.execute("CREATE (:Movie {name: 'The Godfather'})")
conn.execute("CREATE (:Movie {name: 'The Godfather: Part II'})")
conn.execute(
"CREATE (:Movie {name: 'The Godfather Coda: The Death of Michael Corleone'})"
)
conn.execute(
"MATCH (p:Person), (m:Movie) WHERE p.name = 'Al Pacino' AND m.name = 'The Godfather' CREATE (p)-[:ActedIn]->(m)"
)
conn.execute(
"MATCH (p:Person), (m:Movie) WHERE p.name = 'Al Pacino' AND m.name = 'The Godfather: Part II' CREATE (p)-[:ActedIn]->(m)"
)
conn.execute(
"MATCH (p:Person), (m:Movie) WHERE p.name = 'Al Pacino' AND m.name = 'The Godfather Coda: The Death of Michael Corleone' CREATE (p)-[:ActedIn]->(m)"
)
conn.execute(
"MATCH (p:Person), (m:Movie) WHERE p.name = 'Robert De Niro' AND m.name = 'The Godfather: Part II' CREATE (p)-[:ActedIn]->(m)"
)
<kuzu.query_result.QueryResult at 0x107016210>
创建KuzuQAChain
现在我们可以创建KuzuGraph
和KuzuQAChain
。要创建KuzuGraph
,我们只需要将数据库对象传递给KuzuGraph
构造函数。
from langchain.chat_models import ChatOpenAI
from langchain.graphs import KuzuGraph
from langchain.chains import KuzuQAChain
graph = KuzuGraph(db)
chain = KuzuQAChain.from_llm(ChatOpenAI(temperature=0), graph=graph, verbose=True)
刷新图形模式信息
如果数据库的模式发生更改,您可以刷新生成Cypher语句所需的模式信息。
# graph.refresh_schema()
print(graph.get_schema)
Node properties: [{'properties': [('name', 'STRING')], 'label': 'Movie'}, {'properties': [('name', 'STRING'), ('birthDate', 'STRING')], 'label': 'Person'}]
Relationships properties: [{'properties': [], 'label': 'ActedIn'}]
Relationships: ['(:Person)-[:ActedIn]->(:Movie)']
查询图形
现在我们可以使用KuzuQAChain
来询问图形的问题。
chain.run("Who played in The Godfather: Part II?")
> 进入新的链...
生成的Cypher:
MATCH (p:Person)-[:ActedIn]->(m:Movie {name: 'The Godfather: Part II'}) RETURN p.name
完整上下文:
[{'p.name': 'Al Pacino'}, {'p.name': 'Robert De Niro'}]
> 完成链。
'Al Pacino和Robert De Niro都出演了《教父续集》。'
chain.run("Robert De Niro played in which movies?")
> 进入新的链...
生成的Cypher:
MATCH (p:Person {name: 'Robert De Niro'})-[:ActedIn]->(m:Movie)
RETURN m.name
完整上下文:
[{'m.name': 'The Godfather: Part II'}]
> 完成链。
'Robert De Niro出演了《教父续集》。'
chain.run("Robert De Niro is born in which year?")
> 进入新的链...
生成的Cypher:
MATCH (p:Person {name: 'Robert De Niro'})-[:ActedIn]->(m:Movie)
RETURN p.birthDate
完整上下文:
[{'p.birthDate': '1943-08-17'}]
> 完成链。
'Robert De Niro出生于1943年8月17日。'
chain.run("Who is the oldest actor who played in The Godfather: Part II?")
> 进入新的链...
生成的Cypher:
MATCH (p:Person)-[:ActedIn]->(m:Movie{name:'The Godfather: Part II'})
WITH p, m, p.birthDate AS birthDate
ORDER BY birthDate ASC
LIMIT 1
RETURN p.name
完整上下文:
[{'p.name': 'Al Pacino'}]
> 完成链。
'在《教父续集》中,最年长的演员是Al Pacino。'