Update src/rag_utils.py
Browse files- src/rag_utils.py +54 -54
src/rag_utils.py
CHANGED
|
@@ -1,54 +1,54 @@
|
|
| 1 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 2 |
-
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
-
from langchain_chroma import Chroma
|
| 4 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 8 |
-
|
| 9 |
-
def load_pdf_document(file_path):
|
| 10 |
-
document_loader = PyPDFLoader(file_path)
|
| 11 |
-
return document_loader.load()
|
| 12 |
-
|
| 13 |
-
def chunk_documents(raw_documents):
|
| 14 |
-
text_processor = RecursiveCharacterTextSplitter(
|
| 15 |
-
chunk_size = 1000,
|
| 16 |
-
chunk_overlap = 200,
|
| 17 |
-
add_start_index = True
|
| 18 |
-
)
|
| 19 |
-
return text_processor.split_documents(raw_documents)
|
| 20 |
-
|
| 21 |
-
def find_related_documents(query, vector_database):
|
| 22 |
-
# return vector_database.similarity_search(query, k=2)
|
| 23 |
-
return vector_database.max_marginal_relevance_search(query, k=2, fetch_k=5, lambda_mult=0.6)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def ProcessDocuments(document_path: str) -> str:
|
| 27 |
-
|
| 28 |
-
loaded_doc = load_pdf_document(document_path)
|
| 29 |
-
chunked_doc = chunk_documents(loaded_doc)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
vector_database = Chroma(
|
| 33 |
-
persist_directory=f"./chroma_store/{document_path.split("
|
| 34 |
-
embedding_function=embedding_model
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
vector_database.add_documents(chunked_doc)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def generate_context(query: str, file: str):
|
| 41 |
-
|
| 42 |
-
ProcessDocuments(file)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
vector_database = Chroma(
|
| 46 |
-
persist_directory=f"./chroma_store/{file.split("
|
| 47 |
-
embedding_function=embedding_model
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
relevant_docs = find_related_documents(query, vector_database)
|
| 51 |
-
context_text = "\n".join([doc.page_content for doc in relevant_docs])
|
| 52 |
-
|
| 53 |
-
return query, context_text
|
| 54 |
-
|
|
|
|
| 1 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 2 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
+
from langchain_chroma import Chroma
|
| 4 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 8 |
+
|
| 9 |
+
def load_pdf_document(file_path):
|
| 10 |
+
document_loader = PyPDFLoader(file_path)
|
| 11 |
+
return document_loader.load()
|
| 12 |
+
|
| 13 |
+
def chunk_documents(raw_documents):
|
| 14 |
+
text_processor = RecursiveCharacterTextSplitter(
|
| 15 |
+
chunk_size = 1000,
|
| 16 |
+
chunk_overlap = 200,
|
| 17 |
+
add_start_index = True
|
| 18 |
+
)
|
| 19 |
+
return text_processor.split_documents(raw_documents)
|
| 20 |
+
|
| 21 |
+
def find_related_documents(query, vector_database):
|
| 22 |
+
# return vector_database.similarity_search(query, k=2)
|
| 23 |
+
return vector_database.max_marginal_relevance_search(query, k=2, fetch_k=5, lambda_mult=0.6)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def ProcessDocuments(document_path: str) -> str:
|
| 27 |
+
|
| 28 |
+
loaded_doc = load_pdf_document(document_path)
|
| 29 |
+
chunked_doc = chunk_documents(loaded_doc)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
vector_database = Chroma(
|
| 33 |
+
persist_directory=f"./chroma_store/{document_path.split("/")[-1].split(".")[0]}",
|
| 34 |
+
embedding_function=embedding_model
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
vector_database.add_documents(chunked_doc)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def generate_context(query: str, file: str):
|
| 41 |
+
|
| 42 |
+
ProcessDocuments(file)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
vector_database = Chroma(
|
| 46 |
+
persist_directory=f"./chroma_store/{file.split("/")[-1].split(".")[0]}",
|
| 47 |
+
embedding_function=embedding_model
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
relevant_docs = find_related_documents(query, vector_database)
|
| 51 |
+
context_text = "\n".join([doc.page_content for doc in relevant_docs])
|
| 52 |
+
|
| 53 |
+
return query, context_text
|
| 54 |
+
|