Text-to-SQL
The Text-to-SQL Tool (TextToSQLTool
) is a powerful component in the Canso toolkit that enables AI agents to convert natural language queries into valid SQL statements. It leverages underlying language models to understand user intent and generate appropriate SQL code based on database schema information.
Overview
The Text-to-SQL Tool bridges the gap between natural language understanding and database querying, allowing agents to:
Parse natural language questions about data
Convert these questions into valid, optimized SQL queries
Maintain context about database schemas and relationships
Leverage examples and domain knowledge for better query generation
Usage
The constructor for the TextToSQLTool
has the following parameters:
service
An instance of TextToSQLService
with configured LLM and context retriever
Yes
The input parameters for the tool, provided at runtime, include:
query
Natural language query to convert to SQL
"Show me all customers who made purchases last month"
Yes
table_info
Optional table schema information to initialize or update context
[{"table": "customers", "schema": "CREATE TABLE..."}]
No
Integration
The following code snippet illustrates how the TextToSQLTool
can be integrated with your AI Agent:
Memory Integration
The TextToSQLTool
can leverage CansoMemory to:
Store and retrieve database schemas
Save example queries and their SQL translations
Learn from past query conversions
Maintain domain-specific knowledge related to databases
Memory-enhanced Text-to-SQL transformations become increasingly accurate as the system learns from more examples and builds a richer context of your database structure.
Example
When a user asks a natural language question:
The TextToSQLTool
can generate a SQL query like:
Tool Tips
Last updated
Was this helpful?