python --version
pip install openai fastapi uvicorn
export OPENAI_API_KEY=sk-...
Score LLM responses in the background after returning them to users, storing scores for dashboarding without adding latency.
1 import json 2 import statistics 3 from collections import deque 4 from fastapi import FastAPI, BackgroundTasks 5 from openai import OpenAI 6 7 app = FastAPI() 8 client = OpenAI() 9 scores: deque[float] = deque(maxlen=1000) 10 11 def evaluate_in_background(question: str, answer: str): 12 try: 13 prompt = f"""Score correctness 1-5. Return JSON: {{"score": <1-5>}} 14 Q: {question} 15 A: {answer}""" 16 resp = client.chat.completions.create( 17 model="gpt-4o-mini", 18 messages=[{"role": "user", "content": prompt}], 19 response_format={"type": "json_object"}, 20 ) 21 score = json.loads(resp.choices[0].message.content or "{}").get("score", 3) 22 scores.append(float(score)) 23 except Exception: 24 pass # Don't fail the request if eval errors 25 26 @app.post("/chat") 27 async def chat(question: str, background_tasks: BackgroundTasks): 28 resp = client.chat.completions.create( 29 model="gpt-4o-mini", 30 messages=[{"role": "user", "content": question}], 31 ) 32 answer = resp.choices[0].message.content or "" 33 background_tasks.add_task(evaluate_in_background, question, answer) 34 return {"answer": answer} 35 36 @app.get("/eval-stats") 37 async def eval_stats(): 38 if not scores: 39 return {"message": "No evaluations yet"} 40 score_list = list(scores) 41 return { 42 "count": len(score_list), 43 "mean": round(statistics.mean(score_list), 2), 44 "min": min(score_list), 45 "max": max(score_list), 46 } 47
Sign in to share your feedback and join the discussion.