Skip to main content

Setup and Installation

To use this feature, install the langchain-hana package:
And then, create a connection to your SAP HANA Cloud instance.
HanaSparqlQAChain ties together:
  1. Schema-aware SPARQL generation
  2. Query execution against SAP HANA
  3. Natural-language answer formatting

Initialization

You need:
  • An LLM to generate and interpret queries
  • A HanaRdfGraph (with connection, graph_uri, and ontology)
Follow the steps here HanaRdfGraph to know more about creating a HanaRdfGraph instance. Import the HanaSparqlQAChain

Pipeline overview

  1. SPARQL Generation
    • Uses SPARQL_GENERATION_SELECT_PROMPT
    • Inputs:
      • schema (Turtle from graph.get_schema)
      • prompt (user’s question)
  2. 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:)
  3. Execution
    • Calls graph.query(generated_sparql)
  4. Answer Formulation
    • Uses SPARQL_QA_PROMPT
    • Inputs:
      • context (raw query results)
      • prompt (original question)

Prompt templates

”SPARQL Generation” prompt

The sparql_generation_prompt is used to guide the LLM in generating a SPARQL query from the user question and the provided schema.

Answering prompt

The qa_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_prompt must have the input variables: ["schema", "prompt"]
  • qa_prompt must 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 Store
Load the kgdocu_movies example data. See Knowledge Graph Example.
Below we’ll:
  1. Instantiate the HanaRdfGraph pointing at our “movies” data graph
  2. Wrap it in a HanaSparqlQAChain powered by an LLM
  3. Ask natural-language questions and print out the chain’s responses
This demonstrates how the LLM generates SPARQL under the hood, executes it against SAP HANA, and returns a human-readable answer. First, create a connection to your SAP HANA Cloud instance.
Then, set up the knowledge graph instance
After that, initialise the LLM.
Then, we create a SPARQL QA Chain

What’s happening under the hood?

  1. SPARQL Generation The chain invokes the LLM with your Turtle-formatted ontology (graph.get_schema) and the user’s question using the SPARQL_GENERATION_SELECT_PROMPT. The LLM then emits a valid SELECT query tailored to your schema.
  2. 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.
  3. 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.