Update src/qa.py
Browse files
src/qa.py
CHANGED
|
@@ -112,28 +112,45 @@ def retrieve_chunks(query: str, index, chunks: list, top_k: int = 5):
|
|
| 112 |
# ==========================================================
|
| 113 |
def generate_answer(query: str, retrieved_chunks: list, reasoning_mode: bool = False):
|
| 114 |
"""
|
| 115 |
-
Generate answers with
|
| 116 |
-
reasoning_mode=
|
| 117 |
-
reasoning_mode=
|
| 118 |
"""
|
| 119 |
if not retrieved_chunks:
|
| 120 |
return "Sorry, I couldn’t find relevant information in the document."
|
| 121 |
|
| 122 |
context = "\n".join(chunk.strip() for chunk in retrieved_chunks)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
prompt = (REASONING_PROMPT if reasoning_mode else STRICT_PROMPT).format(context=context, query=query)
|
| 124 |
|
| 125 |
try:
|
| 126 |
if reasoning_mode:
|
| 127 |
-
#
|
| 128 |
result = _answer_model(
|
| 129 |
prompt,
|
| 130 |
max_new_tokens=180,
|
| 131 |
-
temperature=0.
|
| 132 |
-
do_sample=False, #
|
| 133 |
pad_token_id=_tokenizer.eos_token_id,
|
| 134 |
)
|
| 135 |
else:
|
| 136 |
-
# ⚡
|
| 137 |
result = _answer_model(
|
| 138 |
prompt,
|
| 139 |
max_new_tokens=120,
|
|
@@ -142,10 +159,10 @@ def generate_answer(query: str, retrieved_chunks: list, reasoning_mode: bool = F
|
|
| 142 |
pad_token_id=_tokenizer.eos_token_id,
|
| 143 |
)
|
| 144 |
|
| 145 |
-
raw = result[0]["generated_text"]
|
| 146 |
if "Answer:" in raw:
|
| 147 |
raw = raw.split("Answer:")[-1].strip()
|
| 148 |
-
return raw
|
| 149 |
|
| 150 |
except Exception as e:
|
| 151 |
print(f"⚠️ Generation failed: {e}")
|
|
|
|
| 112 |
# ==========================================================
|
| 113 |
def generate_answer(query: str, retrieved_chunks: list, reasoning_mode: bool = False):
|
| 114 |
"""
|
| 115 |
+
Generate answers with Phi-2.
|
| 116 |
+
- reasoning_mode=False → strict factual, fast
|
| 117 |
+
- reasoning_mode=True → analytical, richer reasoning (slower)
|
| 118 |
"""
|
| 119 |
if not retrieved_chunks:
|
| 120 |
return "Sorry, I couldn’t find relevant information in the document."
|
| 121 |
|
| 122 |
context = "\n".join(chunk.strip() for chunk in retrieved_chunks)
|
| 123 |
+
|
| 124 |
+
# 🧠 Reasoning prompt: encourages explanation, not just lookup
|
| 125 |
+
REASONING_PROMPT = (
|
| 126 |
+
"You are an expert assistant with strong reasoning skills.\n"
|
| 127 |
+
"Think step by step and form a detailed, logical answer.\n"
|
| 128 |
+
"You can combine hints from the context with your general understanding.\n"
|
| 129 |
+
"If the context doesn't mention the answer, acknowledge that.\n\n"
|
| 130 |
+
"Context:\n{context}\n\nQuestion: {query}\n\nAnswer:"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# ⚡ Strict factual prompt
|
| 134 |
+
STRICT_PROMPT = (
|
| 135 |
+
"Answer based ONLY on the context below.\n"
|
| 136 |
+
"If the answer isn’t in the context, say: 'I don't know based on the provided document.'\n\n"
|
| 137 |
+
"Context:\n{context}\n\nQuestion: {query}\nAnswer:"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
prompt = (REASONING_PROMPT if reasoning_mode else STRICT_PROMPT).format(context=context, query=query)
|
| 141 |
|
| 142 |
try:
|
| 143 |
if reasoning_mode:
|
| 144 |
+
# 🧩 The “brainy” config that produced the great long answer
|
| 145 |
result = _answer_model(
|
| 146 |
prompt,
|
| 147 |
max_new_tokens=180,
|
| 148 |
+
temperature=0.45,
|
| 149 |
+
do_sample=False, # reasoning but deterministic
|
| 150 |
pad_token_id=_tokenizer.eos_token_id,
|
| 151 |
)
|
| 152 |
else:
|
| 153 |
+
# ⚡ Fast factual config
|
| 154 |
result = _answer_model(
|
| 155 |
prompt,
|
| 156 |
max_new_tokens=120,
|
|
|
|
| 159 |
pad_token_id=_tokenizer.eos_token_id,
|
| 160 |
)
|
| 161 |
|
| 162 |
+
raw = result[0]["generated_text"].strip()
|
| 163 |
if "Answer:" in raw:
|
| 164 |
raw = raw.split("Answer:")[-1].strip()
|
| 165 |
+
return raw
|
| 166 |
|
| 167 |
except Exception as e:
|
| 168 |
print(f"⚠️ Generation failed: {e}")
|