Skip to main content

Setup and Installation

To use this feature, install the langchain-hana package:
HanaSparqlQAAgent is an agent-based approach for answering questions over RDF data stored in SAP HANA Cloud. Unlike the chain-based approach, the agent can:
  1. Dynamically retrieve the ontology using a dedicated tool
  2. Generate and execute SPARQL queries iteratively
  3. Self-correct if a query fails or returns unexpected results
  4. Reason step-by-step about complex questions

Initialization

You need:
  • An LLM to power the agent’s reasoning
  • A HanaRdfGraph (with connection, graph_uri, and ontology)
Follow the steps here HanaRdfGraph to know more about creating a HanaRdfGraph instance. Import the HanaSparqlQAAgent

Agent Overview

The agent uses tools iteratively to:
  1. First retrieve the ontology to understand the data structure
  2. Generate appropriate SPARQL queries based on the schema
  3. Execute queries and interpret results
  4. Formulate natural language answers

Defaults

  • Tools:
    • retrieve_ontology - Retrieves the RDF ontology/schema from the graph in Turtle format
    • execute_sparql - Executes SPARQL queries against the HANA RDF graph
  • System Prompt: A default system prompt is provided with instructions for SPARQL generation and tool usage.
  • Middleware: ModelRetryMiddleware(max_retries=3) and ToolRetryMiddleWare(max_retries=2) is added by default to prevent infinite loops

Customizing Your Agent

You can customize the agent’s behavior by providing additional parameters to create_agent:

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.
The example below:
  1. Instantiates the HanaRdfGraph pointing at the movies data graph
  2. Creates a HanaSparqlQAAgent powered by an LLM
  3. Asks natural-language questions and lets the agent reason through the answers
This demonstrates how the agent dynamically retrieves the ontology, generates SPARQL queries, and returns human-readable answers.
Then, set up the knowledge graph instance
After that, initialise the LLM.
Then, create the SPARQL QA Agent.

What’s happening under the hood?

  1. Ontology Retrieval The agent first uses the retrieve_ontology tool to fetch the RDF schema in Turtle format. This gives it understanding of the available classes, properties, and relationships.
  2. SPARQL Generation Based on the ontology and the user’s question, the agent reasons about which entities and properties to query, then generates a valid SELECT query.
  3. Query Execution The agent calls the execute_sparql tool to run the generated query against the HANA RDF graph. If the query fails or returns unexpected results, the agent can self-correct and try again.
  4. Answer Formulation The agent interprets the query results and formulates a concise, human-readable answer based strictly on the retrieved data.