This module defines the core logic for extracting answers from documents using a Retrieval-Augmented Generation (RAG) pipeline powered by Groq LLM.
- Embed and semantically search chunks for each document via FAISS
- Generate answers from top-k retrieved chunks using Groq's LLM
- Return detailed, clause-preserving responses along with paragraph-level citations
extract_answers_from_docs(session_id: str, user_query: str, doc_ids: List[str]) -> List[Dict[str, str]]
Given the session_id, a user question and a list of document IDs:
- Retrieves top-k chunks per document using FAISS
- Constructs a detailed system prompt including:
- Chunk texts
- Paragraph references
- The user’s question
- Sends the prompt to Groq LLM
- Parses the LLM's response (JSON only)
- Returns answers and citations for each document
The prompt sent to Groq instructs the LLM to:
- Extract full, unmodified clauses, sections, and references
- Avoid paraphrasing or summarizing legal/technical terms
- Return a strict JSON format:
{"answer":"...","citation":"Para X"}
Citation Extraction
Each paragraph is referenced as [Para X] in the search prompt
The LLM is instructed to cite the specific paragraph(s)
Invalid responses or “no relevant information” are gracefully handled
-
Groq – LLM client(llama-3.3-70b-versatile used) -
search_top_k_chunks()fromvector_store.py– for semantic chunk retrieval -
config.GROQ_API_KEY– pulled from .env -
loggerfromlogger.pyfor structured logging -
re,json,tabulate– for response validation and formatting
-
Responses are forced into valid JSON with RegEx pre-checking
-
Empty or badly formatted responses are skipped with warnings
-
Errors are logged and do not crash the pipeline
vector_store.py – Provides search_top_k_chunks
pipeline_routes.py – Calls this function during analysis
theme_identifier.py – Uses results from this module to extract themes