Using LangChain with Ontotext GraphDB

Posted: Jan 30, 2025.

The Ontotext GraphDB integration in LangChain allows you to interact with GraphDB, a powerful RDF and SPARQL-compliant graph database. In this guide, we'll explore how to use the OntotextGraphDBGraph class to query and work with graph data.

What is OntotextGraphDBGraph?

OntotextGraphDBGraph is a wrapper class that enables interaction with Ontotext GraphDB graph databases. It provides functionality to:

  • Execute SPARQL queries against a GraphDB endpoint
  • Load and work with ontology schemas from either SPARQL CONSTRUCT queries or local RDF files
  • Handle authentication for secured GraphDB instances

The class is particularly useful when you need to perform semantic queries against RDF data stored in GraphDB and integrate it with LangChain's capabilities.

Reference

Method/AttributeDescription
__init__()Constructor that takes query endpoint URL and either a CONSTRUCT query or local file path to define the ontology schema
query()Executes a SPARQL query against the graph database
get_schemaReturns the schema of the graph database in turtle format

How to use OntotextGraphDBGraph

Basic Setup

First, create an instance of OntotextGraphDBGraph by specifying the query endpoint and schema source:

from langchain_community.graphs import OntotextGraphDBGraph

# Using a CONSTRUCT query
graph = OntotextGraphDBGraph(
    query_endpoint="http://localhost:7200/repositories/myrepo",
    query_ontology="CONSTRUCT {?s ?p ?o} FROM <https://example.com/ontology/> WHERE {?s ?p ?o}"
)

# Or using a local RDF file
graph = OntotextGraphDBGraph(
    query_endpoint="http://localhost:7200/repositories/myrepo",
    local_file="path/to/ontology.ttl"
)

Working with Secured GraphDB

If your GraphDB instance requires authentication, set the credentials using environment variables:

import os

os.environ["GRAPHDB_USERNAME"] = "your-username"
os.environ["GRAPHDB_PASSWORD"] = "your-password"

graph = OntotextGraphDBGraph(
    query_endpoint="http://localhost:7200/repositories/secured-repo",
    query_ontology="CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}"
)

Executing Queries

Use the query() method to execute SPARQL queries:

# Simple query example
results = graph.query("""
    PREFIX : <https://example.com/ontology/>
    SELECT ?name
    WHERE {
        ?person :name ?name
    }
""")

Loading Schema from Different RDF Formats

OntotextGraphDBGraph supports various RDF formats when loading from local files:

# Loading from Turtle format
graph = OntotextGraphDBGraph(
    query_endpoint="http://localhost:7200/repositories/myrepo",
    local_file="ontology.ttl"
)

# Loading from RDF/XML with explicit format
graph = OntotextGraphDBGraph(
    query_endpoint="http://localhost:7200/repositories/myrepo",
    local_file="ontology.xml",
    local_file_format="xml"
)

Supported formats include:

  • Turtle (.ttl)
  • RDF/XML (.xml)
  • JSON-LD (.jsonld)
  • N-Triples (.nt)
  • Notation-3 (.n3)
  • TriG (.trig)
  • TriX (.trix)
  • N-Quads (.nq)

Integration with Other LangChain Components

OntotextGraphDBGraph is commonly used with OntotextGraphDBQAChain for natural language querying:

from langchain.chains import OntotextGraphDBQAChain
from langchain_openai import ChatOpenAI

chain = OntotextGraphDBQAChain.from_llm(
    ChatOpenAI(temperature=0),
    graph=graph,
    verbose=True
)

# Ask questions in natural language
result = chain.invoke({
    "question": "What is the climate on planet X?"
})

This integration enables powerful natural language interactions with your graph database, combining the semantic capabilities of GraphDB with LangChain's LLM features.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs