Note:

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

Prerequisites

Task 1: Set up an OpenSearch Cluster with OCI with OpenSearch

  1. Create a cluster. For more information, see Create an OpenSearch cluster with OCI Search with OpenSearch

  2. Once the cluster is created, copy the API endpoint, along with the user credentials to configure them in the app.

    OpenSearch Cluster API EndPoint

    OpenSearch Cluster user & pwd

Task 2: Configure the Python Environment

  1. Launch an instance with the Oracle Linux image and basic shape in OCI. For more information, see Launching a Linux Instance.

  2. Install Python 3.11 on Oracle Linux. For more information, see Installing Python .

  3. Run the following command to update the Python version.

    sudo update-alternatives  --set python /usr/bin/python3.11
    
  4. Create a project folder named opensearch_rag_chatbot.

  5. 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
    
  6. Run the following command to test the Streamlit installation.

    streamlit hello
    
  7. 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

  1. Create an .oci folder in your home directory and set up the config 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

Task 5: Upload Documents and Test the Chatbot

  1. Run the following command to run the application.

    cd opensearch_rag_chatbot
    python -m venv venv
    source ./venv/bin/activate
    streamlit run app.py
    
  2. Copy the generated URL to access the Streamlit app. Start uploading PDFs and test the functionality of the chatbot.

    upload and test

Troubleshooting and Tips

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.

Acknowledgements

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.