Eclipsys Blog

How to Chat with Oracle 23ai using SQL Agent

Written by Manoj Kumar | Aug 26, 2024 6:00:00 PM

 

Introduction:

In this blog post, I am going to build a simple chatbot where we can interact with the Oracle 23ai database.

To build this chatbot I am using SQL agent, we can also build a chatbot using chain but SQL agent has advantages over chain.

  1. It can answer questions based on the databases’ schema as well as on the databases’ content (like describing a specific table).
  2. It can recover from errors by running a generated query, catching the traceback, and regenerating it correctly.
  3. It can query the database as many times as needed to answer the user’s question.
  4. It will save tokens by only retrieving the schema from relevant tables.

You can check more about SQL agents on this LangChain document… LangChain Doc

 

Pre-requisites:

  1. Oracle 23ai access
  2. Access to any LLM model (I am using here OpenAI model)
  3. VS code and Python

I am using LangChain (It is a tool to connect LLM and provide interfaces to use them) and Streamlit (is a tool that allows us to write UI-included apps with Python.)

 

Steps:

1. Import packages 

 

2. Load environment variables like OpenAI key and define database connection string ( I am using Oracle 23ai from my local machine)

 

3. Setup Agent

 

4. Now we can just invoke the agent to get the result using the below command, you can use any query to get the result from the database. for example.

agent_executor.invoke(“Describe the playlisttrack table”)

 

5. But I want to create a nice UI to query the database using this agent. Below is the Streamlit code.

I am using StreamlitCallbackHandler

StreamlitCallbackHandler is used to make LangChain and Streamlit work together, it’s like for each response from the LangChain agent, it will call a function to update the Streamlit UI. The function is provided by Streamlit and included in LangChain.

 

6. I also define the Page title 

 

7. Now simple chatbot is ready, let’s run it. To run use the below command (test.py is the name of my file)

streamlit run test.py 

and it will open an instance is your browser.

 

8. Now let me ask a question, like “How many customer belongs to India.”

As you see above the beauty of an SQL agent is, that it goes through the tables/schema and shows the query and result.

 

9. Let’s ask another question. (List the number of customers as per country?)