how to chat with Oracle 23ai using SQL agent

 

Introduction:

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

To build this chatbot I am using SQL agent, we can also build a chatbot using chain but SQL agent have 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 question.

4. It will save tokens by only retrieving the schema from relevant tables.

You can check more about SQL agent 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 variable 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 result using below command, you can use any query to get result from database. for example.

agent_executor.invoke("Describe the playlisttrack table")


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

I am using StreamlitCallbackHandler

StreamlitCallbackHandler is use to make LangChain and Streamlit work together, it’s like for each response from 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, lets run it. To run use below command (test.py is the name of my file)

streamlit run test.py 

and it will open a instance is your browser.



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


If you see above the beauty of SQL agent is , it go through the tables/schema and show the query and result.

9. Lets ask another question. (List number of customers as per country?)



Hope you like the Blog!

No comments:

Post a Comment

How To Disable Automatic Database Backups Using DBAASCLI on ExaCC

  In this blog post I will show you how to disable Automatic database backups on ExaCC using dbaascli.