pdf-rag-app / src /generator.py
yamuna1's picture
Update src/generator.py
a392c1f verified
Raw
History Blame Contribute Delete
3.44 kB
import os
from huggingface_hub import InferenceClient
try:
import ollama
except:
ollama = None
IS_LOCAL = os.getenv("ENV", "PROD") == "DEV"
print("DEBUG MODE:", "LOCAL" if IS_LOCAL else "HF")
# πŸ”₯ STRONGER PROMPT
def build_prompt(query, context):
return f"""
You are a strict research assistant.
Follow these rules EXACTLY:
1. Answer ONLY using the provided context.
2. Do NOT use prior knowledge.
3. Do NOT infer or generalize.
4. If the answer is not explicitly stated in the context, return EXACTLY:
Not found in document.
5. If answering:
- Use only sentences from context
- Provide complete information from context (do not omit key details)
- Cite like: (Source: <document>.pdf, Page <number>)
- Do NOT add extra explanation
Context:
{context}
Question:
{query}
"""
def build_context(retrieved_docs):
context = ""
for i, doc in enumerate(retrieved_docs):
context += f"""
[Chunk {i+1}]
Document: {doc['source']}
Page: {doc['page']}
Text: {doc['text']}
"""
return context
# πŸ”₯ BETTER CONFIDENCE CHECK
def is_low_confidence(retrieved_docs):
if not retrieved_docs:
return True
# if all scores are weak β†’ reject
avg_score = sum(d["score"] for d in retrieved_docs) / len(retrieved_docs)
return avg_score > 0.75 # πŸ”₯ tuned for your system
def generate_answer(query, retrieved_docs):
print("DEBUG - entering generate_answer")
print("DEBUG - retrieved_docs:", retrieved_docs)
# πŸ”₯ Step 1: confidence check
if is_low_confidence(retrieved_docs):
return "Not found in document."
# πŸ”₯ Step 2: structured context
context = build_context(retrieved_docs)
# πŸ”₯ Step 3: prompt
prompt = build_prompt(query, context)
# πŸ”Ή LOCAL MODE
if IS_LOCAL:
if ollama is None:
raise RuntimeError("Ollama not running")
response = ollama.chat(
model="mistral",
messages=[{"role": "user", "content": prompt}]
)
answer = response['message']['content']
# πŸ”Ή HF MODE
else:
hf_token = os.getenv("HF_TOKEN")
if hf_token is None:
raise ValueError("HF_TOKEN not set")
client = InferenceClient(
model="meta-llama/Meta-Llama-3-8B-Instruct",
token=hf_token
)
response = client.chat_completion(
messages=[
{"role": "user", "content": prompt}
],
max_tokens=400,
temperature=0.1,
)
answer = response.choices[0].message.content
print("DEBUG - raw answer:", answer)
# πŸ”₯ HARD ENFORCEMENT
if "not found" in answer.lower():
return "Not found in document."
# πŸ”₯ GROUNDING CHECK (VERY IMPORTANT)
# πŸ”₯ RELAXED GROUNDING CHECK
if len(answer.strip()) < 20:
return "Not found in document."
# πŸ”₯ Sources
sources_dict = {}
for doc in retrieved_docs[:2]:
key = (doc['source'], doc['page'])
if key not in sources_dict:
sources_dict[key] = 1
else:
sources_dict[key] += 1
sources = "\n\nSources:\n"
for (source, page), count in sources_dict.items():
if count > 1:
sources += f"- {source}.pdf (Page {page}, {count} chunks)\n"
else:
sources += f"- {source}.pdf (Page {page})\n"
return answer.strip() + sources