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.
You can check more about SQL agents on this LangChain document… LangChain Doc
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.)
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?)