Spaces:
Sleeping
Sleeping
Delete src/qabot.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230"""QueryVault - PDF Question Answering BotCore backend logic: document loading, embedding, retrieval, and LLM response generation."""
import osimport uuidfrom langchain_community.document_loaders import PyPDFLoaderfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom langchain_community.vectorstores import Chromafrom langchain_core.prompts import PromptTemplatefrom langchain_core.runnables import RunnablePassthroughfrom langchain_core.output_parsers import StrOutputParserfrom langchain_groq import ChatGroqfrom langchain_huggingface import HuggingFaceEmbeddings
# ---------------------------------------------------------------------------# 1. LLM Initialization# ---------------------------------------------------------------------------
def init_llm() -> ChatGroq: """ Initialize the Groq LLM (Llama 3 70B Instruct).
Reads credentials from environment variables: GROQ_API_KEY β your Groq API key
Returns: ChatGroq instance ready for inference. """ llm = ChatGroq( model="llama-3.3-70b-versatile", api_key=os.getenv("GROQ_API_KEY"), temperature=0.1 ) return llm
# ---------------------------------------------------------------------------# 2. Embedding Model# ---------------------------------------------------------------------------
def get_embedding_model() -> HuggingFaceEmbeddings: """ Initialize HuggingFace Local Embeddings (all-MiniLM-L6-v2).
Returns: HuggingFaceEmbeddings instance. """ embeddings = HuggingFaceEmbeddings( model_name="all-MiniLM-L6-v2" ) return embeddings
# ---------------------------------------------------------------------------# 3. Document Loader# ---------------------------------------------------------------------------
def load_pdf(file_path: str) -> list: """ Load and parse a PDF file using PyPDFLoader.
Args: file_path: Absolute or relative path to the PDF file.
Returns: List of LangChain Document objects (one per page). """ loader = PyPDFLoader(file_path) documents = loader.load() return documents
# ---------------------------------------------------------------------------# 4. Text Splitter# ---------------------------------------------------------------------------
def split_documents(documents: list) -> list: """ Split loaded documents into smaller chunks for embedding.
Uses RecursiveCharacterTextSplitter with: chunk_size = 1000 characters chunk_overlap = 100 characters (preserves boundary context)
Args: documents: List of LangChain Document objects.
Returns: List of smaller Document chunks. """ splitter = RecursiveCharacterTextSplitter( chunk_size=500, chunk_overlap=50, ) chunks = splitter.split_documents(documents) return chunks
# ---------------------------------------------------------------------------# 5. Vector Store# ---------------------------------------------------------------------------
def create_vector_store(chunks: list) -> Chroma: """ Convert document chunks into embeddings and store in ChromaDB.
Args: chunks: List of Document chunks from the text splitter.
Returns: Chroma vector store instance. """ embedding_model = get_embedding_model() vector_store = Chroma.from_documents( documents=chunks, embedding=embedding_model, collection_name=f"pdf_{uuid.uuid4().hex}" ) return vector_store
# ---------------------------------------------------------------------------# 6. Retriever# ---------------------------------------------------------------------------
def get_retriever(vector_store: Chroma): """ Create a retriever from the vector store using similarity search.
Args: vector_store: Chroma vector store instance.
Returns: LangChain BaseRetriever configured for top-3 similarity search. """ retriever = vector_store.as_retriever( search_type="similarity", search_kwargs={"k": 10}, ) return retriever
# ---------------------------------------------------------------------------# 7. QA Chain# ---------------------------------------------------------------------------
def build_qa_chain(retriever, llm: ChatGroq): """ Build a QA chain combining the retriever and LLM using LCEL.
Args: retriever: LangChain retriever from vector store. llm: Initialized ChatWatsonx instance.
Returns: Runnable chain ready to answer questions. """ template = """You are a helpful AI assistant answering questions based on a provided document.Use the following pieces of context from the document to answer the question. If the question is general (like "what is this document about"), summarize the provided context. If the context does not contain the answer, say "I don't have enough information from the document to answer that."
Context:{context}
Question: {question}
Helpful Answer:""" prompt = PromptTemplate.from_template(template)
def format_docs(docs): return "\\n\\n".join(doc.page_content for doc in docs)
qa_chain = ( {"context": retriever | format_docs, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() ) return qa_chain
# ---------------------------------------------------------------------------# 8. Full Pipeline: process PDF and answer a question# ---------------------------------------------------------------------------
def process_pdf_and_query(file_path: str, question: str) -> str: """ End-to-end pipeline: load PDF β split β embed β retrieve β answer.
Args: file_path: Path to the uploaded PDF file. question: User's natural language question.
Returns: String answer generated by the LLM. """ if not file_path: return "Please upload a PDF file first." if not question or not str(question).strip(): return "Please enter a question."
try: # Step 1 β Load documents = load_pdf(file_path)
# Step 2 β Split chunks = split_documents(documents)
# Step 3 & 4 β Embed + Store vector_store = create_vector_store(chunks)
# Step 5 β Retrieve retriever = get_retriever(vector_store)
# Step 6 β LLM llm = init_llm()
# Step 7 β QA Chain qa_chain = build_qa_chain(retriever, llm)
# Step 8 β Run answer = qa_chain.invoke(question) if not answer: answer = "No answer could be generated." return answer
except Exception as e: return f"Error processing your request: {str(e)}"src/qabot.pyDeleted-230