Setup and Installation
To use this feature, install thelangchain-hana package:
HanaSparqlQAChain ties together:
- Schema-aware SPARQL generation
- Query execution against SAP HANA
- Natural-language answer formatting
Initialization
You need:- An LLM to generate and interpret queries
- A
HanaRdfGraph(with connection,graph_uri, and ontology)
HanaRdfGraph instance.
Import the HanaSparqlQAChain
Pipeline overview
- SPARQL Generation
- Uses
SPARQL_GENERATION_SELECT_PROMPT - Inputs:
schema(Turtle fromgraph.get_schema)prompt(user’s question)
- Query Post-processing
- Extracts the SPARQL code from the llm output.
- Inject
FROM <graph_uri>if missing - Ensure required common prefixes are declared (
rdf:,rdfs:,owl:,xsd:)
- Execution
- Calls
graph.query(generated_sparql)
- Answer Formulation
- Uses
SPARQL_QA_PROMPT - Inputs:
context(raw query results)prompt(original question)
Prompt templates
”SPARQL Generation” prompt
Thesparql_generation_prompt is used to guide the LLM in generating a SPARQL query from the user question and the provided schema.
Answering prompt
Theqa_prompt instructs the LLM to create a natural language answer based solely on the database results.
The default prompts can be found here: prompts.py
Customizing prompts
You can override the defaults at initialization:
sparql_generation_promptmust have the input variables:["schema", "prompt"]qa_promptmust have the input variables:["context", "prompt"]
Example: Question answering over a “Movies” knowledge graph
Prerequisite: You must have an SAP HANA Cloud instance with the triple store feature enabled. For detailed instructions, refer to: Enable Triple StoreLoad the
kgdocu_movies example data. See Knowledge Graph Example.
Below we’ll:
- Instantiate the
HanaRdfGraphpointing at our “movies” data graph - Wrap it in a
HanaSparqlQAChainpowered by an LLM - Ask natural-language questions and print out the chain’s responses
What’s happening under the hood?
- SPARQL Generation
The chain invokes the LLM with your Turtle-formatted ontology (
graph.get_schema) and the user’s question using theSPARQL_GENERATION_SELECT_PROMPT. The LLM then emits a validSELECTquery tailored to your schema. - Pre-processing & Execution
- Extract & clean: Pull the raw SPARQL text out of the LLM’s response.
- Inject graph context: Add
FROM <graph_uri>if it’s missing and ensure common prefixes (rdf:,rdfs:,owl:,xsd:) are declared. - Run on HANA: Execute the finalized query via
HanaRdfGraph.query()over your named graph.
- Answer Formulation
The returned CSV (or Turtle) results feed into the LLM again—this time with the
SPARQL_QA_PROMPT. The LLM produces a concise, human-readable answer strictly based on the retrieved data, without hallucination.