Claude commited on
Commit
1451885
·
unverified ·
1 Parent(s): a807df1

Add error logging and LLM debug endpoint for troubleshooting

Browse files
Files changed (1) hide show
  1. app/main.py +35 -9
app/main.py CHANGED
@@ -1,15 +1,20 @@
1
  """FastAPI application for AIM Learning Companion."""
2
 
 
3
  import re
 
4
  from contextlib import asynccontextmanager
5
  from pathlib import Path
6
  from typing import List
7
 
8
  from fastapi import FastAPI, UploadFile, File
9
- from fastapi.responses import FileResponse
10
  from fastapi.staticfiles import StaticFiles
11
  from pydantic import BaseModel
12
 
 
 
 
13
  from app.rag import load_corpus, retrieve, add_documents, list_documents, delete_document
14
  from app.llm import build_system_prompt, chat, analyze_session
15
 
@@ -75,16 +80,20 @@ def _detect_phase(reply: str, current_phase: int) -> int:
75
 
76
  @app.post("/api/chat", response_model=ChatResponse)
77
  async def api_chat(req: ChatRequest):
78
- rag_chunks = retrieve(req.message)
79
- system_prompt = build_system_prompt(req.mode, req.topic, req.phase, rag_chunks)
 
80
 
81
- messages = [{"role": m["role"], "content": m["content"]} for m in req.history]
82
- messages.append({"role": "user", "content": req.message})
83
 
84
- reply = await chat(system_prompt, messages)
85
- detected_phase = _detect_phase(reply, req.phase)
86
 
87
- return ChatResponse(reply=reply, phase=detected_phase)
 
 
 
88
 
89
 
90
  @app.post("/api/upload")
@@ -149,4 +158,21 @@ async def api_analyze(req: AnalysisRequest):
149
 
150
  @app.get("/api/health")
151
  async def health():
152
- return {"status": "ok"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """FastAPI application for AIM Learning Companion."""
2
 
3
+ import logging
4
  import re
5
+ import traceback
6
  from contextlib import asynccontextmanager
7
  from pathlib import Path
8
  from typing import List
9
 
10
  from fastapi import FastAPI, UploadFile, File
11
+ from fastapi.responses import FileResponse, JSONResponse
12
  from fastapi.staticfiles import StaticFiles
13
  from pydantic import BaseModel
14
 
15
+ logging.basicConfig(level=logging.INFO)
16
+ logger = logging.getLogger(__name__)
17
+
18
  from app.rag import load_corpus, retrieve, add_documents, list_documents, delete_document
19
  from app.llm import build_system_prompt, chat, analyze_session
20
 
 
80
 
81
  @app.post("/api/chat", response_model=ChatResponse)
82
  async def api_chat(req: ChatRequest):
83
+ try:
84
+ rag_chunks = retrieve(req.message)
85
+ system_prompt = build_system_prompt(req.mode, req.topic, req.phase, rag_chunks)
86
 
87
+ messages = [{"role": m["role"], "content": m["content"]} for m in req.history]
88
+ messages.append({"role": "user", "content": req.message})
89
 
90
+ reply = await chat(system_prompt, messages)
91
+ detected_phase = _detect_phase(reply, req.phase)
92
 
93
+ return ChatResponse(reply=reply, phase=detected_phase)
94
+ except Exception as e:
95
+ logger.error(f"Chat error: {e}\n{traceback.format_exc()}")
96
+ return JSONResponse(status_code=500, content={"error": str(e)})
97
 
98
 
99
  @app.post("/api/upload")
 
158
 
159
  @app.get("/api/health")
160
  async def health():
161
+ import os
162
+ return {
163
+ "status": "ok",
164
+ "has_api_key": bool(os.environ.get("OPENROUTER_API_KEY", "")),
165
+ "base_url": os.environ.get("LLM_BASE_URL", "(not set)"),
166
+ "model": os.environ.get("LLM_MODEL", "(not set)"),
167
+ }
168
+
169
+
170
+ @app.get("/api/test-llm")
171
+ async def test_llm():
172
+ """Quick test of the LLM connection."""
173
+ try:
174
+ reply = await chat("Tu es un assistant. Reponds en une phrase.", [{"role": "user", "content": "Dis bonjour."}])
175
+ return {"status": "ok", "reply": reply}
176
+ except Exception as e:
177
+ logger.error(f"LLM test error: {e}\n{traceback.format_exc()}")
178
+ return JSONResponse(status_code=500, content={"error": str(e), "type": type(e).__name__})