Spaces:
Runtime error
Runtime error
Update rag_utility.py
Browse files- rag_utility.py +37 -35
rag_utility.py
CHANGED
|
@@ -1,71 +1,73 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
from langchain_community.document_loaders import UnstructuredPDFLoader
|
| 5 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
from langchain_chroma import Chroma
|
| 8 |
from langchain_groq import ChatGroq
|
| 9 |
from langchain.chains import RetrievalQA
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
config_data = json.load(open(f"{working_dir}/config.json"))
|
| 13 |
load_dotenv()
|
| 14 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 15 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
embedding = HuggingFaceEmbeddings()
|
| 20 |
|
| 21 |
-
#
|
| 22 |
deepseek_llm = ChatGroq(
|
| 23 |
model="deepseek-r1-distill-llama-70b",
|
| 24 |
temperature=0
|
| 25 |
)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
llama3_llm = ChatGroq(
|
| 29 |
model="llama-3.3-70b-versatile",
|
| 30 |
temperature=0
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
| 34 |
def process_document_to_chromadb(file_name):
|
| 35 |
-
|
| 36 |
-
loader = UnstructuredPDFLoader(
|
| 37 |
-
# loading the documents
|
| 38 |
documents = loader.load()
|
| 39 |
-
# splitting the text into
|
| 40 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=200)
|
| 41 |
texts = text_splitter.split_documents(documents)
|
| 42 |
-
vectordb = Chroma.from_documents(
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
def answer_question(user_question):
|
| 49 |
-
|
| 50 |
-
vectordb = Chroma(
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
retriever = vectordb.as_retriever()
|
| 54 |
-
|
| 55 |
-
#
|
| 56 |
-
qa_chain_deepseek = RetrievalQA.from_chain_type(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
response_deepseek = qa_chain_deepseek.invoke({"query": user_question})
|
| 61 |
answer_deepseek = response_deepseek["result"]
|
| 62 |
-
|
| 63 |
-
#
|
| 64 |
-
qa_chain_llama3 = RetrievalQA.from_chain_type(
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
response_llama3 = qa_chain_llama3.invoke({"query": user_question})
|
| 69 |
answer_llama3 = response_llama3["result"]
|
| 70 |
-
|
| 71 |
return {"answer_deepseek": answer_deepseek, "answer_llama3": answer_llama3}
|
|
|
|
| 1 |
import os
|
| 2 |
+
from dotenv import load_dotenv
|
|
|
|
| 3 |
from langchain_community.document_loaders import UnstructuredPDFLoader
|
| 4 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 6 |
from langchain_chroma import Chroma
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
+
|
| 10 |
+
# Load environment variables
|
|
|
|
| 11 |
load_dotenv()
|
| 12 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 13 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
| 14 |
|
| 15 |
+
working_dir = os.path.dirname(os.path.abspath(__file__))
|
| 16 |
|
| 17 |
+
# Initialize the embedding model
|
| 18 |
embedding = HuggingFaceEmbeddings()
|
| 19 |
|
| 20 |
+
# Initialize the DeepSeek-R1 70B model
|
| 21 |
deepseek_llm = ChatGroq(
|
| 22 |
model="deepseek-r1-distill-llama-70b",
|
| 23 |
temperature=0
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Initialize the Llama-3 70B model
|
| 27 |
llama3_llm = ChatGroq(
|
| 28 |
model="llama-3.3-70b-versatile",
|
| 29 |
temperature=0
|
| 30 |
)
|
| 31 |
|
|
|
|
| 32 |
def process_document_to_chromadb(file_name):
|
| 33 |
+
"""Processes a PDF document and stores embeddings in ChromaDB."""
|
| 34 |
+
loader = UnstructuredPDFLoader(os.path.join(working_dir, file_name))
|
|
|
|
| 35 |
documents = loader.load()
|
|
|
|
| 36 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=200)
|
| 37 |
texts = text_splitter.split_documents(documents)
|
| 38 |
+
vectordb = Chroma.from_documents(
|
| 39 |
+
documents=texts,
|
| 40 |
+
embedding=embedding,
|
| 41 |
+
persist_directory=os.path.join(working_dir, "doc_vectorstore")
|
| 42 |
+
)
|
| 43 |
+
return "Document successfully processed and stored."
|
| 44 |
|
| 45 |
def answer_question(user_question):
|
| 46 |
+
"""Retrieves answers from stored documents using DeepSeek-R1 and Llama-3."""
|
| 47 |
+
vectordb = Chroma(
|
| 48 |
+
persist_directory=os.path.join(working_dir, "doc_vectorstore"),
|
| 49 |
+
embedding_function=embedding
|
| 50 |
+
)
|
| 51 |
retriever = vectordb.as_retriever()
|
| 52 |
+
|
| 53 |
+
# DeepSeek-R1 response
|
| 54 |
+
qa_chain_deepseek = RetrievalQA.from_chain_type(
|
| 55 |
+
llm=deepseek_llm,
|
| 56 |
+
chain_type="stuff",
|
| 57 |
+
retriever=retriever,
|
| 58 |
+
return_source_documents=True
|
| 59 |
+
)
|
| 60 |
response_deepseek = qa_chain_deepseek.invoke({"query": user_question})
|
| 61 |
answer_deepseek = response_deepseek["result"]
|
| 62 |
+
|
| 63 |
+
# Llama-3 response
|
| 64 |
+
qa_chain_llama3 = RetrievalQA.from_chain_type(
|
| 65 |
+
llm=llama3_llm,
|
| 66 |
+
chain_type="stuff",
|
| 67 |
+
retriever=retriever,
|
| 68 |
+
return_source_documents=True
|
| 69 |
+
)
|
| 70 |
response_llama3 = qa_chain_llama3.invoke({"query": user_question})
|
| 71 |
answer_llama3 = response_llama3["result"]
|
| 72 |
+
|
| 73 |
return {"answer_deepseek": answer_deepseek, "answer_llama3": answer_llama3}
|