|
| 1 | +# Integrate Oracle 23ai Vector DB with OCI GenAI using Llama-index |
| 2 | + |
| 3 | +[](https://www.python.org/downloads/release/python-3110/) |
| 4 | + |
| 5 | +## Introduction |
| 6 | + |
| 7 | +Accessing the right answers from vast data repositories is a challenge many organizations face. A **Retrieval-Augmented Generation (RAG)** based system can revolutionize how users interact with their data by making information easily accessible and up-to-date. In this workshop, we’ll build a RAG-based chatbot using **Oracle Database 23ai** and **OCI Generative AI** services, allowing users to chat with their unstructured data like PDF, CSV, and TXT files. This approach combines advanced retrieval techniques with generative AI, creating a powerful solution for intelligent and dynamic data interaction. |
| 8 | + |
| 9 | + |
| 10 | +## What is RAG? |
| 11 | + |
| 12 | +**Retrieval-Augmented Generation (RAG)** combines retrieval-based methods with generative AI to provide more accurate and contextually relevant responses by accessing and utilizing large datasets dynamically. [Learn more about RAG](https://www.oracle.com/artificial-intelligence/generative-ai/retrieval-augmented-generation-rag/). |
| 13 | + |
| 14 | +## Prerequisites and Setup |
| 15 | + |
| 16 | +Before you begin, ensure you have the following: |
| 17 | + |
| 18 | +- **Oracle Cloud Account** |
| 19 | + [Sign up here](https://www.oracle.com/cloud/free/) |
| 20 | + |
| 21 | +- **Oracle Database 23AI** |
| 22 | + [Learn more](https://www.oracle.com/database/23ai/) |
| 23 | + |
| 24 | +- **Compute VM** |
| 25 | + This will serve as your web app. Ensure the Compute VM can communicate with the Oracle Database by setting up the appropriate network configurations. |
| 26 | + |
| 27 | +- **OCI Generative AI Services** |
| 28 | + [Documentation](https://docs.oracle.com/en-us/iaas/Content/GenerativeAI/home.htm) |
| 29 | + |
| 30 | +- **LlamaIndex** |
| 31 | + [Documentation](https://pypi.org/project/llama-index/) |
| 32 | + |
| 33 | +- **Python Dependencies** |
| 34 | + Listed in the `requirements.txt` file in the repository. |
| 35 | + |
| 36 | +## Setup |
| 37 | + |
| 38 | +### 1. Clone the Repository in your web server (Compute VM) |
| 39 | + |
| 40 | +```bash |
| 41 | +sudo dnf install git |
| 42 | + |
| 43 | +git clone https://github.com/SaurabhSalunkhe/Oracle-23ai-RAG-Chatbot.git |
| 44 | +``` |
| 45 | + |
| 46 | +### 2. Update and Install Dependencies (Oracle Linux) |
| 47 | +``` |
| 48 | +sudo yum update -y && sudo yum install -y git python3 && sudo yum groupinstall -y "Development Tools" && sudo yum install -y bzip2-devel openssl-devel libffi-devel zlib-devel wget libffi-devel openssl openssl-devel tk-devel xz-devel zlib-devel bzip2-devel readline-devel libuuid-devel ncurses-devel libaio |
| 49 | +
|
| 50 | +``` |
| 51 | + |
| 52 | +### 3. Install Python 3.11.x |
| 53 | +Ensure Python version 3.11.x is installed. |
| 54 | + |
| 55 | +``` |
| 56 | +mkdir -p $HOME/python |
| 57 | +wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz |
| 58 | +tar -xvzf Python-3.11.0.tgz --strip-components=1 -C /home/$USER/python |
| 59 | +cd $HOME/python |
| 60 | +./configure --prefix=$HOME/python |
| 61 | +./configure --enable-optimizations |
| 62 | +make clean; make |
| 63 | +make altinstall |
| 64 | +
|
| 65 | +export PYTHONHOME=$HOME/python |
| 66 | +export PATH=$PYTHONHOME/bin:$PATH |
| 67 | +export LD_LIBRARY_PATH=$PYTHONHOME/lib:$LD_LIBRARY_PATH |
| 68 | +
|
| 69 | +cd $HOME/python/bin |
| 70 | +ln -s python3.11 python3 |
| 71 | +ln -s pip3.11 pip3 |
| 72 | +
|
| 73 | +``` |
| 74 | + |
| 75 | +### 4. Create and Activate Virtual Environment |
| 76 | +``` |
| 77 | +cd Oracle-23ai-RAG-Chatbot |
| 78 | +python3.11 -m venv venv |
| 79 | +source venv/bin/activate |
| 80 | +``` |
| 81 | + |
| 82 | +### 5. Install Python Dependencies |
| 83 | + |
| 84 | +``` |
| 85 | +pip install -r requirements.txt |
| 86 | +``` |
| 87 | + |
| 88 | +### 6. Configure OCI Authentication |
| 89 | +a. Create the .oci Directory |
| 90 | + |
| 91 | +``` |
| 92 | +mkdir -p /home/opc/.oci |
| 93 | +``` |
| 94 | + |
| 95 | +b. Generate OCI API Keys |
| 96 | +Follow the OCI SDK Configuration Guide to generate your API keys. |
| 97 | +https://docs.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm |
| 98 | + |
| 99 | +https://docs.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm#apisigningkey_topic_How_to_Generate_an_API_Signing_Key_Console |
| 100 | + |
| 101 | +### 7. Set Up Oracle Database 23ai |
| 102 | +a. Run SQL Commands from create_tables.sql |
| 103 | +Create the User and Grant Privileges |
| 104 | + |
| 105 | +``` |
| 106 | +-- Create the user with a specified password |
| 107 | +CREATE USER ai_user IDENTIFIED BY "EXamplepassword#_123"; |
| 108 | +
|
| 109 | +-- Grant DBA privileges for full administrative access |
| 110 | +GRANT DBA TO ai_user; |
| 111 | +
|
| 112 | +-- Grant specific roles and privileges needed for Oracle AI Vector Search |
| 113 | +GRANT DB_DEVELOPER_ROLE TO ai_user; |
| 114 | +GRANT CREATE MINING MODEL TO ai_user; |
| 115 | +``` |
| 116 | + |
| 117 | +As ai_user, Create Tables with Vector Data Types |
| 118 | + |
| 119 | + |
| 120 | +``` |
| 121 | +CREATE TABLE BOOKS ( |
| 122 | + ID NUMBER NOT NULL, |
| 123 | + NAME VARCHAR2(100) NOT NULL, |
| 124 | + PRIMARY KEY (ID) |
| 125 | +); |
| 126 | +
|
| 127 | +CREATE TABLE CHUNKS ( |
| 128 | + ID VARCHAR2(64) NOT NULL, |
| 129 | + CHUNK CLOB, |
| 130 | + VEC VECTOR(1024, FLOAT64), |
| 131 | + PAGE_NUM VARCHAR2(10), |
| 132 | + BOOK_ID NUMBER, |
| 133 | + PRIMARY KEY (ID), |
| 134 | + CONSTRAINT fk_book |
| 135 | + FOREIGN KEY (BOOK_ID) |
| 136 | + REFERENCES BOOKS (ID) |
| 137 | +); |
| 138 | +``` |
| 139 | + |
| 140 | +8. Configure the Application |
| 141 | +a. Edit config.py |
| 142 | +Update the following parameters with your details: |
| 143 | + |
| 144 | +``` |
| 145 | +# DB connections (Below are example credentials. Substitute with your credentials) |
| 146 | +DB_USER = "ai_user" |
| 147 | +DB_PWD = "EXamplepassword#_123" |
| 148 | +DB_HOST_IP = "ip:1521" |
| 149 | +DB_SERVICE = "example_PDB1.sub07260203110.vcnss.oraclevcn.com" |
| 150 | +
|
| 151 | +# GenAI configurations |
| 152 | +COMPARTMENT_OCID = "ocid1.compartment.oc1..xxxxxxxxx" |
| 153 | +ENDPOINT = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com" |
| 154 | +COHERE_API_KEY = "xxxxxxxxxxx" # Optional but recommended |
| 155 | +``` |
| 156 | + |
| 157 | +### 8. Run the Streamlit Application |
| 158 | + |
| 159 | +``` |
| 160 | +sudo systemctl stop firewalld |
| 161 | +streamlit run app.py |
| 162 | +``` |
| 163 | + |
| 164 | +### 9. To Run the App in the Background |
| 165 | + |
| 166 | +``` |
| 167 | +nohup streamlit run app.py & |
| 168 | +``` |
| 169 | + |
| 170 | +### 10. Access the Chatbot |
| 171 | +Open your browser and navigate to http://<Your_VM_IP>:8501. |
| 172 | + |
| 173 | +You should see the chatbot interface as shown below: |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | +## Contributing |
| 178 | +<!-- If your project has specific contribution requirements, update the |
| 179 | + CONTRIBUTING.md file to ensure those requirements are clearly explained. --> |
| 180 | + |
| 181 | +This project welcomes contributions from the community. Before submitting a pull |
| 182 | +request, please [review our contribution guide](./CONTRIBUTING.md). |
| 183 | + |
| 184 | +## Security |
| 185 | + |
| 186 | +Please consult the [security guide](./SECURITY.md) for our responsible security |
| 187 | +vulnerability disclosure process. |
| 188 | + |
| 189 | +## License |
| 190 | +Copyright (c) 2024 Oracle and/or its affiliates. |
| 191 | + |
| 192 | +Licensed under the Universal Permissive License (UPL), Version 1.0. |
| 193 | + |
| 194 | +See [LICENSE](LICENSE.txt) for more details. |
| 195 | + |
| 196 | +ORACLE AND ITS AFFILIATES DO NOT PROVIDE ANY WARRANTY WHATSOEVER, EXPRESS OR IMPLIED, FOR ANY SOFTWARE, MATERIAL OR CONTENT OF ANY KIND CONTAINED OR PRODUCED WITHIN THIS REPOSITORY, AND IN PARTICULAR SPECIFICALLY DISCLAIM ANY AND ALL IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. FURTHERMORE, ORACLE AND ITS AFFILIATES DO NOT REPRESENT THAT ANY CUSTOMARY SECURITY REVIEW HAS BEEN PERFORMED WITH RESPECT TO ANY SOFTWARE, MATERIAL OR CONTENT CONTAINED OR PRODUCED WITHIN THIS REPOSITORY. IN ADDITION, AND WITHOUT LIMITING THE FOREGOING, THIRD PARTIES MAY HAVE POSTED SOFTWARE, MATERIAL OR CONTENT TO THIS REPOSITORY WITHOUT ANY REVIEW. USE AT YOUR OWN RISK. |
0 commit comments