Skip to main content

GraphSparqlQAChain 图形SPARQL QA链

图数据库是基于网络模型的应用程序的理想选择。为了标准化这些图的语法和语义,W3C推荐使用语义Web技术,参见语义WebSPARQL作为一种查询语言,类似于SQL或Cypher,用于这些图。本笔记本演示了将LLMs应用为图数据库的自然语言接口,通过生成SPARQL来实现。\ 免责声明:到目前为止,通过LLMs生成SPARQL查询仍然有些不稳定。特别要小心修改图形的UPDATE查询。

您可以针对多个来源运行查询,包括网络上的文件、本地可用的文件、SPARQL端点,例如维基数据三元存储

from langchain.chat_models import ChatOpenAI
from langchain.chains import GraphSparqlQAChain
from langchain.graphs import RdfGraph
graph = RdfGraph(
source_file="http://www.w3.org/People/Berners-Lee/card",
standard="rdf",
local_copy="test.ttl",
)

请注意,如果源是只读的,则提供local_file是必要的,以便在本地存储更改。

刷新图形模式信息

如果数据库的模式发生更改,您可以刷新生成SPARQL查询所需的模式信息。

graph.load_schema()
graph.get_schema
    在下面,每个IRI后面都跟着本地名称,可选地跟着括号中的描述。
RDF图支持以下节点类型:
<http://xmlns.com/foaf/0.1/PersonalProfileDocument>(PersonalProfileDocument,无),<http://www.w3.org/ns/auth/cert#RSAPublicKey>(RSAPublicKey,无),<http://www.w3.org/2000/10/swap/pim/contact#Male>(Male,无),<http://xmlns.com/foaf/0.1/Person>(Person,无),<http://www.w3.org/2006/vcard/ns#Work>(Work,无)
RDF图支持以下关系:
<http://www.w3.org/2000/01/rdf-schema#seeAlso>(seeAlso,无),<http://purl.org/dc/elements/1.1/title>(title,无),<http://xmlns.com/foaf/0.1/mbox_sha1sum>(mbox_sha1sum,无),<http://xmlns.com/foaf/0.1/maker>(maker,无),<http://www.w3.org/ns/solid/terms#oidcIssuer>(oidcIssuer,无),<http://www.w3.org/2000/10/swap/pim/contact#publicHomePage>(publicHomePage,无),<http://xmlns.com/foaf/0.1/openid>(openid,无),<http://www.w3.org/ns/pim/space#storage>(storage,无),<http://xmlns.com/foaf/0.1/name>(name,无),<http://www.w3.org/2000/10/swap/pim/contact#country>(country,无),<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>(type,无),<http://www.w3.org/ns/solid/terms#profileHighlightColor>(profileHighlightColor,无),<http://www.w3.org/ns/pim/space#preferencesFile>(preferencesFile,无),<http://www.w3.org/2000/01/rdf-schema#label>(label,无),<http://www.w3.org/ns/auth/cert#modulus>(modulus,无),<http://www.w3.org/2000/10/swap/pim/contact#participant>(participant,无),<http://www.w3.org/2000/10/swap/pim/contact#street2>(street2,无),<http://www.w3.org/2006/vcard/ns#locality>(locality,无),<http://xmlns.com/foaf/0.1/nick>(nick,无),<http://xmlns.com/foaf/0.1/homepage>(homepage,无),<http://creativecommons.org/ns#license>(license,无),<http://xmlns.com/foaf/0.1/givenname>(givenname,无),<http://www.w3.org/2006/vcard/ns#street-address>(street-address,无),<http://www.w3.org/2006/vcard/ns#postal-code>(postal-code,无),<http://www.w3.org/2000/10/swap/pim/contact#street>(street,无),<http://www.w3.org/2003/01/geo/wgs84_pos#lat>(lat,无),<http://xmlns.com/foaf/0.1/primaryTopic>(primaryTopic,无),<http://www.w3.org/2006/vcard/ns#fn>(fn,无),<http://www.w3.org/2003/01/geo/wgs84_pos#location>(location,无),<http://usefulinc.com/ns/doap#developer>(developer,无),<http://www.w3.org/2000/10/swap/pim/contact#city>(city,无),<http://www.w3.org/2006/vcard/ns#region>(region,无),<http://xmlns.com/foaf/0.1/member>(member,无),<http://www.w3.org/2003/01/geo/wgs84_pos#long>(long,无),<http://www.w3.org/2000/10/swap/pim/contact#address>(address,无),<http://xmlns.com/foaf/0.1/family_name>(family_name,无),<http://xmlns.com/foaf/0.1/account>(account,无),<http://xmlns.com/foaf/0.1/workplaceHomepage>(workplaceHomepage,无),<http://purl.org/dc/terms/title>(title,无),<http://www.w3.org/ns/solid/terms#publicTypeIndex>(publicTypeIndex,无),<http://www.w3.org/2000/10/swap/pim/contact#office>(office,无),<http://www.w3.org/2000/10/swap/pim/contact#homePage>(homePage,无),<http://xmlns.com/foaf/0.1/mbox>(mbox,无),<http://www.w3.org/2000/10/swap/pim/contact#preferredURI>(preferredURI,无),<http://www.w3.org/ns/solid/terms#profileBackgroundColor>(profileBackgroundColor,无),<http://schema.org/owns>(owns,无),<http://xmlns.com/foaf/0.1/based_near>(based_near,无),<http://www.w3.org/2006/vcard/ns#hasAddress>(hasAddress,无),<http://xmlns.com/foaf/0.1/img>(img,无),<http://www.w3.org/2000/10/swap/pim/contact#assistant>(assistant,无),<http://xmlns.com/foaf/0.1/title>(title,无),<http://www.w3.org/ns/auth/cert#key>(key,无),<http://www.w3.org/ns/ldp#inbox>(inbox,无),<http://www.w3.org/ns/solid/terms#editableProfile>(editableProfile,无),<http://www.w3.org/2000/10/swap/pim/contact#postalCode>(postalCode,无),<http://xmlns.com/foaf/0.1/weblog>(weblog,无),<http://www.w3.org/ns/auth/cert#exponent>(exponent,无),<http://rdfs.org/sioc/ns#avatar>(avatar,无)

