Note:
- This tutorial requires access to Oracle Cloud. To sign up for a free account, see Get started with Oracle Cloud Infrastructure Free Tier.
- It uses example values for Oracle Cloud Infrastructure credentials, tenancy, and compartments. When completing your lab, substitute these values with ones specific to your cloud environment.
Create RAG based Chatbot using Oracle Cloud Infrastructure Search with OpenSearch
Introduction
In this tutorial, we will guide you through the process of creating a retrieval-augmented generation (RAG) based chatbot using OCI OpenSearch as a vector database and OCI Generative AI service models (such as Cohere and Llama). By the end of this tutorial, you will have built a chatbot solution capable of processing documents, storing them as vector embeddings in OpenSearch, and retrieving relevant information during conversations.
Additionally, we will create an intuitive user interface using Streamlit, allowing you to upload documents, interact with the chatbot, and access relevant information effortlessly. This tutorial covers everything from setting up your environment to testing the chatbot with your own PDF files.
Objectives
-
Set up an OpenSearch cluster on OCI for storing document embeddings.
-
Configure your development environment with Conda and required dependencies.
-
Set up OCI authentication for seamless integration.
-
Write and configure the chatbot engine code.
-
Build an interactive UI using Streamlit for uploading documents and querying the chatbot.
-
Upload documents (PDFs) and interact with the chatbot to query them.
Prerequisites
-
An OCI account with necessary permissions to create OpenSearch clusters.
-
A virtual machine (VM) with Python installed.
-
Familiarity with vector databases and large language models.
-
Basic understanding of Application Programming Interface (API) and chatbot engine development.
-
Create Search with OpenSearch IAM Policies and OCI Generative AI Policies.
Task 1: Set up an OpenSearch Cluster with OCI with OpenSearch
-
Create a cluster. For more information, see Create an OpenSearch cluster with OCI Search with OpenSearch
-
Once the cluster is created, copy the API endpoint, along with the user credentials to configure them in the app.
Task 2: Configure the Python Environment
-
Launch an instance with the Oracle Linux image and basic shape in OCI. For more information, see Launching a Linux Instance.
-
Install Python 3.11 on Oracle Linux. For more information, see Installing Python .
-
Run the following command to update the Python version.
sudo update-alternatives --set python /usr/bin/python3.11
-
Create a project folder named
opensearch_rag_chatbot
. -
Go to the folder created and install dependencies by creating a file named
requirements.txt
in it.python -m venv venv source ./venv/bin/activate pip install -r requirements.txt
-
Run the following command to test the Streamlit installation.
streamlit hello
-
If Streamlit and OpenSearch ports (
8501
/9200
) are not open in the firewall, add them using the following command.sudo firewall-cmd --list-all sudo firewall-cmd --permanent --add-port=8501/tcp sudo firewall-cmd --permanent --add-port=9200/tcp sudo firewall-cmd --reload
Task 3: Set Up OCI Authentication
-
Create an
.oci
folder in your home directory and set up theconfig
file.[DEFAULT] user=ocid1.user.oc1.. fingerprint=40:02:56: key_file=/path/to/oci_api_key.pem tenancy=ocid1.tenancy.oc1.. region=us-region-1
Note: For more information, see Create A Config File For Oracle Date a Science.
Task 4: Write the Chatbot Engine Code
-
Follow this project structure for the codebase.
opensearch_rag_chatbot/ ├── app.py # Main Streamlit app ├── chat_engine.py # Logic for RAG and GenAI integration ├── config.py # Config file for secrets and settings ├── ingest_data_opens.py # Script for processing and indexing PDFs in OpenSearch ├── oci_utils.py # Utility for OCI configurations
-
Download the following sample code files as working examples.
Note: These sample programs may require customization for specific use cases and additional error handling. Additionally, in the samples, a user principal is used to establish a connection with the OpenSearch cluster. However, it is recommended to store sensitive information, such as secrets and credentials, in OCI Vault for enhanced security. You can then fetch these secrets dynamically at runtime. For more information on securely managing and retrieving secrets, see Keep Your Data Secure Using the Vault.
Task 5: Upload Documents and Test the Chatbot
-
Run the following command to run the application.
cd opensearch_rag_chatbot python -m venv venv source ./venv/bin/activate streamlit run app.py
-
Copy the generated URL to access the Streamlit app. Start uploading PDFs and test the functionality of the chatbot.
Troubleshooting and Tips
-
Connection Errors: Verify the OpenSearch cluster URL and credentials.
-
Index Issues: Ensure OpenSearch configurations in
config.py
are correct. -
PDF Processing Errors: Confirm PDF format compatibility.
-
Configuration Check: Double-check the
config.py
file for accuracy.
Note: Logs are available in the Streamlit console output which provide detailed information on operations and errors.
Next Steps
For next steps, consider expanding your chatbot by adding support for more document formats, such as Word or HTML files, and experimenting with different embedding models to improve the chatbot’s response accuracy in specific domains. You can also enhance the Streamlit UI with additional features, like support for multiple document uploads, query history, or real-time feedback on long processes.
Related Links
Acknowledgements
- Authors - Pavan Upadhyay (Principal Cloud Engineer), Saket Bihari (Principal Cloud Engineer)
More Learning Resources
Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.
For product documentation, visit Oracle Help Center.
Create RAG based Chatbot using Oracle Cloud Infrastructure Search with OpenSearch
G20394-01
November 2024