SmolAlbert / agent.py
blackccpie
feat : added xperimental advanced search mode.
11c0a44
# 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>)"
),
#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()