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smolagents 是一个实验性 API,其可能会随时发生更改。由于 API 或底层模型可能会变化,智能体返回的结果可能会有所不同。
|
| 6 |
+
|
| 7 |
+
</Tip>
|
| 8 |
+
|
| 9 |
+
要了解有关智能体和工具的更多信息,请务必阅读[入门指南](../index)。此页面包含底层类的 API 文档。
|
| 10 |
+
|
| 11 |
+
## 模型
|
| 12 |
+
|
| 13 |
+
您可以自由创建和使用自己的模型为智能体提供支持。
|
| 14 |
+
|
| 15 |
+
您可以使用任何 `model` 可调用对象作为智能体的模型,只要满足以下条件:
|
| 16 |
+
1. 它遵循[消息格式](./chat_templating)(`List[Dict[str, str]]`),将其作为输入 `messages`,并返回一个 `str`。
|
| 17 |
+
2. 它在生成的序列到达 `stop_sequences` 参数中指定的内容之前停止生成输出。
|
| 18 |
+
|
| 19 |
+
要定义您的 LLM,可以创建一个 `custom_model` 方法,该方法接受一个 [messages](./chat_templating) 列表,并返回一个包含 `.content` 属性的对象,其中包含生成的文本。此可调用对象还需要接受一个 `stop_sequences` 参数,用于指示何时停止生成。
|
| 20 |
+
|
| 21 |
+
```python
|
| 22 |
+
from huggingface_hub import login, InferenceClient
|
| 23 |
+
|
| 24 |
+
login("<YOUR_HUGGINGFACEHUB_API_TOKEN>")
|
| 25 |
+
|
| 26 |
+
model_id = "meta-llama/Llama-3.3-70B-Instruct"
|
| 27 |
+
|
| 28 |
+
client = InferenceClient(model=model_id)
|
| 29 |
+
|
| 30 |
+
def custom_model(messages, stop_sequences=["Task"]):
|
| 31 |
+
response = client.chat_completion(messages, stop=stop_sequences, max_tokens=1000)
|
| 32 |
+
answer = response.choices[0].message
|
| 33 |
+
return answer
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
此外,`custom_model` 还可以接受一个 `grammar` 参数。如果在智能体初始化时指定了 `grammar`,则此参数将在调用模型时传递,以便进行[约束生成](https://huggingface.co/docs/text-generation-inference/conceptual/guidance),从而强制生成格式正确的智能体输出。
|
| 37 |
+
|
| 38 |
+
### TransformersModel
|
| 39 |
+
|
| 40 |
+
为了方便起见,我们添加了一个 `TransformersModel`,该模型通过为初始化时指定的 `model_id` 构建一个本地 `transformers` pipeline 来实现上述功能。
|
| 41 |
+
|
| 42 |
+
```python
|
| 43 |
+
from smolagents import TransformersModel
|
| 44 |
+
|
| 45 |
+
model = TransformersModel(model_id="HuggingFaceTB/SmolLM-135M-Instruct")
|
| 46 |
+
|
| 47 |
+
print(model([{"role": "user", "content": [{"type": "text", "text": "Ok!"}]}], stop_sequences=["great"]))
|
| 48 |
+
```
|
| 49 |
+
```text
|
| 50 |
+
>>> What a
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
> [!TIP]
|
| 54 |
+
> 您必须在机器上安装 `transformers` 和 `torch`。如果尚未安装,请运行 `pip install smolagents[transformers]`。
|
| 55 |
+
|
| 56 |
+
[[autodoc]] TransformersModel
|
| 57 |
+
|
| 58 |
+
### InferenceClientModel
|
| 59 |
+
|
| 60 |
+
`InferenceClientModel` 封装了 huggingface_hub 的 [InferenceClient](https://huggingface.co/docs/huggingface_hub/main/en/guides/inference),用于执行 LLM。它支持 HF 的 [Inference API](https://huggingface.co/docs/api-inference/index) 以及 Hub 上所有可用的[Inference Providers](https://huggingface.co/blog/inference-providers)。
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
from smolagents import InferenceClientModel
|
| 64 |
+
|
| 65 |
+
messages = [
|
| 66 |
+
{"role": "user", "content": [{"type": "text", "text": "Hello, how are you?"}]}
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
model = InferenceClientModel()
|
| 70 |
+
print(model(messages))
|
| 71 |
+
```
|
| 72 |
+
```text
|
| 73 |
+
>>> Of course! If you change your mind, feel free to reach out. Take care!
|
| 74 |
+
```
|
| 75 |
+
[[autodoc]] InferenceClientModel
|
| 76 |
+
|
| 77 |
+
### LiteLLMModel
|
| 78 |
+
|
| 79 |
+
`LiteLLMModel` 利用 [LiteLLM](https://www.litellm.ai/) 支持来自不同提供商的 100+ 个 LLM。您可以在模型初始化时传递 `kwargs`,这些参数将在每次使用模型时被使用,例如下面的示例中传递了 `temperature`。
|
| 80 |
+
|
| 81 |
+
```python
|
| 82 |
+
from smolagents import LiteLLMModel
|
| 83 |
+
|
| 84 |
+
messages = [
|
| 85 |
+
{"role": "user", "content": [{"type": "text", "text": "Hello, how are you?"}]}
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
model = LiteLLMModel(model_id="anthropic/claude-3-5-sonnet-latest", temperature=0.2, max_tokens=10)
|
| 89 |
+
print(model(messages))
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
[[autodoc]] LiteLLMModel
|
| 93 |
+
|
| 94 |
+
### OpenAIServerModel
|
| 95 |
+
|
| 96 |
+
此类允许您调用任何 OpenAIServer 兼容模型。
|
| 97 |
+
以下是设置方法(您可以自定义 `api_base` URL 指向其他服务器):
|
| 98 |
+
```py
|
| 99 |
+
import os
|
| 100 |
+
from smolagents import OpenAIServerModel
|
| 101 |
+
|
| 102 |
+
model = OpenAIServerModel(
|
| 103 |
+
model_id="gpt-4o",
|
| 104 |
+
api_base="https://api.openai.com/v1",
|
| 105 |
+
api_key=os.environ["OPENAI_API_KEY"],
|
| 106 |
+
)
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
[[autodoc]] OpenAIServerModel
|
| 110 |
+
|
| 111 |
+
### AzureOpenAIServerModel
|
| 112 |
+
|
| 113 |
+
`AzureOpenAIServerModel` 允许您连接到任何 Azure OpenAI 部署。
|
| 114 |
+
|
| 115 |
+
下面是设置示例,请注意,如果已经设置了相应的环境变量,您可以省略 `azure_endpoint`、`api_key` 和 `api_version` 参数——环境变量包括 `AZURE_OPENAI_ENDPOINT`、`AZURE_OPENAI_API_KEY` 和 `OPENAI_API_VERSION`。
|
| 116 |
+
|
| 117 |
+
请注意,`OPENAI_API_VERSION` 没有 `AZURE_` 前缀,这是由于底层 [openai](https://github.com/openai/openai-python) 包的设计所致。
|
| 118 |
+
|
| 119 |
+
```py
|
| 120 |
+
import os
|
| 121 |
+
|
| 122 |
+
from smolagents import AzureOpenAIServerModel
|
| 123 |
+
|
| 124 |
+
model = AzureOpenAIServerModel(
|
| 125 |
+
model_id = os.environ.get("AZURE_OPENAI_MODEL"),
|
| 126 |
+
azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
|
| 127 |
+
api_key=os.environ.get("AZURE_OPENAI_API_KEY"),
|
| 128 |
+
api_version=os.environ.get("OPENAI_API_VERSION")
|
| 129 |
+
)
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
[[autodoc]] AzureOpenAIServerModel
|
| 133 |
+
|
| 134 |
+
### MLXModel
|
| 135 |
+
|
| 136 |
+
```python
|
| 137 |
+
from smolagents import MLXModel
|
| 138 |
+
|
| 139 |
+
model = MLXModel(model_id="HuggingFaceTB/SmolLM-135M-Instruct")
|
| 140 |
+
|
| 141 |
+
print(model([{"role": "user", "content": "Ok!"}], stop_sequences=["great"]))
|
| 142 |
+
```
|
| 143 |
+
```text
|
| 144 |
+
>>> What a
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
> [!TIP]
|
| 148 |
+
> 您必须在机器上安装 `mlx-lm`。如果尚未安装,请运行 `pip install smolagents[mlx-lm]`。
|
| 149 |
+
|
| 150 |
+
[[autodoc]] MLXModel
|
docs/source/zh/reference/tools.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 工具
|
| 2 |
+
|
| 3 |
+
<Tip warning={true}>
|
| 4 |
+
|
| 5 |
+
Smolagents 是一个实验性 API,可能会随时更改。由于 API 或底层模型可能发生变化,代理返回的结果可能会有所不同。
|
| 6 |
+
|
| 7 |
+
</Tip>
|
| 8 |
+
|
| 9 |
+
要了解更多关于智能体和工具的信息,请务必阅读[入门指南](../index)。本页面包含底层类的 API 文档。
|
| 10 |
+
|
| 11 |
+
## 工具
|
| 12 |
+
|
| 13 |
+
### load_tool
|
| 14 |
+
|
| 15 |
+
[[autodoc]] load_tool
|
| 16 |
+
|
| 17 |
+
### tool
|
| 18 |
+
|
| 19 |
+
[[autodoc]] tool
|
| 20 |
+
|
| 21 |
+
### Tool
|
| 22 |
+
|
| 23 |
+
[[autodoc]] Tool
|
| 24 |
+
|
| 25 |
+
### launch_gradio_demo
|
| 26 |
+
|
| 27 |
+
[[autodoc]] launch_gradio_demo
|
| 28 |
+
|
| 29 |
+
## 默认工具
|
| 30 |
+
|
| 31 |
+
### PythonInterpreterTool
|
| 32 |
+
|
| 33 |
+
[[autodoc]] PythonInterpreterTool
|
| 34 |
+
|
| 35 |
+
### FinalAnswerTool
|
| 36 |
+
|
| 37 |
+
[[autodoc]] FinalAnswerTool
|
| 38 |
+
|
| 39 |
+
### UserInputTool
|
| 40 |
+
|
| 41 |
+
[[autodoc]] UserInputTool
|
| 42 |
+
|
| 43 |
+
### DuckDuckGoSearchTool
|
| 44 |
+
|
| 45 |
+
[[autodoc]] DuckDuckGoSearchTool
|
| 46 |
+
|
| 47 |
+
### GoogleSearchTool
|
| 48 |
+
|
| 49 |
+
[[autodoc]] GoogleSearchTool
|
| 50 |
+
|
| 51 |
+
### VisitWebpageTool
|
| 52 |
+
|
| 53 |
+
[[autodoc]] VisitWebpageTool
|
| 54 |
+
|
| 55 |
+
### SpeechToTextTool
|
| 56 |
+
|
| 57 |
+
[[autodoc]] SpeechToTextTool
|
| 58 |
+
|
| 59 |
+
## 工具集合
|
| 60 |
+
|
| 61 |
+
[[autodoc]] ToolCollection
|
| 62 |
+
|
| 63 |
+
## 智能体类型
|
| 64 |
+
|
| 65 |
+
智能体可以处理工具之间的任何类型的对象;工具是完全多模态的,可以接受和返回文本、图像、音频、视频以及其他类型的对象。为了增加工具之间的兼容性,以及正确呈现在 ipython(jupyter、colab、ipython notebooks 等)中的返回结果,我们为这些类型实现了包装类。
|
| 66 |
+
|
| 67 |
+
被包装的对象应该继续保持其初始行为;例如,一个文本对象应继续表现为字符串,一个图像对象应继续表现为 `PIL.Image`。
|
| 68 |
+
|
| 69 |
+
这些类型有三个特定的用途:
|
| 70 |
+
|
| 71 |
+
- 调用 `to_raw` 方法时,应返回底层对象
|
| 72 |
+
- 调用 `to_string` 方法时,应将对象转换为字符串:对于 `AgentText` 类型,可以直接返回字符串;对于其他实例,则返回对象序列化版本的路径
|
| 73 |
+
- 在 ipython 内核中显示时,应正确显示对象
|
| 74 |
+
|
| 75 |
+
### AgentText
|
| 76 |
+
|
| 77 |
+
[[autodoc]] smolagents.agent_types.AgentText
|
| 78 |
+
|
| 79 |
+
### AgentImage
|
| 80 |
+
|
| 81 |
+
[[autodoc]] smolagents.agent_types.AgentImage
|
| 82 |
+
|
| 83 |
+
### AgentAudio
|
| 84 |
+
|
| 85 |
+
[[autodoc]] smolagents.agent_types.AgentAudio
|