官方教程地址:Agents - Guided tour
1. 安装
pip install smolagents[litellm]
或者
uv add smolagents[litellm]
2. 配置api key
这里我用的火山的api,注意如果是使用的火山或阿里云的这种服务商的api,model_id这里要以"openai/" 开头,后跟模型名称。
model = LiteLLMModel(
model_id="openai/deepseek-v3-250324", # This model is a bit weak for agentic behaviours though
api_base="https://ark.cn-beijing.volces.com/api/v3",
api_key="", # replace with API key if necessary
num_ctx=8192,
)
3. 完整代码
from smolagents import CodeAgent, LiteLLMModel
from smolagents import ToolCallingAgent, GradioUI, load_tool
model = LiteLLMModel(
model_id="openai/deepseek-v3-250324", # This model is a bit weak for agentic behaviours though
api_base="https://ark.cn-beijing.volces.com/api/v3",
api_key="", # replace with API key if necessary
num_ctx=8192,
)
# Initialize the agent with the image generation tool
agent = CodeAgent(tools=[], model=model, add_base_tools=True)
agent.run("斐波那契数列的第100个数是什么")
smolagents 中的CodeAgent是使用代码作为模型的中间输出,然后调用本地的python编译器工具执行代码,最终得到query的答案。