Skip to content

Commit 519b1d3

Browse files
authored
Merge pull request #877 from oracle-devrel/newRAGCode
changes on doc summarization and new files in rag
2 parents 1cd65bc + 95b1104 commit 519b1d3

File tree

4 files changed

+78
-17
lines changed

4 files changed

+78
-17
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from langchain_community.embeddings import OCIGenAIEmbeddings
2+
from langchain.chains import RetrievalQA
3+
from langchain_community.vectorstores import Qdrant
4+
from langchain_core.prompts import PromptTemplate
5+
from langchain_community.llms import OCIGenAI
6+
from langchain_community.document_loaders import UnstructuredURLLoader
7+
compartment_id = "ocid1.compartment.oc1..aaaaaaaa7ggqkd4ptkeb7ugk6ipsl3gqjofhkr6yacluwj4fitf2ufrdm65q"
8+
embeddings = OCIGenAIEmbeddings(model_id="cohere.embed-english-light-v3.0",service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",compartment_id= compartment_id,)
9+
testurls = ['https://docs.oracle.com/iaas/odsaz/odsa-rotate-wallet.html', 'https://docs.oracle.com/iaas/odsaz/odsa-change-password.html', 'https://docs.oracle.com/iaas/odsaz/odsa-database-actions.html']
10+
loader = UnstructuredURLLoader(urls=testurls)
11+
docs = loader.load()
12+
vectorstore = Qdrant.from_documents(docs,embeddings,location=":memory:",prefer_grpc=False,collection_name="test_db")
13+
retriever = vectorstore.as_retriever()
14+
rag_prompt_template = """Answer the question based only on the following context:
15+
{context}
16+
Question: {question}"""
17+
rag_prompt = PromptTemplate.from_template(rag_prompt_template)
18+
llm = OCIGenAI(model_id="cohere.command",service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",compartment_id= compartment_id,model_kwargs={"temperature": 0, "max_tokens": 300})
19+
rag = RetrievalQA.from_chain_type(llm=llm,retriever=retriever,chain_type_kwargs={"prompt": rag_prompt,},)
20+
data = rag.invoke("What is rotate a wallet")
21+
print(data['result'])

ai-and-app-modernisation/ai-services/generative-ai-service/rag-genai/files/README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,27 @@
33
## Introduction
44
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.
55

6+
Find below the code of building a RAG using llamaIndex with Oracle Generative AI Service.
7+
Also check the file LangChainRAG.py which allows you to create an application (implementing RAG) using Langchain and the file langChainRagWithUI.py which includes a UI build with Streamlit.
8+
69
<img src="./RagArchitecture.svg">
710
</img>
811

9-
## Limited Availability
10-
11-
Oracle Generative AI Service is in Limited Availability as of today when we are creating this repo.
12-
13-
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.
14-
15-
Code and functionalities can change, as a result of changes and new features
16-
1712
## Prerequisites
1813

1914
Before getting started, make sure you have the following installed:
2015

2116
- Oracle Generative AI Service
22-
- llama index
23-
- qdrant client
17+
- Llama index
18+
- Langchain
19+
- Qdrant client
2420
- SentenceTransformerEmbeddings
2521

2622
## Setting up the Environment
2723
1. Install the required packages:
2824
```bash
29-
pip install oci==2.118.1+preview.1.1697 llama-index qdrant-client sentence-transformers
25+
pip install -U langchain oci
26+
pip install langchain llama-index qdrant-client sentence-transformers transformers
3027
```
3128

3229
## Loading data
@@ -41,26 +38,26 @@ sitemap used : https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frpj5kvxry
4138
## Entire code
4239

4340
```bash
44-
from genai_langchain_integration.langchain_oci import OCIGenAI
4541
from llama_index import VectorStoreIndex
4642
from llama_index import ServiceContext
4743
from llama_index.vector_stores.qdrant import QdrantVectorStore
4844
from llama_index.storage.storage_context import StorageContext
4945
from qdrant_client import qdrant_client
50-
from langchain.embeddings import SentenceTransformerEmbeddings
46+
from langchain_community.embeddings import SentenceTransformerEmbeddings
5147
from llama_hub.web.sitemap import SitemapReader
48+
from langchain_community.llms import OCIGenAI
5249
loader = SitemapReader()
53-
documents = loader.load_data(sitemap_url='https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frpj5kvxryk1/b/thisIsThePlace/o/combined.xml')
50+
documents = loader.load_data(sitemap_url='https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frpj5kvxryk1/b/thisIsThePlace/o/latest.xml')
5451
client = qdrant_client.QdrantClient(location=":memory:")
5552
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)
53+
llm = OCIGenAI(model_id="cohere.command",service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",model_kwargs={"temperature": 0.0, "max_tokens": 300},compartment_id = "ocid1.compartment.oc1..aaaaaaaa7ggqkd4ptkeb7ugk6ipsl3gqjofhkr6yacluwj4fitf2ufrdm65q")
5754
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?"
5855
service_context = ServiceContext.from_defaults(llm=llm, chunk_size=1000, chunk_overlap=100, embed_model=embeddings,system_prompt=system_prompt)
5956
vector_store = QdrantVectorStore(client=client, collection_name="ansh")
6057
storage_context = StorageContext.from_defaults(vector_store=vector_store)
6158
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context, service_context=service_context)
6259
query_engine = index.as_query_engine()
63-
response = query_engine.query("can i use OCI document understanding for files in french ?")
60+
response = query_engine.query('What is activity auditing report?')
6461
print(response)
6562
```
6663

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import streamlit as st
2+
from langchain_community.embeddings import OCIGenAIEmbeddings
3+
from langchain.chains import RetrievalQA
4+
from langchain_community.vectorstores import Qdrant
5+
from langchain_core.prompts import PromptTemplate
6+
from langchain_community.llms import OCIGenAI
7+
from langchain_community.document_loaders import UnstructuredURLLoader
8+
st.title("Oracle QA Chatbot")
9+
st.text_input("Ask a question:", key="question") # Input field for questions
10+
# Data loading (outside any function)
11+
compartment_id = "ocid1.compartment.oc1..aaaaaaaa7ggqkd4ptkeb7ugk6ipsl3gqjofhkr6yacluwj4fitf2ufrdm65q"
12+
embeddings = OCIGenAIEmbeddings(model_id="cohere.embed-english-light-v3.0",service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",compartment_id=compartment_id,)
13+
testurls = ['https://docs.oracle.com/iaas/odsaz/odsa-rotate-wallet.html','https://docs.oracle.com/iaas/odsaz/odsa-change-password.html','https://docs.oracle.com/iaas/odsaz/odsa-database-actions.html',]
14+
# Cache the loaded documents (outside any function)
15+
@st.cache_data
16+
def load_documents():
17+
docs = UnstructuredURLLoader(urls=testurls).load()
18+
print("Loading data")
19+
print(docs)
20+
return docs # Return the loaded documents
21+
docs = load_documents()
22+
vectorstore = Qdrant.from_documents(docs, embeddings, location=":memory:", prefer_grpc=False, collection_name="test_db")
23+
retriever = vectorstore.as_retriever()
24+
rag_prompt_template = """Answer the question based only on the following context:
25+
{context}
26+
Question: {question}"""
27+
rag_prompt = PromptTemplate.from_template(rag_prompt_template)
28+
llm = OCIGenAI(
29+
model_id="cohere.command",
30+
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
31+
compartment_id=compartment_id,
32+
model_kwargs={"temperature": 0, "max_tokens": 300},
33+
)
34+
rag = RetrievalQA.from_chain_type(llm=llm, retriever=retriever, chain_type_kwargs={"prompt": rag_prompt})
35+
# Answer generation when a question is asked
36+
if st.button("Get Answer"):
37+
question = st.session_state.question
38+
# Ensure correct access to cached documents
39+
docs = load_documents() # Call the cached function to retrieve documents
40+
data = rag.invoke(question, context=docs) # Pass documents as context
41+
answer = data["result"]
42+
st.write("Answer:", answer)

ai-and-app-modernisation/ai-services/generative-ai-service/summarize-genai/files/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ streamlit
22
langchain
33
unstructured
44
langchain_community
5-
pypdf
5+
pypdf
6+
transformers

0 commit comments

Comments
 (0)