Generate a query for structured data - Amazon Bedrock

Generate a query for structured data

If you connect a structured data store to your knowledge base, your knowledge base can query it by converting the natural language query provided by the user into an SQL query, based on the structure of the data source being queried. When you use Retrieve, the response returns the result of the SQL query execution. When you use RetrieveAndGenerate, the generated response is based on the result of the SQL query execution

Amazon Bedrock Knowledge Bases also allows you to decouple the conversion of the query from the retrieval process, by using the GenerateQuery API operation to transform a query into SQL. You can use the response with a subsequent Retrieve or RetrieveAndGenerate action, or insert it into other workflows. GenerateQuery allows you to efficiently transform queries into SQL queries by taking into consideration the structure of your knowledge base's data source.

Important

The accuracy of a generated SQL query can vary depending on context, table schemas, and the intent of a user query. Evaluate the generated queries to ensure that they suit your use case before using them in your workload.

To turn a natural language query into a SQL query, submit a GenerateQuery request with an Agents for Amazon Bedrock runtime endpoint. The GenerateQuery request contains the following fields:

  • queryGenerationInput – Specify TEXT as the type and include the query in the text field.

    Note

    Queries must be written in English.

  • transformationConfiguration – Specify TEXT_TO_SQL as the mode. In the textToSqlConfiguration field, specify KNOWLEDGE_BASE as the type. Then, specify the ARN of the knowledge base.

Note

The GenerateQuery API has a quota of 2 requests per second.

The response returns an array containing a GeneratedQuery object in the queries field. The object contains an SQL query for the query in the sql field.