Update src/qa.py
Browse files
src/qa.py
CHANGED
|
@@ -125,16 +125,30 @@ def generate_answer(query: str, retrieved_chunks: list):
|
|
| 125 |
prompt = PROMPT_TEMPLATE.format(context=context, query=query)
|
| 126 |
|
| 127 |
try:
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
)
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
# ==========================================================
|
|
|
|
| 125 |
prompt = PROMPT_TEMPLATE.format(context=context, query=query)
|
| 126 |
|
| 127 |
try:
|
| 128 |
+
result = _answer_model(
|
| 129 |
+
prompt,
|
| 130 |
+
max_new_tokens=350, # allow longer, more complete answers
|
| 131 |
+
do_sample=True, # enable sampling for natural flow
|
| 132 |
+
temperature=0.7, # slightly higher = more expressive responses
|
| 133 |
+
top_p=0.95, # nucleus sampling for coherence
|
| 134 |
+
repetition_penalty=1.2 # discourages repetitive phrasing
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
answer = result[0]["generated_text"].strip()
|
| 138 |
+
|
| 139 |
+
# 🧩 If the model outputs something too short, expand gracefully
|
| 140 |
+
if len(answer.split()) < 8:
|
| 141 |
+
answer = (
|
| 142 |
+
"The document mentions this briefly. Based on the context, here's what it suggests: "
|
| 143 |
+
+ answer
|
| 144 |
)
|
| 145 |
+
|
| 146 |
+
return answer
|
| 147 |
+
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"⚠️ Generation failed: {e}")
|
| 150 |
+
return "⚠️ Error: Could not generate an answer at the moment."
|
| 151 |
+
|
| 152 |
|
| 153 |
|
| 154 |
# ==========================================================
|