parent
2f1c779170
commit
b398753c72
@ -1,53 +1,78 @@
|
|||||||
|
import os
|
||||||
from swarms import Agent
|
from swarms import Agent
|
||||||
from swarms.tools.mcp_integration import MCPServerSseParams
|
from swarms.tools.mcp_integration import MCPServerSseParams
|
||||||
from swarms.prompts.agent_prompts import MATH_AGENT_PROMPT
|
from swarms.prompts.agent_prompts import MATH_AGENT_PROMPT
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
# Fallback in case the import fails
|
# Set OpenAI API key
|
||||||
if 'MATH_AGENT_PROMPT' not in locals():
|
|
||||||
MATH_AGENT_PROMPT = """You are a specialized math agent that can perform calculations by calling external math service APIs.
|
|
||||||
Key responsibilities:
|
|
||||||
1. Understand mathematical queries and break them down into basic operations
|
|
||||||
2. Use available math tools (add, multiply, divide) appropriately
|
|
||||||
3. Provide clear explanations of calculations
|
|
||||||
4. Handle errors gracefully if operations fail
|
|
||||||
|
|
||||||
Remember to use the available MCP tools for calculations rather than doing them directly."""
|
def initialize_math_system():
|
||||||
|
"""Initialize the math agent with MCP server configuration."""
|
||||||
|
# Configure MCP server connection with SSE transport
|
||||||
def main():
|
|
||||||
# Configure MCP server connection
|
|
||||||
math_server = MCPServerSseParams(
|
math_server = MCPServerSseParams(
|
||||||
url="http://0.0.0.0:8000",
|
url="http://localhost:8000",
|
||||||
headers={"Content-Type": "application/json"},
|
headers={
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Accept": "text/event-stream"
|
||||||
|
},
|
||||||
timeout=5.0,
|
timeout=5.0,
|
||||||
sse_read_timeout=30.0)
|
sse_read_timeout=30.0)
|
||||||
|
|
||||||
TOOL_CALL_INSTRUCTION = """When you want to use a math tool, reply with a JSON object only:
|
# Initialize math agent with specific model
|
||||||
{"tool_name": "<add|multiply|divide>", "a": <int>, "b": <int>}"""
|
|
||||||
|
|
||||||
# Initialize math agent
|
|
||||||
math_agent = Agent(
|
math_agent = Agent(
|
||||||
agent_name="Math Agent",
|
agent_name="Math Agent",
|
||||||
agent_description="Specialized agent for mathematical computations",
|
agent_description="Basic math calculator",
|
||||||
system_prompt=MATH_AGENT_PROMPT + "\n" + TOOL_CALL_INSTRUCTION,
|
system_prompt=MATH_AGENT_PROMPT,
|
||||||
max_loops=1,
|
max_loops=1,
|
||||||
mcp_servers=[math_server],
|
mcp_servers=[math_server],
|
||||||
streaming_on=False)
|
streaming_on=False,
|
||||||
|
model_name="gpt-4o-mini",
|
||||||
|
temperature=0.1)
|
||||||
|
|
||||||
print("\nMath Agent System Initialized")
|
return math_agent
|
||||||
print("\nAvailable operations:")
|
|
||||||
print("Math Agent: add, multiply, divide")
|
|
||||||
|
|
||||||
while True:
|
def process_query(math_agent, query):
|
||||||
query = input("\nEnter your query (or 'exit' to quit): ")
|
"""Process a single math query."""
|
||||||
|
try:
|
||||||
|
result = math_agent.run(query)
|
||||||
|
# Clean up the result to show only the number or error message
|
||||||
|
if isinstance(result, (int, float)):
|
||||||
|
return result
|
||||||
|
elif "error" in result.lower():
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
# Try to extract just the number from the result
|
||||||
|
try:
|
||||||
|
return float(result)
|
||||||
|
except:
|
||||||
|
return "Error: Invalid result format"
|
||||||
|
except Exception as e:
|
||||||
|
return f"Error: {str(e)}"
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# Initialize the math system
|
||||||
|
math_agent = initialize_math_system()
|
||||||
|
print("\nMath Calculator Ready!")
|
||||||
|
print("Available operations: add, multiply, divide")
|
||||||
|
print("Example: 'add 5 and 3' or 'multiply 4 by 6'")
|
||||||
|
print("Type 'exit' to quit\n")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
query = input("Enter math operation: ").strip()
|
||||||
|
if not query:
|
||||||
|
continue
|
||||||
if query.lower() == 'exit':
|
if query.lower() == 'exit':
|
||||||
break
|
break
|
||||||
|
|
||||||
# Process with math agent
|
result = process_query(math_agent, query)
|
||||||
math_result = math_agent.run(query)
|
print(f"Result: {result}")
|
||||||
print("\nMath Agent Response:", math_result)
|
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\nExiting...")
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in new issue