Spaces:
Sleeping
Sleeping
daniel-simeone commited on
Commit ·
7e002df
1
Parent(s): 9977437
update references
Browse files
app.py
CHANGED
|
@@ -242,10 +242,33 @@ Question: {query}
|
|
| 242 |
|
| 243 |
Answer:"""
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
# Generate response using chat/comversational API (Mistral instruct uses this)
|
| 246 |
try:
|
| 247 |
response_text = self._generate_with_chat(prompt, max_new_tokens=512)
|
| 248 |
if response_text:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
return response_text
|
| 250 |
raise ValueError("Empty response from model")
|
| 251 |
except Exception as api_error:
|
|
|
|
| 242 |
|
| 243 |
Answer:"""
|
| 244 |
|
| 245 |
+
# Build mapping from context index to source label for resolving references
|
| 246 |
+
context_index_to_source = {}
|
| 247 |
+
for i, result in enumerate(results, 1):
|
| 248 |
+
meta = result.get("metadata") or {}
|
| 249 |
+
context_index_to_source[i] = (
|
| 250 |
+
meta.get("document_title") or meta.get("source") or f"Source {i}"
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
# Generate response using chat/comversational API (Mistral instruct uses this)
|
| 254 |
try:
|
| 255 |
response_text = self._generate_with_chat(prompt, max_new_tokens=512)
|
| 256 |
if response_text:
|
| 257 |
+
# Resolve [Context N] to actual source labels in the body
|
| 258 |
+
for i, source_label in context_index_to_source.items():
|
| 259 |
+
response_text = response_text.replace(
|
| 260 |
+
f"[Context {i}]",
|
| 261 |
+
f"({source_label})",
|
| 262 |
+
)
|
| 263 |
+
# Append a References section so users see what each source is
|
| 264 |
+
ref_lines = [
|
| 265 |
+
"",
|
| 266 |
+
"---",
|
| 267 |
+
"**References**",
|
| 268 |
+
]
|
| 269 |
+
for i, source_label in context_index_to_source.items():
|
| 270 |
+
ref_lines.append(f"{i}. {source_label}")
|
| 271 |
+
response_text = response_text.rstrip() + "\n\n" + "\n".join(ref_lines)
|
| 272 |
return response_text
|
| 273 |
raise ValueError("Empty response from model")
|
| 274 |
except Exception as api_error:
|