SQL Retriever

BasicSQLRetriever runs natural-language or SQL queries against a SQL data store and returns results as a DataFrame. It uses the legacy BaseSQLDataStore interface. You can optionally use an LM to fix failed queries (with retries).

Best For:

  • Natural-language or SQL queries over a relational database

  • Structured, tabular results

  • Optional automatic query correction via an LM when execution fails

Key Features:

  • Single query string (natural language or SQL)

  • Returns pd.DataFrame or (executed_query, pd.DataFrame) when return_query=True

  • Optional lm_request_processor and max_retries to fix and re-run failed queries

  • Optional preprocess_query_func and extract_func for custom query handling

chevron-rightPrerequisiteshashtag

You should be familiar with:

  1. LM Request Processor (optional, for retrying failed queries)

Installation

pip install --extra-index-url https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/ "gllm-retrieval"

What it does

The SQL Retriever executes a query (natural language or SQL) against the configured SQL data store. If execution fails and you provided an lm_request_processor with max_retries > 0, it can ask the LM to correct the query and retry.

Usage

Basic usage with a SQL data store:

With automatic retry on failure (LM fixes the query):

circle-info

Implementation notes: BasicSQLRetriever uses a default system prompt to ask the LM to correct failed SQL queries. You can pass a custom prompt via LMRequestProcessor. The extract_func argument controls how the modified query is parsed from the LM output; the default expects a single string. See the API Referencearrow-up-right for details.

Last updated

Was this helpful?