Setup and Installation
To use this feature, install the@sap/hana-langchain package and its peer dependencies:
HanaSparqlQAChain ties together:
- Schema-aware SPARQL generation
- Query execution against SAP HANA
- Natural-language answer formatting
Initialization
- 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 a SPARQL-generation prompt
- Inputs:
schema(Turtle fromgraph.getSchema()),prompt(user question)
- Query Post-processing
- Extract SPARQL from model output
- Inject
FROM <graph_uri>if missing - Ensure common prefixes (
rdf:,rdfs:,owl:,xsd:)
- Execution
graph.query(generatedSparql)
- Answer Formulation
- Uses a QA prompt with inputs:
context(results),prompt(original question)
- Uses a QA prompt with inputs:
Prompt templates
”SPARQL Generation” prompt
ThesparqlGenerationPrompt is used to guide the LLM in generating a SPARQL query from the user question and the provided schema.
Answering prompt
TheqaPrompt instructs the LLM to create a natural language answer based solely on the database results.
The default prompts can be found here: prompts.ts
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.getSchema()) 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.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

