Spaces:
Sleeping
Sleeping
Create agent.py
Browse filesStill under construction, must parametrize tool calling from mcp server
agent.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
from typing import Optional, Tuple
|
| 3 |
+
from smolagents import tool
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@tool
|
| 8 |
+
def real_number_calculator(
|
| 9 |
+
a: float, b: float, operation: Literal["add", "subtract", "multiply", "divide"]
|
| 10 |
+
) -> float:
|
| 11 |
+
"""
|
| 12 |
+
Perform basic arithmetic operations on two real numbers.
|
| 13 |
+
|
| 14 |
+
This function acts as a simple calculator that can add, subtract,
|
| 15 |
+
multiply, or divide two floating-point numbers.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
a (float): The first number.
|
| 19 |
+
b (float): The second number.
|
| 20 |
+
operation (Literal["add", "subtract", "multiply", "divide"]):
|
| 21 |
+
The arithmetic operation to perform.
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
float: The result of the arithmetic operation.
|
| 25 |
+
|
| 26 |
+
Raises:
|
| 27 |
+
ValueError: If the operation is invalid or if division by zero is attempted.
|
| 28 |
+
|
| 29 |
+
Example:
|
| 30 |
+
>>> real_number_calculator(10, 5, "add")
|
| 31 |
+
15.0
|
| 32 |
+
>>> real_number_calculator(10, 5, "divide")
|
| 33 |
+
2.0
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
if operation == "add":
|
| 37 |
+
return a + b
|
| 38 |
+
elif operation == "subtract":
|
| 39 |
+
return a - b
|
| 40 |
+
elif operation == "multiply":
|
| 41 |
+
return a * b
|
| 42 |
+
elif operation == "divide":
|
| 43 |
+
if b == 0:
|
| 44 |
+
raise ValueError("Division by zero is not allowed.")
|
| 45 |
+
return a / b
|
| 46 |
+
else:
|
| 47 |
+
raise ValueError(f"Invalid operation: {operation}")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class CourseAgent:
|
| 53 |
+
def __init__(self):
|
| 54 |
+
|
| 55 |
+
# import code agent and basic tool from smolagent
|
| 56 |
+
from smolagents import CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool, FinalAnswerTool, VisitWebpageTool
|
| 57 |
+
|
| 58 |
+
# import additional tool from langchain @ https://docs.langchain.com/oss/python/integrations/tools
|
| 59 |
+
from langchain.agents import load_tools
|
| 60 |
+
from smolagents import Tool
|
| 61 |
+
wikipedia_tool = Tool.from_langchain(load_tools(["wikipedia"])[0])
|
| 62 |
+
|
| 63 |
+
# import tools from MCP servers @ https://github.com/mcp
|
| 64 |
+
from mcp import StdioServerParameters
|
| 65 |
+
server_parameters = StdioServerParameters(command="uvx",
|
| 66 |
+
args=["--quiet", "pubmedmcp@0.1.3"],
|
| 67 |
+
env={"UV_PYTHON": "3.12", **os.environ},
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
model = OpenAIServerModel(model_id="gpt-4o")
|
| 71 |
+
model = InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 72 |
+
# Instantiate the agent
|
| 73 |
+
self.agent = CodeAgent(
|
| 74 |
+
tools=[real_number_calculator(), # homemade tool
|
| 75 |
+
DuckDuckGoSearchTool(), # basic tools from smolagent
|
| 76 |
+
VisitWebpageTool(),
|
| 77 |
+
wikipedia_tool(top_k_results=3), # tool from langchain with extra parmaeters
|
| 78 |
+
FinalAnswerTool()],
|
| 79 |
+
model=model,
|
| 80 |
+
max_steps=3,
|
| 81 |
+
verbosity_level=2
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
def __call__(self, question: str) -> str:
|
| 85 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 86 |
+
answer = self.agent.invoke(question)
|
| 87 |
+
print(f"Agent returning his answer: {answer}")
|
| 88 |
+
return fixed_answer
|