diff --git a/README.md b/README.md index 6af3a5251..199c4e0e7 100644 --- a/README.md +++ b/README.md @@ -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 + 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. \ No newline at end of file diff --git a/get_embedding_function.py b/get_embedding_function.py index 79d04113b..9b9161ccf 100644 --- a/get_embedding_function.py +++ b/get_embedding_function.py @@ -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 diff --git a/populate_database.py b/populate_database.py index 3d2a1ab8a..f12c30402 100644 --- a/populate_database.py +++ b/populate_database.py @@ -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" @@ -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") diff --git a/query_data.py b/query_data.py index 33299e582..4534d401d 100644 --- a/query_data.py +++ b/query_data.py @@ -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 @@ -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] diff --git a/requirements.txt b/requirements.txt index b290b554c..1547656ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,8 @@ pypdf langchain +langchain_ollama +langchain_aws +langchain_community chromadb # Vector storage pytest -boto3 +boto3 \ No newline at end of file diff --git a/test_rag.py b/test_rag.py index e19ed0388..4d1d9dd73 100644 --- a/test_rag.py +++ b/test_rag.py @@ -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} @@ -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() @@ -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'." ) +