SmolAgents快速上手:最优雅的Agent构建工具

Githubhttps://github.com/huggingface/smolagents

官方文档https://huggingface.co/docs/smolagents/index

smolagents是HuggingFace官方推出的Agent开发库,HF出品的库,往往的设计理念是“低门槛,高天花板,可拓展”,所以知道HF出了Agent相关的框架后,也是很快体验了一下。

首先来介绍一下smolagents吧,smol是small的俏皮用法,故smolagents的含义是“轻量的agent工具”。smolagents库提供:

简洁性:Agent逻辑仅需约千行代码即可实现。在原生代码之上,我们将抽象保持至最简形态!

🌐 支持任何大语言模型:既支持通过transformers或推理API加载Hugging Face Hub托管的模型,也兼容OpenAI、Anthropic等模型。使用任何大语言模型驱动Agent都变得轻而易举。

🧑‍💻 代码Agent一流支持:即通过代码编写自身行为的Agent(而非"用于编写代码的Agent")

🤗 Hub集成:您可向/从HuggingFace Hub分享及加载工具,更多功能即将推出!

安装方式

pip install smolagents

快速开始

这是一段官方提供的Hello World代码:

from smolagents import CodeAgent, HfApiModel

model_id = "meta-llama/Llama-3.3-70B-Instruct"

model = HfApiModel(model_id=model_id, token="<YOUR_HUGGINGFACEHUB_API_TOKEN>")
agent = CodeAgent(tools=[], model=model, add_base_tools=True)

agent.run(
    "Could you give me the 118th number in the Fibonacci sequence?",
)

