Ontotext GraphDB 是一个符合 RDFSPARQL 的图数据库和知识发现工具。
本笔记本展示了如何使用 LLM 为 Ontotext GraphDB 提供自然语言查询(NLQ 到 SPARQL,也称为 text2sparql)。

GraphDB LLM 功能

GraphDB 支持一些 LLM 集成功能,如此处所述: gpt-queries
  • 使用知识图(KG)中的数据向 LLM 询问文本、列表或表格的魔法谓词
  • 查询解释
  • 结果解释、摘要、改写、翻译
retrieval-graphdb-connector
  • 在向量数据库中索引 KG 实体
  • 支持任何文本嵌入算法和向量数据库
  • 使用与 GraphDB 用于 Elastic、Solr、Lucene 相同的强大连接器(索引)语言
  • 自动同步 RDF 数据中的更改到 KG 实体索引
  • 支持嵌套对象(GraphDB 版本 10.5 中无 UI 支持)
  • 将 KG 实体序列化为文本,如下所示(例如,对于葡萄酒数据集):
Franvino:
- is a RedWine.
- made from grape Merlo.
- made from grape Cabernet Franc.
- has sugar dry.
- has year 2012.
talk-to-graph
  • 使用定义的 KG 实体索引的简单聊天机器人
对于本教程,我们不会使用 GraphDB LLM 集成,而是从 NLQ 生成 SPARQL。我们将使用 Star Wars API (SWAPI) 本体和数据集,您可以在此处查看。

设置

您需要一个运行中的 GraphDB 实例。本教程展示了如何使用 GraphDB Docker 镜像在本地运行数据库。它提供了一个 docker compose 设置,用 Star Wars 数据集填充 GraphDB。包括此笔记本在内的所有必要文件都可以从 GitHub 仓库 langchain-graphdb-qa-chain-demo 下载。
docker build --tag graphdb .
docker compose up -d graphdb
您需要等待几秒钟让数据库在 http://localhost:7200/ 上启动。Star Wars 数据集 starwars-data.trig 会自动加载到 langchain 存储库中。本地 SPARQL 端点 http://localhost:7200/repositories/langchain 可用于运行查询。您也可以从您喜欢的网络浏览器打开 GraphDB Workbench http://localhost:7200/sparql,在那里您可以交互式地进行查询。
  • 设置工作环境
如果您使用 conda,创建并激活一个新的 conda 环境,例如:
conda create -n graph_ontotext_graphdb_qa python=3.12
conda activate graph_ontotext_graphdb_qa
Install the following libraries:
pip install jupyter==1.1.1
pip install rdflib==7.1.1
pip install langchain-community==0.3.4
pip install langchain-openai==0.2.4
Run Jupyter with
jupyter notebook

指定本体

为了使 LLM 能够生成 SPARQL,它需要知道知识图模式(本体)。可以通过 OntotextGraphDBGraph 类的两个参数之一提供:
  • query_ontology:在 SPARQL 端点上执行的 CONSTRUCT 查询,返回 KG 模式语句。我们建议您将本体存储在自己的命名图中,这样可以更容易地仅获取相关语句(如下面的示例)。不支持 DESCRIBE 查询,因为 DESCRIBE 返回对称简洁有界描述(SCBD),即还包括传入的类链接。对于有数百万实例的大型图,这效率不高。查看 github.com/eclipse-rdf4j/rdf4j/issues/4857
  • local_file:本地 RDF 本体文件。支持的 RDF 格式有 TurtleRDF/XMLJSON-LDN-TriplesNotation-3TrigTrixN-Quads
无论哪种情况,本体转储应该:
  • 包含关于类、属性、属性附加到类(使用 rdfs:domain、schema:domainIncludes 或 OWL 限制)和分类法(重要个体)的足够信息。
  • 不包括过于冗长且不相关的定义和示例,这些对 SPARQL 构建没有帮助。
from langchain_community.graphs import OntotextGraphDBGraph

# feeding the schema using a user construct query

graph = OntotextGraphDBGraph(
    query_endpoint="http://localhost:7200/repositories/langchain",
    query_ontology="CONSTRUCT {?s ?p ?o} FROM [swapi.co/ontology/](https://swapi.co/ontology/) WHERE {?s ?p ?o}",
)
# feeding the schema using a local RDF file

