SQL Agent with Cohere and LangChain (i-5O Case Study)
This notebook was created in collaboration with i-5O
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
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.
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 sets up the language model to use Cohere’s model. This prepares the necessary components for querying the SQL database using natural language.
Create a prompt template with few shot examples
Next, we create a prompt template with few-shot examples. 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.
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.