File size: 4,072 Bytes
7224e82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b51b727
7224e82
b51b727
16f8a25
7224e82
 
 
 
 
 
 
 
 
b51b727
7224e82
 
 
 
 
 
 
11c0a44
 
 
 
a4b6466
 
 
 
7224e82
11c0a44
7224e82
 
 
 
 
 
 
 
11c0a44
62c395a
11c0a44
7224e82
 
11c0a44
 
 
 
 
 
 
 
 
 
 
7224e82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16f8a25
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
# The MIT License

# Copyright (c) 2025 Albert Murienne

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os

from smolagents import CodeAgent, InferenceClientModel

from other_tools import ImageQueryTool
from web_tools import TavilyBaseClient, TavilySearchTool, TavilyExtractTool, TavilyImageURLSearchTool

class SmolAlbert(CodeAgent):
    """
    A specialized CodeAgent that uses Tavily tools and a specific model.
    """

    #model_id = "Qwen/Qwen3-Coder-30B-A3B-Instruct"
    #model_id = "Qwen/Qwen3-30B-A3B-Thinking-2507"
    model_id = "Qwen/Qwen3-235B-A22B-Instruct-2507"
    #model_id = "google/gemma-3-27b-it"
    provider = "auto"

    def __init__(self):
        """
        Initialize the SmolAlbert agent with Tavily tools and a model.
        """
        # Set up the agent with the Tavily tool and a model
        self.search_tool = TavilySearchTool()
        self.image_search_tool = TavilyImageURLSearchTool()
        self.extract_tool = TavilyExtractTool()
        self.image_query_tool = ImageQueryTool()
        model = InferenceClientModel(
            model_id=self.model_id,
            provider=self.provider,
            token=os.getenv("HF_API_KEY"))
        self.agent = CodeAgent(
            tools=[self.search_tool, self.image_search_tool, self.extract_tool, self.image_query_tool], 
            model=model,
            stream_outputs=True,
            instructions=(
                "When writing the final answer, including the most relevant URL(s) "
                "from your search results as inline Markdown hyperlinks is MANDATORY. "
                "Example format: ... (see [1](https://example1.com)) ... (see [2](https://example2.com)) ... "
                "Do not invent URL(s) — only use the ones you were provided."
                "If the answer includes an image URL, include it as an inline Markdown image: ![image](<image_url>)"
            ),
            planning_interval=5,  # plan every 5 steps
            #additional_authorized_imports=[""]
        )

        self.advanced_mode = False

    def enable_advanced_mode(self, enable: bool):
        """
        Enable or disable advanced mode for the search tool.
        Advanced mode uses more credits but yields better results.
        """
        self.advanced_mode = enable
        self.search_tool.enable_advanced_mode(enable)
        self.extract_tool.enable_advanced_mode(enable)

    def run(self, task: str, additional_args: dict | None = None) -> str:
        """
        Run the agent with a given query and return the final answer.
        """
        return self.agent.run(
            task=task,
            stream=True,
            reset=False,
            max_steps=5,
            additional_args=additional_args
        )
        
    def reset(self):
        """
        Reset the agent's internal state.
        """
        self.agent.memory.reset()

    @staticmethod
    def get_search_credits() -> str:
        """
        Get the current search credits of the Tavily API.
        """
        return TavilyBaseClient.get_usage()