smolagents
Hugging Face's smolagents, through OpenAIModel.
Connect
sh
pip install 'smolagents[openai]'BASE is http://127.0.0.1:4010/v1 and KEY is your plugboard key.
py
model = OpenAIModel(model_id=model_id, api_base=BASE, api_key=KEY, max_completion_tokens=1024)
message = model.generate([{"role": "user", "content": [{"type": "text", "text": PROMPT}]}])Keep /v1 in api_base. smolagents alone, without the openai extra, raises ModuleNotFoundError for OpenAIModel.
To stream, iterate model.generate_stream:
py
deltas = list(model.generate_stream([{"role": "user", "content": [{"type": "text", "text": PROMPT}]}]))A tool is a @tool function with a docstring, run by a ToolCallingAgent:
py
@tool
def get_weather(city: str) -> str:
"""Current weather for a city.
Args:
city: The city name.
"""
return '{"temp_c": 18, "sky": "cloudy"}'py
agent = ToolCallingAgent(tools=[get_weather], model=model, max_steps=4, verbosity_level=LogLevel.OFF)
answer = agent.run(TOOL_PROMPT)A script with a text reply, a stream and a tool-calling agent is in examples/smolagents/:
sh
cd examples/smolagents
PLUGBOARD_KEY=<app key> uv run example.pyWhat works
model.generate, model.generate_stream, and ToolCallingAgent with @tool functions.
Limits
- Keep smolagents' default role conversion. Forcing tool results into the
toolrole fails with 400Tool messages need a tool_call_id, because smolagents sends no call id. - An agent run ends with a
final_answertool call, so its last turn finishes withtool_calls, notstop.