这段代码的逻辑是:

  1. 定义模型:在HuggingFace上调用meta-llama/Llama-3.3-70B-Instruct模型的API(免费可用,但有速率限制;如果你有HuggingFace PRO账号,可以通过token参数传给HfApiModel

  2. 定义Agent:使用CodeAgent,这个Agent将在每个步骤中编写和执行 python 代码片段。它有三个参数:

    1. tools:可被Agent调用的工具
    2. model:使用的LLM
    3. add_base_tools:加入基础工具?
  3. agent.run:输入input信息,执行Agent操作逻辑

在模型选择上,还有以下的选择:

使用本地Transformers模型:

from smolagents import CodeAgent, TransformersModel

model_id = "meta-llama/Llama-3.2-3B-Instruct"

model = TransformersModel(model_id=model_id)
agent = CodeAgent(tools=[], model=model, add_base_tools=True)

agent.run(
    "Could you give me the 118th number in the Fibonacci sequence?",
)

使用云端API(基于LiteLLM):

用LiteLLM可以包罗非常多的第三方闭源API,包括我个人比较常用的DeepSeek。

from smolagents import CodeAgent, LiteLLMModel

model = LiteLLMModel(model_id="anthropic/claude-3-5-sonnet-latest", api_key="YOUR_ANTHROPIC_API_KEY") # Could use 'gpt-4o'
agent = CodeAgent(tools=[], model=model, add_base_tools=True)

agent.run(
    "Could you give me the 118th number in the Fibonacci sequence?",
)

使用Ollama:

from smolagents import CodeAgent, LiteLLMModel

model = LiteLLMModel(
    model_id="ollama_chat/llama3.2", # This model is a bit weak for agentic behaviours though
    api_base="http://localhost:11434", # replace with 127.0.0.1:11434 or remote open-ai compatible server if necessary
    api_key="YOUR_API_KEY" # replace with API key if necessary
    num_ctx=8192 # ollama default is 2048 which will fail horribly. 8192 works for easy tasks, more is better. Check https://huggingface.co/spaces/NyxKrage/LLM-Model-VRAM-Calculator to calculate how much VRAM this will need for the selected model.
)

agent = CodeAgent(tools=[], model=model, add_base_tools=True)

agent.run(
    "Could you give me the 118th number in the Fibonacci sequence?",
)

一些方法

  • agent.log:存储 agent 的精细日志。在Agent运行的每一步,所有内容都存储在字典中,然后附加到 agent.logs
  • agent.write_inner_memory_from_logs:运行这个方法,会创建agent日志的内部Memory,以供 LLM view 作为聊天消息列表。此方法会遍历日志的每个步骤,并且仅将其感兴趣的内容存储为消息:例如,它将系统提示符和任务保存在单独的消息中,然后对于每个步骤,LLM它将输出存储为一条消息,并将工具调用输出存储为另一条消息。如果您想要更高级别的视图来了解所发生的情况,请使用此方法 - 但并非每个日志都会通过此方法进行转录。

工具

工具是Agent程序要使用的原子函数。

工具需要一些构成API属性的信息,一般是用于描述LLM如何调用这个工具的,包括名称、描述、输入类型和对应描述、输出类型。(还有个执行操作的forward方法)。

在初始化Agent时,工具属性会用于生成工具描述,这个描述被嵌入到Agent的system prompt中,让Agent知道它可以使用哪些工具,以及使用的理由。

默认工具箱

  • DuckDuckGo网页搜索工具
  • Python代码解释器
  • Transcriber语音转文本

手动调用工具:

from smolagents import DuckDuckGoSearchTool

search_tool = DuckDuckGoSearchTool()
print(search_tool("Who's the current president of Russia?"))

执行这个工具给的结果是下面这个感觉:

## Search Results

[President of Russia - Wikipedia](https://en.wikipedia.org/wiki/President_of_Russia)
The president of Russia, officially the president of the Russian Federation (Russian: Президент Российской Федерации, romanized: Prezident Rossiyskoy Federatsii), is the executive head of state of Russia.The president is the chair of the Federal State Council and the supreme commander-in-chief of the Russian Armed Forces.It is the highest office in Russia.

[List of presidents of Russia - Wikipedia](https://en.wikipedia.org/wiki/List_of_presidents_of_Russia)
The office of the president of Russia is the highest authority in the Russian Federation.The holder is the federation's head of state and has formal presidency over the State Council as well as being the commander in chief of the Russian Armed Forces.The office was introduced in 1918 after the February Revolution with the current office emerging after a referendum of 1991. [1]

[Vladimir Putin - Wikipedia](https://en.wikipedia.org/wiki/Vladimir_Putin)
New York City-based NGO Human Rights Watch, in a report entitled Laws of Attrition, authored by Hugh Williamson, the British director of HRW's Europe & Central Asia Division, has claimed that since May 2012, when Putin was reelected as president, Russia has enacted many restrictive laws, started inspections of non-governmental organizations ...
...

创建新工具

下面这段代码的作用是,通过huggingface_hub库,获取给定任务的下载次数最多的模型。

from huggingface_hub import list_models

task = "text-classification"

most_downloaded_model = next(iter(list_models(filter=task, sort="downloads", direction=-1)))
print(most_downloaded_model.id)

变成可供smolagents 调用的两种方法:

  1. 使用@tool装饰器
from smolagents import tool

@tool
def model_download_tool(task: str) -> str:
    """
    This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub.
    It returns the name of the checkpoint.

    Args:
        task: The task for which to get the download count.
    """
    most_downloaded_model = next(iter(list_models(filter=task, sort="downloads", direction=-1)))
    return most_downloaded_model.id
  1. 继承Tool类
from smolagents import Tool

class ModelDownloadTool(Tool):
    name = "model_download_tool"
    description = "This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub. It returns the name of the checkpoint."
    inputs = {"task": {"type": "string", "description": "The task for which to get the download count."}}
    output_type = "string"

    def forward(self, task: str) -> str:
        most_downloaded_model = next(iter(list_models(filter=task, sort="downloads", direction=-1)))
        return most_downloaded_model.id

然后,初始化Agent:

from smolagents import CodeAgent, HfApiModel
agent = CodeAgent(tools=[model_download_tool], model=HfApiModel())
agent.run(
    "Can you give me the name of the model that has the most downloads in the 'text-to-video' task on the Hugging Face Hub?"
)

一段用DeepSeek API,能直接跑的代码:

from smolagents import CodeAgent, LiteLLMModel, Tool
from huggingface_hub import list_models

class ModelDownloadTool(Tool):
    name = "model_download_tool"
    description = "This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub. It returns the name of the checkpoint."
    inputs = {"task": {"type": "string", "description": "The task for which to get the download count."}}
    output_type = "string"

    def forward(self, task: str) -> str:
        most_downloaded_model = next(iter(list_models(filter=task, sort="downloads", direction=-1)))
        return most_downloaded_model.id

model_download_tool = ModelDownloadTool()

model = LiteLLMModel(model_id="deepseek/deepseek-chat", api_key="sk-...")
agent = CodeAgent(tools=[model_download_tool], model=model)

agent.run("Can you give me the name of the model that has the most downloads in the 'text-to-video' task on the Hugging Face Hub?")

打印结果:

image

HuggingFace上的工具

调用方法:

from smolagents import load_tool

image_generation_tool = load_tool("m-ric/text-to-image", trust_remote_code=True)

那么这里肯定会好奇,难道HF出了工具社区吗?

其实,是把工具内置到了Space中:

imageimage

多Agent

多Agent要使用ManagedAgent这个类来封装子Agent,然后这些子Agent和工具一样要填写name和description,以及和工具一样的逻辑被主Agent调用。

HF文档:有关高效多智能体实施的深入示例,请参阅我们如何将多智能体系统推向 GAIA 排行榜的顶部

下面是一个网络搜索多Agent的案例:

from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, ManagedAgent

model = LiteLLMModel(model_id="deepseek/deepseek-chat", api_key="sk-...")

web_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=model)

managed_web_agent = ManagedAgent(
    agent=web_agent,
    name="web_search",
    description="Runs web searches for you. Give it your query as an argument."
)

manager_agent = CodeAgent(
    tools=[], model=model, managed_agents=[managed_web_agent]
)

manager_agent.run("Who is the CEO of Hugging Face?")

运行得到的完整输出:

(datax) (base) ➜  data_excel_process git:(master) ✗ python 1.py

╭─────────────────────────────────────────────────────────────────────────────── New run ────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                        │
│ Who is the CEO of Hugging Face?                                                                                                                                        │
│                                                                                                                                                                        │
╰─ LiteLLMModel - deepseek/deepseek-chat ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 0 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing this code: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
  ceo_info = web_search(request="Who is the current CEO of Hugging Face?")                                                                                            
  print(ceo_info)                                                                                                                                                     
 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
╭─────────────────────────────────────────────────────────────────────────────── New run ────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                        │
│ You're a helpful agent named 'web_search'.                                                                                                                             │
│ You have been submitted this task by your manager.                                                                                                                     │
│ ---                                                                                                                                                                    │
│ Task:                                                                                                                                                                  │
│ Who is the current CEO of Hugging Face?                                                                                                                                │
│ ---                                                                                                                                                                    │
│ You're helping your manager solve a wider task: so make sure to not provide a one-line answer, but give as much information as possible to give them a clear           │
│ understanding of the answer.                                                                                                                                           │
│                                                                                                                                                                        │
│ Your final_answer WILL HAVE to contain these parts:                                                                                                                    │
│ ### 1. Task outcome (short version):                                                                                                                                   │### 2. Task outcome (extremely detailed version):                                                                                                                      │### 3. Additional context (if relevant):                                                                                                                               │
│                                                                                                                                                                        │
│ Put all these in your final_answer tool, everything that you do not pass as an argument to final_answer will be lost.                                                  │
│ And even if your task resolution is not successful, please return as much context as possible, so that your manager can act upon this feedback.                        │
│ {additional_prompting}                                                                                                                                                 │
│                                                                                                                                                                        │
╰─ LiteLLMModel - deepseek/deepseek-chat ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 0 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing this code: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
  ceo_info = web_search(query="current CEO of Hugging Face")                                                                                                          
  print(ceo_info)                                                                                                                                                     
 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
Execution logs:
## Search Results

[Clem Delangue - Hugging Face - LinkedIn](https://www.linkedin.com/in/clementdelangue)
A lot of you were curious why I decided to start it after transitioning from CEO to a Board Member at Mighty Buildings (a top Y Combinator company) — here's why 👇 I'm a 
rare founder who ...

[Who is the CEO of Hugging Face? Clem Delangue 's Bio - Clay](https://www.clay.com/dossier/hugging-face-ceo)
Clément Delangue is the CEO and co-founder of Hugging Face, an open and collaborative platform for AI builders. With a background in entrepreneurship and education from 
Stanford University, Delangue has played a pivotal role in growing Hugging Face into a leading AI platform valued at $4.5 billion, hosting over a million open-source 
repositories and serving more than 15,000 companies ...

[Hugging Face - Wikipedia](https://en.wikipedia.org/wiki/Hugging_Face)
Hugging Face, Inc. is an American company incorporated under the Delaware General Corporation Law [1] and based in New York City that develops computation tools for 
building applications using machine learning.It is most notable for its transformers library built for natural language processing applications and its platform that 
allows users to share machine learning models and datasets and ...

[List of Hugging Face Executives & Org Chart | Clay](https://www.clay.com/dossier/hugging-face-executives)
At the top of Hugging Face's organizational chart is Clement Delangue, the Co-Founder and CEO, who oversees the company's overall vision and strategic direction. Directly
reporting to him are Julien Chaumond, the Co-Founder and CTO, and Thomas Wolf, the Co-Founder and Chief Science Officer, both of whom play crucial roles in driving ...

[Clément Delangue, CEO of Hugging Face, built the GitHub of AI](https://www.fastcompany.com/90909717/clement-delangue-ceo-hugging-face-most-creative-people-2023)
How Clément Delangue, CEO of Hugging Face, built the GitHub of AI. Users have shared more than 250,000 models and 50,000 data sets, helping propel the company to a $2 
billion valuation. LOGIN

[Hugging Face CEO and Key Executive Team | Craft.co](https://craft.co/hugging-face/executives)
Hugging Face's Co-Founder, CEO is Clement Delangue. Other executives include Julien Chaumond, Co-Founder, CTO; Thomas Wolf, Co-Founder - CSO and 1 others. See the full 
leadership team at Craft.

[Clément Delangue: The 100 Most Influential People in AI 2023 - TIME](https://time.com/collection/time100-ai/6308994/clement-delangue/)
C lément Delangue is the CEO of Hugging Face, an open-source, for-profit machine-learning platform where researchers from around the world convene to share their AI 
models, datasets, and best ...

[Hugging Face CEO, Founder, Key Executive Team, Board of Directors ...](https://www.cbinsights.com/company/hugging-face/people)
Explore {Hugging Face's key management people. Discover current leadership team members including founders, CEO, other executives and board directors. ... Hugging Face's 
current Founder, Chief Executive Officer is Clément Delangue. Name. Work History. Title. Status. Clément Delangue. Mention Solutions, Moodstocks, VideoNotes, and eBay.

[Hugging Face - Company Profile - Tracxn](https://tracxn.com/d/companies/hugging-face/___89yhA9z0-ZrLstW87xWDVe15Bkl70IZOkQf38SXzmQ)
Hugging Face - Platform offering frameworks for implementation and development of ML and NLP models. Raised a total funding of $400M over 8 rounds from 33 investors. ... 
Who is the current CEO of Hugging Face? Clement Delangue is the CEO of Hugging Face. View details of Hugging Face's Founder profiles and Board Members. Hugging Face's ...

[Meet Hugging Face CEO Clement Delangue | ICT-Mirror](https://ictmirror.com/entertainment/hugging-face-ceo-clement-delangue/)
Entrepreneurial Journey Of The Hugging Face CEO Clement Delangue. Clement Delangue is the CEO and co-founder of Hugging Face. He is a native of quaint town of La Bassee 
in northern France.. Growing up in an ordinary household, Clem's interest in technology was piqued when he got his first computer at the age of 12.

Out: None
[Step 0: Duration 4.40 seconds| Input tokens: 2,210 | Output tokens: 77]

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing this code: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
  final_answer({                                                                                                                                                      
      "1. Task outcome (short version)": "The current CEO of Hugging Face is Clément Delangue.",                                                                      
      "2. Task outcome (extremely detailed version)": "Clément Delangue is the CEO and co-founder of Hugging Face, an open and collaborative platform for AI builders.  
  With a background in entrepreneurship and education from Stanford University, Delangue has been instrumental in growing Hugging Face into a leading AI platform valued  
  at $4.5 billion. The company hosts over a million open-source repositories and serves more than 15,000 companies. Delangue's leadership has been pivotal in         
  establishing Hugging Face as a key player in the AI and machine learning community.",                                                                               
      "3. Additional context (if relevant)": "Hugging Face, Inc. is an American company based in New York City that develops computation tools for building applications  
  using machine learning. It is most notable for its transformers library built for natural language processing applications and its platform that allows users to share  
  machine learning models and datasets. The company was founded by Clément Delangue, Julien Chaumond (CTO), and Thomas Wolf (Chief Science Officer). Hugging Face has   
  raised a total funding of $400 million over 8 rounds from 33 investors and is valued at $4.5 billion."                                                              
  })                                                                                                                                                                  
 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
Out - Final answer: {'1. Task outcome (short version)': 'The current CEO of Hugging Face is Clément Delangue.', '2. Task outcome (extremely detailed version)': "Clément 
Delangue is the CEO and co-founder of Hugging Face, an open and collaborative platform for AI builders. With a background in entrepreneurship and education from Stanford 
University, Delangue has been instrumental in growing Hugging Face into a leading AI platform valued at $4.5 billion. The company hosts over a million open-source 
repositories and serves more than 15,000 companies. Delangue's leadership has been pivotal in establishing Hugging Face as a key player in the AI and machine learning 
community.", '3. Additional context (if relevant)': 'Hugging Face, Inc. is an American company based in New York City that develops computation tools for building 
applications using machine learning. It is most notable for its transformers library built for natural language processing applications and its platform that allows users
to share machine learning models and datasets. The company was founded by Clément Delangue, Julien Chaumond (CTO), and Thomas Wolf (Chief Science Officer). Hugging Face 
has raised a total funding of $400 million over 8 rounds from 33 investors and is valued at $4.5 billion.'}
[Step 1: Duration 8.87 seconds| Input tokens: 5,570 | Output tokens: 440]
Execution logs:
{'1. Task outcome (short version)': 'The current CEO of Hugging Face is Clément Delangue.', '2. Task outcome (extremely detailed version)': "Clément Delangue is the CEO 
and co-founder of Hugging Face, an open and collaborative platform for AI builders. With a background in entrepreneurship and education from Stanford University, Delangue
has been instrumental in growing Hugging Face into a leading AI platform valued at $4.5 billion. The company hosts over a million open-source repositories and serves more
than 15,000 companies. Delangue's leadership has been pivotal in establishing Hugging Face as a key player in the AI and machine learning community.", '3. Additional 
context (if relevant)': 'Hugging Face, Inc. is an American company based in New York City that develops computation tools for building applications using machine 
learning. It is most notable for its transformers library built for natural language processing applications and its platform that allows users to share machine learning 
models and datasets. The company was founded by Clément Delangue, Julien Chaumond (CTO), and Thomas Wolf (Chief Science Officer). Hugging Face has raised a total funding 
of $400 million over 8 rounds from 33 investors and is valued at $4.5 billion.'}

Out: None
[Step 0: Duration 15.81 seconds| Input tokens: 3,360 | Output tokens: 363]

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing this code: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
  final_answer(answer="Clément Delangue")                                                                                                                             
 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
evaluate_python_code.<locals>.final_answer() got an unexpected keyword argument 'answer'
[Step 1: Duration 2.15 seconds| Input tokens: 5,847 | Output tokens: 414]

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 2 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing this code: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
  final_answer("Clément Delangue")                                                                                                                                    
 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
Out - Final answer: Clément Delangue
[Step 2: Duration 3.35 seconds| Input tokens: 8,485 | Output tokens: 508]

可以大概窥见smolagents的agent执行逻辑。

提示词:

│ You're a helpful agent named 'web_search'.                                                                                                                             │
│ You have been submitted this task by your manager.                                                                                                                     │
│ ---                                                                                                                                                                    │
│ Task:                                                                                                                                                                  │
│ Who is the current CEO of Hugging Face?                                                                                                                                │
│ ---                                                                                                                                                                    │
│ You're helping your manager solve a wider task: so make sure to not provide a one-line answer, but give as much information as possible to give them a clear           │
│ understanding of the answer.                                                                                                                                           │
│                                                                                                                                                                        │
│ Your final_answer WILL HAVE to contain these parts:                                                                                                                    │
│ ### 1. Task outcome (short version):                                                                                                                                   │### 2. Task outcome (extremely detailed version):                                                                                                                      │### 3. Additional context (if relevant):                                                                                                                               │
│                                                                                                                                                                        │
│ Put all these in your final_answer tool, everything that you do not pass as an argument to final_answer will be lost.                                                  │
│ And even if your task resolution is not successful, please return as much context as possible, so that your manager can act upon this feedback.                        │
│ {additional_prompting}  

中文翻译:

│ 你是一个名为“web_search”的助手。                                                                                                                             │
│ 你的经理向你提交了这项任务。                                                                                                                     │
│ ---                                                                                                                                                                    │
│ 任务:                                                                                                                                                                  │
│ Hugging Face 的现任 CEO 是谁?                                                                                                                                │
│ ---                                                                                                                                                                    │
│ 你正在帮助你的经理解决一个更广泛的任务:因此,请确保不要只提供一行答案,而是尽可能提供更多信息,以便他们能够清晰地理解答案。     
│                                                                                                                                                                        │
│ 你的最终答案(final_answer)必须包含以下部分:                                                                                                                    │
│ ### 1. 任务结果(简短版本):                                                                                                                                   │### 2. 任务结果(极其详细的版本):                                                                                                                      │### 3. 额外背景信息(如果相关):                                                                                                                               │
│                                                                                                                                                                        │
│ 请将所有内容放入你的 final_answer 工具中,任何未作为参数传递给 final_answer 的内容都将丢失。                                                  │
│ 即使你的任务未能成功完成,也请尽可能提供更多背景信息,以便你的经理可以根据这些反馈采取行动。                    
│ {additional_prompting}

创建Gradio网页

from smolagents import (
    CodeAgent,
    LiteLLMModel,
    GradioUI,
    DuckDuckGoSearchTool
)

# Import tool from Hub
search_tool = DuckDuckGoSearchTool()

model = LiteLLMModel(model_id="deepseek/deepseek-chat", api_key="sk-...")

# Initialize the agent with the image generation tool
agent = CodeAgent(tools=[search_tool], model=model)

GradioUI(agent).launch()

image

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值