SQL Database Chain
Overview
The SQL Database Chain is a powerful feature in AnswerAI that allows you to query SQL databases using natural language. This feature bridges the gap between human language and SQL, enabling you to interact with your databases more intuitively and efficiently.
Key Benefits
- Natural Language Queries: Ask questions about your data in plain English, without writing complex SQL queries.
- Multiple Database Support: Compatible with SQLite, PostgreSQL, MSSQL, and MySQL databases.
- Customizable and Flexible: Tailor the chain to your specific needs with various configuration options.
How to Use
Follow these steps to set up and use the SQL Database Chain:
-
Select the Language Model: Choose a language model that will interpret your natural language queries and generate SQL.
-
Choose Your Database Type: Select the type of database you're working with (SQLite, PostgreSQL, MSSQL, or MySQL).
-
Provide Connection Details: Enter the connection string or file path for your database.
-
Configure Table Selection (Optional):
- To include specific tables: Enter table names separated by commas in the "Include Tables" field.
- To exclude specific tables: Enter table names separated by commas in the "Ignore Tables" field.
Note: You can use either "Include Tables" or "Ignore Tables", but not both simultaneously.
-
Set Additional Parameters (Optional):
- Sample Rows: Specify the number of sample rows to load for each table.
- Top K Results: Limit the maximum number of results returned for each query.
-
Customize the Prompt (Optional): If needed, provide a custom prompt to guide the language model's interpretation of your queries.
-
Enable Input Moderation (Optional): Set up input moderation to filter out potentially harmful queries before they reach the language model.
-
Run Your Query: Enter your natural language question about the database, and the SQL Database Chain will process it and return the results.
Tips and Best Practices
-
Start Simple: Begin with straightforward queries to understand how the system interprets your questions.
-
Be Specific: The more precise your question, the more accurate the results will be.
-
Use Table Names: Including relevant table names in your query can help the system generate more accurate SQL.
-
Leverage Sample Rows: Setting an appropriate number of sample rows can improve the system's understanding of your data structure without overloading it.
-
Optimize with Top K: Use the Top K parameter to limit results for large tables, improving performance and reducing token usage.
-
Customize Prompts Carefully: If you're using a custom prompt, ensure it includes the required variables:
{input}
,{dialect}
, and{table_info}
.
Troubleshooting
-
Connection Issues:
- Double-check your connection string or file path.
- Ensure your database is accessible from the environment where AnswerAI is running.
-
Unexpected Results:
- Verify that you've included all necessary tables if using the "Include Tables" option.
- Check if any crucial tables are being excluded by the "Ignore Tables" option.
-
Performance Problems:
- If queries are slow, try reducing the number of sample rows or using the Top K parameter to limit results.
-
Incorrect Interpretations:
- Rephrase your question to be more specific or include key table names.
- Consider using a custom prompt to guide the model's interpretation if you're consistently getting incorrect results for certain types of queries.
Remember, the SQL Database Chain is a powerful tool that becomes more effective as you learn to phrase your questions in a way that aligns with your database structure. Don't hesitate to experiment and refine your approach to get the best results.