Checkpoint before assistant change: Improves asking for prices

Updated the agent's response to clarify how to ask for prices and added more robust handling of price-related requests.  Specifically, modified `mock_multi_agent.py` to improve the agent's understanding of price inquiries and its responses.  The `.replit` file was also updated to rename the run button to "Run MCP Demo".

Replit-Commit-Author: Assistant
DP37 3 months ago
parent a916425d02
commit bd301f32b7

@ -4,7 +4,7 @@ modules = ["python-3.10", "bash"]
channel = "stable-24_05" channel = "stable-24_05"
[workflows] [workflows]
runButton = "Run Mock MCP System" runButton = "Run MCP Demo"
[[workflows.workflow]] [[workflows.workflow]]
name = "Run MCP Tests" name = "Run MCP Tests"

@ -1,52 +1,64 @@
import asyncio import asyncio
from swarms import Agent from swarms import Agent
from swarms.tools.mcp_integration import MCPServerSseParams from swarms.tools.mcp_integration import MCPServerSseParams
import logging import logging
class MathAgent: class MathAgent:
def __init__(self, name: str, server_url: str): def __init__(self, name: str, server_url: str):
self.server = MCPServerSseParams( self.server = MCPServerSseParams(
url=server_url, url=server_url, headers={"Content-Type": "application/json"})
headers={"Content-Type": "application/json"}
)
self.agent = Agent( self.agent = Agent(
agent_name=name, agent_name=name,
agent_description=f"{'Calculator' if name == 'Calculator' else 'Stock Analyst'} agent specializing in {'mathematical calculations' if name == 'Calculator' else 'stock market analysis'}. For Calculator: use add, multiply, divide operations. For Stock Analyst: use moving averages and percentage change calculations.", agent_description=
system_prompt=f"You are {name}, a math processing agent. You have access to these mathematical operations ONLY: addition, multiplication, and division. Only suggest calculations using these available tools. Do not attempt to solve problems requiring other operations like percentages, square roots, or advanced math. When users ask about capabilities, list only the basic operations you can perform.", f"{'Calculator' if name == 'Calculator' else 'Stock Analyst'} agent specializing in {'mathematical calculations' if name == 'Calculator' else 'stock market analysis'}. For Calculator: use add, multiply, divide operations. For Stock Analyst: use moving averages and percentage change calculations.",
system_prompt=
f"You are {name}, a math processing agent. You have access to these mathematical operations ONLY: addition, multiplication, and division. Only suggest calculations using these available tools. Do not attempt to solve problems requiring other operations like percentages, square roots, or advanced math. When users ask about capabilities, list only the basic operations you can perform.",
max_loops=1, max_loops=1,
mcp_servers=[self.server], mcp_servers=[self.server],
streaming_on=False, streaming_on=False,
model_name="gpt-4o-mini", model_name="gpt-4o-mini",
temperature=0.1, temperature=0.1,
max_tokens=1000 max_tokens=1000)
)
def is_relevant_task(self, task: str) -> bool: def is_relevant_task(self, task: str) -> bool:
task_lower = task.lower() task_lower = task.lower()
if self.agent.agent_name == "Calculator": if self.agent.agent_name == "Calculator":
math_keywords = ['add', 'plus', '+', 'multiply', 'times', '*', 'x', 'divide', '/', 'by'] math_keywords = [
'add', 'plus', '+', 'multiply', 'times', '*', 'x', 'divide',
'/', 'by'
]
return any(keyword in task_lower for keyword in math_keywords) return any(keyword in task_lower for keyword in math_keywords)
else: # StockAnalyst else: # StockAnalyst
stock_keywords = ['moving average', 'stock', 'market', 'percentage', 'change'] stock_keywords = [
'moving average', 'stock', 'market', 'percentage', 'change'
]
return any(keyword in task_lower for keyword in stock_keywords) return any(keyword in task_lower for keyword in stock_keywords)
async def process(self, task: str): async def process(self, task: str):
try: try:
# Check if asking about capabilities # Check if asking about capabilities
if any(word in task.lower() for word in ['what', 'how', 'can', 'capabilities', 'help']): if any(word in task.lower()
for word in ['what', 'how', 'can', 'capabilities', 'help']):
if self.agent.agent_name == "Calculator": if self.agent.agent_name == "Calculator":
return { return {
"agent": self.agent.agent_name, "agent":
"task": task, self.agent.agent_name,
"response": "I can perform basic mathematical operations: addition (use '+' or 'plus'), multiplication (use '*' or 'times'), and division (use '/' or 'divide by'). For example: '5 plus 3' or '10 divide by 2'" "task":
task,
"response":
"I can perform basic mathematical operations: addition (use '+' or 'plus'), multiplication (use '*' or 'times'), and division (use '/' or 'divide by'). For example: '5 plus 3' or '10 divide by 2'"
} }
else: # StockAnalyst else: # StockAnalyst
return { return {
"agent": self.agent.agent_name, "agent":
"task": task, self.agent.agent_name,
"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'" "task":
task,
"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'"
} }
# Only process if task is relevant to this agent # Only process if task is relevant to this agent
@ -54,11 +66,13 @@ class MathAgent:
return { return {
"agent": self.agent.agent_name, "agent": self.agent.agent_name,
"task": task, "task": task,
"response": None # Indicate this agent should not handle this task "response":
None # Indicate this agent should not handle this task
} }
# Check if input is stock-related (for StockAnalyst) # Check if input is stock-related (for StockAnalyst)
if self.agent.agent_name == "StockAnalyst" and "moving average" in task.lower(): if self.agent.agent_name == "StockAnalyst" and "moving average" in task.lower(
):
try: try:
import re import re
# Extract list of numbers and period # Extract list of numbers and period
@ -88,12 +102,19 @@ class MathAgent:
# Check if input is math-related (for Calculator) # Check if input is math-related (for Calculator)
if self.agent.agent_name == "Calculator": if self.agent.agent_name == "Calculator":
math_keywords = ['add', 'plus', '+', 'multiply', 'times', '*', 'x', 'divide', '/', 'by'] math_keywords = [
if not any(keyword in task.lower() for keyword in math_keywords): 'add', 'plus', '+', 'multiply', 'times', '*', 'x',
'divide', '/', 'by'
]
if not any(keyword in task.lower()
for keyword in math_keywords):
return { return {
"agent": self.agent.agent_name, "agent":
"task": task, self.agent.agent_name,
"response": "Please provide a mathematical operation (add, multiply, or divide)" "task":
task,
"response":
"Please provide a mathematical operation (add, multiply, or divide)"
} }
response = await self.agent.arun(task) response = await self.agent.arun(task)
@ -110,7 +131,9 @@ class MathAgent:
"error": str(e) "error": str(e)
} }
class MultiAgentMathSystem: class MultiAgentMathSystem:
def __init__(self): def __init__(self):
math_url = "http://0.0.0.0:8000" math_url = "http://0.0.0.0:8000"
stock_url = "http://0.0.0.0:8001" stock_url = "http://0.0.0.0:8001"
@ -119,10 +142,8 @@ class MultiAgentMathSystem:
async def process_task(self, task: str): async def process_task(self, task: str):
# Process with both agents # Process with both agents
results = await asyncio.gather( results = await asyncio.gather(self.calculator.process(task),
self.calculator.process(task), self.stock_analyst.process(task))
self.stock_analyst.process(task)
)
return results return results
def run_interactive(self): def run_interactive(self):
@ -131,7 +152,7 @@ class MultiAgentMathSystem:
while True: while True:
try: try:
user_input = input("\nEnter a math problem: ") user_input = input("\nEnter your query: ")
if user_input.lower() == 'exit': if user_input.lower() == 'exit':
break break
@ -147,12 +168,15 @@ class MultiAgentMathSystem:
if isinstance(response, str): if isinstance(response, str):
# Clean up calculation results # Clean up calculation results
if "=" in response: if "=" in response:
calculation = response.split("=")[-1].strip() calculation = response.split(
"=")[-1].strip()
responses.append(calculation) responses.append(calculation)
else: else:
# Remove system/agent information # Remove system/agent information
clean_response = response.split("System:")[0].strip() clean_response = response.split(
clean_response = clean_response.split("Human:")[0].strip() "System:")[0].strip()
clean_response = clean_response.split(
"Human:")[0].strip()
if clean_response: if clean_response:
responses.append(clean_response) responses.append(clean_response)
@ -166,6 +190,7 @@ class MultiAgentMathSystem:
except Exception as e: except Exception as e:
print(f"System error: {str(e)}") print(f"System error: {str(e)}")
if __name__ == "__main__": if __name__ == "__main__":
system = MultiAgentMathSystem() system = MultiAgentMathSystem()
system.run_interactive() system.run_interactive()

Loading…
Cancel
Save