Skip to content

Commit

Permalink
WMS id is: 11619, RAG lab for AI Vector Search (#124)
Browse files Browse the repository at this point in the history
* New repo for RAG and Oracle AI Vector Search

Files for RAG and Oracle AI Vector Search LiveLabs

* Only available in green button

Removing folder for running on your own tenancy.  Only green button for the lab. for now.

* Added links to Oracle 23ai documentation

* Added step to copy ocid to jupyter clipboard

ocid copied to clipboard

* Small edit to prep-env

save to saved

* Updated AI Vector Search RAG LiveLab

Updated with easier to use instructions.  No longer need to copy compartment OCID.

* Small changes to improve instructions

Update image to streamlit with AI Vector Search name

Update instruction to open terminal

* Update rag1.md to change search algo

Change distance search from max inner to do product.

* Update AI Vector Search RAG lab

Updated with review from David and Michelle.  Improve lab step description
  • Loading branch information
mw-orcl authored May 6, 2024
1 parent 0d2e951 commit 1519ad4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 41 deletions.
8 changes: 5 additions & 3 deletions langchain-rag-23ai/introduction/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ The labs in this workshop focus on the following:

- An Oracle LiveLabs Account


## Learn More

See below for more information on Oracle Database 23ai and Oracle AI Vector Search

* [About Oracle Database 23ai](https://docs.oracle.com/en/database/oracle/oracle-database/)
* [Oracle AI Vector Search](https://www.oracle.com/news/announcement/ocw-integrated-vector-database-augments-generative-ai-2023-09-19/)
* [Oracle AI Vector Search](https://blogs.oracle.com/database/post/oracle-announces-general-availability-of-ai-vector-search-in-oracle-database-23ai)


You may now [proceed to the next lab](#next).

## Acknowledgements
* **Authors** - Milton Wan, Vijay Balebail
* **Authors** - Milton Wan, Vijay Balebail, Douglas Hood
* **Last Updated By/Date** - Milton Wan, April 2024
58 changes: 27 additions & 31 deletions langchain-rag-23ai/lab1/rag1.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ In this lab, you will:
![Run Jupyter](images/runjupyter.png)


1. Open the notebook **RAG_with_Oracle23ai_gold1.ipynb**. You can double click or right-click and select **Open**.
1. Open the notebook **"RAG_with_Oracle23ai_gold1.ipynb"**. You can double click or right-click and select **Open**.

![Open RAG notebook](images/openragwithoracle23ai.png)

Expand All @@ -53,11 +53,11 @@ If you want to enlarge the window and have larger fonts, you can zoom in with th

## Task 2: Run the RAG application code snippets in Jupyter notebook

Now you're ready to run each code snippet in sequence starting from the top in Jupyter. To run a code snippet, select the cell of the code and click **Run** to execute the code.
Now you're ready to run each code snippet in sequence starting from the top in Jupyter. To run a code snippet, select the cell of the code and click **Run** to execute the code. You don't need to edit any of the code for this application to run.

When the code snippet has completed running a number will appear in the square brackets. You can then proceed to the next cell and code snippet. Some of the code will print an output so you can get feedback. Pay attention to the 7 steps for RAG as you proceed. At any time you can also re-run the code snippets in the Jupyter cell. For example: later you can change the chunk size for the embedding, and re-run the code.
When the code snippet has completed running a number will appear in the square brackets. You can then proceed to the next cell and code snippet. Some of the code will print an output so you can get feedback. Pay attention to the 7 steps for RAG as you proceed. At any time you can also re-run the code snippets in the Jupyter cell.

Python libraries and modules have already been installed for the RAG application. Note the libraries for **LangChain** and a new library for the Oracle AI Vector Search, **OracleVS**.
Python libraries and modules have already been installed for this RAG application. Note the libraries for **LangChain** and a new library for the Oracle AI Vector Search, **OracleVS**.

Let's import the libraries.

Expand Down Expand Up @@ -110,7 +110,7 @@ def chunks_to_docs_wrapper(row: dict) -> Document:
print("Successfully defined metadata wrapper")
```

3. Load the environment variables and connect to Oracle Database 23ai with the credentials and connection string. Select the code snippet and click **Run**.
1. This code loads the environment variables and connects to Oracle Database 23ai with the credentials and connection string. Select the code snippet and click **Run**.

```python
# Load environment variables
Expand All @@ -137,15 +137,15 @@ except Exception as e:

**RAG Step 1 - Load the document**

The documents in our use case are in PDF format. We are loading a PDF document and printing the number of pages and printing page 1 for your visual feedback.
The document in our use case is in PDF format. We are loading a PDF document and printing the total number of pages, and printing page 1 for your visual feedback.

4. Select the code snippet and click **Run**.

```python
# RAG Step 1 - Load the document

# creating a pdf reader object
pdf = PdfReader('Oracle Announces Fiscal 2024 Second Quarter Financial Results.pdf')
pdf = PdfReader('oracle-database-23ai-new-features-guide.pdf')

# print number of pages in pdf file
print("The number of pages in this document is ",len(pdf.pages))
Expand All @@ -156,16 +156,16 @@ The documents in our use case are in PDF format. We are loading a PDF document a

**RAG Step 2 - Transform the document to text**

5. Transform each page of the PDF document to text. Click **Run** to execute the code.
5. The code transforms each page of the PDF document to text. Click **Run** to execute the code.
```python
# RAG Step 2 - Transform the document to text

if pdf is not None:
print("not null")
print("Transforming your document to text, this may take a few seconds")
text=""
for page in pdf.pages:
text += page.extract_text()
print(text)
print("The PDF document has been transformed to text")

```

Expand All @@ -181,9 +181,9 @@ Our chunk size will be 800 characters, with an overlap of 100 characters with ea
chunks = text_splitter.split_text(text)
print(chunks[0])
```
We print the first chunk for your feedback. You can also change the chunk size and overlap size and re-run the code to see the effect.
We print the first chunk for your feedback.

7. Add metadata such as id to each chunk for the database table. Click **Run** to execute the code.
7. The code adds metadata such as id to each chunk for the database table. Click **Run** to execute the code.

```python
# Create metadata wrapper to store additional information in the vector store
Expand Down Expand Up @@ -229,16 +229,16 @@ Note: Embedding models are used to vectorize data. To learn more about embedding

**RAG Step 5 - Build the prompt to query the document**

10. Click **Run** to execute the code.
10. The code issues a prompt related to the document we loaded. Click **Run** to execute the code.

```python
# RAG Step 5 - Build the prompt to query the document

user_question = ("Summarize the Oracle Fiscal Year 2024 Q2 Financial Results")
user_question = ("List the features in Oracle Database 23ai")
print ("The prompt to the LLM will be:",user_question)
```

11. Note the timings for searching the database. Click **Run** to execute the code.
11. The code records the timing for searching the database. It's quick! Click **Run** to execute the code.

```python
# Setup timings to check performance
Expand All @@ -254,7 +254,7 @@ Note: Embedding models are used to vectorize data. To learn more about embedding

**Choose an LLM to generate your response**

You have 3 choices in this lab. Choose only one, but you can go back and choose a different one to see its response. Just go back to the LLM cell and run it.
You have 3 LLM choices in this lab. Choose only one, but you can go back later and choose a different one to see its response.
Note: For OpenAI, you **CANNOT** run this in the LiveLabs environment. This sample code is for informational purposes so you can run this example in your own environment.

* Choice 1 - OCI GenAI LLM with meta.llama-2.70b-chat
Expand All @@ -263,7 +263,7 @@ Note: For OpenAI, you **CANNOT** run this in the LiveLabs environment. This sam

Note: To learn more about using other LLMs and accessing LLMs with secure API keys, see the LiveLabs on LLMs for Oracle AI Vector Search.

12a. **Choice 1** - Set up the OCI GenAI Service LLM to use Meta Llama. Click **Run** to execute the code.
12a. **Choice 1** - The code below sets up the OCI GenAI Service LLM to use Meta Llama. Click **Run** to execute the code.

```python
# Choice 1, Set the OCI GenAI LLM
Expand All @@ -280,7 +280,7 @@ Note: To learn more about using other LLMs and accessing LLMs with secure API ke
auth_type="API_KEY",
)
```
12b. **Choice 2** - Set up OpenAI LLM. Click **Run** to execute the code.
12b. **Choice 2** - The code below sets up OpenAI LLM. Click **Run** to execute the code.

```python
# Choice 2, Set up OpenAI LLM
Expand All @@ -301,7 +301,7 @@ Note: To learn more about using other LLMs and accessing LLMs with secure API ke

```

12c. **Choice 3** - Set up Cohere LLM. Click **Run** to execute the code.
12c. **Choice 3** - The code below sets up OCI GenAI Service to use the Cohere LLM. Click **Run** to execute the code.

```python
# Choice 3, Set up Cohere LLM
Expand All @@ -322,7 +322,7 @@ Note: To learn more about using other LLMs and accessing LLMs with secure API ke

```

1. Build the prompt template to include both the question and the context. Instantiate the knowledge base class to use the retriever to retrieve context from Oracle Database 23ai. Click **Run** to execute the code.
1. The code below builds the prompt template to include both the question and the context, and instantiates the knowledge base class to use the retriever to retrieve context from Oracle Database 23ai. Click **Run** to execute the code.

```python
# Set up the template for the questions and context, and instantiate the database retriever object
Expand All @@ -336,7 +336,7 @@ Note: To learn more about using other LLMs and accessing LLMs with secure API ke

This is the key part of the RAG application. It is the LangChain pipeline that chains all the components together to produce an LLM response with context. The chain will embed the question as a vector. This vector will be used to search for other vectors that are similar. The top similar vectors will be returned as text chunks (context). Together the question and the context will form the prompt to the LLM for processing. And ultimately generating the response. A detailed explanation of the chain follows the code.

14. Define the RAG chain process and invoke the chain. Click **Run** to execute the code.
1. The code defines the RAG chain process and invokes the chain. Click **Run** to execute the code.

```python
# RAG Steps 6 and 7 Chain the entire process together, retrieve the context, construct the prompt with the question and context, and pass to LLM for the response
Expand All @@ -360,7 +360,7 @@ This is the key part of the RAG application. It is the LangChain pipeline that
print( f"Send user question and ranked chunks to LLM and get answer duration: {round(s6time - s5time, 1)} sec.")

```
This code sets up a processing pipeline where user_question is processed sequentially by retriever, prompt, llmOCI, and StrOutputParser, with each step performing some transformation or analysis on the input. The final result is stored in the variable response.
The code above sets up a processing pipeline where user_question is processed sequentially by retriever, prompt, llmOCI, and StrOutputParser, with each step performing some transformation or analysis on the input. The final result is stored in the variable response.
chain = (...): This line initializes a chain of processing steps. Each step is represented as an object or function.
1. {"context": retriever, "question": RunnablePassthrough()}: This is the first step in the chain. It involves a dictionary with keys "context" and "question" mapped to some objects or functions named retriever and RunnablePassthrough() respectively.
2. | prompt: The | operator is used to chain the output of the previous step with the prompt object or function. This suggests that the output of the first step will be passed as input to prompt.
Expand All @@ -370,22 +370,18 @@ chain = (...): This line initializes a chain of processing steps. Each step is r

**Finish**

You're done with this lab. You can proceed to the next lab or stay here and try Step 12 again with a different LLM, 12a or 12c, and run through to Step 14, and see the response from a different LLM.

Click **Run** to execute the congrats code.

```python
print("")
print("Congratulations! You've completed your RAG application with Oracle Database 23ai using LangChain")
print("Congratulations! You've completed your RAG application with AI Vector Search in Oracle Database 23ai using LangChain")
```

### Conclusion

By using Oracle AI Vector Search in Oracle Database 23ai, you can build RAG applications with important context without having to retrain the LLM. The context is stored, searched and retrieved from Oracle Database 23ai and passed to the LLM to generate accurate, up to date, and targeted responses to your prompts. Businesses and enterprises can use RAG with Oracle Database 23ai and an LLM to securely respond to important business questions or to generate content for use cases using private, internal business content.

You may now [proceed to the next lab](#next).

**More information on Oracle Database 23ai and Oracle AI Vector Search**

* [About Oracle Database 23ai](https://docs.oracle.com/en/database/oracle/oracle-database/)
* [Oracle AI Vector Search](https://www.oracle.com/news/announcement/ocw-integrated-vector-database-augments-generative-ai-2023-09-19/)


## Acknowledgements
* **Authors** - Vijay Balebail, Milton Wan, Douglas Hood, Rajeev Rumale
Expand Down
30 changes: 23 additions & 7 deletions langchain-rag-23ai/lab2/rag2-streamlit.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Estimated Time: 5 min

### Objectives

In this lab you will run the application with a UI using Streamlit, load a PDF document, and ask a question in the prompt. This is the same RAG application as the previous lab but demonstrates use of a user interface where you can select a PDF document to load.
In this lab you will run the RAG application interactively using the Streamlit user interface. You can select and load from several PDF documents, and ask your own question in the prompt. This is the same application with the 7 essential RAG steps as the previous lab but demonstrates use through a user interface.

### Prerequisites

Expand All @@ -26,18 +26,28 @@ In this lab you will run the application with a UI using Streamlit, load a PDF d
$ streamlit run genai.py
```
3. Launch a browser from this same virtual environment, or open a tab with existing browser. To launch a browser click on the Activities menu and select Firefox or Chrome.
3. A browser will launch. If not, open a browser from the Activities menu and select Firefox or Chrome.
4. Enter **localhost:8501** for the URL in the browser.
Your application will now be running. Simply follow the UI, load a PDF document, and ask a question of the document. The LLM will answer the question and respond with context from the document. This application follows the same 7 essential RAG steps from the previous lab.
Your application will now be running. Simply follow the UI.
5. Click on the **Browse files** button and load a PDF document.
After a few seconds the PDF document will be loaded, transformed to text, chunked, and vectorized into Oracle Database 23ai.
![Streamlit UI](images/streamlitocigenai.png)
A few suggestions for the questions are listed below.
A prompt area will now appear for you to ask a question about your PDF. You can type in your own question or use the suggestions listed below.
6. Type a question.
The LLM will answer the question and respond with context stored in Oracle Database 23ai.
For the Oracle Database 23c documentation:
- What are the new features in Oracle Database 23c
**Suggestions**
For the Oracle Database 23ai documentation:
- What are the new features in Oracle Database 23ai
- Tell me more about AI Vector Search
- Tell me more about new enhancement to SQL
- Tell me more about JSON Relational Duality
Expand All @@ -46,6 +56,7 @@ For the Oracle Fiscal Year 2024 Quarter 2 Financial Results document:
- What did Larry say in the Oracle Fiscal Year 2024 Quarter 2 financial results
**Code for Streamlit application**
Now check out the application code using the Streamlit UI. From a terminal window type:
Expand All @@ -56,9 +67,14 @@ Now check out the application code using the Streamlit UI. From a terminal windo
$ more genai.py
```
Note the additional libraries and **st** objects for Streamlit.
Note the additional imported libraries and **st** objects for Streamlit.
But essentially the code is the same 7 steps for building a RAG application with Oracle AI Vector Search and LangChain.
### Conclusion
By using AI Vector Search in Oracle Database 23ai, you can build RAG applications with important context without having to retrain the LLM. The context is stored, searched and retrieved from Oracle Database 23ai and passed to the LLM to generate accurate, up to date, and targeted responses to your prompts. Businesses and enterprises can use RAG with AI Vector Search in Oracle Database 23ai and an LLM to securely respond to important business questions or to generate content for many use cases using private, internal business information.
**Completed. Thanks for running the lab.**
## Acknowledgements
Expand Down

0 comments on commit 1519ad4

Please sign in to comment.