Building a personalized agent with text-in-sql using Amazon Bedrock and Converse API
Developing strong text skills in SQL is a critical challenge in the field of natural language processing (NLP) and database management. The complexity of NLP management and database increases in this area, especially while dealing with complex questions and database structures. In this post, we present a direct but powerful solution with the accompanying code in the text-in-SQL using a custom agent application along with Amazon Bedrock and Converse API.
The ability to translate natural language questions into SQL statements is a player for businesses and organizations because users can now interact with databases in a more intuitive and accessible way. However, the complexity of database schemes, the relationship between tables and natural language shades can often lead to incorrect or incomplete SQL questions. This not only endangers the integrity of the data, but also impedes the general user experience. Through a direct but powerful architecture, the agent can understand your question, develop an execution plan, create SQL statements, self -origate if there is a SQL error, and learn from its execution to improve in the future. Overtime, the agent can develop a cohesive understanding of what to do and what not to do to answer the questions efficiently from the users.
Settlement
The solution is made up of an AWS Lambda function containing the logic of the agent communicating with Amazon Dynamodb for long -term memory holding, calls the anthropic Sonet Claude to Amazon Bedrock through Converse API, uses AWS secret manager to obtain details of AWS database. Lambda function is associated with a private virtual cloud (VPC) and communicates with Dynamodb, Amazon Bedrock, and Secret Manager through AWS Privatelink VPC Point so that Lambda can communicate with RDS database by keeping private traffic through AWS network.
At the demonstration, you can interact with the agent through the Lambda function. You can provide her with a natural language question, such as “How many employees are there in each department in each region?” or “what is the mixing of employees by gender in each region”. Below is the solution architecture.
A custom agent is built using API Converse
Converse API is provided by Amazon Bedrock for you to be able to create conversational applications. Enables powerful features such as the use of tools. Using tools is the ability for a large language model (LLM) to choose from a list of tools, such as running SQL questions against a database, and decide which means to use depending on the context of the conversation. Using Converse API also means that you can hold a series of messages between user roles and assistants to carry out a conversation with a LLM such as anthropic’s Claude 3.5. In this post, a custom agent called ConseSeqlagent was specifically created for executions of tall agents and to follow an execution plan.
Loop Agent: agent’s planning, self-correction and long-term learning
The agent contains several key features: planning and transmission, execution and use of tools, sqlalchemy and self-correction, reflection and long-term learning using memory.
Planning and carrying
The first step the agent takes is to create an execution plan to perform the task of the text-in-SQL. First he thinks through what the user is looking for and develops a plan on how to meet the user’s request. This behavior is controlled using a quick thought, which determines how the agent should behave. After the agent thinks through what to do, he draws the plan.
One of the challenges with the execution of the long agent is that sometimes the agent will forget the plan to be executed as the context becomes longer and longer as it performs its steps. One of the main ways to deal with this is “continuing“ The initial plan by injecting it back into a speed section of the system. System speed is part of any API Converse call, and improves the agent’s ability to follow his plan. Because the agent can review its plan as it progresses through execution, the rapid plan of the system is updated as new plans appear. Refer to the following figure how the carrying works.
The execution and use of tools
Once the plan has been created, the agent will execute his plan one step at a time. Can decide to call one or more tools for which there is access. With Converse API, you can switch to a toolconfig containing toolspec for each tool in which it has access. Toolspec determines what tool, a description of the tool and the parameters that the tool requires. When LLM decides to use a tool, it issues a block of using tools as part of its response. The app, in this case Lambda Code, must identify that block of use of vehicles, execute the relevant tool, add the response of the vehicle output to the messaging list, and call again API Converse. As shown in (a) in the following figure, you can add tools for LLM to select by adding to a toolconfig along with toolspecs. The part (b) indicates that in the implementation of Crosesqlagent, the tool groups contain a collection of tools, and each tool contains the toolspec and the function. The vehicle groups are added to the agent, which in turn adds it to the API Converse call. Tool group instructions are additional instructions on how to use the group of tools that are injected rapidly of the system. Although you can add descriptions to each individual tool, having group instructions can enable the most effective use of the group.
Sqlalchemy and self-correction
The SQL tool group (these tools are part of the given demo code), as shown in the previous figure, is implemented using SQLALCheMY, which is a SQL toolkit Piton that you can use to interfere with various databases without having to worry about the SQL specific data base syntax. You can connect with postgrares, MySQL, and more without having to change your code every time.
In this post, there is an infokesqlquery tool that allows the agent to execute arbitrary SQL statements. Although almost all specific database tasks, such as the search for schemes and tables, can be accomplished through invasksqlquery, it is best to ensure SQLALCHEMI applications for specific tasks, such as GetdatabaSaSemas, which takes each scheme in the database, greatly reducing the time to generate the agent. Think you give the agent a shortcut to get the information it needs. Agents can make errors in questioning the database through the involesqlquery tool. The involesqlquery will respond with the mistake of the agent again, and the agent may perform self-correction to correct the question. This flow is shown in the diagram below.
Reflection and long -term learning using memory
Although self-correction is an important feature of the agent, the agent must be able to learn through his mistakes to avoid the same mistake in the future. Otherwise, the agent will continue to make the mistake, greatly reducing effectiveness and efficiency. The agent holds a hierarchical memory structure, as shown in the following figure. The agent decides how to structure his memory. Here is an example of how it can structure it.
The agent may reflect on his execution, learn best practices and avoiding error, and maintaining it in long -term memory. Long -term memory is applied through a hierarchical memory structure with Amazon Dynamodb. The agent holds a main memory that has indicative of other memories. Memoryo memory is represented as a record in a Dynamodb table. As the agent learns through his execution and the mistakes of the meetings, he can update his main memory and create new memories while preserving an index of memories in the main memory. It can then get into this memory in the future to avoid mistakes and even improve the efficiency of questions while maintaining facts.
PRECONDITIONS
Before you start, make sure you have the following prerequisites:
Set up the solution
Full code and instructions are available in Gititub in the Readme file.
- Cloning the code in your work environment:
git clone https://github.com/aws-samples/aws-field-samples.git
- wend
ConverseSqlAgent
DOSSIER - Follow the steps in the Reading File in Repo Github
cleaning
To dispose of the stack then, call the following command:
cdk destroy
cONcluSiON
Developing strong text skills in SQL is a critical challenge in processing natural language and managing the database. Although current approaches have made progress, there remains room for improvement, especially with complex questions and database structures. Introducing conseqlagent, an application of the custom agent using Amazon Bedrock and Converse API, represents a promising solution to this problem. The architecture of the agent, containing planning and maintenance, execution and use of tools, self-correction through SQLALCheMY, and long-term reflection-based learning, demonstrates his ability to understand natural language questions, develop and execute SQL plans and continuously improve his abilities. As businesses require more intuitive ways to enter and manage data, solutions such as consensqlagent maintain the potential to overcome the gap between the natural language and structured database questions, unlocking new levels of productivity and data -driven decision -making. To dive deeper and learn more about generating, check out these additional sources:
About
Pavan Kumar It is an architect of Amazon Web Services (AWS) solutions, helping clients design powerful, scaled solutions in the cloud through numerous industries. With a backdrop in enterprise architecture and software development, Pavan has contributed to the creation of solutions to address API security, API management, microsizations and cases of use of geospatial information for its clients. It is passionate about teaching new technologies and solving, automation and simplification of customer problems using these solutions.
Abdullah siddiqui He is an architect of partners’ sales solutions at Amazon Web Services (AWS) based on Toronto. It helps partners and clients AWS create solutions using AWS services and specialized in resistance and migration. In his free time, he enjoys spending time with his family and traveling.
Srivastava It is an architect of Amazon Web Services (AWS) solutions, helping customers of enterprises with successful cloud adoption and migration. During his professional career, he has been widely involved in complex digital transformation projects. It is also passionate about building innovative solutions about geospatial aspects of address.
Leave feedback about this