Update app.py
Browse files
app.py
CHANGED
|
@@ -196,41 +196,41 @@ def financial_score(payload: ScoreRequest):
|
|
| 196 |
# STRICT GPT-4.1 JSON (Option A)
|
| 197 |
# ===========================
|
| 198 |
try:
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
}
|
| 215 |
}
|
| 216 |
-
}
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
| 222 |
except Exception as exc:
|
| 223 |
raise HTTPException(status_code=502, detail=f"OpenAI request failed: {exc}")
|
| 224 |
-
|
| 225 |
-
# `.parsed` contains
|
| 226 |
try:
|
| 227 |
-
parsed = response.
|
| 228 |
except Exception:
|
| 229 |
raise HTTPException(
|
| 230 |
status_code=502,
|
| 231 |
detail="OpenAI response did not include parsed JSON (unexpected)"
|
| 232 |
)
|
| 233 |
-
|
| 234 |
# Score
|
| 235 |
score_val = parsed.get("score", 0)
|
| 236 |
try:
|
|
@@ -238,9 +238,10 @@ def financial_score(payload: ScoreRequest):
|
|
| 238 |
except:
|
| 239 |
score_val = 0
|
| 240 |
score_val = max(0, min(100, score_val))
|
| 241 |
-
|
| 242 |
return {
|
| 243 |
"userId": payload.userId,
|
| 244 |
"score": score_val,
|
| 245 |
"explanation": parsed.get("explanation")
|
| 246 |
}
|
|
|
|
|
|
| 196 |
# STRICT GPT-4.1 JSON (Option A)
|
| 197 |
# ===========================
|
| 198 |
try:
|
| 199 |
+
response = openai.responses.create(
|
| 200 |
+
model="gpt-4o-mini",
|
| 201 |
+
temperature=0.6,
|
| 202 |
+
response_format={
|
| 203 |
+
"type": "json_schema",
|
| 204 |
+
"json_schema": {
|
| 205 |
+
"name": "financial_score",
|
| 206 |
+
"schema": {
|
| 207 |
+
"type": "object",
|
| 208 |
+
"properties": {
|
| 209 |
+
"score": {"type": "number"},
|
| 210 |
+
"explanation": {"type": "string"}
|
| 211 |
+
},
|
| 212 |
+
"required": ["score", "explanation"],
|
| 213 |
+
"additionalProperties": False
|
|
|
|
| 214 |
}
|
| 215 |
+
}
|
| 216 |
+
},
|
| 217 |
+
messages=[
|
| 218 |
+
{"role": "system", "content": "You are a financial scoring engine."},
|
| 219 |
+
{"role": "user", "content": prompt}
|
| 220 |
+
]
|
| 221 |
+
)
|
| 222 |
except Exception as exc:
|
| 223 |
raise HTTPException(status_code=502, detail=f"OpenAI request failed: {exc}")
|
| 224 |
+
|
| 225 |
+
# `.output[0].content[0].parsed` contains guaranteed valid JSON
|
| 226 |
try:
|
| 227 |
+
parsed = response.output[0].content[0].parsed
|
| 228 |
except Exception:
|
| 229 |
raise HTTPException(
|
| 230 |
status_code=502,
|
| 231 |
detail="OpenAI response did not include parsed JSON (unexpected)"
|
| 232 |
)
|
| 233 |
+
|
| 234 |
# Score
|
| 235 |
score_val = parsed.get("score", 0)
|
| 236 |
try:
|
|
|
|
| 238 |
except:
|
| 239 |
score_val = 0
|
| 240 |
score_val = max(0, min(100, score_val))
|
| 241 |
+
|
| 242 |
return {
|
| 243 |
"userId": payload.userId,
|
| 244 |
"score": score_val,
|
| 245 |
"explanation": parsed.get("explanation")
|
| 246 |
}
|
| 247 |
+
|