graph = OntotextGraphDBGraph(
    query_endpoint="http://localhost:7200/repositories/langchain",
    local_file="/path/to/langchain_graphdb_tutorial/starwars-ontology.nt",  # change the path here
)
无论哪种方式,本体(模式)都以 Turtle 格式提供给 LLM,因为带有适当前缀的 Turtle 最紧凑,LLM 最容易记住。 Star Wars 本体有点不寻常,因为它包含许多关于类的特定三元组,例如物种 :Aleena 生活在 <planet/38> 上,它们是 :Reptile 的子类,具有某些典型特征(平均身高、平均寿命、皮肤颜色),特定个体(角色)是该类的代表:
@prefix : [swapi.co/vocabulary/](https://swapi.co/vocabulary/) .
@prefix owl: [www.w3.org/2002/07/owl#](http://www.w3.org/2002/07/owl#) .
@prefix rdfs: [www.w3.org/2000/01/rdf-schema#](http://www.w3.org/2000/01/rdf-schema#) .
@prefix xsd: [www.w3.org/2001/XMLSchema#](http://www.w3.org/2001/XMLSchema#) .

:Aleena a owl:Class, :Species ;
    rdfs:label "Aleena" ;
    rdfs:isDefinedBy [swapi.co/ontology/](https://swapi.co/ontology/) ;
    rdfs:subClassOf :Reptile, :Sentient ;
    :averageHeight 80.0 ;
    :averageLifespan "79" ;
    :character [swapi.co/resource/aleena/47](https://swapi.co/resource/aleena/47) ;
    :film [swapi.co/resource/film/4](https://swapi.co/resource/film/4) ;
    :language "Aleena" ;
    :planet [swapi.co/resource/planet/38](https://swapi.co/resource/planet/38) ;
    :skinColor "blue", "gray" .

    ...

为了保持本教程简单,我们使用未加密的 GraphDB。如果 GraphDB 已加密,您应该在初始化 OntotextGraphDBGraph 之前设置环境变量 ‘GRAPHDB_USERNAME’ 和 ‘GRAPHDB_PASSWORD’。
os.environ["GRAPHDB_USERNAME"] = "graphdb-user"
os.environ["GRAPHDB_PASSWORD"] = "graphdb-password"

graph = OntotextGraphDBGraph(
    query_endpoint=...,
    query_ontology=...
)

针对 StarWars 数据集的问答

我们现在可以使用 OntotextGraphDBQAChain 提出一些问题。
import os

from langchain.chains import OntotextGraphDBQAChain
from langchain_openai import ChatOpenAI

# 我们将使用需要 OpenAI API 密钥的 OpenAI 模型。
# 但是,也可以使用其他模型:
# https://python.langchain.com/docs/integrations/chat/

# 将环境变量 `OPENAI_API_KEY` 设置为您的 OpenAI API 密钥
os.environ["OPENAI_API_KEY"] = "sk-***"

# 这里可以使用任何可用的 OpenAI 模型。
# 我们使用 'gpt-4-1106-preview' 是因为它有更大的上下文窗口。
# 'gpt-4-1106-preview' model_name 将来会被弃用,将更改为 'gpt-4-turbo' 或类似名称,
# 因此请务必查阅 OpenAI API https://platform.openai.com/docs/models 以获取正确的命名。

chain = OntotextGraphDBQAChain.from_llm(
    ChatOpenAI(temperature=0, model_name="gpt-4-1106-preview"),
    graph=graph,
    verbose=True,
    allow_dangerous_requests=True,
)
让我们问一个简单的问题。
chain.invoke({chain.input_key: "What is the climate on Tatooine?"})[chain.output_key]


> Entering new OntotextGraphDBQAChain chain...
Generated SPARQL:
PREFIX : [swapi.co/vocabulary/](https://swapi.co/vocabulary/)
PREFIX rdfs: [www.w3.org/2000/01/rdf-schema#](http://www.w3.org/2000/01/rdf-schema#)

SELECT ?climate
WHERE {
  ?planet rdfs:label "Tatooine" ;
          :climate ?climate .
}

> Finished chain.
'The climate on Tatooine is arid.'
还有一个稍微复杂一点的问题。
chain.invoke({chain.input_key: "What is the climate on Luke Skywalker's home planet?"})[
    chain.output_key
]
> Entering new OntotextGraphDBQAChain chain...
Generated SPARQL:
PREFIX : [swapi.co/vocabulary/](https://swapi.co/vocabulary/)
PREFIX owl: [www.w3.org/2002/07/owl#](http://www.w3.org/2002/07/owl#)
PREFIX rdfs: [www.w3.org/2000/01/rdf-schema#](http://www.w3.org/2000/01/rdf-schema#)
PREFIX xsd: [www.w3.org/2001/XMLSchema#](http://www.w3.org/2001/XMLSchema#)

SELECT ?climate
WHERE {
  ?character rdfs:label "Luke Skywalker" .
  ?character :homeworld ?planet .
  ?planet :climate ?climate .
}

> Finished chain.
"The climate on Luke Skywalker's home planet is arid."
我们还可以提出更复杂的问题,例如
chain.invoke(
    {
        chain.input_key: "What is the average box office revenue for all the Star Wars movies?"
    }
)[chain.output_key]
> Entering new OntotextGraphDBQAChain chain...
Generated SPARQL:
PREFIX : [swapi.co/vocabulary/](https://swapi.co/vocabulary/)
PREFIX xsd: [www.w3.org/2001/XMLSchema#](http://www.w3.org/2001/XMLSchema#)

SELECT (AVG(?boxOffice) AS ?averageBoxOfficeRevenue)
WHERE {
  ?film a :Film .
  ?film :boxOffice ?boxOfficeValue .
  BIND(xsd:decimal(?boxOfficeValue) AS ?boxOffice)
}


> Finished chain.
'The average box office revenue for all the Star Wars movies is approximately 754.1 million dollars.'

链修饰符

Ontotext GraphDB QA 链允许提示优化,以进一步改进您的 QA 链并增强应用程序的整体用户体验。

“SPARQL 生成”提示

该提示用于基于用户问题和 KG 模式生成 SPARQL 查询。
  • sparql_generation_prompt Default value:
      GRAPHDB_SPARQL_GENERATION_TEMPLATE = """
      Write a SPARQL SELECT query for querying a graph database.
      The ontology schema delimited by triple backticks in Turtle format is:
    
    Use only the classes and properties provided in the schema to construct the SPARQL query.
    Do not use any classes or properties that are not explicitly provided in the SPARQL query.
    Include all necessary prefixes.
    Do not include any explanations or apologies in your responses.
    Do not wrap the query in backticks.
    Do not include any text except the SPARQL query generated.
    The question delimited by triple backticks is:
    
    """
    GRAPHDB_SPARQL_GENERATION_PROMPT = PromptTemplate(
        input_variables=["schema", "prompt"],
        template=GRAPHDB_SPARQL_GENERATION_TEMPLATE,
    )
    

“SPARQL 修复”提示

有时,LLM 可能会生成带有语法错误或缺少前缀等的 SPARQL 查询。链将通过提示 LLM 多次纠正来尝试修复此问题。
  • sparql_fix_prompt Default value:
      GRAPHDB_SPARQL_FIX_TEMPLATE = """
      This following SPARQL query delimited by triple backticks
    
    is not valid.
    The error delimited by triple backticks is
    
    Give me a correct version of the SPARQL query.
    Do not change the logic of the query.
    Do not include any explanations or apologies in your responses.
    Do not wrap the query in backticks.
    Do not include any text except the SPARQL query generated.
    The ontology schema delimited by triple backticks in Turtle format is:
    
    """
    
    GRAPHDB_SPARQL_FIX_PROMPT = PromptTemplate(
        input_variables=["error_message", "generated_sparql", "schema"],
        template=GRAPHDB_SPARQL_FIX_TEMPLATE,
    )
    
  • max_fix_retries Default value: 5

”回答”提示

该提示用于根据从数据库返回的结果和初始用户问题来回答问题。默认情况下,LLM 被指示仅使用返回结果中的信息。如果结果集为空,LLM 应告知它无法回答问题。
  • qa_prompt Default value:
      GRAPHDB_QA_TEMPLATE = """Task: Generate a natural language response from the results of a SPARQL query.
      You are an assistant that creates well-written and human understandable answers.
      The information part contains the information provided, which you can use to construct an answer.
      The information provided is authoritative, you must never doubt it or try to use your internal knowledge to correct it.
      Make your response sound like the information is coming from an AI assistant, but don't add any information.
      Don't use internal knowledge to answer the question, just say you don't know if no information is available.
      Information:
      {context}
    
      Question: {prompt}
      Helpful Answer:"""
      GRAPHDB_QA_PROMPT = PromptTemplate(
          input_variables=["context", "prompt"], template=GRAPHDB_QA_TEMPLATE
      )
    
完成 GraphDB 的 QA 操作后,您可以通过从包含 Docker compose 文件的目录运行以下命令来关闭 Docker 环境: docker compose down -v --remove-orphans
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.