|
| 1 | + |
| 2 | +# Creating a RAG (Retrieval-Augmented Generation) with Oracle Generative AI Service in just 21 lines of code |
| 3 | + |
| 4 | +## Introduction |
| 5 | +In this article, we'll explore how to create a Retrieval-Augmented Generation (RAG) model using Oracle Gen AI, llama index, Qdrant Vector Database, and SentenceTransformerEmbeddings. This 21-line code will allow you to scrape through web pages, use llama index for indexing, Oracle Generative AI Service for question generation, and Qdrant for vector indexing. |
| 6 | + |
| 7 | +<img src="./RagArchitecture.svg"> |
| 8 | +</img> |
| 9 | + |
| 10 | +## Limited Availability |
| 11 | + |
| 12 | +Oracle Generative AI Service is in Limited Availability as of today when we are creating this repo. |
| 13 | + |
| 14 | +Customers can easily enter in the LA programs. To test these functionalities you need to enrol in the LA programs and install the proper versions of software libraries. |
| 15 | + |
| 16 | +Code and functionalities can change, as a result of changes and new features |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | +Before getting started, make sure you have the following installed: |
| 20 | + |
| 21 | +- Oracle Generative AI Service |
| 22 | +- llama index |
| 23 | +- qdrant client |
| 24 | +- SentenceTransformerEmbeddings |
| 25 | + |
| 26 | +## Setting up the Environment |
| 27 | +1. Install the required packages: |
| 28 | + ```bash |
| 29 | + pip install oci==2.112.1+preview.1.1649 llama-index qdrant-client sentence-transformers |
| 30 | + ``` |
| 31 | + |
| 32 | +## Loading data |
| 33 | + |
| 34 | +You need to create a sitemap.xml file where you can specify or list the webpages which you want to include in your RAG. |
| 35 | +Here we have used SentenceTransformerEmbeddings to create the embeddings but you can easily use any embeddings model . In the next blog we will show how easily you can use Oracle Generative AI Service embeddings model. |
| 36 | + |
| 37 | +In this example we have used some Oracle documentation pages and created a xml file for the same and have placed it in Oracle object storage. |
| 38 | + |
| 39 | +sitemap used : https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frpj5kvxryk1/b/thisIsThePlace/o/combined.xml |
| 40 | + |
| 41 | +## Entire code |
| 42 | + |
| 43 | + ```bash |
| 44 | + from genai_langchain_integration.langchain_oci import OCIGenAI |
| 45 | +from llama_index import VectorStoreIndex |
| 46 | +from llama_index import ServiceContext |
| 47 | +from llama_index.vector_stores.qdrant import QdrantVectorStore |
| 48 | +from llama_index.storage.storage_context import StorageContext |
| 49 | +from qdrant_client import qdrant_client |
| 50 | +from langchain.embeddings import SentenceTransformerEmbeddings |
| 51 | +from llama_hub.web.sitemap import SitemapReader |
| 52 | +loader = SitemapReader() |
| 53 | +documents = loader.load_data(sitemap_url='https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frpj5kvxryk1/b/thisIsThePlace/o/combined.xml') |
| 54 | +client = qdrant_client.QdrantClient(location=":memory:") |
| 55 | +embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2") |
| 56 | +llm = OCIGenAI(model_id="cohere.command",service_endpoint="https://generativeai.aiservice.us-chicago-1.oci.oraclecloud.com",compartment_id = "ocid1.tenancy.oc1..aaaaaaaa5hwtrus75rauufcfvtnjnz3mc4xm2bzibbigva2bw4ne7ezkvzha",temperature=0.0) |
| 57 | +system_prompt="As a support engineer, your role is to leverage the information in the context provided. Your task is to respond to queries based strictly on the information available in the provided context. Do not create new information under any circumstances. Refrain from repeating yourself. Extract your response solely from the context mentioned above. If the context does not contain relevant information for the question, respond with 'How can I assist you with questions related to the document?" |
| 58 | +service_context = ServiceContext.from_defaults(llm=llm, chunk_size=1000, chunk_overlap=100, embed_model=embeddings,system_prompt=system_prompt) |
| 59 | +vector_store = QdrantVectorStore(client=client, collection_name="ansh") |
| 60 | +storage_context = StorageContext.from_defaults(vector_store=vector_store) |
| 61 | +index = VectorStoreIndex.from_documents(documents, storage_context=storage_context, service_context=service_context) |
| 62 | +query_engine = index.as_query_engine() |
| 63 | +response = query_engine.query("can i use OCI document understanding for files in french ?") |
| 64 | +print(response) |
| 65 | + ``` |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +## Conclusion |
| 70 | + |
| 71 | +In this article, we've covered the process of creating a RAG model using Oracle Generative AI Service, llama index, Qdrant, and SentenceTransformerEmbeddings. Feel free to experiment with different web pages and datasets to enhance the capabilities of your model. |
| 72 | + |
| 73 | +In a future blog post, we'll explore how to integrate Oracle Vector Database and Oracle Gen AI embeddings model into this RAG setup. |
| 74 | + |
| 75 | +Feel free to modify and expand upon this template according to your specific use case and preferences. Good luck with your article! |
0 commit comments