MJ-Prod commited on
Commit
e8bd2a5
·
1 Parent(s): f0d57e4

JWT-update_4

Browse files
Files changed (2) hide show
  1. app.py +1 -0
  2. fiscal.py +9 -6
app.py CHANGED
@@ -79,6 +79,7 @@ def chat(
79
  # Use token from request if provided, fallback to sandbox
80
  access_token = request.access_token if request.access_token else SANDBOX_ACCESS_TOKEN
81
  financial_context = get_financial_snapshot(access_token)
 
82
 
83
  answer = get_answer(
84
  message=request.message,
 
79
  # Use token from request if provided, fallback to sandbox
80
  access_token = request.access_token if request.access_token else SANDBOX_ACCESS_TOKEN
81
  financial_context = get_financial_snapshot(access_token)
82
+ print(f"Financial context preview: {financial_context[:100]}")
83
 
84
  answer = get_answer(
85
  message=request.message,
fiscal.py CHANGED
@@ -142,9 +142,16 @@ def clear_memory(user_id: str) -> None:
142
 
143
  def stream_answer(message: str, user_id: str, financial_context: str = "") -> Generator:
144
  """Stream answer token by token."""
 
 
 
 
 
 
 
145
  filled_template = QA_TEMPLATE.replace(
146
  "{financial_context}",
147
- financial_context or "No bank data available."
148
  )
149
 
150
  prompt = PromptTemplate(
@@ -152,14 +159,10 @@ def stream_answer(message: str, user_id: str, financial_context: str = "") -> Ge
152
  input_variables=["context", "question"],
153
  )
154
 
155
- # Get relevant docs manually for streaming
156
- docs = retriever.get_relevant_documents(message)
157
  context = "\n\n".join([doc.page_content for doc in docs])
158
-
159
- # Build the full prompt
160
  filled_prompt = prompt.format(context=context, question=message)
161
 
162
- # Stream directly from the LLM
163
  for chunk in llm.stream(filled_prompt):
164
  if hasattr(chunk, 'content'):
165
  yield chunk.content
 
142
 
143
  def stream_answer(message: str, user_id: str, financial_context: str = "") -> Generator:
144
  """Stream answer token by token."""
145
+
146
+ # Same error detection as get_answer
147
+ if not financial_context or financial_context == "NO_BANK_DATA" or "Unable to fetch" in financial_context or "Bad Request" in financial_context or "access token" in financial_context.lower():
148
+ context_block = "The user has not connected their bank account yet. If relevant, gently mention they can connect their bank account in the Options menu to get personalized advice based on their real transactions and balances."
149
+ else:
150
+ context_block = f"The user has connected their bank account. Here is their live financial data:\n{financial_context}"
151
+
152
  filled_template = QA_TEMPLATE.replace(
153
  "{financial_context}",
154
+ context_block
155
  )
156
 
157
  prompt = PromptTemplate(
 
159
  input_variables=["context", "question"],
160
  )
161
 
162
+ docs = retriever.invoke(message)
 
163
  context = "\n\n".join([doc.page_content for doc in docs])
 
 
164
  filled_prompt = prompt.format(context=context, question=message)
165
 
 
166
  for chunk in llm.stream(filled_prompt):
167
  if hasattr(chunk, 'content'):
168
  yield chunk.content