File size: 3,386 Bytes
8c162e4
 
 
 
 
 
 
 
 
 
3f7ddfb
8c162e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a95e66e
8c162e4
 
 
a95e66e
8c162e4
 
 
 
 
a95e66e
8c162e4
 
 
 
a95e66e
8c162e4
 
a95e66e
 
8c162e4
a95e66e
8c162e4
 
 
 
 
a95e66e
 
8c162e4
 
 
 
 
 
 
 
 
 
a95e66e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import math
from typing import Optional, Tuple
from smolagents import tool



@tool
def real_number_calculator(
    a: float, b: float, operation: Literal["add", "subtract", "multiply", "divide"]
) -> float:
    """
    Perform basic arithmetic operations on two real numbers.

    This function acts as a simple calculator that can add, subtract, 
    multiply, or divide two floating-point numbers.

    Args:
        a (float): The first number.
        b (float): The second number.
        operation (Literal["add", "subtract", "multiply", "divide"]): 
            The arithmetic operation to perform.

    Returns:
        float: The result of the arithmetic operation.

    Raises:
        ValueError: If the operation is invalid or if division by zero is attempted.

    Example:
        >>> real_number_calculator(10, 5, "add")
        15.0
        >>> real_number_calculator(10, 5, "divide")
        2.0
    """

    if operation == "add":
        return a + b
    elif operation == "subtract":
        return a - b
    elif operation == "multiply":
        return a * b
    elif operation == "divide":
        if b == 0:
            raise ValueError("Division by zero is not allowed.")
        return a / b
    else:
        raise ValueError(f"Invalid operation: {operation}")




    class TestAgent:
        def __init__(self):
            
            # import code agent and basic tool from smolagent
            from smolagents import CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool, FinalAnswerTool, VisitWebpageTool, MCPServerTool

            # import additional tool from langchain @ https://docs.langchain.com/oss/python/integrations/tools
            from langchain.agents import load_tools
            from smolagents import Tool
            wikipedia_tool = Tool.from_langchain(load_tools(["wikipedia"])[0])
            wikipedia_tool.top_k_results=3

            # import tools from MCP servers @ https://github.com/mcp
            from mcp import StdioServerParameters
            server_parameters = StdioServerParameters(command="uvx",
                                                      args=["--quiet", "youtubeqa@0.2.1"],
                                                      env={"UV_PYTHON": "3.12", **os.environ},
                                                     )
            youtube_tools = MCPServerTool(server_params=server_parameters)

            model = OpenAIServerModel(model_id="gpt-4o")
            #model = InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct")
            # Instantiate the agent
            self.agent = CodeAgent(
                tools=[real_number_calculator(),        # homemade tool
                       DuckDuckGoSearchTool(),          # basic tools from smolagent
                       VisitWebpageTool(),
                       wikipedia_tool,                  # tool from langchain with extra parmaeters
                       youtube_tools,                   # tool from MCP server
                       FinalAnswerTool()],
                model=model,
                max_steps=3,
                verbosity_level=2
            )

        def __call__(self, question: str) -> str:
            print(f"Agent received question (first 50 chars): {question[:50]}...")
            answer = self.agent.invoke(question)
            print(f"Agent returning his answer: {answer}")
            return answer