Navya-Sree commited on
Commit
3d841fd
·
verified ·
1 Parent(s): e14e905

Update llm_utils.py

Browse files
Files changed (1) hide show
  1. 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
- # 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.
@@ -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 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
  )
 
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
  )