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.

How?

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 https://github.com/OpenLinkSoftware/langchain.git
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 langchain_to_dbpedia_movies.py and langchain_to_dbpedia_movies.ipynb Jupyter project files, containing:
import os
os.environ["OPENAI_API_KEY"] = "{Your OpenAI API Key}"

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

graph = RdfGraph(query_endpoint="https://dbpedia.org/sparql")

#graph = RdfGraph(
#   query_endpoint="https://dbpedia.org/sparql",
#    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:  <http://dbpedia.org/resource/>
 dbo:  <http://dbpedia.org/ontology/>
 rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

List movies by Spike Lee
"""

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

print(res)
  • Set the OPENAI_API_KEY in the source file to a valid OpenAI Key and run the application with the command:
python3 langchain_to_dbpedia_movies.py

Example output running the langchain_to_dbpedia_movies.py Python sample application from command line:


$  python3 langchain_to_dbpedia_movies.py
 
> Entering new GraphSparqlQAChain chain...
Identified intent:
SELECT
Generated SPARQL:
``
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?movie
WHERE {
    ?movie dbo:director <http://dbpedia.org/resource/Spike_Lee> .
}
``
Full Context:
[(rdflib.term.URIRef('http://dbpedia.org/resource/Rodney_King_(film)'),), (rdflib.term.URIRef('http://dbpedia.org/resource/School_Daze'),), (rdflib.term.URIRef('http://dbpedia.org/resource/BlacKkKlansman'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Crooklyn'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Clockers_(film)'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Freak_(film)'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Get_on_the_Bus'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Girl_6'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Miracle_at_St._Anna'),), (rdflib.term.URIRef("http://dbpedia.org/resource/Mo'_Better_Blues"),), (rdflib.term.URIRef('http://dbpedia.org/resource/NYC_Epicenters_9/11→2021½'),), (rdflib.term.URIRef('http://dbpedia.org/resource/The_Original_Kings_of_Comedy'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Malcolm_X_(1992_film)'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Bad_25_(film)'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Bamboozled'),), (rdflib.term.URIRef('http://dbpedia.org/resource/When_the_Levees_Broke'),), (rdflib.term.URIRef('http://dbpedia.org/resource/25th_Hour'),), (rdflib.term.URIRef('http://dbpedia.org/resource/4_Little_Girls'),), (rdflib.term.URIRef('http://dbpedia.org/resource/American_Utopia_(film)'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Da_5_Bloods'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Da_Sweet_Blood_of_Jesus'),), (rdflib.term.URIRef('http://dbpedia.org/resource/He_Got_Game'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Sucker_Free_City'),), (rdflib.term.URIRef('http://dbpedia.org/resource/A_Huey_P._Newton_Story'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Chi-Raq'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Jim_Brown:_All-American'),), (rdflib.term.URIRef("http://dbpedia.org/resource/Joe's_Bed-Stuy_Barbershop:_We_Cut_Heads"),), (rdflib.term.URIRef('http://dbpedia.org/resource/Jungle_Fever'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Summer_of_Sam'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Do_the_Right_Thing'),), (rdflib.term.URIRef("http://dbpedia.org/resource/If_God_Is_Willing_and_da_Creek_Don't_Rise"),), (rdflib.term.URIRef("http://dbpedia.org/resource/Kobe_Doin'_Work"),), (rdflib.term.URIRef("http://dbpedia.org/resource/Michael_Jackson's_Journey_from_Motown_to_Off_the_Wall"),), (rdflib.term.URIRef('http://dbpedia.org/resource/Oldboy_(2013_film)'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Red_Hook_Summer'),), (rdflib.term.URIRef("http://dbpedia.org/resource/She's_Gotta_Have_It"),), (rdflib.term.URIRef("http://dbpedia.org/resource/She's_Gotta_Have_It_(TV_series)"),), (rdflib.term.URIRef('http://dbpedia.org/resource/She_Hate_Me'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Pass_Over__Pass_Over__1'),), (rdflib.term.URIRef('http://dbpedia.org/resource/Inside_Man__Inside_Man__1'),), (rdflib.term.URIRef('http://dbpedia.org/resource/All_the_Invisible_Children__Jesus_Children_of_America__1'),)]

> 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.
$

Related