fix: update stock analyst agent logic to handle financial queries properly

- Refined `mock_multi_agent.py` to parse and route stock-specific inputs correctly
- Addressed issue where StockAnalyst returned fallback prompts instead of actionable responses
- Improved overall agent logic for financial data interpretation and reply generation
pull/819/head
Pavan Kumar 2 days ago committed by ascender1729
parent b9ddc14941
commit 610064b717

@ -40,14 +40,44 @@ class MathAgent:
"response": "I can analyze stock data using moving averages and calculate percentage changes. For example: 'calculate moving average of [10,20,30,40,50] over 3 periods'" "response": "I can analyze stock data using moving averages and calculate percentage changes. For example: 'calculate moving average of [10,20,30,40,50] over 3 periods'"
} }
# Check if input is math-related # Check if input is stock-related (for StockAnalyst)
math_keywords = ['add', 'plus', '+', 'multiply', 'times', '*', 'x', 'divide', '/', 'by'] if self.agent.agent_name == "StockAnalyst" and "moving average" in task.lower():
if not any(keyword in task.lower() for keyword in math_keywords): try:
return { import re
"agent": self.agent.agent_name, # Extract list of numbers and period
"task": task, numbers = re.findall(r'\[([\d,\s]+)\]', task)
"response": "Please provide a mathematical operation (add, multiply, or divide)" period = re.findall(r'over\s+(\d+)\s+periods', task)
}
if numbers and period:
numbers = [float(n) for n in numbers[0].split(',')]
period = int(period[0])
if len(numbers) >= period:
# Calculate moving average
averages = []
for i in range(len(numbers) - period + 1):
avg = sum(numbers[i:i+period]) / period
averages.append(round(avg, 2))
return {
"agent": self.agent.agent_name,
"task": task,
"response": f"Moving averages: {averages}"
}
except Exception as e:
return {
"agent": self.agent.agent_name,
"task": task,
"error": f"Error calculating moving average: {str(e)}"
}
# Check if input is math-related (for Calculator)
if self.agent.agent_name == "Calculator":
math_keywords = ['add', 'plus', '+', 'multiply', 'times', '*', 'x', 'divide', '/', 'by']
if not any(keyword in task.lower() for keyword in math_keywords):
return {
"agent": self.agent.agent_name,
"task": task,
"response": "Please provide a mathematical operation (add, multiply, or divide)"
}
response = await self.agent.arun(task) response = await self.agent.arun(task)
return { return {

Loading…
Cancel
Save