Skip to content

Commit c0496dc

Browse files
authored
[i18n] Add Chinese translation(zh) for smolagents (#156)
* Add Chinese version (`zh`) of the documentation for smolagents
1 parent 60b1abd commit c0496dc

14 files changed

+2013
-0
lines changed

docs/source/zh/_config.py

+14
Original file line numberDiff line numberDiff line change
@@ -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

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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/tools
12+
title: 🛠️ 工具 - 深度指南
13+
- local: tutorials/secure_code_execution
14+
title: 🛡️ 使用 E2B 保护你的代码执行
15+
- title: Conceptual guides
16+
sections:
17+
- local: conceptual_guides/intro_agents
18+
title: 🤖 Agent 化系统介绍
19+
- local: conceptual_guides/react
20+
title: 🤔 多步骤 Agent 是如何工作的?
21+
- title: Examples
22+
sections:
23+
- local: examples/text_to_sql
24+
title: Self-correcting Text-to-SQL
25+
- local: examples/rag
26+
title: Master you knowledge base with agentic RAG
27+
- local: examples/multiagents
28+
title: Orchestrate a multi-agent system
29+
- title: Reference
30+
sections:
31+
- local: reference/agents
32+
title: Agent-related objects
33+
- local: reference/tools
34+
title: Tool-related objects
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!--Copyright 2024 The HuggingFace Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
12+
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
13+
rendered properly in your Markdown viewer.
14+
15+
-->
16+
17+
# Agent 简介
18+
19+
> [!TIP]
20+
> 译者注:Agent 的业内术语是“智能体”。本译文将保留 agent,不作翻译,以带来更高效的阅读体验。(在中文为主的文章中,It's easier to 注意到英文。Attention Is All You Need!)
21+
22+
## 🤔 什么是 agent?
23+
24+
任何使用 AI 的高效系统都需要为 LLM 提供某种访问现实世界的方式:例如调用搜索工具获取外部信息,或者操作某些程序以完成任务。换句话说,LLM 应该具有 **_Agent 能力_**。Agent 程序是 LLM 通往外部世界的门户。
25+
26+
> [!TIP]
27+
> AI agent 是 **LLM 输出控制工作流的程序**
28+
29+
任何利用 LLM 的系统都会将 LLM 输出集成到代码中。LLM 输入对代码工作流的影响程度就是 LLM 在系统中的 agent 能力级别。
30+
31+
请注意,根据这个定义,"Agent" 不是一个离散的、非 0 即 1 的定义:相反,"Agent 能力" 是一个连续谱系,随着你在工作流中给予 LLM 更多或更少的权力而变化。
32+
33+
请参见下表中 agent 能力在不同系统中的变化:
34+
35+
| Agent 能力级别 | 描述 | 名称 | 示例模式 |
36+
| ------------ | ---------------------------------------------- | ---------- | -------------------------------------------------- |
37+
| ☆☆☆ | LLM 输出对程序流程没有影响 | 简单处理器 | `process_llm_output(llm_response)` |
38+
| ★☆☆ | LLM 输出决定 if/else 分支 | 路由 | `if llm_decision(): path_a() else: path_b()` |
39+
| ★★☆ | LLM 输出决定函数执行 | 工具调用者 | `run_function(llm_chosen_tool, llm_chosen_args)` |
40+
| ★★★ | LLM 输出控制迭代和程序继续 | 多步 Agent | `while llm_should_continue(): execute_next_step()` |
41+
| ★★★ | 一个 agent 工作流可以启动另一个 agent 工作流 | 多 Agent | `if llm_trigger(): execute_agent()` |
42+
43+
多步 agent 具有以下代码结构:
44+
45+
```python
46+
memory = [user_defined_task]
47+
while llm_should_continue(memory): # 这个循环是多步部分
48+
action = llm_get_next_action(memory) # 这是工具调用部分
49+
observations = execute_action(action)
50+
memory += [action, observations]
51+
```
52+
53+
这个 agent 系统在一个循环中运行,每一步执行一个新动作(该动作可能涉及调用一些预定义的 *工具*,这些工具只是函数),直到其观察结果表明已达到解决给定任务的满意状态。以下是一个多步 agent 如何解决简单数学问题的示例:
54+
55+
<div class="flex justify-center">
56+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/Agent_ManimCE.gif"/>
57+
</div>
58+
59+
## ✅ 何时使用 agent / ⛔ 何时避免使用
60+
61+
当你需要 LLM 确定应用程序的工作流时,agent 很有用。但它们通常有些过度。问题是:我真的需要工作流的灵活性来有效解决手头的任务吗?
62+
如果预定义的工作流经常不足,这意味着你需要更多的灵活性。
63+
让我们举个例子:假设你正在开发一个处理冲浪旅行网站客户请求的应用程序。
64+
65+
你可以提前知道请求将属于 2 个类别之一(基于用户选择),并且你为这 2 种情况都有预定义的工作流。
66+
67+
1. 想要了解旅行信息?⇒ 给他们访问搜索栏以搜索你的知识库
68+
2. 想与销售交谈?⇒ 让他们填写联系表单。
69+
70+
如果这个确定性工作流适合所有查询,那就直接编码吧!这将为你提供一个 100% 可靠的系统,没有让不可预测的 LLM 干扰你的工作流而引入错误的风险。为了简单和稳健起见,建议规范化不使用任何 agent 行为。
71+
72+
但如果工作流不能提前确定得那么好呢?
73+
74+
例如,用户想问:`"I can come on Monday, but I forgot my passport so risk being delayed to Wednesday, is it possible to take me and my stuff to surf on Tuesday morning, with a cancellation insurance?"` 这个问题涉及许多因素,可能上述预定的标准都不足以满足这个请求。
75+
76+
如果预定义的工作流经常不足,这意味着你需要更多的灵活性。
77+
78+
这就是 agent 设置发挥作用的地方。
79+
80+
在上面的例子中,你可以创建一个多步 agent,它可以访问天气 API 获取天气预报,Google Maps API 计算旅行距离,员工在线仪表板和你的知识库上的 RAG 系统。
81+
82+
直到最近,计算机程序还局限于预定义的工作流,试图通过堆积 if/else 分支来处理复杂性。它们专注于极其狭窄的任务,如"计算这些数字的总和"或"找到这个图中的最短路径"。但实际上,大多数现实生活中的任务,如我们上面的旅行示例,都不适合预定义的工作流。agent 系统为程序打开了现实世界任务的大门!
83+
84+
## 为什么选择 `smolagents`
85+
86+
对于一些低级的 agent 用例,如链或路由器,你可以自己编写所有代码。这样会更好,因为它可以让你更好地控制和理解你的系统。
87+
88+
但一旦你开始追求更复杂的行为,比如让 LLM 调用函数(即"工具调用")或让 LLM 运行 while 循环("多步 agent"),一些抽象就变得必要:
89+
90+
- 对于工具调用,你需要解析 agent 的输出,因此这个输出需要一个预定义的格式,如"Thought: I should call tool 'get_weather'. Action: get_weather(Paris).",你用预定义的函数解析它,并且给 LLM 的系统提示应该通知它这个格式。
91+
- 对于 LLM 输出决定循环的多步 agent,你需要根据上次循环迭代中发生的情况给 LLM 不同的提示:所以你需要某种记忆能力。
92+
93+
看到了吗?通过这两个例子,我们已经发现需要一些项目来帮助我们:
94+
95+
- 当然,一个作为系统引擎的 LLM
96+
- agent 可以访问的工具列表
97+
- 从 LLM 输出中提取工具调用的解析器
98+
- 与解析器同步的系统提示
99+
- 记忆能力
100+
101+
但是等等,既然我们给 LLM 在决策中留出了空间,它们肯定会犯错误:所以我们需要错误日志记录和重试机制。
102+
103+
所有这些元素都需要紧密耦合才能形成一个功能良好的系统。这就是为什么我们决定需要制作基本构建块来让所有这些东西协同工作。
104+
105+
## 代码 agent
106+
107+
在多步 agent 中,每一步 LLM 都可以编写一个动作,形式为调用外部工具。编写这些动作的常见格式(由 Anthropic、OpenAI 等使用)通常是"将动作编写为工具名称和要使用的参数的 JSON,然后解析以知道要执行哪个工具以及使用哪些参数"的不同变体。
108+
109+
[多项](https://huggingface.co/papers/2402.01030) [研究](https://huggingface.co/papers/2411.01747) [论文](https://huggingface.co/papers/2401.00812) 表明,在代码中进行工具调用的 LLM 要好得多。
110+
111+
原因很简单,_我们专门设计了我们的代码语言,使其成为表达计算机执行动作的最佳方式_。如果 JSON 片段是更好的表达方式,JSON 将成为顶级编程语言,编程将变得非常困难。
112+
113+
下图取自 [Executable Code Actions Elicit Better LLM Agents](https://huggingface.co/papers/2402.01030),说明了用代码编写动作的一些优势:
114+
115+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/code_vs_json_actions.png">
116+
117+
与 JSON 片段相比,用代码编写动作提供了更好的:
118+
119+
- **可组合性:** 你能像定义 python 函数一样,将 JSON 动作嵌套在一起,或定义一组 JSON 动作以供重用吗?
120+
- **对象管理:** 你如何在 JSON 中存储像 `generate_image` 这样的动作的输出?
121+
- **通用性:** 代码被构建为简单地表达任何你可以让计算机做的事情。
122+
- **LLM 训练数据中的表示:** 大量高质量的代码动作已经包含在 LLM 的训练数据中,这意味着它们已经为此进行了训练!
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!--Copyright 2024 The HuggingFace Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
12+
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
13+
rendered properly in your Markdown viewer.
14+
15+
-->
16+
# 多步骤 agent 是如何工作的?
17+
18+
ReAct 框架([Yao et al., 2022](https://huggingface.co/papers/2210.03629))是目前构建 agent 的主要方法。
19+
20+
该名称基于两个词的组合:"Reason" (推理)和 "Act" (行动)。实际上,遵循此架构的 agent 将根据需要尽可能多的步骤来解决其任务,每个步骤包括一个推理步骤,然后是一个行动步骤,在该步骤中,它制定工具调用,使其更接近解决手头的任务。
21+
22+
ReAct 过程涉及保留过去步骤的记忆。
23+
24+
> [!TIP]
25+
> 阅读 [Open-source LLMs as LangChain Agents](https://huggingface.co/blog/open-source-llms-as-agents) 博客文章以了解更多关于多步 agent 的信息。
26+
27+
以下是其工作原理的视频概述:
28+
29+
<div class="flex justify-center">
30+
<img
31+
class="block dark:hidden"
32+
src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/Agent_ManimCE.gif"
33+
/>
34+
<img
35+
class="hidden dark:block"
36+
src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/Agent_ManimCE.gif"
37+
/>
38+
</div>
39+
40+
![ReAct agent 的框架](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/open-source-llms-as-agents/ReAct.png)
41+
42+
我们实现了两个版本的 ToolCallingAgent:
43+
- [`ToolCallingAgent`] 在其输出中生成 JSON 格式的工具调用。
44+
- [`CodeAgent`] 是一种新型的 ToolCallingAgent,它生成代码块形式的工具调用,这对于具有强大编码性能的 LLM 非常有效。
45+
46+
> [!TIP]
47+
> 我们还提供了一个选项来以单步模式运行 agent:只需在启动 agent 时传递 `single_step=True`,例如 `agent.run(your_task, single_step=True)`

0 commit comments

Comments
 (0)