Interacting with Virtuoso via LangChain

LangChain is a well-regarded Python framework designed for constructing applications that include fine-tuning oriented guardrails, such as Retrieval Augmented Generation (RAG)-enhanced prompt responses, around Large Language Models (LLMs). This post highlights how SPARQL access, facilitated by Virtuoso, integrates the Linked Open Data (LOD Cloud) into the framework, using DBpedia, thanks to our LangChain Module.


Environment Setup & Checks

  1. Ensure Python ver >= 3.7.1 for OpenAI is installed and functional.

  2. Ensure the latest pip package is installed:

python3 -m pip install --upgrade pip
  1. Install the OpenLink custom LangChain package:
git clone
cd langchain/libs/langchain/
pip3 install -e .
  1. Install the langchain-openai package:
pip3 install langchain-openai
  1. The OpenLink langchain.samples git repository contains Python sample applications and langchain_to_dbpedia_movies.ipynb Jupyter project files, containing:
import os
os.environ["OPENAI_API_KEY"] = "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"

from langchain_openai import ChatOpenAI
from langchain_community.graphs import RdfGraph
from langchain.chains import GraphSparqlQAChain

graph = RdfGraph(query_endpoint="")

#graph = RdfGraph(
#   query_endpoint="",
#    standard="rdf",
#    local_copy="test.ttl"

chain = GraphSparqlQAChain.from_llm(
    ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0, verbose=True), graph=graph, verbose=True
#    ChatOpenAI(model="gpt-4-0125-preview", temperature=0, verbose=True), graph=graph, verbose=True

query = """
Relevant DBpedia Knowledge Graph relationship types (relations):
  ?movie rdf:type dbo:Film .
  ?movie dbo:director ?name .

Associated namespaces:
 dbr:  <>
 dbo:  <>
 rdf:  <>

List movies by Spike Lee

res = chain.invoke({chain.input_key: query})[chain.output_key]

  • Set the OPENAI_API_KEY in the source file to a valid OpenAI Key and run the application with the command:

Example output running the Python sample application from command line:

$  python3
> Entering new GraphSparqlQAChain chain...
Identified intent:
Generated SPARQL:
PREFIX dbo: <>
SELECT ?movie
    ?movie dbo:director <> .
Full Context:
[(rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef("'_Better_Blues"),), (rdflib.term.URIRef('→2021½'),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef("'s_Bed-Stuy_Barbershop:_We_Cut_Heads"),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef("'t_Rise"),), (rdflib.term.URIRef("'_Work"),), (rdflib.term.URIRef("'s_Journey_from_Motown_to_Off_the_Wall"),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef("'s_Gotta_Have_It"),), (rdflib.term.URIRef("'s_Gotta_Have_It_(TV_series)"),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),), (rdflib.term.URIRef(''),)]

> Finished chain.
Certainly! Here is a list of movies directed by Spike Lee:

1. Rodney King
2. School Daze
3. BlacKkKlansman
4. Crooklyn
5. Clockers
6. Freak
7. Get on the Bus
8. Girl 6
9. Miracle at St. Anna
10. Mo' Better Blues
11. NYC Epicenters 9/11→2021½
12. The Original Kings of Comedy
13. Malcolm X (1992)
14. Bad 25
15. Bamboozled
16. When the Levees Broke
17. 25th Hour
18. 4 Little Girls
19. American Utopia
20. Da 5 Bloods
21. Da Sweet Blood of Jesus
22. He Got Game
23. Sucker Free City
24. A Huey P. Newton Story
25. Chi-Raq
26. Jim Brown: All-American
27. Joe's Bed-Stuy Barbershop: We Cut Heads
28. Jungle Fever
29. Summer of Sam
30. Do the Right Thing
31. If God Is Willing and da Creek Don't Rise
32. Kobe Doin' Work
33. Michael Jackson's Journey from Motown to Off the Wall
34. Oldboy (2013)
35. Red Hook Summer
36. She's Gotta Have It
37. She's Gotta Have It (TV series)
38. She Hate Me
39. Pass Over
40. Inside Man
41. All the Invisible Children (segment "Jesus Children of America")

These films showcase Spike Lee's diverse filmmaking style, ranging from drama and comedy to documentary and biographical films.