查询图形

现在,您可以使用图形SPARQL QA链来询问有关图形的问题。

chain = GraphSparqlQAChain.from_llm(
ChatOpenAI(temperature=0), graph=graph, verbose=True
)
chain.run("Tim Berners-Lee的工作主页是什么?")
    

> 进入新的GraphSparqlQAChain链...
识别到的意图:
SELECT
生成的SPARQL:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?homepage
WHERE {
?person foaf:name "Tim Berners-Lee" .
?person foaf:workplaceHomepage ?homepage .
}
完整上下文:
[]

> 完成链。





"Tim Berners-Lee的工作主页是http://www.w3.org/People/Berners-Lee/。"

更新图形

类似地,您可以使用自然语言更新图形,即插入三元组。

chain.run(
"保存名为'Timothy Berners-Lee'的人在'http://www.w3.org/foo/bar/'上有一个工作主页"
)
    

> 进入新的GraphSparqlQAChain链...
识别到的意图:
UPDATE
生成的SPARQL:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
INSERT {
?person foaf:workplaceHomepage <http://www.w3.org/foo/bar/> .
}
WHERE {
?person foaf:name "Timothy Berners-Lee" .
}

> 完成链。





'成功将三元组插入图形。'

让我们验证结果:

query = (
"""PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"""
"""SELECT ?hp\n"""
"""WHERE {\n"""
""" ?person foaf:name "Timothy Berners-Lee" . \n"""
""" ?person foaf:workplaceHomepage ?hp .\n"""
"""}"""
)
graph.query(query)
    [(rdflib.term.URIRef('https://www.w3.org/'),),
(rdflib.term.URIRef('http://www.w3.org/foo/bar/'),)]