|
# 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` ็ฎก้ๅจๆฌๅฐๆบๅจไธ่ฟ่กๆจ็ |
|
- [`InferenceClientModel`] ๅจๅบๅฑไฝฟ็จ `huggingface_hub.InferenceClient` |
|
- [`LiteLLMModel`] ่ฎฉๆจ้่ฟ [LiteLLM](https://docs.litellm.ai/) ่ฐ็จ 100+ ไธๅ็ๆจกๅ๏ผ |
|
- [`AzureOpenAIServerModel`] ๅ
่ฎธๆจไฝฟ็จ้จ็ฝฒๅจ [Azure](https://azure.microsoft.com/en-us/products/ai-services/openai-service) ไธญ็ OpenAI ๆจกๅใ |
|
- [`MLXModel`] ๅฏๅๅปบ [mlx-lm](https://pypi.org/project/mlx-lm/) ๆตๆฐด็บฟ๏ผไปฅไพฟๅจๆฌๅฐๆบๅจไธ่ฟ่กๆจ็ใ |
|
|
|
- `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/)ใ[Azure OpenAI](https://azure.microsoft.com/en-us/products/ai-services/openai-service)๏ผ่ฟๆฏ[mlx-lm](https://pypi.org/project/mlx-lm/).ใ |
|
|
|
<hfoptions id="้ๆฉไธไธชLLM"> |
|
<hfoption id="Hugging Face API"> |
|
|
|
Hugging Face API ๅฏไปฅๅ
่ดนไฝฟ็จ่ๆ ้ token๏ผไฝไผๆ้็้ๅถใ |
|
|
|
่ฆ่ฎฟ้ฎๅ้ๆจกๅๆไฝฟ็จ PRO ่ดฆๆทๆ้ซ้็้ๅถ๏ผๆจ้่ฆ่ฎพ็ฝฎ็ฏๅขๅ้ `HF_TOKEN` ๆๅจๅๅงๅ `InferenceClientModel` ๆถไผ ้ `token` ๅ้ใ |
|
|
|
```python |
|
from smolagents import CodeAgent, InferenceClientModel |
|
|
|
model_id = "meta-llama/Llama-3.3-70B-Instruct" |
|
|
|
model = InferenceClientModel(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> |
|
<hfoption id="Azure OpenAI"> |
|
|
|
่ฆ่ฟๆฅๅฐ Azure OpenAI๏ผๆจๅฏไปฅ็ดๆฅไฝฟ็จ `AzureOpenAIServerModel`๏ผๆไฝฟ็จ `LiteLLMModel` ๅนถ่ฟ่ก็ธๅบ้
็ฝฎใ |
|
|
|
ๅๅงๅ `AzureOpenAIServerModel` ๅฎไพๆถ๏ผ้่ฆไผ ้ๆจกๅ้จ็ฝฒๅ็งฐ๏ผๅฏ้ๆฉไปฅไธไปปไธ็งๆนๅผ๏ผ1.ไผ ้ `azure_endpoint`ใ`api_key` ๅ `api_version` ๅๆฐ๏ผ2.่ฎพ็ฝฎ็ฏๅขๅ้ `AZURE_OPENAI_ENDPOINT`ใ`AZURE_OPENAI_API_KEY` ๅ `OPENAI_API_VERSION` |
|
|
|
```python |
|
# !pip install smolagents[openai] |
|
from smolagents import CodeAgent, AzureOpenAIServerModel |
|
|
|
model = AzureOpenAIServerModel(model_id="gpt-4o-mini") |
|
agent = CodeAgent(tools=[], model=model, add_base_tools=True) |
|
|
|
agent.run( |
|
"Could you give me the 118th number in the Fibonacci sequence?", |
|
) |
|
``` |
|
|
|
ไนๅฏๆๅฆไธๆนๅผ้
็ฝฎ `LiteLLMModel` ่ฟๆฅ Azure OpenAI๏ผ |
|
|
|
- ๅฐๆจกๅ้จ็ฝฒๅ็งฐไฝไธบ `model_id` ๅๆฐไผ ้๏ผๅนถ็กฎไฟๅ
ถๅ็ผไธบ `azure/` |
|
- ็กฎไฟ่ฎพ็ฝฎ็ฏๅขๅ้ `AZURE_API_VERSION` |
|
- ไปป้ๅ
ถไธ๏ผ1.ไผ ้ `api_base` ๅ `api_key` ๅๆฐ๏ผ2.่ฎพ็ฝฎ็ฏๅขๅ้ `AZURE_API_KEY` ๅ `AZURE_API_BASE` |
|
|
|
```python |
|
import os |
|
from smolagents import CodeAgent, LiteLLMModel |
|
|
|
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME="gpt-35-turbo-16k-deployment" # example of deployment name |
|
|
|
os.environ["AZURE_API_KEY"] = "" # api_key |
|
os.environ["AZURE_API_BASE"] = "" # "https://example-endpoint.openai.azure.com" |
|
os.environ["AZURE_API_VERSION"] = "" # "2024-10-01-preview" |
|
|
|
model = LiteLLMModel(model_id="azure/" + AZURE_OPENAI_CHAT_DEPLOYMENT_NAME) |
|
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="mlx-lm"> |
|
|
|
```python |
|
# !pip install smolagents[mlx-lm] |
|
from smolagents import CodeAgent, MLXModel |
|
|
|
mlx_model = MLXModel("mlx-community/Qwen2.5-Coder-32B-Instruct-4bit") |
|
agent = CodeAgent(model=mlx_model, tools=[], 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 |
|
|
|
model = InferenceClientModel() |
|
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 WebSearchTool |
|
|
|
search_tool = WebSearchTool() |
|
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, InferenceClientModel |
|
agent = CodeAgent(tools=[model_download_tool], model=InferenceClientModel()) |
|
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? โ |
|
โ โ |
|
โฐโ InferenceClientModel - 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๏ผๅฐฑๅๆไปฌๅฏนๅทฅๅ
ทๆๅ็้ฃๆ ทใ |
|
|
|
ไปฅไธๆฏไธไธชไฝฟ็จๆไปฌ็ [`WebSearchTool`] ๅถไฝไธไธช็ฎก็็นๅฎ็ฝ้กตๆ็ดข agent ็ agent ็็คบไพ๏ผ |
|
|
|
```py |
|
from smolagents import CodeAgent, InferenceClientModel, WebSearchTool, ManagedAgent |
|
|
|
model = InferenceClientModel() |
|
|
|
web_agent = CodeAgent(tools=[WebSearchTool()], 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, |
|
InferenceClientModel, |
|
GradioUI |
|
) |
|
|
|
# ไป Hub ๅฏผๅ
ฅๅทฅๅ
ท |
|
image_generation_tool = load_tool("m-ric/text-to-image") |
|
|
|
model = InferenceClientModel(model_id=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ๅ๏ผๅณๅฏๅฐๅ
ถๅไบซ่ณ Hub๏ผ |
|
|
|
```py |
|
agent.push_to_hub("m-ric/my_agent") |
|
``` |
|
|
|
็ฑปไผผๅฐ๏ผ่ฅ่ฆๅ ่ฝฝๅทฒๆจ้่ณ Hub ็agent๏ผๅจไฟกไปปๅ
ถๅทฅๅ
ทไปฃ็ ็ๅๆไธ๏ผๅฏไฝฟ็จ๏ผ |
|
|
|
```py |
|
agent.from_hub("m-ric/my_agent", trust_remote_code=True) |
|
``` |
|
|
|
่ฆๆดๆทฑๅ
ฅๅฐไฝฟ็จ๏ผๆจๅฐ้่ฆๆฅ็ๆไปฌ็ๆ็จ๏ผ |
|
- [ๆไปฌ็ไปฃ็ agent ๅฆไฝๅทฅไฝ็่งฃ้](./tutorials/secure_code_execution) |
|
- [ๆฌๆๅๅ
ณไบๅฆไฝๆๅปบๅฅฝ็ agent](./tutorials/building_good_agents)ใ |
|
- [ๅทฅๅ
ทไฝฟ็จ็ๆทฑๅ
ฅๆๅ](./tutorials/tools)ใ |
|
|