Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,10 @@
|
|
| 3 |
Financial Health Score Service (FastAPI)
|
| 4 |
|
| 5 |
Rewrite includes:
|
| 6 |
-
- Uses GPT-
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
- No messy regex parsing
|
| 11 |
"""
|
| 12 |
|
| 13 |
import json
|
|
@@ -193,36 +192,36 @@ def financial_score(payload: ScoreRequest):
|
|
| 193 |
prompt = build_prompt(budgets, transactions)
|
| 194 |
|
| 195 |
# ===========================
|
| 196 |
-
# STRICT GPT-
|
| 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 |
-
#
|
| 226 |
try:
|
| 227 |
parsed = response.output[0].content[0].parsed
|
| 228 |
except Exception:
|
|
@@ -230,18 +229,17 @@ def financial_score(payload: ScoreRequest):
|
|
| 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:
|
| 237 |
score_val = int(float(score_val))
|
| 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 |
-
|
|
|
|
| 3 |
Financial Health Score Service (FastAPI)
|
| 4 |
|
| 5 |
Rewrite includes:
|
| 6 |
+
- Uses GPT-4o-mini with strict JSON schema (response_format)
|
| 7 |
+
- Correct new OpenAI API (`openai.responses.create`)
|
| 8 |
+
- Fully valid `.output[0].content[0].parsed`
|
| 9 |
+
- Bulletproof Mongo fetch + normalization
|
|
|
|
| 10 |
"""
|
| 11 |
|
| 12 |
import json
|
|
|
|
| 192 |
prompt = build_prompt(budgets, transactions)
|
| 193 |
|
| 194 |
# ===========================
|
| 195 |
+
# STRICT GPT-4o-mini JSON
|
| 196 |
# ===========================
|
| 197 |
try:
|
| 198 |
+
response = openai.responses.create(
|
| 199 |
+
model="gpt-4o-mini",
|
| 200 |
+
temperature=0.6,
|
| 201 |
+
response_format={
|
| 202 |
+
"type": "json_schema",
|
| 203 |
+
"json_schema": {
|
| 204 |
+
"name": "financial_score",
|
| 205 |
+
"schema": {
|
| 206 |
+
"type": "object",
|
| 207 |
+
"properties": {
|
| 208 |
+
"score": {"type": "number"},
|
| 209 |
+
"explanation": {"type": "string"}
|
| 210 |
+
},
|
| 211 |
+
"required": ["score", "explanation"],
|
| 212 |
+
"additionalProperties": False
|
| 213 |
+
}
|
| 214 |
}
|
| 215 |
+
},
|
| 216 |
+
messages=[
|
| 217 |
+
{"role": "system", "content": "You are a financial scoring engine."},
|
| 218 |
+
{"role": "user", "content": prompt}
|
| 219 |
+
]
|
| 220 |
+
)
|
|
|
|
| 221 |
except Exception as exc:
|
| 222 |
raise HTTPException(status_code=502, detail=f"OpenAI request failed: {exc}")
|
| 223 |
+
|
| 224 |
+
# GUARANTEED VALID JSON
|
| 225 |
try:
|
| 226 |
parsed = response.output[0].content[0].parsed
|
| 227 |
except Exception:
|
|
|
|
| 229 |
status_code=502,
|
| 230 |
detail="OpenAI response did not include parsed JSON (unexpected)"
|
| 231 |
)
|
| 232 |
+
|
| 233 |
+
# Score normalization
|
| 234 |
score_val = parsed.get("score", 0)
|
| 235 |
try:
|
| 236 |
score_val = int(float(score_val))
|
| 237 |
except:
|
| 238 |
score_val = 0
|
| 239 |
score_val = max(0, min(100, score_val))
|
| 240 |
+
|
| 241 |
return {
|
| 242 |
"userId": payload.userId,
|
| 243 |
"score": score_val,
|
| 244 |
"explanation": parsed.get("explanation")
|
| 245 |
}
|
|
|