Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -272,6 +272,31 @@ def build_rag_prompt(query: str, contexts: list[dict]) -> str:
|
|
| 272 |
prompt = RAG_PROMPT_TEMPLATE.format(context=context_text, question=query)
|
| 273 |
return prompt
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
def rag_query(query: str, top_k: int = TOP_K_RESULTS) -> dict:
|
| 277 |
start_time = time.perf_counter()
|
|
|
|
| 272 |
prompt = RAG_PROMPT_TEMPLATE.format(context=context_text, question=query)
|
| 273 |
return prompt
|
| 274 |
|
| 275 |
+
def _call_model(cfg: dict, prompt: str) -> dict:
|
| 276 |
+
return call_llm_with_metrics(
|
| 277 |
+
prompt,
|
| 278 |
+
endpoint_url=cfg["endpoint_url"],
|
| 279 |
+
api_key=AZURE_API_KEY,
|
| 280 |
+
model=cfg["model"],
|
| 281 |
+
max_completion_tokens=cfg["max_completion_tokens"],
|
| 282 |
+
temperature=cfg["temperature"],
|
| 283 |
+
top_p=cfg["top_p"],
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def _parse_llm_json(raw_content: str):
|
| 288 |
+
json_str = raw_content.strip()
|
| 289 |
+
if json_str.startswith("```"):
|
| 290 |
+
json_str = json_str.split("\n", 1)[-1]
|
| 291 |
+
json_str = json_str.rsplit("```", 1)[0].strip()
|
| 292 |
+
try:
|
| 293 |
+
parsed = json.loads(json_str)
|
| 294 |
+
answer = parsed["answer"]
|
| 295 |
+
explanation = parsed.get("explanation", "")
|
| 296 |
+
confident = bool(parsed.get("confident", True)) # absent => on suppose confiant (pas d'escalade)
|
| 297 |
+
return answer, explanation, confident
|
| 298 |
+
except (json.JSONDecodeError, KeyError):
|
| 299 |
+
return raw_content, "LLM did not return a structured explanation.", True
|
| 300 |
|
| 301 |
def rag_query(query: str, top_k: int = TOP_K_RESULTS) -> dict:
|
| 302 |
start_time = time.perf_counter()
|