SQL Agent with Cohere and LangChain (i-5O Case Study)
SQL Agent Demo with Cohere and LangChain
This tutorial demonstrates how to create a SQL agent using Cohere and LangChain. The agent can translate natural language queries coming from users into SQL, and execute them against a database. This powerful combination allows for intuitive interaction with databases without requiring direct SQL knowledge.
Key topics covered:
- Setting up the necessary libraries and environment
- Connecting to a SQLite database
- Configuring the LangChain SQL Toolkit
- Creating a custom prompt template with few-shot examples
- Building and running the SQL agent
- Adding memory to the agent to keep track of historical messages
By the end of this tutorial, you’ll have a functional SQL agent that can answer questions about your data using natural language.
This tutorial uses a mocked up data of a manufacturing environment where a product item’s production is tracked across multiple stations, allowing for analysis of production efficiency, station performance, and individual item progress through the manufacturing process. This is modelled after a real customer use case.
The database contains two tables:
- The
product_tracking
table records the movement of items through different zones in manufacturing stations, including start and end times, station names, and product IDs. - The
status
table logs the operational status of stations, including timestamps, station names, and whether they are productive or in downtime.
- Import the required libraries
- Load the database
- Setup the LangChain SQL Toolkit
- Create a prompt template with few shot examples
- Create the agent
- Run the agent
Import the required libraries
First, let’s import the necessary libraries for creating a SQL agent using Cohere and LangChain. These libraries enable natural language interaction with databases and provide tools for building AI-powered agents.
Load the database
Next, we load the database for our manufacturing data.
Download the sql files from the link below to create the database.
We create an in-memory SQLite database using SQL scripts for the product_tracking
and status
tables. You can get the SQL tables here.
We then create a SQLDatabase instance, which will be used by our LangChain tools and agents to interact with the data.
Setup the LangChain SQL Toolkit
Next, we initialize the LangChain SQL Toolkit and initialize the language model to use Cohere’s LLM. This prepares the necessary components for querying the SQL database using natural language.
Create a prompt template
Next, we create a prompt template. In this section, we will introduce a simple system message, and then also show how we can improve the prompt by introducing few shot prompting examples in the later sections. The system message is used to communicate instructions or provide context to the model at the beginning of a conversation.
In this case, we provide the model with context on what SQL dialect it should use, how many samples to query among other instructions.
Create a few-shot prompt template
In the above step, we’ve created a simple system prompt. Now, let us see how we can create a better few shot prompt template in this section. Few-shot examples are used to provide the model with context and improve its performance on specific tasks. In this case, we’ll prepare examples of natural language queries and their corresponding SQL queries to help the model generate accurate SQL statements for our database.
In this example, we use SemanticSimilarityExampleSelector
to select the top k examples that are most similar to an input query out of all the examples available.
Create the agent
Next, we create an instance of the SQL agent using the LangChain framework, specifically using create_sql_agent
.
This agent will be capable of interpreting natural language queries, converting them into SQL queries, and executing them against our database. The agent uses the LLM we defined earlier, along with the SQL toolkit and the custom prompt we created.
Run the agent
Now, we can run the agent and test it with a few different queries.
Memory in the sql agent
We may want the agent to hold memory of our previous messages so that we’re able to coherently engage with the agent to answer our queries. In this section, let’s take a look at how we can add memory to the agent so that we’re able to achieve this outcome!
In the code snippets below, we create a class to store the chat history in memory. This can be customised to store the messages from a database or any other suitable data store.
In the below code snippet, we make use of the RunnableWithMessageHistory abstraction to wrap around the agent we’ve created above to provide the message history to the agent that we can now utilize by chatting with the agent_with_chat_history
as shown below.
We can see from the above code snippets that the agent is automatically able to infer and query with respect to ‘stn2’ in the above question without us having to specify it explicitly. This allows us to have more coherent conversations with the agent.
Conclusion
This tutorial demonstrated how to create a SQL agent using Cohere and LangChain. The agent can translate natural language queries coming from users into SQL, and execute them against a database. This powerful combination allows for intuitive interaction with databases without requiring direct SQL knowledge.