MaoShen's picture
Upload folder using huggingface_hub
2eb41d7 verified
<!--Copyright 2024 The HuggingFace Team. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
-->
# Agents - ๅฏผ่งˆ
[[open-in-colab]]
ๅœจๆœฌๅฏผ่งˆไธญ๏ผŒๆ‚จๅฐ†ๅญฆไน ๅฆ‚ไฝ•ๆž„ๅปบไธ€ไธช agent๏ผˆๆ™บ่ƒฝไฝ“๏ผ‰๏ผŒๅฆ‚ไฝ•่ฟ่กŒๅฎƒ๏ผŒไปฅๅŠๅฆ‚ไฝ•่‡ชๅฎšไน‰ๅฎƒไปฅไฝฟๅ…ถๆ›ดๅฅฝๅœฐ้€‚ๅบ”ๆ‚จ็š„ไฝฟ็”จๅœบๆ™ฏใ€‚
> [!TIP]
> ่ฏ‘่€…ๆณจ๏ผšAgent ็š„ไธšๅ†…ๆœฏ่ฏญๆ˜ฏโ€œๆ™บ่ƒฝไฝ“โ€ใ€‚ๆœฌ่ฏ‘ๆ–‡ๅฐ†ไฟ็•™ agent๏ผŒไธไฝœ็ฟป่ฏ‘๏ผŒไปฅๅธฆๆฅๆ›ด้ซ˜ๆ•ˆ็š„้˜…่ฏปไฝ“้ชŒใ€‚(ๅœจไธญๆ–‡ไธบไธป็š„ๆ–‡็ซ ไธญ๏ผŒIt's easier to ๆณจๆ„ๅˆฐ่‹ฑๆ–‡ใ€‚Attention Is All You Need!)
> [!TIP]
> ไธญๆ–‡็คพๅŒบๅ‘ๅธƒไบ†ๅ…ณไบŽ smolagents ็š„ไป‹็ปๅ’Œๅฎž่ทต่ฎฒ่งฃ่ง†้ข‘(ๆฅๆบ๏ผš[Issue#80](https://github.com/huggingface/smolagents/issues/80))๏ผŒไฝ ๅฏไปฅ่ฎฟ้—ฎ[่ฟ™้‡Œ](https://www.youtube.com/watch?v=wwN3oAugc4c)่ฟ›่กŒ่ง‚็œ‹๏ผ
### ๆž„ๅปบๆ‚จ็š„ agent
่ฆๅˆๅง‹ๅŒ–ไธ€ไธชๆœ€ๅฐๅŒ–็š„ agent๏ผŒๆ‚จ่‡ณๅฐ‘้œ€่ฆไปฅไธ‹ไธคไธชๅ‚ๆ•ฐ๏ผš
- `model`๏ผŒไธ€ไธชไธบๆ‚จ็š„ agent ๆไพ›ๅŠจๅŠ›็š„ๆ–‡ๆœฌ็”Ÿๆˆๆจกๅž‹ - ๅ› ไธบ agent ไธŽ็ฎ€ๅ•็š„ LLM ไธๅŒ๏ผŒๅฎƒๆ˜ฏไธ€ไธชไฝฟ็”จ LLM ไฝœไธบๅผ•ๆ“Ž็š„็ณป็ปŸใ€‚ๆ‚จๅฏไปฅไฝฟ็”จไปฅไธ‹ไปปไธ€้€‰้กน๏ผš
- [`TransformersModel`] ไฝฟ็”จ้ข„ๅˆๅง‹ๅŒ–็š„ `transformers` ็ฎก้“ๅœจๆœฌๅœฐๆœบๅ™จไธŠ่ฟ่กŒๆŽจ็†
- [`HfApiModel`] ๅœจๅบ•ๅฑ‚ไฝฟ็”จ `huggingface_hub.InferenceClient`
- [`LiteLLMModel`] ่ฎฉๆ‚จ้€š่ฟ‡ [LiteLLM](https://docs.litellm.ai/) ่ฐƒ็”จ 100+ ไธๅŒ็š„ๆจกๅž‹๏ผ
- `tools`๏ผŒagent ๅฏไปฅ็”จๆฅ่งฃๅ†ณไปปๅŠก็š„ `Tools` ๅˆ—่กจใ€‚ๅฎƒๅฏไปฅๆ˜ฏไธ€ไธช็ฉบๅˆ—่กจใ€‚ๆ‚จ่ฟ˜ๅฏไปฅ้€š่ฟ‡ๅฎšไน‰ๅฏ้€‰ๅ‚ๆ•ฐ `add_base_tools=True` ๅœจๆ‚จ็š„ `tools` ๅˆ—่กจไน‹ไธŠๆทปๅŠ ้ป˜่ฎคๅทฅๅ…ท็ฎฑใ€‚
ไธ€ๆ—ฆๆœ‰ไบ†่ฟ™ไธคไธชๅ‚ๆ•ฐ `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/)ใ€‚
<hfoptions id="้€‰ๆ‹ฉไธ€ไธชLLM">
<hfoption id="Hugging Face API">
Hugging Face API ๅฏไปฅๅ…่ดนไฝฟ็”จ่€Œๆ— ้œ€ token๏ผŒไฝ†ไผšๆœ‰้€Ÿ็އ้™ๅˆถใ€‚
่ฆ่ฎฟ้—ฎๅ—้™ๆจกๅž‹ๆˆ–ไฝฟ็”จ PRO ่ดฆๆˆทๆ้ซ˜้€Ÿ็އ้™ๅˆถ๏ผŒๆ‚จ้œ€่ฆ่ฎพ็ฝฎ็Žฏๅขƒๅ˜้‡ `HF_TOKEN` ๆˆ–ๅœจๅˆๅง‹ๅŒ– `HfApiModel` ๆ—ถไผ ้€’ `token` ๅ˜้‡ใ€‚
```python
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?",
)
```
</hfoption>
<hfoption id="ๆœฌๅœฐTransformersๆจกๅž‹">
```python
# !pip install smolagents[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?",
)
```
</hfoption>
<hfoption id="OpenAIๆˆ–Anthropic API">
่ฆไฝฟ็”จ `LiteLLMModel`๏ผŒๆ‚จ้œ€่ฆ่ฎพ็ฝฎ็Žฏๅขƒๅ˜้‡ `ANTHROPIC_API_KEY` ๆˆ– `OPENAI_API_KEY`๏ผŒๆˆ–่€…ๅœจๅˆๅง‹ๅŒ–ๆ—ถไผ ้€’ `api_key` ๅ˜้‡ใ€‚
```python
# !pip install smolagents[litellm]
from smolagents import CodeAgent, LiteLLMModel
model = LiteLLMModel(model_id="anthropic/claude-3-5-sonnet-latest", api_key="YOUR_ANTHROPIC_API_KEY") # ไนŸๅฏไปฅไฝฟ็”จ 'gpt-4o'
agent = CodeAgent(tools=[], model=model, add_base_tools=True)
agent.run(
"Could you give me the 118th number in the Fibonacci sequence?",
)
```
</hfoption>
<hfoption id="Ollama">
```python
# !pip install smolagents[litellm]
from smolagents import CodeAgent, LiteLLMModel
model = LiteLLMModel(
model_id="ollama_chat/llama3.2", # ่ฟ™ไธชๆจกๅž‹ๅฏนไบŽ agent ่กŒไธบๆฅ่ฏดๆœ‰็‚นๅผฑ
api_base="http://localhost:11434", # ๅฆ‚ๆžœ้œ€่ฆๅฏไปฅๆ›ฟๆขไธบ่ฟœ็จ‹ open-ai ๅ…ผๅฎนๆœๅŠกๅ™จ
api_key="YOUR_API_KEY" # ๅฆ‚ๆžœ้œ€่ฆๅฏไปฅๆ›ฟๆขไธบ API key
num_ctx=8192 # https://huggingface.co/spaces/NyxKrage/LLM-Model-VRAM-Calculator
)
agent = CodeAgent(tools=[], model=model, add_base_tools=True)
agent.run(
"Could you give me the 118th number in the Fibonacci sequence?",
)
```
</hfoption>
</hfoptions>
#### CodeAgent ๅ’Œ ToolCallingAgent
[`CodeAgent`] ๆ˜ฏๆˆ‘ไปฌ็š„้ป˜่ฎค agentใ€‚ๅฎƒๅฐ†ๅœจๆฏไธ€ๆญฅ็ผ–ๅ†™ๅนถๆ‰ง่กŒ Python ไปฃ็ ็‰‡ๆฎตใ€‚
้ป˜่ฎคๆƒ…ๅ†ตไธ‹๏ผŒๆ‰ง่กŒๆ˜ฏๅœจๆ‚จ็š„ๆœฌๅœฐ็ŽฏๅขƒไธญๅฎŒๆˆ็š„ใ€‚
่ฟ™ๅบ”่ฏฅๆ˜ฏๅฎ‰ๅ…จ็š„๏ผŒๅ› ไธบๅ”ฏไธ€ๅฏไปฅ่ฐƒ็”จ็š„ๅ‡ฝๆ•ฐๆ˜ฏๆ‚จๆไพ›็š„ๅทฅๅ…ท๏ผˆ็‰นๅˆซๆ˜ฏๅฆ‚ๆžœๅชๆœ‰ Hugging Face ็š„ๅทฅๅ…ท๏ผ‰ๅ’Œไธ€็ป„้ข„ๅฎšไน‰็š„ๅฎ‰ๅ…จๅ‡ฝๆ•ฐ๏ผŒๅฆ‚ `print` ๆˆ– `math` ๆจกๅ—ไธญ็š„ๅ‡ฝๆ•ฐ๏ผŒๆ‰€ไปฅๆ‚จๅทฒ็ป้™ๅˆถไบ†ๅฏไปฅๆ‰ง่กŒ็š„ๅ†…ๅฎนใ€‚
Python ่งฃ้‡Šๅ™จ้ป˜่ฎคไนŸไธๅ…่ฎธๅœจๅฎ‰ๅ…จๅˆ—่กจไน‹ๅค–ๅฏผๅ…ฅ๏ผŒๆ‰€ไปฅๆ‰€ๆœ‰ๆœ€ๆ˜Žๆ˜พ็š„ๆ”ปๅ‡ป้ƒฝไธๅบ”่ฏฅๆˆไธบ้—ฎ้ข˜ใ€‚
ๆ‚จๅฏไปฅ้€š่ฟ‡ๅœจๅˆๅง‹ๅŒ– [`CodeAgent`] ๆ—ถๅฐ†ๆŽˆๆƒๆจกๅ—ไฝœไธบๅญ—็ฌฆไธฒๅˆ—่กจไผ ้€’็ป™ๅ‚ๆ•ฐ `additional_authorized_imports` ๆฅๆŽˆๆƒ้ขๅค–็š„ๅฏผๅ…ฅ๏ผš
```py
from smolagents import CodeAgent
agent = CodeAgent(tools=[], model=model, additional_authorized_imports=['requests', 'bs4'])
agent.run("Could you get me the title of the page at url 'https://huggingface.co/blog'?")
```
> [!WARNING]
> LLM ๅฏไปฅ็”Ÿๆˆไปปๆ„ไปฃ็ ็„ถๅŽๆ‰ง่กŒ๏ผšไธ่ฆๆทปๅŠ ไปปไฝ•ไธๅฎ‰ๅ…จ็š„ๅฏผๅ…ฅ๏ผ
ๅฆ‚ๆžœ็”Ÿๆˆ็š„ไปฃ็ ๅฐ่ฏ•ๆ‰ง่กŒ้žๆณ•ๆ“ไฝœๆˆ–ๅ‡บ็Žฐๅธธ่ง„ Python ้”™่ฏฏ๏ผŒๆ‰ง่กŒๅฐ†ๅœๆญขใ€‚
ๆ‚จไนŸๅฏไปฅไฝฟ็”จ [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"`ใ€‚
> [!TIP]
> ๅœจ [่ฏฅๆ•™็จ‹ไธญ](tutorials/secure_code_execution) ไบ†่งฃๆ›ดๅคšๅ…ณไบŽไปฃ็ ๆ‰ง่กŒ็š„ๅ†…ๅฎนใ€‚
ๆˆ‘ไปฌ่ฟ˜ๆ”ฏๆŒๅนฟๆณ›ไฝฟ็”จ็š„ๅฐ†ๅŠจไฝœ็ผ–ๅ†™ไธบ JSON-like ๅ—็š„ๆ–นๅผ๏ผš[`ToolCallingAgent`]๏ผŒๅฎƒ็š„ๅทฅไฝœๆ–นๅผไธŽ [`CodeAgent`] ้žๅธธ็›ธไผผ๏ผŒๅฝ“็„ถๆฒกๆœ‰ `additional_authorized_imports`๏ผŒๅ› ไธบๅฎƒไธๆ‰ง่กŒไปฃ็ ๏ผš
```py
from smolagents import ToolCallingAgent
agent = ToolCallingAgent(tools=[], model=model)
agent.run("Could you get me the title of the page at url 'https://huggingface.co/blog'?")
```
### ๆฃ€ๆŸฅ agent ่ฟ่กŒ
ไปฅไธ‹ๆ˜ฏไธ€ไบ›ๆœ‰็”จ็š„ๅฑžๆ€ง๏ผŒ็”จไบŽๆฃ€ๆŸฅ่ฟ่กŒๅŽๅ‘็”Ÿไบ†ไป€ไนˆ๏ผš
- `agent.logs` ๅญ˜ๅ‚จ agent ็š„็ป†็ฒ’ๅบฆๆ—ฅๅฟ—ใ€‚ๅœจ agent ่ฟ่กŒ็š„ๆฏไธ€ๆญฅ๏ผŒๆ‰€ๆœ‰ๅ†…ๅฎน้ƒฝไผšๅญ˜ๅ‚จๅœจไธ€ไธชๅญ—ๅ…ธไธญ๏ผŒ็„ถๅŽ้™„ๅŠ ๅˆฐ `agent.logs` ไธญใ€‚
- ่ฟ่กŒ `agent.write_memory_to_messages()` ไผšไธบ LLM ๅˆ›ๅปบไธ€ไธช agent ๆ—ฅๅฟ—็š„ๅ†…้ƒจๅ†…ๅญ˜๏ผŒไฝœไธบ่Šๅคฉๆถˆๆฏๅˆ—่กจใ€‚ๆญคๆ–นๆณ•ไผš้ๅކๆ—ฅๅฟ—็š„ๆฏไธ€ๆญฅ๏ผŒๅนถไป…ๅญ˜ๅ‚จๅฎƒๆ„Ÿๅ…ด่ถฃ็š„ๅ†…ๅฎนไฝœไธบๆถˆๆฏ๏ผšไพ‹ๅฆ‚๏ผŒๅฎƒไผšๅฐ†็ณป็ปŸๆ็คบๅ’ŒไปปๅŠกๅญ˜ๅ‚จไธบๅ•็‹ฌ็š„ๆถˆๆฏ๏ผŒ็„ถๅŽๅฏนไบŽๆฏไธ€ๆญฅ๏ผŒๅฎƒไผšๅฐ† LLM ่พ“ๅ‡บๅญ˜ๅ‚จไธบไธ€ๆกๆถˆๆฏ๏ผŒๅทฅๅ…ท่ฐƒ็”จ่พ“ๅ‡บๅญ˜ๅ‚จไธบๅฆไธ€ๆกๆถˆๆฏใ€‚ๅฆ‚ๆžœๆ‚จๆƒณ่ฆๆ›ด้ซ˜็บงๅˆซ็š„่ง†ๅ›พ - ไฝ†ไธๆ˜ฏๆฏไธชๆ—ฅๅฟ—้ƒฝไผš่ขซๆญคๆ–นๆณ•่ฝฌๅฝ•ใ€‚
## ๅทฅๅ…ท
ๅทฅๅ…ทๆ˜ฏ agent ไฝฟ็”จ็š„ๅŽŸๅญๅ‡ฝๆ•ฐใ€‚ไธบไบ†่ขซ LLM ไฝฟ็”จ๏ผŒๅฎƒ่ฟ˜้œ€่ฆไธ€ไบ›ๆž„ๆˆๅ…ถ API ็š„ๅฑžๆ€ง๏ผŒ่ฟ™ไบ›ๅฑžๆ€งๅฐ†็”จไบŽๅ‘ LLM ๆ่ฟฐๅฆ‚ไฝ•่ฐƒ็”จๆญคๅทฅๅ…ท๏ผš
- ๅ็งฐ
- ๆ่ฟฐ
- ่พ“ๅ…ฅ็ฑปๅž‹ๅ’Œๆ่ฟฐ
- ่พ“ๅ‡บ็ฑปๅž‹
ไพ‹ๅฆ‚๏ผŒๆ‚จๅฏไปฅๆŸฅ็œ‹ [`PythonInterpreterTool`]๏ผšๅฎƒๆœ‰ไธ€ไธชๅ็งฐใ€ๆ่ฟฐใ€่พ“ๅ…ฅๆ่ฟฐใ€่พ“ๅ‡บ็ฑปๅž‹ๅ’Œไธ€ไธชๆ‰ง่กŒๆ“ไฝœ็š„ `forward` ๆ–นๆณ•ใ€‚
ๅฝ“ agent ๅˆๅง‹ๅŒ–ๆ—ถ๏ผŒๅทฅๅ…ทๅฑžๆ€ง็”จไบŽ็”Ÿๆˆๅทฅๅ…ทๆ่ฟฐ๏ผŒ่ฏฅๆ่ฟฐ่ขซๅตŒๅ…ฅๅˆฐ agent ็š„็ณป็ปŸๆ็คบไธญใ€‚่ฟ™่ฎฉ agent ็Ÿฅ้“ๅฎƒๅฏไปฅไฝฟ็”จๅ“ชไบ›ๅทฅๅ…ทไปฅๅŠไธบไป€ไนˆใ€‚
### ้ป˜่ฎคๅทฅๅ…ท็ฎฑ
`smolagents` ้™„ๅธฆไบ†ไธ€ไธช็”จไบŽๅขžๅผบ agent ็š„้ป˜่ฎคๅทฅๅ…ท็ฎฑ๏ผŒๆ‚จๅฏไปฅๅœจๅˆๅง‹ๅŒ–ๆ—ถ้€š่ฟ‡ๅ‚ๆ•ฐ `add_base_tools = True` ๅฐ†ๅ…ถๆทปๅŠ ๅˆฐๆ‚จ็š„ agent ไธญ๏ผš
- **DuckDuckGo ็ฝ‘้กตๆœ็ดข**๏ผšไฝฟ็”จ DuckDuckGo ๆต่งˆๅ™จๆ‰ง่กŒ็ฝ‘้กตๆœ็ดขใ€‚
- **Python ไปฃ็ ่งฃ้‡Šๅ™จ**๏ผšๅœจๅฎ‰ๅ…จ็Žฏๅขƒไธญ่ฟ่กŒ LLM ็”Ÿๆˆ็š„ Python ไปฃ็ ใ€‚ๅชๆœ‰ๅœจไฝฟ็”จ `add_base_tools=True` ๅˆๅง‹ๅŒ– [`ToolCallingAgent`] ๆ—ถๆ‰ไผšๆทปๅŠ ๆญคๅทฅๅ…ท๏ผŒๅ› ไธบๅŸบไบŽไปฃ็ ็š„ agent ๅทฒ็ปๅฏไปฅๅŽŸ็”Ÿๆ‰ง่กŒ Python ไปฃ็ 
- **่ฝฌๅฝ•ๅ™จ**๏ผšๅŸบไบŽ Whisper-Turbo ๆž„ๅปบ็š„่ฏญ้Ÿณ่ฝฌๆ–‡ๆœฌ็ฎก้“๏ผŒๅฐ†้Ÿณ้ข‘่ฝฌๅฝ•ไธบๆ–‡ๆœฌใ€‚
ๆ‚จๅฏไปฅ้€š่ฟ‡่ฐƒ็”จ [`load_tool`] ๅ‡ฝๆ•ฐๅ’Œ่ฆๆ‰ง่กŒ็š„ไปปๅŠกๆ‰‹ๅŠจไฝฟ็”จๅทฅๅ…ทใ€‚
```python
from smolagents import DuckDuckGoSearchTool
search_tool = DuckDuckGoSearchTool()
print(search_tool("Who's the current president of Russia?"))
```
### ๅˆ›ๅปบไธ€ไธชๆ–ฐๅทฅๅ…ท
ๆ‚จๅฏไปฅๅˆ›ๅปบ่‡ชๅทฑ็š„ๅทฅๅ…ท๏ผŒ็”จไบŽ Hugging Face ้ป˜่ฎคๅทฅๅ…ทๆœชๆถต็›–็š„็”จไพ‹ใ€‚
ไพ‹ๅฆ‚๏ผŒ่ฎฉๆˆ‘ไปฌๅˆ›ๅปบไธ€ไธชๅทฅๅ…ท๏ผŒ่ฟ”ๅ›ž Hub ไธŠ็ป™ๅฎšไปปๅŠกไธ‹่ฝฝ้‡ๆœ€ๅคš็š„ๆจกๅž‹ใ€‚
ๆ‚จๅฐ†ไปŽไปฅไธ‹ไปฃ็ ๅผ€ๅง‹ใ€‚
```python
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)
```
่ฟ™ๆฎตไปฃ็ ๅฏไปฅ้€š่ฟ‡ๅฐ†ๅ…ถๅŒ…่ฃ…ๅœจไธ€ไธชๅ‡ฝๆ•ฐไธญๅนถๆทปๅŠ  `tool` ่ฃ…้ฅฐๅ™จๅฟซ้€Ÿ่ฝฌๆขไธบๅทฅๅ…ท๏ผš
่ฟ™ไธๆ˜ฏๆž„ๅปบๅทฅๅ…ท็š„ๅ”ฏไธ€ๆ–นๆณ•๏ผšๆ‚จๅฏไปฅ็›ดๆŽฅๅฐ†ๅ…ถๅฎšไน‰ไธบ [`Tool`] ็š„ๅญ็ฑป๏ผŒ่ฟ™ไธบๆ‚จๆไพ›ไบ†ๆ›ดๅคš็š„็ตๆดปๆ€ง๏ผŒไพ‹ๅฆ‚ๅˆๅง‹ๅŒ–้‡ๅž‹็ฑปๅฑžๆ€ง็š„ๅฏ่ƒฝๆ€งใ€‚
่ฎฉๆˆ‘ไปฌ็œ‹็œ‹่ฟ™ไธค็ง้€‰้กน็š„ๅทฅไฝœๅŽŸ็†๏ผš
<hfoptions id="ๆž„ๅปบๅทฅๅ…ท">
<hfoption id="ไฝฟ็”จ@tool่ฃ…้ฅฐไธ€ไธชๅ‡ฝๆ•ฐ">
```py
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
```
่ฏฅๅ‡ฝๆ•ฐ้œ€่ฆ๏ผš
- ไธ€ไธชๆธ…ๆ™ฐ็š„ๅ็งฐใ€‚ๅ็งฐๅบ”่ฏฅ่ถณๅคŸๆ่ฟฐๆญคๅทฅๅ…ท็š„ๅŠŸ่ƒฝ๏ผŒไปฅๅธฎๅŠฉไธบ agent ๆไพ›ๅŠจๅŠ›็š„ LLMใ€‚็”ฑไบŽๆญคๅทฅๅ…ท่ฟ”ๅ›žไปปๅŠกไธ‹่ฝฝ้‡ๆœ€ๅคš็š„ๆจกๅž‹๏ผŒๆˆ‘ไปฌๅฐ†ๅ…ถๅ‘ฝๅไธบ `model_download_tool`ใ€‚
- ่พ“ๅ…ฅๅ’Œ่พ“ๅ‡บ็š„็ฑปๅž‹ๆ็คบ
- ไธ€ไธชๆ่ฟฐ๏ผŒๅ…ถไธญๅŒ…ๆ‹ฌไธ€ไธช 'Args:' ้ƒจๅˆ†๏ผŒๅ…ถไธญๆฏไธชๅ‚ๆ•ฐ้ƒฝ่ขซๆ่ฟฐ๏ผˆ่ฟ™ๆฌกๆฒกๆœ‰็ฑปๅž‹ๆŒ‡็คบ๏ผŒๅฎƒๅฐ†ไปŽ็ฑปๅž‹ๆ็คบไธญๆๅ–๏ผ‰ใ€‚ไธŽๅทฅๅ…ทๅ็งฐไธ€ๆ ท๏ผŒๆญคๆ่ฟฐๆ˜ฏไธบๆ‚จ็š„ agent ๆไพ›ๅŠจๅŠ›็š„ LLM ็š„่ฏดๆ˜Žไนฆ๏ผŒๆ‰€ไปฅไธ่ฆๅฟฝ่ง†ๅฎƒใ€‚
ๆ‰€ๆœ‰่ฟ™ไบ›ๅ…ƒ็ด ๅฐ†ๅœจๅˆๅง‹ๅŒ–ๆ—ถ่‡ชๅŠจๅตŒๅ…ฅๅˆฐ agent ็š„็ณป็ปŸๆ็คบไธญ๏ผšๅ› ๆญค่ฆๅŠชๅŠ›ไฝฟๅฎƒไปฌๅฐฝๅฏ่ƒฝๆธ…ๆ™ฐ๏ผ
> [!TIP]
> ๆญคๅฎšไน‰ๆ ผๅผไธŽ `apply_chat_template` ไธญไฝฟ็”จ็š„ๅทฅๅ…ทๆจกๅผ็›ธๅŒ๏ผŒๅ”ฏไธ€็š„ๅŒบๅˆซๆ˜ฏๆทปๅŠ ไบ† `tool` ่ฃ…้ฅฐๅ™จ๏ผš[่ฟ™้‡Œ](https://huggingface.co/blog/unified-tool-use#passing-tools-to-a-chat-template) ไบ†่งฃๆ›ดๅคšๅ…ณไบŽๆˆ‘ไปฌ็š„ๅทฅๅ…ทไฝฟ็”จ APIใ€‚
</hfoption>
<hfoption id="ๅญ็ฑปๅŒ–Tool">
```py
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
```
ๅญ็ฑป้œ€่ฆไปฅไธ‹ๅฑžๆ€ง๏ผš
- ไธ€ไธชๆธ…ๆ™ฐ็š„ `name`ใ€‚ๅ็งฐๅบ”่ฏฅ่ถณๅคŸๆ่ฟฐๆญคๅทฅๅ…ท็š„ๅŠŸ่ƒฝ๏ผŒไปฅๅธฎๅŠฉไธบ agent ๆไพ›ๅŠจๅŠ›็š„ LLMใ€‚็”ฑไบŽๆญคๅทฅๅ…ท่ฟ”ๅ›žไปปๅŠกไธ‹่ฝฝ้‡ๆœ€ๅคš็š„ๆจกๅž‹๏ผŒๆˆ‘ไปฌๅฐ†ๅ…ถๅ‘ฝๅไธบ `model_download_tool`ใ€‚
- ไธ€ไธช `description`ใ€‚ไธŽ `name` ไธ€ๆ ท๏ผŒๆญคๆ่ฟฐๆ˜ฏไธบๆ‚จ็š„ agent ๆไพ›ๅŠจๅŠ›็š„ LLM ็š„่ฏดๆ˜Žไนฆ๏ผŒๆ‰€ไปฅไธ่ฆๅฟฝ่ง†ๅฎƒใ€‚
- ่พ“ๅ…ฅ็ฑปๅž‹ๅ’Œๆ่ฟฐ
- ่พ“ๅ‡บ็ฑปๅž‹
ๆ‰€ๆœ‰่ฟ™ไบ›ๅฑžๆ€งๅฐ†ๅœจๅˆๅง‹ๅŒ–ๆ—ถ่‡ชๅŠจๅตŒๅ…ฅๅˆฐ agent ็š„็ณป็ปŸๆ็คบไธญ๏ผšๅ› ๆญค่ฆๅŠชๅŠ›ไฝฟๅฎƒไปฌๅฐฝๅฏ่ƒฝๆธ…ๆ™ฐ๏ผ
</hfoption>
</hfoptions>
็„ถๅŽๆ‚จๅฏไปฅ็›ดๆŽฅๅˆๅง‹ๅŒ–ๆ‚จ็š„ agent๏ผš
```py
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?"
)
```
ๆ‚จๅฐ†่Žทๅพ—ไปฅไธ‹ๆ—ฅๅฟ—๏ผš
```text
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ New 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? โ”‚
โ”‚ โ”‚
โ•ฐโ”€ HfApiModel - Qwen/Qwen2.5-Coder-32B-Instruct โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” Step 0 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
โ•ญโ”€ Executing this code: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ 1 model_name = model_download_tool(task="text-to-video") โ”‚
โ”‚ 2 print(model_name) โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
Execution logs:
ByteDance/AnimateDiff-Lightning
Out: None
[Step 0: Duration 0.27 seconds| Input tokens: 2,069 | Output tokens: 60]
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” Step 1 โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
โ•ญโ”€ Executing this code: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ 1 final_answer("ByteDance/AnimateDiff-Lightning") โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
Out - Final answer: ByteDance/AnimateDiff-Lightning
[Step 1: Duration 0.10 seconds| Input tokens: 4,288 | Output tokens: 148]
Out[20]: 'ByteDance/AnimateDiff-Lightning'
```
> [!TIP]
> ๅœจ [ไธ“็”จๆ•™็จ‹](./tutorials/tools#what-is-a-tool-and-how-to-build-one) ไธญไบ†่งฃๆ›ดๅคšๅ…ณไบŽๅทฅๅ…ท็š„ๅ†…ๅฎนใ€‚
## ๅคš agent
ๅคš agent ็ณป็ปŸๆ˜ฏ้š็€ๅพฎ่ฝฏ็š„ๆก†ๆžถ [Autogen](https://huggingface.co/papers/2308.08155) ๅผ•ๅ…ฅ็š„ใ€‚
ๅœจ่ฟ™็ง็ฑปๅž‹็š„ๆก†ๆžถไธญ๏ผŒๆ‚จๆœ‰ๅคšไธช agent ไธ€่ตทๅทฅไฝœๆฅ่งฃๅ†ณๆ‚จ็š„ไปปๅŠก๏ผŒ่€Œไธๆ˜ฏๅชๆœ‰ไธ€ไธชใ€‚
็ป้ชŒ่กจๆ˜Ž๏ผŒ่ฟ™ๅœจๅคงๅคšๆ•ฐๅŸบๅ‡†ๆต‹่ฏ•ไธญ่กจ็Žฐๆ›ดๅฅฝใ€‚่ฟ™็งๆ›ดๅฅฝ่กจ็Žฐ็š„ๅŽŸๅ› ๅœจๆฆ‚ๅฟตไธŠๅพˆ็ฎ€ๅ•๏ผšๅฏนไบŽ่ฎธๅคšไปปๅŠก๏ผŒไธŽๅ…ถไฝฟ็”จไธ€ไธชๅ…จ่ƒฝ็ณป็ปŸ๏ผŒๆ‚จๆ›ดๆ„ฟๆ„ๅฐ†ๅ•ๅ…ƒไธ“้—จ็”จไบŽๅญไปปๅŠกใ€‚ๅœจ่ฟ™้‡Œ๏ผŒๆ‹ฅๆœ‰ๅ…ทๆœ‰ๅ•็‹ฌๅทฅๅ…ท้›†ๅ’Œๅ†…ๅญ˜็š„ agent ๅฏไปฅๅฎž็Žฐ้ซ˜ๆ•ˆ็š„ไธ“ไธšๅŒ–ใ€‚ไพ‹ๅฆ‚๏ผŒไธบไป€ไนˆ่ฆ็”จ็ฝ‘้กตๆœ็ดข agent ่ฎฟ้—ฎ็š„ๆ‰€ๆœ‰็ฝ‘้กตๅ†…ๅฎนๅกซๅ……ไปฃ็ ็”Ÿๆˆ agent ็š„ๅ†…ๅญ˜๏ผŸๆœ€ๅฅฝๅฐ†ๅฎƒไปฌๅˆ†ๅผ€ใ€‚
ๆ‚จๅฏไปฅไฝฟ็”จ `smolagents` ่ฝปๆพๆž„ๅปบๅˆ†ๅฑ‚ๅคš agent ็ณป็ปŸใ€‚
ไธบๆญค๏ผŒๅฐ† agent ๅฐ่ฃ…ๅœจ [`ManagedAgent`] ๅฏน่ฑกไธญใ€‚ๆญคๅฏน่ฑก้œ€่ฆๅ‚ๆ•ฐ `agent`ใ€`name` ๅ’Œ `description`๏ผŒ่ฟ™ไบ›ๅ‚ๆ•ฐๅฐ†ๅตŒๅ…ฅๅˆฐ็ฎก็† agent ็š„็ณป็ปŸๆ็คบไธญ๏ผŒไปฅ่ฎฉๅฎƒ็Ÿฅ้“ๅฆ‚ไฝ•่ฐƒ็”จๆญคๆ‰˜็ฎก agent๏ผŒๅฐฑๅƒๆˆ‘ไปฌๅฏนๅทฅๅ…ทๆ‰€ๅš็š„้‚ฃๆ ทใ€‚
ไปฅไธ‹ๆ˜ฏไธ€ไธชไฝฟ็”จๆˆ‘ไปฌ็š„ [`DuckDuckGoSearchTool`] ๅˆถไฝœไธ€ไธช็ฎก็†็‰นๅฎš็ฝ‘้กตๆœ็ดข agent ็š„ agent ็š„็คบไพ‹๏ผš
```py
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, ManagedAgent
model = HfApiModel()
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?")
```
> [!TIP]
> ๆœ‰ๅ…ณ้ซ˜ๆ•ˆๅคš agent ๅฎž็Žฐ็š„ๆทฑๅ…ฅ็คบไพ‹๏ผŒ่ฏทๅ‚้˜… [ๆˆ‘ไปฌๅฆ‚ไฝ•ๅฐ†ๅคš agent ็ณป็ปŸๆŽจๅ‘ GAIA ๆŽ’่กŒๆฆœ็š„้กถ้ƒจ](https://huggingface.co/blog/beating-gaia)ใ€‚
## ไธŽๆ‚จ็š„ agent ไบค่ฐˆๅนถๅœจ้…ท็‚ซ็š„ Gradio ็•Œ้ขไธญๅฏ่ง†ๅŒ–ๅ…ถๆ€่€ƒ่ฟ‡็จ‹
ๆ‚จๅฏไปฅไฝฟ็”จ `GradioUI` ไบคไบ’ๅผๅœฐๅ‘ๆ‚จ็š„ agent ๆไบคไปปๅŠกๅนถ่ง‚ๅฏŸๅ…ถๆ€่€ƒๅ’Œๆ‰ง่กŒ่ฟ‡็จ‹๏ผŒไปฅไธ‹ๆ˜ฏไธ€ไธช็คบไพ‹๏ผš
```py
from smolagents import (
load_tool,
CodeAgent,
HfApiModel,
GradioUI
)
# ไปŽ Hub ๅฏผๅ…ฅๅทฅๅ…ท
image_generation_tool = load_tool("m-ric/text-to-image")
model = HfApiModel(model_id)
# ไฝฟ็”จๅ›พๅƒ็”Ÿๆˆๅทฅๅ…ทๅˆๅง‹ๅŒ– agent
agent = CodeAgent(tools=[image_generation_tool], model=model)
GradioUI(agent).launch()
```
ๅœจๅบ•ๅฑ‚๏ผŒๅฝ“็”จๆˆท่พ“ๅ…ฅๆ–ฐ็ญ”ๆกˆๆ—ถ๏ผŒagent ไผšไปฅ `agent.run(user_request, reset=False)` ๅฏๅŠจใ€‚
`reset=False` ๆ ‡ๅฟ—ๆ„ๅ‘ณ็€ๅœจๅฏๅŠจๆญคๆ–ฐไปปๅŠกไน‹ๅ‰ไธไผšๅˆทๆ–ฐ agent ็š„ๅ†…ๅญ˜๏ผŒ่ฟ™ไฝฟๅพ—ๅฏน่ฏๅฏไปฅ็ปง็ปญใ€‚
ๆ‚จไนŸๅฏไปฅๅœจๅ…ถไป– agent ๅŒ–ๅบ”็”จ็จ‹ๅบไธญไฝฟ็”จๆญค `reset=False` ๅ‚ๆ•ฐๆฅไฟๆŒๅฏน่ฏ็ปง็ปญใ€‚
## ไธ‹ไธ€ๆญฅ
่ฆๆ›ดๆทฑๅ…ฅๅœฐไฝฟ็”จ๏ผŒๆ‚จๅฐ†้œ€่ฆๆŸฅ็œ‹ๆˆ‘ไปฌ็š„ๆ•™็จ‹๏ผš
- [ๆˆ‘ไปฌ็š„ไปฃ็  agent ๅฆ‚ไฝ•ๅทฅไฝœ็š„่งฃ้‡Š](./tutorials/secure_code_execution)
- [ๆœฌๆŒ‡ๅ—ๅ…ณไบŽๅฆ‚ไฝ•ๆž„ๅปบๅฅฝ็š„ agent](./tutorials/building_good_agents)ใ€‚
- [ๅทฅๅ…ทไฝฟ็”จ็š„ๆทฑๅ…ฅๆŒ‡ๅ—](./tutorials/tools)ใ€‚