Spaces:
Sleeping
Sleeping
Update src/llm_utils.py
Browse files- src/llm_utils.py +41 -25
src/llm_utils.py
CHANGED
|
@@ -1,32 +1,39 @@
|
|
| 1 |
-
# src/llm_utils.py
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
-
# You can either:
|
| 6 |
-
# - Set OPENAI_API_KEY as a secret in Hugging Face, OR
|
| 7 |
-
# - Replace "YOUR_OPENAI_API_KEY" directly (less secure)
|
| 8 |
API_KEY = os.getenv("OPENAI_API_KEY", "YOUR_OPENAI_API_KEY")
|
| 9 |
-
|
| 10 |
client = OpenAI(api_key=API_KEY)
|
| 11 |
|
| 12 |
|
| 13 |
def explain_savings_plan(payload: dict) -> str:
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
Guardrails:
|
| 17 |
-
-
|
| 18 |
-
-
|
| 19 |
-
- Explanation
|
| 20 |
"""
|
|
|
|
| 21 |
system_prompt = """
|
| 22 |
-
You are an AI
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
""".strip()
|
| 31 |
|
| 32 |
user_content = f"Here is the JSON for this user's plan: {payload}"
|
|
@@ -38,16 +45,25 @@ Be neutral, concise, and non-promotional.
|
|
| 38 |
{"role": "system", "content": system_prompt},
|
| 39 |
{"role": "user", "content": user_content},
|
| 40 |
],
|
| 41 |
-
max_tokens=
|
| 42 |
-
temperature=0.
|
| 43 |
)
|
| 44 |
explanation = completion.choices[0].message.content.strip()
|
| 45 |
return explanation
|
|
|
|
| 46 |
except Exception:
|
| 47 |
-
# Safe fallback if LLM fails
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return (
|
| 49 |
-
"We calculated your
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
API_KEY = os.getenv("OPENAI_API_KEY", "YOUR_OPENAI_API_KEY")
|
|
|
|
| 5 |
client = OpenAI(api_key=API_KEY)
|
| 6 |
|
| 7 |
|
| 8 |
def explain_savings_plan(payload: dict) -> str:
|
| 9 |
"""
|
| 10 |
+
Uses a small LLM model to turn the numeric savings plan into a
|
| 11 |
+
short, plain-English explanation.
|
| 12 |
+
|
| 13 |
Guardrails:
|
| 14 |
+
- MUST NOT invent or change any numbers
|
| 15 |
+
- Uses ONLY the fields in `payload`
|
| 16 |
+
- Explanation only; it does not make credit decisions
|
| 17 |
"""
|
| 18 |
+
|
| 19 |
system_prompt = """
|
| 20 |
+
You are an AI coach explaining a down-payment savings plan for a first-time homebuyer.
|
| 21 |
+
|
| 22 |
+
Rules:
|
| 23 |
+
- Do NOT invent or change any numeric values.
|
| 24 |
+
- Use ONLY the numbers provided in the JSON.
|
| 25 |
+
- Be clear, friendly, and non-promotional.
|
| 26 |
+
- Write 3–4 short sentences, no bullet points.
|
| 27 |
+
|
| 28 |
+
Content to cover:
|
| 29 |
+
1) Start with the goal: buying a home at the given home_budget with the given down_payment_percent.
|
| 30 |
+
2) Explain how much they already have (current_savings) and how much more they need (remaining_need).
|
| 31 |
+
3) Explain the recommended_monthly_savings over timeline_years / timeline_months.
|
| 32 |
+
4) If savings_to_income_ratio is provided:
|
| 33 |
+
- Briefly say whether this monthly amount is a light, moderate, or heavy lift
|
| 34 |
+
(around 0.10 = light, 0.20 = moderate, 0.30+ = heavy), BUT use only that ratio,
|
| 35 |
+
do not guess at after-tax income.
|
| 36 |
+
5) Encourage them to adjust the timeline or budget sliders if the amount feels too high.
|
| 37 |
""".strip()
|
| 38 |
|
| 39 |
user_content = f"Here is the JSON for this user's plan: {payload}"
|
|
|
|
| 45 |
{"role": "system", "content": system_prompt},
|
| 46 |
{"role": "user", "content": user_content},
|
| 47 |
],
|
| 48 |
+
max_tokens=230,
|
| 49 |
+
temperature=0.35,
|
| 50 |
)
|
| 51 |
explanation = completion.choices[0].message.content.strip()
|
| 52 |
return explanation
|
| 53 |
+
|
| 54 |
except Exception:
|
| 55 |
+
# Safe fallback if the LLM call fails
|
| 56 |
+
hb = payload.get("home_budget")
|
| 57 |
+
dp_pct = payload.get("down_payment_percent")
|
| 58 |
+
dp_amt = payload.get("down_payment_amount")
|
| 59 |
+
rem = payload.get("remaining_need")
|
| 60 |
+
m = payload.get("timeline_months")
|
| 61 |
+
rec = payload.get("recommended_monthly_savings")
|
| 62 |
+
|
| 63 |
return (
|
| 64 |
+
"We calculated your plan using your home budget, down-payment percentage, "
|
| 65 |
+
f"and current savings. For example, to buy a home around ${hb:,} with a "
|
| 66 |
+
f"{dp_pct}% down payment (about ${dp_amt:,}), you still need roughly "
|
| 67 |
+
f"${rem:,}. Spreading that over about {m} months leads to a recommended "
|
| 68 |
+
f"savings of about ${rec:,} per month, including closing cost and interest assumptions."
|
| 69 |
+
)
|