RDFLib
RDFLib is a pure Python package for working with RDF.
RDFLib
contains most things you need to work withRDF
, including:
- parsers and serializers for RDF/XML, N3, NTriples, N-Quads, Turtle, TriX, Trig and JSON-LD
- a Graph interface which can be backed by any one of a number of Store implementations
- store implementations for in-memory, persistent on disk (Berkeley DB) and remote SPARQL endpoints
- a SPARQL 1.1 implementation - supporting SPARQL 1.1 Queries and Update statements
- SPARQL function extension mechanisms
Graph databases are an excellent choice for applications based on network-like models. To standardize the syntax and semantics of such graphs, the W3C recommends Semantic Web Technologies
, cp. Semantic Web.
SPARQL serves as a query language analogously to SQL
or Cypher
for these graphs. This notebook demonstrates the application of LLMs as a natural language interface to a graph database by generating SPARQL
.
Disclaimer: To date, SPARQL
query generation via LLMs is still a bit unstable. Be especially careful with UPDATE
queries, which alter the graph.
Setting upโ
We have to install a python library:
!pip install rdflib
There are several sources you can run queries against, including files on the web, files you have available locally, SPARQL endpoints, e.g., Wikidata, and triple stores.
from langchain.chains import GraphSparqlQAChain
from langchain_community.graphs import RdfGraph
from langchain_openai import ChatOpenAI
graph = RdfGraph(
source_file="http://www.w3.org/People/Berners-Lee/card",
standard="rdf",
local_copy="test.ttl",
)
Note that providing a local_file
is necessary for storing changes locally if the source is read-only.