Spaces:
Sleeping
Sleeping
Update llm_utils.py
Browse files- llm_utils.py +11 -13
llm_utils.py
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
|
| 4 |
# You can either:
|
| 5 |
-
#
|
| 6 |
-
#
|
| 7 |
-
API_KEY = os.getenv("OPENAI_API_KEY", "
|
| 8 |
|
| 9 |
client = OpenAI(api_key=API_KEY)
|
| 10 |
|
| 11 |
|
| 12 |
def explain_savings_plan(payload: dict) -> str:
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
-
short, plain-English explanation.
|
| 16 |
-
|
| 17 |
Guardrails:
|
| 18 |
-
-
|
| 19 |
-
-
|
| 20 |
-
- Explanation
|
| 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.
|
|
@@ -46,10 +44,10 @@ Be neutral, concise, and non-promotional.
|
|
| 46 |
explanation = completion.choices[0].message.content.strip()
|
| 47 |
return explanation
|
| 48 |
except Exception:
|
| 49 |
-
# Safe fallback
|
| 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
|
| 54 |
-
"
|
| 55 |
)
|
|
|
|
| 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 |
+
Calls a small LLM model to explain the numeric savings plan.
|
|
|
|
|
|
|
| 16 |
Guardrails:
|
| 17 |
+
- Do NOT invent new numbers
|
| 18 |
+
- Use ONLY fields from payload
|
| 19 |
+
- Explanation-only; no decisions
|
| 20 |
"""
|
|
|
|
| 21 |
system_prompt = """
|
| 22 |
You are an AI assistant that explains home down-payment savings plans for first-time buyers.
|
| 23 |
You MUST NOT invent or change any numeric values.
|
|
|
|
| 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 recommended monthly savings using your home budget, "
|
| 50 |
"target down payment percentage, current savings, and chosen timeline. "
|
| 51 |
+
"The remaining amount is spread over the available months, with estimated "
|
| 52 |
+
"closing costs added and expected interest on savings subtracted."
|
| 53 |
)
|