Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,10 @@
|
|
| 2 |
"""
|
| 3 |
Financial Health Score Service (FastAPI)
|
| 4 |
|
| 5 |
-
|
| 6 |
-
- Uses GPT-4o-mini with strict JSON schema
|
| 7 |
-
- Correct
|
| 8 |
-
- Fully valid `.
|
| 9 |
- Bulletproof Mongo fetch + normalization
|
| 10 |
"""
|
| 11 |
|
|
@@ -43,6 +43,7 @@ budget_collection = default_db["budget"]
|
|
| 43 |
transaction_collection = default_db["transactions"]
|
| 44 |
currencies_collection = default_db["currencies"]
|
| 45 |
|
|
|
|
| 46 |
openai = OpenAI(api_key=OPENAI_API_KEY)
|
| 47 |
|
| 48 |
app = FastAPI(title="Financial Health Score Service")
|
|
@@ -181,7 +182,7 @@ def financial_score(payload: ScoreRequest):
|
|
| 181 |
)
|
| 182 |
transactions = normalize_transactions(txns_raw)
|
| 183 |
|
| 184 |
-
# If no data
|
| 185 |
if not budgets and not transactions:
|
| 186 |
return {
|
| 187 |
"userId": payload.userId,
|
|
@@ -192,10 +193,10 @@ def financial_score(payload: ScoreRequest):
|
|
| 192 |
prompt = build_prompt(budgets, transactions)
|
| 193 |
|
| 194 |
# ===========================
|
| 195 |
-
# STRICT GPT-4o-mini JSON
|
| 196 |
# ===========================
|
| 197 |
try:
|
| 198 |
-
response = openai.
|
| 199 |
model="gpt-4o-mini",
|
| 200 |
temperature=0.6,
|
| 201 |
response_format={
|
|
@@ -221,13 +222,13 @@ def financial_score(payload: ScoreRequest):
|
|
| 221 |
except Exception as exc:
|
| 222 |
raise HTTPException(status_code=502, detail=f"OpenAI request failed: {exc}")
|
| 223 |
|
| 224 |
-
#
|
| 225 |
try:
|
| 226 |
-
parsed = response.
|
| 227 |
-
except Exception:
|
| 228 |
raise HTTPException(
|
| 229 |
status_code=502,
|
| 230 |
-
detail="
|
| 231 |
)
|
| 232 |
|
| 233 |
# Score normalization
|
|
@@ -236,6 +237,7 @@ def financial_score(payload: ScoreRequest):
|
|
| 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 {
|
|
|
|
| 2 |
"""
|
| 3 |
Financial Health Score Service (FastAPI)
|
| 4 |
|
| 5 |
+
Fully updated version:
|
| 6 |
+
- Uses GPT-4o-mini with strict JSON schema output
|
| 7 |
+
- Correct OpenAI Chat Completions API
|
| 8 |
+
- Fully valid `.choices[0].message.parsed`
|
| 9 |
- Bulletproof Mongo fetch + normalization
|
| 10 |
"""
|
| 11 |
|
|
|
|
| 43 |
transaction_collection = default_db["transactions"]
|
| 44 |
currencies_collection = default_db["currencies"]
|
| 45 |
|
| 46 |
+
# OpenAI Client
|
| 47 |
openai = OpenAI(api_key=OPENAI_API_KEY)
|
| 48 |
|
| 49 |
app = FastAPI(title="Financial Health Score Service")
|
|
|
|
| 182 |
)
|
| 183 |
transactions = normalize_transactions(txns_raw)
|
| 184 |
|
| 185 |
+
# If no data
|
| 186 |
if not budgets and not transactions:
|
| 187 |
return {
|
| 188 |
"userId": payload.userId,
|
|
|
|
| 193 |
prompt = build_prompt(budgets, transactions)
|
| 194 |
|
| 195 |
# ===========================
|
| 196 |
+
# STRICT GPT-4o-mini JSON SCHEMA
|
| 197 |
# ===========================
|
| 198 |
try:
|
| 199 |
+
response = openai.chat.completions.create(
|
| 200 |
model="gpt-4o-mini",
|
| 201 |
temperature=0.6,
|
| 202 |
response_format={
|
|
|
|
| 222 |
except Exception as exc:
|
| 223 |
raise HTTPException(status_code=502, detail=f"OpenAI request failed: {exc}")
|
| 224 |
|
| 225 |
+
# SAFE PARSE JSON
|
| 226 |
try:
|
| 227 |
+
parsed = response.choices[0].message.parsed
|
| 228 |
+
except Exception as e:
|
| 229 |
raise HTTPException(
|
| 230 |
status_code=502,
|
| 231 |
+
detail=f"Could not parse JSON output: {e}"
|
| 232 |
)
|
| 233 |
|
| 234 |
# Score normalization
|
|
|
|
| 237 |
score_val = int(float(score_val))
|
| 238 |
except:
|
| 239 |
score_val = 0
|
| 240 |
+
|
| 241 |
score_val = max(0, min(100, score_val))
|
| 242 |
|
| 243 |
return {
|