Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,52 @@
# rag-tutorial-v2

This repository is a starter guide for learning how to use Retrieval-Augmented Generation (RAG) with Ollama. Follow the steps below to set up and run the project.

## Prerequisites

Ensure you have the following installed:
- Python 3.8 or higher
- pip (Python package installer)

## Installation

1. Clone the repository:
```sh
git clone <repository-url>
cd rag-tutorial-v2
```

2. Install the required Python packages:
```sh
pip install -r requirements.txt
```

## Setting Up the Database

1. Place your PDF documents in the [data](http://_vscodecontentref_/0) directory.

2. Populate the database with the documents:
```sh
python populate_database.py --reset
```

## Querying the Database

To query the database, run the [query_data.py](http://_vscodecontentref_/1) script with your query text:
```sh
python query_data.py "Your query text here"
```
## Project Structure

- `__pycache__/`: Directory containing Python bytecode files.
- `.gitignore`: Git ignore file to specify untracked files to ignore.
- `chroma/`: Directory containing the Chroma database files.
- `77943a61-1aef-45fe-af55-a29fb829e25b/`: Subdirectory for Chroma database.
- `chroma.sqlite3`: SQLite database file for Chroma.
- `data/`: Directory to store PDF documents.
- `get_embedding_function.py`: Script to get the embedding function.
- `populate_database.py`: Script to populate the Chroma database with documents.
- `query_data.py`: Script to query the Chroma database.
- `README.md`: This README file.
- `requirements.txt`: List of required Python packages.
- `test_rag.py`: Script to run tests on the RAG setup.
12 changes: 6 additions & 6 deletions get_embedding_function.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from langchain_community.embeddings.ollama import OllamaEmbeddings
from langchain_community.embeddings.bedrock import BedrockEmbeddings
from langchain_ollama import OllamaEmbeddings
from langchain_aws.embeddings.bedrock import BedrockEmbeddings


def get_embedding_function():
embeddings = BedrockEmbeddings(
credentials_profile_name="default", region_name="us-east-1"
)
# embeddings = OllamaEmbeddings(model="nomic-embed-text")
# embeddings = BedrockEmbeddings(
# credentials_profile_name="default", region_name="us-east-1"
# )
embeddings = OllamaEmbeddings(model="mxbai-embed-large")
return embeddings
5 changes: 2 additions & 3 deletions populate_database.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import argparse
import os
import shutil
from langchain.document_loaders.pdf import PyPDFDirectoryLoader
from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.schema.document import Document
from get_embedding_function import get_embedding_function
from langchain.vectorstores.chroma import Chroma
from langchain_chroma import Chroma


CHROMA_PATH = "chroma"
Expand Down Expand Up @@ -67,7 +67,6 @@ def add_to_chroma(chunks: list[Document]):
print(f"👉 Adding new documents: {len(new_chunks)}")
new_chunk_ids = [chunk.metadata["id"] for chunk in new_chunks]
db.add_documents(new_chunks, ids=new_chunk_ids)
db.persist()
else:
print("✅ No new documents to add")

Expand Down
6 changes: 3 additions & 3 deletions query_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argparse
from langchain.vectorstores.chroma import Chroma
from langchain_chroma import Chroma
from langchain.prompts import ChatPromptTemplate
from langchain_community.llms.ollama import Ollama
from langchain_ollama import OllamaLLM

from get_embedding_function import get_embedding_function

Expand Down Expand Up @@ -40,7 +40,7 @@ def query_rag(query_text: str):
prompt = prompt_template.format(context=context_text, question=query_text)
# print(prompt)

model = Ollama(model="mistral")
model = OllamaLLM(model="mistral")
response_text = model.invoke(prompt)

sources = [doc.metadata.get("id", None) for doc, _score in results]
Expand Down
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pypdf
langchain
langchain_ollama
langchain_aws
langchain_community
chromadb # Vector storage
pytest
boto3
boto3
5 changes: 3 additions & 2 deletions test_rag.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from query_data import query_rag
from langchain_community.llms.ollama import Ollama
from langchain_ollama import OllamaLLM

EVAL_PROMPT = """
Expected Response: {expected_response}
Expand Down Expand Up @@ -29,7 +29,7 @@ def query_and_validate(question: str, expected_response: str):
expected_response=expected_response, actual_response=response_text
)

model = Ollama(model="mistral")
model = OllamaLLM(model="mistral")
evaluation_results_str = model.invoke(prompt)
evaluation_results_str_cleaned = evaluation_results_str.strip().lower()

Expand All @@ -47,3 +47,4 @@ def query_and_validate(question: str, expected_response: str):
raise ValueError(
f"Invalid evaluation result. Cannot determine if 'true' or 'false'."
)