# 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 search_tool = TavilySearchTool() image_search_tool = TavilyImageURLSearchTool() extract_tool = TavilyExtractTool() image_query_tool = ImageQueryTool() model = InferenceClientModel( model_id=self.model_id, provider=self.provider, token=os.getenv("HF_API_KEY")) self.agent = CodeAgent( tools=[search_tool, image_search_tool, extract_tool, 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]()" ) ) 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()