Create a Text2SQL agent
Build a Text2SQL agent leveraging AIBuilder's RAG capabilities. Contributed by @TeslaZY.
#
ScenarioThe Text2SQL agent bridges the gap between Natural Language Processing (NLP) and Structured Query Language (SQL). Its key advantages are as follows:
Assisting non-technical users with SQL: Not all users have a background in SQL or understand the structure of the tables involved in queries. With a Text2SQL agent, users can pose questions or request data in natural language without needing an in-depth knowledge of the database structure or SQL syntax.
Enhancing SQL development efficiency: For those familiar with SQL, the Text2SQL agent streamlines the process by enabling users to construct complex queries quickly, without the need to code each part manually.
Minimizing errors: Manually writing SQL queries can be error-prone, particularly for complex queries or for users not well-versed in the database structure. The Text2SQL agent can interpret natural language instructions and generate accurate SQL queries, thereby reducing potential syntax and logic errors.
Boosting data analysis capabilities: In business intelligence and data analysis, swiftly gaining insights from data is critical. The Text2SQL agent facilitates extracting valuable information from databases more directly and conveniently, thus aiding in accelerating decision-making.
Automation and integration: The Text2SQL agent can be integrated into larger systems to support automated workflows, such as automatic report generation and data monitoring. It can also integrate seamlessly with other services and technologies, offering richer application possibilities.
Support for multiple languages and varied expressions: People can express the same idea in numerous ways. An effective Text2SQL system should be capable of understanding diverse expressions and accurately converting them into SQL queries.
In summary, the Text2SQL agent seeks to make database queries more intuitive and user-friendly while ensuring efficiency and accuracy. It caters to a broad spectrum of users, from completely non-technical individuals to seasoned data analysts and developers.
However, traditional Text2SQL solutions often require model fine-tuning, which can substantially escalate deployment and maintenance costs when implemented in enterprise environments alongside RAG or Agent components. AIBuilder’s RAG-based Text2SQL utilizes an existing (connected) large language model (LLM), allowing for seamless integration with other RAG/Agent components without the necessity for additional fine-tuned models.
#
RecipeA list of components required:
#
Procedure#
Preparation of Data#
Database EnvironmentMysql-8.0.39
#
Database Table Creation Statements#
Generate Test Data#
Configure Knowledge BaseFor AIBuilder’s RAG-based Text2SQL, the following knowledge bases are typically required:
- DDL: Database table creation statements.
- DB_Description: Detailed descriptions of tables and columns.
- Q->SQL: Natural language query descriptions along with corresponding SQL query examples (Question-Answer pairs).
However, in specialized query scenarios, user queries might include abbreviations or synonyms for domain-specific terms. If a user references a synonym for a domain-specific term, the system may fail to generate the correct SQL query. Therefore, it is advisable to incorporate a thesaurus for synonyms to assist the Agent in generating more accurate SQL queries.
- TextSQL_Thesaurus: A thesaurus covering domain-specific terms and their synonyms.
#
Configure DDL Knowledge Base- The content of the DDL text is as follows:
- Set the chunk data for the DLL knowledge base
#
Configure DB_Description Knowledge Base- the content of the DB_Description text is as follows:
- set the chunk data for the DB_Description knowledge base
#
Configure Q->SQL Knowledge Base- Q->SQL Excel Document QA.xlsx
- Upload the Q->SQL Excel document to the Q->SQL knowledge base and set the chunk data as follows via parsing:
#
Configure TextSQL_Thesaurus Knowledge Base- the content of the TextSQL_Thesaurus text is as follows:
- set the chunk data for the TextSQL_Thesaurus knowledge base
#
Build the Agent- Create an Agent using the Text2SQL Agent template.
- Enter the configuration page of the Agent to start the setup process.
- Create a Retrieval node and name it Thesaurus; create an ExeSQL node.
- Configure the Q->SQL, DDL, DB_Description, and TextSQL_Thesaurus knowledge bases. Please refer to the following:
- Configure the Generate node, named LLM's prompt:
- Add this content to the prompt provided by the template to provide the thesaurus content to the LLM:
- Ensure the mapping between keys and component IDs is configured correctly.
- The configuration result should look like this:
- Add this content to the prompt provided by the template to provide the thesaurus content to the LLM:
- Configure the ExecSQL node, filling in the configuration information for the MySQL database.
- Set an opener in the Begin component like:
#
Run and Test the Agent- click the Run button to start the agent.
- input the question:
- click the send button to send the question to the agent.
- The agent will respond with the following:
#
Debug the AgentSince version 0.15.0, AIBuilder has introduced step-by-step execution for Agent components/tools, providing a robust mechanism for debugging and testing. Let's explore how to perform a step run.
To enter Test Run mode, you can either click the triangle icon located above the component or access the component's detail page by clicking on the component itself. Once there, select the Test Run button in the upper right corner of the component details.
Enter a question that does not exist in the Q->SQL knowledge base but is similar in nature. Click the Run button to receive the component's output.
3. As the image shows, no matching information was retrieved from the Q->SQL knowledge base, yet a similar question exists within the database. Adjust the Rerank model, "Similarity threshold," or "Keyword similarity weight" accordingly to return relevant content.
Observe the inputs and outputs of the LLM node and ExeSQL node.
The agent now produces the correct SQL query result.
For a query about "mobile phone," the agent successfully generates the appropriate SQL query using "Smartphone." This showcases how the thesaurus guides the LLM in generating accurate SQL queries.
With this, you maybe appreciate the capabilities of Step Run. It undoubtedly assists in constructing more effective agents.
#
Troubleshooting#
Total: 0 No record in the database!- Confirm if the sql is correct. If so, check the connection information of the database.
- If the connection information is correct, maybe there is actually no data matching your query in the database.
#
ConsiderationsIn real production scenarios within vertical domains, several considerations are essential for effective Text2SQL implementation:
Handling DDL and DB_Description: Dealing with Data Definition Language (DDL) statements and database descriptions requires substantial debugging experience. It is crucial to discern which information is vital and which may be redundant, depending on the true business context. This includes determining the relevance of table attributes such as primary keys, foreign keys, indexes, and so forth.
Maintaining Quality QA Data: Ensuring a high standard for question-and-answer data significantly aids the LLM in generating more accurate SQL queries.
Managing Domain-Specific Synonyms: Professional domain synonyms can greatly impact the generation of SQL query conditions. Therefore, maintaining an extensive and up-to-date synonym library is critical to mitigate this challenge.
Facilitating User Feedback: Implementing a feedback mechanism within the Agent allows users to provide correct SQL queries. Administrators can then use this feedback to automatically generate corresponding QA data, reducing the need for manual maintenance.
In summary, achieving high-quality output from Text2SQL remains contingent upon high-quality input. Constructing robust question-and-answer datasets is at the core of optimizing AIBuilder's Text2SQL capabilities.