Skip to content

Commit

Permalink
Add CLI for smolagents (#431)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Merve Noyan <merveenoyan@gmail.com>
  • Loading branch information
aymeric-roucher and merveenoyan authored Jan 30, 2025
1 parent 595781b commit fa1f8d0
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 77 deletions.
6 changes: 3 additions & 3 deletions docs/source/en/reference/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ These types have three specific purposes:

### AgentText

[[autodoc]] smolagents.types.AgentText
[[autodoc]] smolagents.agent_types.AgentText

### AgentImage

[[autodoc]] smolagents.types.AgentImage
[[autodoc]] smolagents.agent_types.AgentImage

### AgentAudio

[[autodoc]] smolagents.types.AgentAudio
[[autodoc]] smolagents.agent_types.AgentAudio
6 changes: 3 additions & 3 deletions docs/source/hi/reference/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ Smolagents एक experimental API है जो किसी भी समय

### AgentText

[[autodoc]] smolagents.types.AgentText
[[autodoc]] smolagents.agent_types.AgentText

### AgentImage

[[autodoc]] smolagents.types.AgentImage
[[autodoc]] smolagents.agent_types.AgentImage

### AgentAudio

[[autodoc]] smolagents.types.AgentAudio
[[autodoc]] smolagents.agent_types.AgentAudio
6 changes: 3 additions & 3 deletions docs/source/zh/reference/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ These types have three specific purposes:

### AgentText

[[autodoc]] smolagents.types.AgentText
[[autodoc]] smolagents.agent_types.AgentText

### AgentImage

[[autodoc]] smolagents.types.AgentImage
[[autodoc]] smolagents.agent_types.AgentImage

### AgentAudio

[[autodoc]] smolagents.types.AgentAudio
[[autodoc]] smolagents.agent_types.AgentAudio
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies = [
"pillow>=11.0.0",
"markdownify>=0.14.1",
"duckduckgo-search>=6.3.7",
"python-dotenv"
]

[project.optional-dependencies]
Expand Down Expand Up @@ -97,3 +98,7 @@ lint.select = ["E", "F", "I", "W"]
[tool.ruff.lint.isort]
known-first-party = ["smolagents"]
lines-after-imports = 2

[project.scripts]
smolagent = "smolagents.cli:main"
webagent = "smolagents.vlm_web_browser:main"
5 changes: 3 additions & 2 deletions src/smolagents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
# limitations under the License.
__version__ = "1.7.0.dev0"

from .agents import *
from .agent_types import * # noqa: I001
from .agents import * # Above noqa avoids a circular dependency due to cli.py
from .default_tools import *
from .e2b_executor import *
from .gradio_ui import *
Expand All @@ -26,5 +27,5 @@
from .monitoring import *
from .prompts import *
from .tools import *
from .types import *
from .utils import *
from .cli import *
File renamed without changes.
4 changes: 2 additions & 2 deletions src/smolagents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
from rich.rule import Rule
from rich.text import Text

from smolagents.agent_types import AgentAudio, AgentImage, handle_agent_output_types
from smolagents.memory import ActionStep, AgentMemory, PlanningStep, SystemPromptStep, TaskStep, ToolCall
from smolagents.monitoring import (
YELLOW_HEX,
AgentLogger,
LogLevel,
)
from smolagents.types import AgentAudio, AgentImage, handle_agent_output_types
from smolagents.utils import (
AgentError,
AgentExecutionError,
Expand All @@ -43,6 +43,7 @@
truncate_content,
)

from .agent_types import AgentType
from .default_tools import TOOL_MAPPING, FinalAnswerTool
from .e2b_executor import E2BExecutor
from .local_python_executor import (
Expand Down Expand Up @@ -73,7 +74,6 @@
Tool,
get_tool_description_with_args,
)
from .types import AgentType


logger = getLogger(__name__)
Expand Down
118 changes: 118 additions & 0 deletions src/smolagents/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python
# coding=utf-8

# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os

from dotenv import load_dotenv

from smolagents import CodeAgent, HfApiModel, LiteLLMModel, Model, OpenAIServerModel, Tool, TransformersModel
from smolagents.default_tools import TOOL_MAPPING


leopard_prompt = "How many seconds would it take for a leopard at full speed to run through Pont des Arts?"


def parse_arguments(description):
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"prompt",
type=str,
nargs="?", # Makes it optional
default=leopard_prompt,
help="The prompt to run with the agent",
)
parser.add_argument(
"--model-type",
type=str,
default="HfApiModel",
help="The model type to use (e.g., HfApiModel, OpenAIServerModel, LiteLLMModel, TransformersModel)",
)
parser.add_argument(
"--model-id",
type=str,
default="Qwen/Qwen2.5-Coder-32B-Instruct",
help="The model ID to use for the specified model type",
)
parser.add_argument(
"--imports",
nargs="*", # accepts zero or more arguments
default=[],
help="Space-separated list of imports to authorize (e.g., 'numpy pandas')",
)
parser.add_argument(
"--tools",
nargs="*",
default=["web_search"],
help="Space-separated list of tools that the agent can use (e.g., 'tool1 tool2 tool3')",
)
parser.add_argument(
"--verbosity-level",
type=int,
default=1,
help="The verbosity level, as an int in [0, 1, 2].",
)
return parser.parse_args()


