Skip to main content

Setup and Installation

To use this feature, install the @sap/hana-langchain package and its peer dependencies:
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

  • 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 a SPARQL-generation prompt
    • Inputs: schema (Turtle from graph.getSchema()), prompt (user question)
  2. Query Post-processing
    • Extract SPARQL from model output
    • Inject FROM <graph_uri> if missing
    • Ensure common prefixes (rdf:, rdfs:, owl:, xsd:)
  3. Execution
    • graph.query(generatedSparql)
  4. Answer Formulation
    • Uses a QA prompt with inputs: context (results), prompt (original question)

Prompt templates

”SPARQL Generation” prompt

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

Answering prompt

The qaPrompt 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_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.getSchema()) 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.