Abid Ali Awan commited on
Commit
92d263d
·
1 Parent(s): 3a614b5

Refine portfolio data extraction in financial agent: Enhance JSON response to include total investment field, improve markdown handling, and validate JSON structure for better accuracy. Update error messages in tools.py to include debug information for input received.

Browse files
Files changed (2) hide show
  1. agents/financial_agent.py +13 -5
  2. agents/tools.py +2 -1
agents/financial_agent.py CHANGED
@@ -193,7 +193,7 @@ Return only valid JSON, nothing else."""
193
 
194
  try:
195
  response = self.llm.invoke([
196
- SystemMessage(content="You are a portfolio data extraction assistant. Return only valid JSON with holdings array."),
197
  HumanMessage(content=extraction_prompt)
198
  ])
199
 
@@ -201,12 +201,20 @@ Return only valid JSON, nothing else."""
201
  extracted_data = response.content.strip()
202
  # Remove any markdown formatting
203
  if extracted_data.startswith("```"):
204
- extracted_data = extracted_data.split("\n")[1:-1]
205
- extracted_data = "\n".join(extracted_data)
 
 
 
206
 
207
  # Validate JSON
208
- json.loads(extracted_data)
209
- return extracted_data
 
 
 
 
 
210
 
211
  except Exception:
212
  pass
 
193
 
194
  try:
195
  response = self.llm.invoke([
196
+ SystemMessage(content="You are a portfolio data extraction assistant. Return only valid JSON with holdings array and total_investment field."),
197
  HumanMessage(content=extraction_prompt)
198
  ])
199
 
 
201
  extracted_data = response.content.strip()
202
  # Remove any markdown formatting
203
  if extracted_data.startswith("```"):
204
+ lines = extracted_data.split("\n")
205
+ # Find the start and end of JSON content
206
+ start_idx = 1 if lines[0].startswith("```") else 0
207
+ end_idx = -1 if lines[-1].startswith("```") or lines[-1] == "```" else len(lines)
208
+ extracted_data = "\n".join(lines[start_idx:end_idx])
209
 
210
  # Validate JSON
211
+ parsed_json = json.loads(extracted_data)
212
+ # Ensure it has the required structure
213
+ if isinstance(parsed_json, dict) and "holdings" in parsed_json:
214
+ return extracted_data
215
+ else:
216
+ # If structure is wrong, fall back to regex
217
+ pass
218
 
219
  except Exception:
220
  pass
agents/tools.py CHANGED
@@ -632,7 +632,8 @@ class FinancialTools:
632
 
633
  # If no valid holdings found, return early to avoid using this tool
634
  if not holdings_info:
635
- return "Portfolio analyzer requires specific holdings with percentages or shares. Please provide portfolio details like 'AAPL 40%, MSFT 30%' or JSON format."
 
636
 
637
  portfolio_data = []
638
  total_calculated_value = 0
 
632
 
633
  # If no valid holdings found, return early to avoid using this tool
634
  if not holdings_info:
635
+ # Debug: show what we received
636
+ return f"Portfolio analyzer debug - received input: {input_str[:200]}... No holdings found. Please provide portfolio details like 'AAPL 40%, MSFT 30%' or JSON format."
637
 
638
  portfolio_data = []
639
  total_calculated_value = 0