def load_model(model_type: str, model_id: str) -> Model:
if model_type == "OpenAIServerModel":
return OpenAIServerModel(
api_key=os.getenv("FIREWORKS_API_KEY"),
api_base="https://api.fireworks.ai/inference/v1",
model_id=model_id,
)
elif model_type == "LiteLLMModel":
return LiteLLMModel(
model_id=model_id,
api_key=os.getenv("OPENAI_API_KEY"),
)
elif model_type == "TransformersModel":
return TransformersModel(model_id=model_id, device_map="auto", flatten_messages_as_text=False)
elif model_type == "HfApiModel":
return HfApiModel(
token=os.getenv("HF_API_KEY"),
model_id=model_id,
)
else:
raise ValueError(f"Unsupported model type: {model_type}")


def main():
load_dotenv()

args = parse_arguments(description="Run a CodeAgent with all specified parameters")

model = load_model(args.model_type, args.model_id)

available_tools = []
for tool_name in args.tools:
if "/" in tool_name:
available_tools.append(Tool.from_space(tool_name))
else:
if tool_name in TOOL_MAPPING:
available_tools.append(TOOL_MAPPING[tool_name]())
else:
raise ValueError(f"Tool {tool_name} is not recognized either as a default tool or a Space.")

print(f"Running agent with these tools: {args.tools}")
agent = CodeAgent(tools=available_tools, model=model, additional_authorized_imports=args.imports)

agent.run(args.prompt)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion src/smolagents/default_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
from dataclasses import dataclass
from typing import Any, Dict, Optional

from .agent_types import AgentAudio
from .local_python_executor import (
BASE_BUILTIN_MODULES,
BASE_PYTHON_TOOLS,
evaluate_python_code,
)
from .tools import PipelineTool, Tool
from .types import AgentAudio


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion src/smolagents/gradio_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import shutil
from typing import Optional

from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
from smolagents.agents import ActionStep, MultiStepAgent
from smolagents.memory import MemoryStep
from smolagents.types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
from smolagents.utils import _is_package_available


Expand Down
2 changes: 1 addition & 1 deletion src/smolagents/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
get_imports,
get_json_schema,
)
from .agent_types import handle_agent_input_types, handle_agent_output_types
from .tool_validation import MethodChecker, validate_tool_attributes
from .types import handle_agent_input_types, handle_agent_output_types
from .utils import _is_package_available, _is_pillow_available, get_source, instance_to_source


Expand Down
Loading

0 comments on commit fa1f8d0

Please sign in to comment.