Spaces:
Sleeping
Sleeping
Update llm_utils.py
Browse files- llm_utils.py +18 -20
llm_utils.py
CHANGED
|
@@ -1,38 +1,36 @@
|
|
| 1 |
-
# llm_utils.py
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
-
|
| 6 |
-
#
|
|
|
|
| 7 |
API_KEY = os.getenv("OPENAI_API_KEY", "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")
|
| 8 |
|
| 9 |
-
|
| 10 |
client = OpenAI(api_key=API_KEY)
|
| 11 |
|
| 12 |
|
| 13 |
def explain_savings_plan(payload: dict) -> str:
|
| 14 |
"""
|
| 15 |
-
Uses a small LLM model to turn numeric savings plan
|
| 16 |
short, plain-English explanation.
|
| 17 |
|
| 18 |
-
|
| 19 |
-
- MUST NOT invent new numbers
|
| 20 |
-
- Only
|
| 21 |
-
-
|
| 22 |
"""
|
| 23 |
|
| 24 |
system_prompt = """
|
| 25 |
-
You are an AI assistant that explains
|
| 26 |
-
You MUST NOT invent any
|
| 27 |
Use ONLY the fields provided in the JSON payload.
|
| 28 |
Write 2–3 short sentences that:
|
| 29 |
1) State what the user is trying to achieve (reach a down payment).
|
| 30 |
2) Explain why this recommended monthly savings amount makes sense.
|
| 31 |
3) Mention the timeline (years and months).
|
| 32 |
-
Be neutral,
|
| 33 |
""".strip()
|
| 34 |
|
| 35 |
-
# We send the raw payload as JSON text in the user message.
|
| 36 |
user_content = f"Here is the JSON for this user's plan: {payload}"
|
| 37 |
|
| 38 |
try:
|
|
@@ -42,16 +40,16 @@ Be neutral, clear, and non-promotional.
|
|
| 42 |
{"role": "system", "content": system_prompt},
|
| 43 |
{"role": "user", "content": user_content},
|
| 44 |
],
|
| 45 |
-
max_tokens=
|
| 46 |
temperature=0.3,
|
| 47 |
)
|
| 48 |
explanation = completion.choices[0].message.content.strip()
|
| 49 |
return explanation
|
| 50 |
-
except Exception
|
| 51 |
-
#
|
| 52 |
return (
|
| 53 |
-
"We calculated your recommended monthly savings
|
| 54 |
-
"target down payment
|
| 55 |
-
"The amount
|
| 56 |
-
"
|
| 57 |
)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
|
| 4 |
+
# You can either:
|
| 5 |
+
# 1) Set OPENAI_API_KEY in Hugging Face Secrets
|
| 6 |
+
# 2) Or replace "YOUR_OPENAI_API_KEY" with your key directly (less secure)
|
| 7 |
API_KEY = os.getenv("OPENAI_API_KEY", "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")
|
| 8 |
|
|
|
|
| 9 |
client = OpenAI(api_key=API_KEY)
|
| 10 |
|
| 11 |
|
| 12 |
def explain_savings_plan(payload: dict) -> str:
|
| 13 |
"""
|
| 14 |
+
Uses a small LLM model to turn the numeric savings plan into a
|
| 15 |
short, plain-English explanation.
|
| 16 |
|
| 17 |
+
Guardrails:
|
| 18 |
+
- MUST NOT invent any new numbers
|
| 19 |
+
- Only uses fields in payload
|
| 20 |
+
- Explanation only; no decision-making
|
| 21 |
"""
|
| 22 |
|
| 23 |
system_prompt = """
|
| 24 |
+
You are an AI assistant that explains home down-payment savings plans for first-time buyers.
|
| 25 |
+
You MUST NOT invent or change any numeric values.
|
| 26 |
Use ONLY the fields provided in the JSON payload.
|
| 27 |
Write 2–3 short sentences that:
|
| 28 |
1) State what the user is trying to achieve (reach a down payment).
|
| 29 |
2) Explain why this recommended monthly savings amount makes sense.
|
| 30 |
3) Mention the timeline (years and months).
|
| 31 |
+
Be neutral, concise, and non-promotional.
|
| 32 |
""".strip()
|
| 33 |
|
|
|
|
| 34 |
user_content = f"Here is the JSON for this user's plan: {payload}"
|
| 35 |
|
| 36 |
try:
|
|
|
|
| 40 |
{"role": "system", "content": system_prompt},
|
| 41 |
{"role": "user", "content": user_content},
|
| 42 |
],
|
| 43 |
+
max_tokens=220,
|
| 44 |
temperature=0.3,
|
| 45 |
)
|
| 46 |
explanation = completion.choices[0].message.content.strip()
|
| 47 |
return explanation
|
| 48 |
+
except Exception:
|
| 49 |
+
# Safe fallback explanation if LLM is unavailable
|
| 50 |
return (
|
| 51 |
+
"We calculated your recommended monthly savings using your home budget, "
|
| 52 |
+
"target down payment percentage, current savings, and chosen timeline. "
|
| 53 |
+
"The remaining amount needed is spread over the available months, "
|
| 54 |
+
"with estimated closing costs added and expected interest on savings subtracted."
|
| 55 |
)
|