Skip to content

Commit d981929

Browse files
committed
changes on doc summarization and new files in rag
1 parent 379d56a commit d981929

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
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 GEN AI .
7+
Also we have a file LangChainRAG.py which allows you to create a rag using langchain and a file langChainRagWithUI.py which also has a streamlit ui attached to the langchain rag
8+
69
<img src="./RagArchitecture.svg">
710
</img>
811

@@ -20,13 +23,15 @@ Before getting started, make sure you have the following installed:
2023

2124
- Oracle Generative AI Service
2225
- llama index
26+
- langchain
2327
- qdrant client
2428
- SentenceTransformerEmbeddings
2529

2630
## Setting up the Environment
2731
1. Install the required packages:
2832
```bash
29-
pip install oci==2.118.1+preview.1.1697 llama-index qdrant-client sentence-transformers
33+
pip install -U langchain oci
34+
pip install langchain llama-index qdrant-client sentence-transformers transformers
3035
```
3136

3237
## Loading data
@@ -41,26 +46,26 @@ sitemap used : https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frpj5kvxry
4146
## Entire code
4247

4348
```bash
44-
from genai_langchain_integration.langchain_oci import OCIGenAI
4549
from llama_index import VectorStoreIndex
4650
from llama_index import ServiceContext
4751
from llama_index.vector_stores.qdrant import QdrantVectorStore
4852
from llama_index.storage.storage_context import StorageContext
4953
from qdrant_client import qdrant_client
50-
from langchain.embeddings import SentenceTransformerEmbeddings
54+
from langchain_community.embeddings import SentenceTransformerEmbeddings
5155
from llama_hub.web.sitemap import SitemapReader
56+
from langchain_community.llms import OCIGenAI
5257
loader = SitemapReader()
53-
documents = loader.load_data(sitemap_url='https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frpj5kvxryk1/b/thisIsThePlace/o/combined.xml')
58+
documents = loader.load_data(sitemap_url='https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/frpj5kvxryk1/b/thisIsThePlace/o/latest.xml')
5459
client = qdrant_client.QdrantClient(location=":memory:")
5560
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)
61+
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")
5762
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?"
5863
service_context = ServiceContext.from_defaults(llm=llm, chunk_size=1000, chunk_overlap=100, embed_model=embeddings,system_prompt=system_prompt)
5964
vector_store = QdrantVectorStore(client=client, collection_name="ansh")
6065
storage_context = StorageContext.from_defaults(vector_store=vector_store)
6166
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context, service_context=service_context)
6267
query_engine = index.as_query_engine()
63-
response = query_engine.query("can i use OCI document understanding for files in french ?")
68+
response = query_engine.query('What is activity auditing report?')
6469
print(response)
6570
```
6671

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)