Duibonduil commited on
Commit
dc9a80d
·
verified ·
1 Parent(s): d40f40d

Upload 4 files

Browse files
docs/source/zh/_config.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # docstyle-ignore
2
+ INSTALL_CONTENT = """
3
+ # Installation
4
+ ! pip install smolagents
5
+ # To install from source instead of the last release, comment the command above and uncomment the following one.
6
+ # ! pip install git+https://github.com/huggingface/smolagents.git
7
+ """
8
+
9
+ notebook_first_cells = [{"type": "code", "content": INSTALL_CONTENT}]
10
+ black_avoid_patterns = {
11
+ "{processor_class}": "FakeProcessorClass",
12
+ "{model_class}": "FakeModelClass",
13
+ "{object_class}": "FakeObjectClass",
14
+ }
docs/source/zh/_toctree.yml ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ - title: 起步
2
+ sections:
3
+ - local: index
4
+ title: 🤗 Agents
5
+ - local: guided_tour
6
+ title: 导览
7
+ - title: Tutorials
8
+ sections:
9
+ - local: tutorials/building_good_agents
10
+ title: ✨ 构建好用的 agents
11
+ - local: tutorials/inspect_runs
12
+ title: 📊 监控 Agent 的运行
13
+ - local: tutorials/tools
14
+ title: 🛠️ 工具 - 深度指南
15
+ - local: tutorials/secure_code_execution
16
+ title: 🛡️ 使用 E2B 保护你的代码执行
17
+ - local: tutorials/memory
18
+ title: 📚 管理 Agent 的记忆
19
+ - title: Conceptual guides
20
+ sections:
21
+ - local: conceptual_guides/intro_agents
22
+ title: 🤖 Agent 化系统介绍
23
+ - local: conceptual_guides/react
24
+ title: 🤔 多步骤 Agent 是如何工作的?
25
+ - title: Examples
26
+ sections:
27
+ - local: examples/text_to_sql
28
+ title: 自我修正 Text-to-SQL
29
+ - local: examples/rag
30
+ title: 借助 agentic RAG 掌控知识库
31
+ - local: examples/multiagents
32
+ title: 编排 multi-agent 系统
33
+ - local: examples/web_browser
34
+ title: 基于视觉模型构建能够浏览网页的agent
35
+ - title: Reference
36
+ sections:
37
+ - local: reference/agents
38
+ title: Agent-related objects
39
+ - local: reference/models
40
+ title: Model-related objects
41
+ - local: reference/tools
42
+ title: Tool-related objects
docs/source/zh/guided_tour.md ADDED
@@ -0,0 +1,426 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Agents - 导览
2
+
3
+ [[open-in-colab]]
4
+
5
+ 在本导览中,您将学习如何构建一个 agent(智能体),如何运行它,以及如何自定义它以使其更好地适应您的使用场景。
6
+
7
+ > [!TIP]
8
+ > 译者注:Agent 的业内术语是“智能体”。本译文将保留 agent,不作翻译,以带来更高效的阅读体验。(在中文为主的文章中,It's easier to 注意到英文。Attention Is All You Need!)
9
+
10
+ > [!TIP]
11
+ > 中文社区发布了关于 smolagents 的介绍和实践讲解视频(来源:[Issue#80](https://github.com/huggingface/smolagents/issues/80)),你可以访问[这里](https://www.youtube.com/watch?v=wwN3oAugc4c)进行观看!
12
+
13
+ ### 构建您的 agent
14
+
15
+ 要初始化一个最小化的 agent,您至少需要以下两个参数:
16
+
17
+ - `model`,一个为您的 agent 提供动力的文本生成模型 - 因为 agent 与简单的 LLM 不同,它是一个使用 LLM 作为引擎的系统。您可以使用以下任一选项:
18
+ - [`TransformersModel`] 使用预初始化的 `transformers` 管道在本地机器上运行推理
19
+ - [`InferenceClientModel`] 在底层使用 `huggingface_hub.InferenceClient`
20
+ - [`LiteLLMModel`] 让您通过 [LiteLLM](https://docs.litellm.ai/) 调用 100+ 不同的模型!
21
+ - [`AzureOpenAIServerModel`] 允许您使用部署在 [Azure](https://azure.microsoft.com/en-us/products/ai-services/openai-service) 中的 OpenAI 模型。
22
+ - [`MLXModel`] 可创建 [mlx-lm](https://pypi.org/project/mlx-lm/) 流水线,以便在本地机器上运行推理。
23
+
24
+ - `tools`,agent 可以用来解决任务的 `Tools` 列表。它可以是一个空列表。您还可以通过定义可选参数 `add_base_tools=True` 在您的 `tools` 列表之上添加默认工具箱。
25
+
26
+ 一旦有了这两个参数 `tools` 和 `model`,您就可以创建一个 agent 并运行它。您可以使用任何您喜欢的 LLM,无论是通过 [Hugging Face API](https://huggingface.co/docs/api-inference/en/index)、[transformers](https://github.com/huggingface/transformers/)、[ollama](https://ollama.com/)、[LiteLLM](https://www.litellm.ai/)、[Azure OpenAI](https://azure.microsoft.com/en-us/products/ai-services/openai-service),还是[mlx-lm](https://pypi.org/project/mlx-lm/).。
27
+
28
+ <hfoptions id="选择一个LLM">
29
+ <hfoption id="Hugging Face API">
30
+
31
+ Hugging Face API 可以免费使用而无需 token,但会有速率限制。
32
+
33
+ 要访问受限模型或使用 PRO 账户提高速率限制,您需要设置环境变量 `HF_TOKEN` 或在初始化 `InferenceClientModel` 时传递 `token` 变量。
34
+
35
+ ```python
36
+ from smolagents import CodeAgent, InferenceClientModel
37
+
38
+ model_id = "meta-llama/Llama-3.3-70B-Instruct"
39
+
40
+ model = InferenceClientModel(model_id=model_id, token="<YOUR_HUGGINGFACEHUB_API_TOKEN>")
41
+ agent = CodeAgent(tools=[], model=model, add_base_tools=True)
42
+
43
+ agent.run(
44
+ "Could you give me the 118th number in the Fibonacci sequence?",
45
+ )
46
+ ```
47
+ </hfoption>
48
+ <hfoption id="本地Transformers模型">
49
+
50
+ ```python
51
+ # !pip install smolagents[transformers]
52
+ from smolagents import CodeAgent, TransformersModel
53
+
54
+ model_id = "meta-llama/Llama-3.2-3B-Instruct"
55
+
56
+ model = TransformersModel(model_id=model_id)
57
+ agent = CodeAgent(tools=[], model=model, add_base_tools=True)
58
+
59
+ agent.run(
60
+ "Could you give me the 118th number in the Fibonacci sequence?",
61
+ )
62
+ ```
63
+ </hfoption>
64
+ <hfoption id="OpenAI或Anthropic API">
65
+
66
+ 要使用 `LiteLLMModel`,您需要设置环境变量 `ANTHROPIC_API_KEY` 或 `OPENAI_API_KEY`,或者在初始化时传递 `api_key` 变量。
67
+
68
+ ```python
69
+ # !pip install smolagents[litellm]
70
+ from smolagents import CodeAgent, LiteLLMModel
71
+
72
+ model = LiteLLMModel(model_id="anthropic/claude-3-5-sonnet-latest", api_key="YOUR_ANTHROPIC_API_KEY") # 也可以使用 'gpt-4o'
73
+ agent = CodeAgent(tools=[], model=model, add_base_tools=True)
74
+
75
+ agent.run(
76
+ "Could you give me the 118th number in the Fibonacci sequence?",
77
+ )
78
+ ```
79
+ </hfoption>
80
+ <hfoption id="Ollama">
81
+
82
+ ```python
83
+ # !pip install smolagents[litellm]
84
+ from smolagents import CodeAgent, LiteLLMModel
85
+
86
+ model = LiteLLMModel(
87
+ model_id="ollama_chat/llama3.2", # 这个模型对于 agent 行为来说有点弱
88
+ api_base="http://localhost:11434", # 如果需要可以替换为远程 open-ai 兼容服务器
89
+ api_key="YOUR_API_KEY" # 如果需要可以替换为 API key
90
+ num_ctx=8192 # https://huggingface.co/spaces/NyxKrage/LLM-Model-VRAM-Calculator
91
+ )
92
+
93
+ agent = CodeAgent(tools=[], model=model, add_base_tools=True)
94
+
95
+ agent.run(
96
+ "Could you give me the 118th number in the Fibonacci sequence?",
97
+ )
98
+ ```
99
+ </hfoption>
100
+ <hfoption id="Azure OpenAI">
101
+
102
+ 要连接到 Azure OpenAI,您可以直接使用 `AzureOpenAIServerModel`,或使用 `LiteLLMModel` 并进行相应配置。
103
+
104
+ 初始化 `AzureOpenAIServerModel` 实例时,需要传递模型部署名称,可选择以下任一种方式:1.传递 `azure_endpoint`、`api_key` 和 `api_version` 参数;2.设置环境变量 `AZURE_OPENAI_ENDPOINT`、`AZURE_OPENAI_API_KEY` 和 `OPENAI_API_VERSION`
105
+
106
+ ```python
107
+ # !pip install smolagents[openai]
108
+ from smolagents import CodeAgent, AzureOpenAIServerModel
109
+
110
+ model = AzureOpenAIServerModel(model_id="gpt-4o-mini")
111
+ agent = CodeAgent(tools=[], model=model, add_base_tools=True)
112
+
113
+ agent.run(
114
+ "Could you give me the 118th number in the Fibonacci sequence?",
115
+ )
116
+ ```
117
+
118
+ 也可按如下方式配置 `LiteLLMModel` 连接 Azure OpenAI:
119
+
120
+ - 将模型部署名称作为 `model_id` 参数传递,并确保其前缀为 `azure/`
121
+ - 确保设置环境变量 `AZURE_API_VERSION`
122
+ - 任选其一:1.传递 `api_base` 和 `api_key` 参数;2.设置环境变量 `AZURE_API_KEY` 和 `AZURE_API_BASE`
123
+
124
+ ```python
125
+ import os
126
+ from smolagents import CodeAgent, LiteLLMModel
127
+
128
+ AZURE_OPENAI_CHAT_DEPLOYMENT_NAME="gpt-35-turbo-16k-deployment" # example of deployment name
129
+
130
+ os.environ["AZURE_API_KEY"] = "" # api_key
131
+ os.environ["AZURE_API_BASE"] = "" # "https://example-endpoint.openai.azure.com"
132
+ os.environ["AZURE_API_VERSION"] = "" # "2024-10-01-preview"
133
+
134
+ model = LiteLLMModel(model_id="azure/" + AZURE_OPENAI_CHAT_DEPLOYMENT_NAME)
135
+ agent = CodeAgent(tools=[], model=model, add_base_tools=True)
136
+
137
+ agent.run(
138
+ "Could you give me the 118th number in the Fibonacci sequence?",
139
+ )
140
+ ```
141
+
142
+ </hfoption>
143
+ <hfoption id="mlx-lm">
144
+
145
+ ```python
146
+ # !pip install smolagents[mlx-lm]
147
+ from smolagents import CodeAgent, MLXModel
148
+
149
+ mlx_model = MLXModel("mlx-community/Qwen2.5-Coder-32B-Instruct-4bit")
150
+ agent = CodeAgent(model=mlx_model, tools=[], add_base_tools=True)
151
+
152
+ agent.run("Could you give me the 118th number in the Fibonacci sequence?")
153
+ ```
154
+
155
+ </hfoption>
156
+ </hfoptions>
157
+
158
+ #### CodeAgent 和 ToolCallingAgent
159
+
160
+ [`CodeAgent`] 是我们的默认 agent。它将在每一步编写并执行 Python 代码片段。
161
+
162
+ 默认情况下,执行是在您的本地环境中完成的。
163
+ 这应该是安全的,因为唯一可以调用的函数是您提供的工具(特别是如果只有 Hugging Face 的工具)和一组预定义的安全函数,如 `print` 或 `math` 模块中的函数,所以您已经限制了可以执行的内容。
164
+
165
+ Python 解释器默认也不允许在安全列表之外导入,所以所有最明显的攻击都不应该成为问题。
166
+ 您可以通过在初始化 [`CodeAgent`] 时将授权模块作为字符串列表传递给参数 `additional_authorized_imports` 来授权额外的导入:
167
+
168
+ ```py
169
+ from smolagents import CodeAgent
170
+
171
+ model = InferenceClientModel()
172
+ agent = CodeAgent(tools=[], model=model, additional_authorized_imports=['requests', 'bs4'])
173
+ agent.run("Could you get me the title of the page at url 'https://huggingface.co/blog'?")
174
+ ```
175
+
176
+ > [!WARNING]
177
+ > LLM 可以生成任意代码然后执行:不要添加任何不安全的导入!
178
+
179
+ 如果生成的代码尝试执行非法操作或出现常规 Python 错误,执行将停止。
180
+
181
+ 您也可以使用 [E2B 代码执行器](https://e2b.dev/docs#what-is-e2-b) 或 Docker 而不是本地 Python 解释器。对于 E2B,首先 [设置 `E2B_API_KEY` 环境变量](https://e2b.dev/dashboard?tab=keys),然后在初始化 agent 时传递 `executor_type="e2b"`。对于 Docker,在初始化时传递 `executor_type="docker"`。
182
+
183
+ > [!TIP]
184
+ > 在 [该教程中](tutorials/secure_code_execution) 了解更多关于代码执行的内容。
185
+
186
+ 我们还支持广泛使用的将动作编写为 JSON-like 块的方式:[`ToolCallingAgent`],它的工作方式与 [`CodeAgent`] 非常相似,当然没有 `additional_authorized_imports`,因为它不执行代码:
187
+
188
+ ```py
189
+ from smolagents import ToolCallingAgent
190
+
191
+ agent = ToolCallingAgent(tools=[], model=model)
192
+ agent.run("Could you get me the title of the page at url 'https://huggingface.co/blog'?")
193
+ ```
194
+
195
+ ### 检查 agent 运行
196
+
197
+ 以下是一些有用的属性,用于检查运行后发生了什么:
198
+ - `agent.logs` 存储 agent 的细粒度日志。在 agent 运行的每一步,所有内容都会存储在一个字典中,然后附加到 `agent.logs` 中。
199
+ - 运行 `agent.write_memory_to_messages()` 会为 LLM 创建一个 agent 日志的内部内存,作为聊天消息列表。此方法会遍历日志的每一步,并仅存储它感兴趣的内容作为消息:例如,它会将系统提示和任务存储为单独的消息,然后对于每一步,它会将 LLM 输出存储为一条消息,工具调用输出存储为另一条消息。如果您想要更高级别的视图 - 但不是每个日志都会被此方法转录。
200
+
201
+ ## 工具
202
+
203
+ 工具是 agent 使用的原子函数。为了被 LLM 使用,它还需要一些构成其 API 的属性,这些属性将用于向 LLM 描述如何调用此工具:
204
+ - 名称
205
+ - 描述
206
+ - 输入类型和描述
207
+ - 输出类型
208
+
209
+ 例如,您可以查看 [`PythonInterpreterTool`]:它有一个名称、描述、输入描述、输出类型和一个执行操作的 `forward` 方法。
210
+
211
+ 当 agent 初始化时,工具属性用于生成工具描述,该描述被嵌入到 agent 的系统提示中。这让 agent 知道它可以使用哪些工具以及为什么。
212
+
213
+ ### 默认工具箱
214
+
215
+ `smolagents` 附带了一个用于增强 agent 的默认工具箱,您可以在初始化���通过参数 `add_base_tools=True` 将其添加到您的 agent 中:
216
+
217
+ - **DuckDuckGo 网页搜索**:使用 DuckDuckGo 浏览器执行网页搜索。
218
+ - **Python 代码解释器**:在安全环境中运行 LLM 生成的 Python 代码。只有在使用 `add_base_tools=True` 初始化 [`ToolCallingAgent`] 时才会添加此工具,因为基于代码的 agent 已经可以原生执行 Python 代码
219
+ - **转录器**:基于 Whisper-Turbo 构建的语音转文本管道,将音频转录为文本。
220
+
221
+ 您可以通过调用 [`load_tool`] 函数和要执行的任务手动使用工具。
222
+
223
+ ```python
224
+ from smolagents import WebSearchTool
225
+
226
+ search_tool = WebSearchTool()
227
+ print(search_tool("Who's the current president of Russia?"))
228
+ ```
229
+
230
+ ### 创建一个新工具
231
+
232
+ 您可以创建自己的工具,用于 Hugging Face 默认工具未涵盖的用例。
233
+ 例如,让我们创建一个工具,返回 Hub 上给定任务下载量最多的模型。
234
+
235
+ 您将从以下代码开始。
236
+
237
+ ```python
238
+ from huggingface_hub import list_models
239
+
240
+ task = "text-classification"
241
+
242
+ most_downloaded_model = next(iter(list_models(filter=task, sort="downloads", direction=-1)))
243
+ print(most_downloaded_model.id)
244
+ ```
245
+
246
+ 这段代码可以通过将其包装在一个函数中并添加 `tool` 装饰器快速转换为工具:
247
+ 这不是构建工具的唯一方法:您可以直接将其定义为 [`Tool`] 的子类,这为您提供了更多的灵活性,例如初始化重型类属性的可能性。
248
+
249
+ 让我们看看这两种选项的工作原理:
250
+
251
+ <hfoptions id="构建工具">
252
+ <hfoption id="使用@tool装饰一个函数">
253
+
254
+ ```py
255
+ from smolagents import tool
256
+
257
+ @tool
258
+ def model_download_tool(task: str) -> str:
259
+ """
260
+ This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub.
261
+ It returns the name of the checkpoint.
262
+
263
+ Args:
264
+ task: The task for which to get the download count.
265
+ """
266
+ most_downloaded_model = next(iter(list_models(filter=task, sort="downloads", direction=-1)))
267
+ return most_downloaded_model.id
268
+ ```
269
+
270
+ 该函数需要:
271
+ - 一个清晰的名称。名称应该足够描述此工具的功能,以帮助为 agent 提供动力的 LLM。由于此工具返回任务下载量最多的模型,我们将其命名为 `model_download_tool`。
272
+ - 输入和输出的类型提示
273
+ - 一个描述,其中包括一个 'Args:' 部分,其中每个参数都被描述(这次没有类型指示,它将从类型提示中提取)。与工具名称一样,此描述是为您的 agent 提供动力的 LLM 的说明书,所以不要忽视它。
274
+ 所有这些元素将在初始化时自动嵌入到 agent 的系统提示中:因此要努力使它们尽可能清晰!
275
+
276
+ > [!TIP]
277
+ > 此定义格式与 `apply_chat_template` 中使用的工具模式相同,唯一的区别是添加了 `tool` 装饰器:[这里](https://huggingface.co/blog/unified-tool-use#passing-tools-to-a-chat-template) 了解更多关于我们的工具使用 API。
278
+ </hfoption>
279
+ <hfoption id="子类化Tool">
280
+
281
+ ```py
282
+ from smolagents import Tool
283
+
284
+ class ModelDownloadTool(Tool):
285
+ name = "model_download_tool"
286
+ 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."
287
+ inputs = {"task": {"type": "string", "description": "The task for which to get the download count."}}
288
+ output_type = "string"
289
+
290
+ def forward(self, task: str) -> str:
291
+ most_downloaded_model = next(iter(list_models(filter=task, sort="downloads", direction=-1)))
292
+ return most_downloaded_model.id
293
+ ```
294
+
295
+ 子类需要以下属性:
296
+ - 一个清晰的 `name`。名称应该足够描述此工具的功能,以帮助为 agent 提供动力的 LLM。由于此工具返回任务下载量最多的模型,我们将其命名为 `model_download_tool`。
297
+ - 一个 `description`。与 `name` 一样,此描述是为您的 agent 提供动力的 LLM 的说明书,所以不要忽视它。
298
+ - 输入类型和描述
299
+ - 输出类型
300
+ 所有这些属性将在初始化时自动嵌入到 agent 的系统提示中:因此要努力使它们尽可能清晰!
301
+ </hfoption>
302
+ </hfoptions>
303
+
304
+
305
+ 然后您可以直接初始化您的 agent:
306
+ ```py
307
+ from smolagents import CodeAgent, InferenceClientModel
308
+ agent = CodeAgent(tools=[model_download_tool], model=InferenceClientModel())
309
+ agent.run(
310
+ "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?"
311
+ )
312
+ ```
313
+
314
+ 您将获得以下日志:
315
+ ```text
316
+ ╭──────────────────────────────────────── New run ─────────────────────────────────────────╮
317
+ │ │
318
+ │ Can you give me the name of the model that has the most downloads in the 'text-to-video' │
319
+ │ task on the Hugging Face Hub? │
320
+ │ │
321
+ ╰─ InferenceClientModel - Qwen/Qwen2.5-Coder-32B-Instruct ───────────────────────────────────────────╯
322
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 0 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
323
+ ╭─ Executing this code: ───────────────────────────────────────────────────────────────────╮
324
+ │ 1 model_name = model_download_tool(task="text-to-video") │
325
+ │ 2 print(model_name) │
326
+ ╰──────────────────────────────────────────────────────────────────────────────────────────╯
327
+ Execution logs:
328
+ ByteDance/AnimateDiff-Lightning
329
+
330
+ Out: None
331
+ [Step 0: Duration 0.27 seconds| Input tokens: 2,069 | Output tokens: 60]
332
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
333
+ ╭─ Executing this code: ───────────────────────────────────────────────────────────────────╮
334
+ │ 1 final_answer("ByteDance/AnimateDiff-Lightning") │
335
+ ╰──────────────────────────────────────────────────────────────────────────────────────────╯
336
+ Out - Final answer: ByteDance/AnimateDiff-Lightning
337
+ [Step 1: Duration 0.10 seconds| Input tokens: 4,288 | Output tokens: 148]
338
+ Out[20]: 'ByteDance/AnimateDiff-Lightning'
339
+ ```
340
+
341
+ > [!TIP]
342
+ > 在 [专用教程](./tutorials/tools#what-is-a-tool-and-how-to-build-one) 中了解更多关于工具的内容。
343
+
344
+ ## 多 agent
345
+
346
+ 多 agent 系统是随着微软的框架 [Autogen](https://huggingface.co/papers/2308.08155) 引入的。
347
+
348
+ 在这种类型的框架中,您有多个 agent 一起工作来解决您的任务,而不是只有一个。
349
+ 经验表明,这在大多数基准测试中表现更好。这种更好表现的原因在概念上很简单:对于许多任务,与其使用一个全能系统,您更愿意将单元专门用于子任务。在这里,拥有具有单独工具集和内存的 agent 可以实现高效的专业化。例如,为什么要用网页搜索 agent 访问的所有网页内容填充代码生成 agent 的内存?最好将它们分开。
350
+
351
+ 您可以使用 `smolagents` 轻松构建分层多 agent 系统。
352
+
353
+ 为此,将 agent 封装在 [`ManagedAgent`] 对象中。此对象需要参数 `agent`、`name` 和 `description`,这些参数将嵌入到管理 agent 的系统提示中,以让它知道如何调用此托管 agent,就像我们对工具所做的那样。
354
+
355
+ 以下是一个使用我们的 [`WebSearchTool`] 制作一个管理特定网页搜索 agent 的 agent 的示例:
356
+
357
+ ```py
358
+ from smolagents import CodeAgent, InferenceClientModel, WebSearchTool, ManagedAgent
359
+
360
+ model = InferenceClientModel()
361
+
362
+ web_agent = CodeAgent(tools=[WebSearchTool()], model=model)
363
+
364
+ managed_web_agent = ManagedAgent(
365
+ agent=web_agent,
366
+ name="web_search",
367
+ description="Runs web searches for you. Give it your query as an argument."
368
+ )
369
+
370
+ manager_agent = CodeAgent(
371
+ tools=[], model=model, managed_agents=[managed_web_agent]
372
+ )
373
+
374
+ manager_agent.run("Who is the CEO of Hugging Face?")
375
+ ```
376
+
377
+ > [!TIP]
378
+ > 有关高效多 agent 实现的深入示例,请参阅 [我们如何将多 agent 系统推向 GAIA 排行榜的顶部](https://huggingface.co/blog/beating-gaia)。
379
+
380
+
381
+ ## 与您的 agent 交谈并在酷炫的 Gradio 界面中可视化其思考过程
382
+
383
+ 您可以使用 `GradioUI` 交互式地向您的 agent 提交任务并观察其思考和执行过程,以下是一个示例:
384
+
385
+ ```py
386
+ from smolagents import (
387
+ load_tool,
388
+ CodeAgent,
389
+ InferenceClientModel,
390
+ GradioUI
391
+ )
392
+
393
+ # 从 Hub 导入工具
394
+ image_generation_tool = load_tool("m-ric/text-to-image")
395
+
396
+ model = InferenceClientModel(model_id=model_id)
397
+
398
+ # 使用图像生成工具初始化 agent
399
+ agent = CodeAgent(tools=[image_generation_tool], model=model)
400
+
401
+ GradioUI(agent).launch()
402
+ ```
403
+
404
+ 在底层,当用户输入新答案时,agent 会以 `agent.run(user_request, reset=False)` 启动。
405
+ `reset=False` 标志意味着在启动此新任务之前不会刷新 agent 的内存,这使得对话可以继续。
406
+
407
+ 您也可以在其他 agent 化应用程序中使用此 `reset=False` 参数来保持对话继续。
408
+
409
+ ## 下一步
410
+
411
+ 最后,当您按需配置好agent后,即可将其分享至 Hub!
412
+
413
+ ```py
414
+ agent.push_to_hub("m-ric/my_agent")
415
+ ```
416
+
417
+ 类似地,若要加载已推送至 Hub 的agent,在信任其工具代码的前提下,可使用:
418
+
419
+ ```py
420
+ agent.from_hub("m-ric/my_agent", trust_remote_code=True)
421
+ ```
422
+
423
+ 要更深入地使用,您将需要查看我们的教程:
424
+ - [我们的代码 agent 如何工作的解释](./tutorials/secure_code_execution)
425
+ - [本指南关于如何构建好的 agent](./tutorials/building_good_agents)。
426
+ - [工具使用的深入指南](./tutorials/tools)。
docs/source/zh/index.md ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # `smolagents`
2
+
3
+ 这是构建强大 agent 的最简单框架!顺便问一下,什么是 "agent"?我们在[此页面](conceptual_guides/intro_agents)提供了我们的定义,您还可以找到关于何时使用或不使用它们的建议(剧透:通常不使用 agent 会更好)。
4
+
5
+ > [!TIP]
6
+ > 译者注:Agent 的业内术语是“智能体”。本译文将保留 agent,不作翻译,以带来更高效的阅读体验。(在中文为主的文章中,It's easier to 注意到英文。Attention Is All You Need!)
7
+
8
+ 本库提供:
9
+
10
+ ✨ **简洁性**:Agent 逻辑仅需约千行代码。我们将抽象保持在原始代码之上的最小形态!
11
+
12
+ 🌐 **支持任何 LLM**:支持通过 Hub 托管的模型,使用其 `transformers` 版本或通过我们的推理 API 加载,也支持 OpenAI、Anthropic 等模型。使用任何 LLM 为 agent 提供动力都非常容易。
13
+
14
+ 🧑‍💻 **一流的代码 agent 支持**,即编写代码作为其操作的 agent(与"用于编写代码的 agent"相对),[在此了解更多](tutorials/secure_code_execution)。
15
+
16
+ 🤗 **Hub 集成**:您可以在 Hub 上共享和加载工具,更多功能即将推出!
17
+
18
+ <div class="mt-10">
19
+ <div class="w-full flex flex-col space-y-4 md:space-y-0 md:grid md:grid-cols-2 md:gap-y-4 md:gap-x-5">
20
+ <a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./guided_tour"
21
+ ><div class="w-full text-center bg-gradient-to-br from-blue-400 to-blue-500 rounded-lg py-1.5 font-semibold mb-5 text-white text-lg leading-relaxed">导览</div>
22
+ <p class="text-gray-700">学习基础知识并熟悉使用 agent。如果您是第一次使用 agent,请从这里开始!</p>
23
+ </a>
24
+ <a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./examples/text_to_sql"
25
+ ><div class="w-full text-center bg-gradient-to-br from-indigo-400 to-indigo-500 rounded-lg py-1.5 font-semibold mb-5 text-white text-lg leading-relaxed">操作指南</div>
26
+ <p class="text-gray-700">实用指南,帮助您实现特定目标:创建一个生成和测试 SQL 查询的 agent!</p>
27
+ </a>
28
+ <a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./conceptual_guides/intro_agents"
29
+ ><div class="w-full text-center bg-gradient-to-br from-pink-400 to-pink-500 rounded-lg py-1.5 font-semibold mb-5 text-white text-lg leading-relaxed">概念指南</div>
30
+ <p class="text-gray-700">高级解释,帮助您更好地理解重要主题。</p>
31
+ </a>
32
+ <a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./tutorials/building_good_agents"
33
+ ><div class="w-full text-center bg-gradient-to-br from-purple-400 to-purple-500 rounded-lg py-1.5 font-semibold mb-5 text-white text-lg leading-relaxed">教程</div>
34
+ <p class="text-gray-700">涵盖构建 agent 重要方面的横向教程。</p>
35
+ </a>
36
+ </div>
37
+ </div>