Moonshot_DeepResearch / docs /source /zh /tutorials /building_good_agents.mdx
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.
-->
# ๆž„ๅปบๅฅฝ็”จ็š„ agent
[[open-in-colab]]
่ƒฝ่‰ฏๅฅฝๅทฅไฝœ็š„ agent ๅ’Œไธ่ƒฝๅทฅไฝœ็š„ agent ไน‹้—ด๏ผŒๆœ‰ๅคฉๅฃคไน‹ๅˆซใ€‚
ๆˆ‘ไปฌๆ€Žไนˆๆ ทๆ‰่ƒฝๆž„ๅปบๅ‡บๅฑžไบŽๅ‰่€…็š„ agent ๅ‘ข๏ผŸ
ๅœจๆœฌๆŒ‡ๅ—ไธญ๏ผŒๆˆ‘ไปฌๅฐ†็œ‹ๅˆฐๆž„ๅปบ agent ็š„ๆœ€ไฝณๅฎž่ทตใ€‚
> [!TIP]
> ๅฆ‚ๆžœไฝ ๆ˜ฏ agent ๆž„ๅปบ็š„ๆ–ฐๆ‰‹๏ผŒ่ฏท็กฎไฟ้ฆ–ๅ…ˆ้˜…่ฏป [agent ไป‹็ป](../conceptual_guides/intro_agents) ๅ’Œ [smolagents ๅฏผ่งˆ](../guided_tour)ใ€‚
### ๆœ€ๅฅฝ็š„ agent ็ณป็ปŸๆ˜ฏๆœ€็ฎ€ๅ•็š„๏ผšๅฐฝๅฏ่ƒฝ็ฎ€ๅŒ–ๅทฅไฝœๆต
ๅœจไฝ ็š„ๅทฅไฝœๆตไธญ่ต‹ไบˆ LLM ไธ€ไบ›่‡ชไธปๆƒ๏ผŒไผšๅผ•ๅ…ฅไธ€ไบ›้”™่ฏฏ้ฃŽ้™ฉใ€‚
็ป่ฟ‡่‰ฏๅฅฝ็ผ–็จ‹็š„ agent ็ณป็ปŸ๏ผŒ้€šๅธธๅ…ทๆœ‰่‰ฏๅฅฝ็š„้”™่ฏฏๆ—ฅๅฟ—่ฎฐๅฝ•ๅ’Œ้‡่ฏ•ๆœบๅˆถ๏ผŒๅ› ๆญค LLM ๅผ•ๆ“Žๆœ‰ๆœบไผš่‡ชๆˆ‘็บ ้”™ใ€‚ไฝ†ไธบไบ†ๆœ€ๅคง้™ๅบฆๅœฐ้™ไฝŽ LLM ้”™่ฏฏ็š„้ฃŽ้™ฉ๏ผŒไฝ ๅบ”่ฏฅ็ฎ€ๅŒ–ไฝ ็š„ๅทฅไฝœๆต๏ผ
่ฎฉๆˆ‘ไปฌๅ›ž้กพไธ€ไธ‹ [agent ไป‹็ป](../conceptual_guides/intro_agents) ไธญ็š„ไพ‹ๅญ๏ผšไธ€ไธชไธบๅ†ฒๆตชๆ—…่กŒๅ…ฌๅธๅ›ž็ญ”็”จๆˆทๅ’จ่ฏข็š„ๆœบๅ™จไบบใ€‚
ไธŽๅ…ถ่ฎฉ agent ๆฏๆฌก่ขซ้—ฎๅŠๆ–ฐ็š„ๅ†ฒๆตชๅœฐ็‚นๆ—ถ๏ผŒ้ƒฝๅˆ†ๅˆซ่ฐƒ็”จ "ๆ—…่กŒ่ท็ฆป API" ๅ’Œ "ๅคฉๆฐ” API"๏ผŒไฝ ๅฏไปฅๅชๅˆ›ๅปบไธ€ไธช็ปŸไธ€็š„ๅทฅๅ…ท "return_spot_information"๏ผŒไธ€ไธชๅŒๆ—ถ่ฐƒ็”จ่ฟ™ไธคไธช API๏ผŒๅนถ่ฟ”ๅ›žๅฎƒไปฌ่ฟžๆŽฅ่พ“ๅ‡บ็š„ๅ‡ฝๆ•ฐใ€‚
่ฟ™ๅฏไปฅ้™ไฝŽๆˆๆœฌใ€ๅปถ่ฟŸๅ’Œ้”™่ฏฏ้ฃŽ้™ฉ๏ผ
ไธป่ฆ็š„ๆŒ‡ๅฏผๅŽŸๅˆ™ๆ˜ฏ๏ผšๅฐฝๅฏ่ƒฝๅ‡ๅฐ‘ LLM ่ฐƒ็”จ็š„ๆฌกๆ•ฐใ€‚
่ฟ™ๅฏไปฅๅธฆๆฅไธ€ไบ›ๅฏๅ‘๏ผš
- ๅฐฝๅฏ่ƒฝๆŠŠไธคไธชๅทฅๅ…ทๅˆๅนถไธบไธ€ไธช๏ผŒๅฐฑๅƒๆˆ‘ไปฌไธคไธช API ็š„ไพ‹ๅญใ€‚
- ๅฐฝๅฏ่ƒฝๅŸบไบŽ็กฎๅฎšๆ€งๅ‡ฝๆ•ฐ๏ผŒ่€Œไธๆ˜ฏ agent ๅ†ณ็ญ–๏ผŒๆฅๅฎž็Žฐ้€ป่พ‘ใ€‚
### ๆ”นๅ–„ๆตๅ‘ LLM ๅผ•ๆ“Ž็š„ไฟกๆฏๆต
่ฎฐไฝ๏ผŒไฝ ็š„ LLM ๅผ•ๆ“Žๅฐฑๅƒไธ€ไธช ~ๆ™บ่ƒฝ~ ๆœบๅ™จไบบ๏ผŒ่ขซๅ…ณๅœจไธ€ไธชๆˆฟ้—ด้‡Œ๏ผŒไธŽๅค–็•Œๅ”ฏไธ€็š„ไบคๆตๆ–นๅผๆ˜ฏ้€š่ฟ‡้—จ็ผไผ ้€’็š„็บธๆกใ€‚
ๅฆ‚ๆžœไฝ ๆฒกๆœ‰ๆ˜Ž็กฎๅœฐๅฐ†ไฟกๆฏๆ”พๅ…ฅๅ…ถๆ็คบไธญ๏ผŒๅฎƒๅฐ†ไธ็Ÿฅ้“ๅ‘็”Ÿ็š„ไปปไฝ•ไบ‹ๆƒ…ใ€‚
ๆ‰€ไปฅ้ฆ–ๅ…ˆ่ฆ่ฎฉไฝ ็š„ไปปๅŠก้žๅธธๆธ…ๆ™ฐ๏ผ
็”ฑไบŽ agent ็”ฑ LLM ้ฉฑๅŠจ๏ผŒไปปๅŠก่กจ่ฟฐ็š„ๅพฎๅฐๅ˜ๅŒ–ๅฏ่ƒฝไผšไบง็”ŸๅฎŒๅ…จไธๅŒ็š„็ป“ๆžœใ€‚
็„ถๅŽ๏ผŒๆ”นๅ–„ๅทฅๅ…ทไฝฟ็”จไธญๆตๅ‘ agent ็š„ไฟกๆฏๆตใ€‚
้œ€่ฆ้ตๅพช็š„ๅ…ทไฝ“ๆŒ‡ๅ—๏ผš
- ๆฏไธชๅทฅๅ…ท้ƒฝๅบ”่ฏฅ่ฎฐๅฝ•๏ผˆๅช้œ€ๅœจๅทฅๅ…ท็š„ `forward` ๆ–นๆณ•ไธญไฝฟ็”จ `print` ่ฏญๅฅ๏ผ‰ๅฏน LLM ๅผ•ๆ“Žๅฏ่ƒฝๆœ‰็”จ็š„ๆ‰€ๆœ‰ไฟกๆฏใ€‚
- ็‰นๅˆซๆ˜ฏ๏ผŒ่ฎฐๅฝ•ๅทฅๅ…ทๆ‰ง่กŒ้”™่ฏฏ็š„่ฏฆ็ป†ไฟกๆฏไผšๅพˆๆœ‰ๅธฎๅŠฉ๏ผ
ไพ‹ๅฆ‚๏ผŒ่ฟ™้‡Œๆœ‰ไธ€ไธชๆ นๆฎไฝ็ฝฎๅ’Œๆ—ฅๆœŸๆ—ถ้—ดๆฃ€็ดขๅคฉๆฐ”ๆ•ฐๆฎ็š„ๅทฅๅ…ท๏ผš
้ฆ–ๅ…ˆ๏ผŒ่ฟ™ๆ˜ฏไธ€ไธช็ณŸ็ณ•็š„็‰ˆๆœฌ๏ผš
```python
import datetime
from smolagents import tool
def get_weather_report_at_coordinates(coordinates, date_time):
# ่™šๆ‹Ÿๅ‡ฝๆ•ฐ๏ผŒ่ฟ”ๅ›ž [ๆธฉๅบฆ๏ผˆยฐC๏ผ‰๏ผŒ้™้›จ้ฃŽ้™ฉ๏ผˆ0-1๏ผ‰๏ผŒๆตช้ซ˜๏ผˆm๏ผ‰]
return [28.0, 0.35, 0.85]
def get_coordinates_from_location(location):
# ่ฟ”ๅ›ž่™šๆ‹Ÿๅๆ ‡
return [3.3, -42.0]
@tool
def get_weather_api(location: str, date_time: str) -> str:
"""
Returns the weather report.
Args:
location: the name of the place that you want the weather for.
date_time: the date and time for which you want the report.
"""
lon, lat = convert_location_to_coordinates(location)
date_time = datetime.strptime(date_time)
return str(get_weather_report_at_coordinates((lon, lat), date_time))
```
ไธบไป€ไนˆๅฎƒไธๅฅฝ๏ผŸ
- ๆฒกๆœ‰่ฏดๆ˜Ž `date_time` ๅบ”่ฏฅไฝฟ็”จ็š„ๆ ผๅผ
- ๆฒกๆœ‰่ฏดๆ˜Žไฝ็ฝฎๅบ”่ฏฅๅฆ‚ไฝ•ๆŒ‡ๅฎš
- ๆฒกๆœ‰่ฎฐๅฝ•ๆœบๅˆถๆฅๅค„็†ๆ˜Ž็กฎ็š„ๆŠฅ้”™ๆƒ…ๅ†ต๏ผŒๅฆ‚ไฝ็ฝฎๆ ผๅผไธๆญฃ็กฎๆˆ– date_time ๆ ผๅผไธๆญฃ็กฎ
- ่พ“ๅ‡บๆ ผๅผ้šพไปฅ็†่งฃ
ๅฆ‚ๆžœๅทฅๅ…ท่ฐƒ็”จๅคฑ่ดฅ๏ผŒๅ†…ๅญ˜ไธญ่ฎฐๅฝ•็š„้”™่ฏฏ่ทŸ่ธช๏ผŒๅฏไปฅๅธฎๅŠฉ LLM ้€†ๅ‘ๅทฅ็จ‹ๅทฅๅ…ทๆฅไฟฎๅค้”™่ฏฏใ€‚ไฝ†ไธบไป€ไนˆ่ฆ่ฎฉๅฎƒๅš่ฟ™ไนˆๅคš็น้‡็š„ๅทฅไฝœๅ‘ข๏ผŸ
ๆž„ๅปบ่ฟ™ไธชๅทฅๅ…ท็š„ๆ›ดๅฅฝๆ–นๅผๅฆ‚ไธ‹๏ผš
```python
@tool
def get_weather_api(location: str, date_time: str) -> str:
"""
Returns the weather report.
Args:
location: the name of the place that you want the weather for. Should be a place name, followed by possibly a city name, then a country, like "Anchor Point, Taghazout, Morocco".
date_time: the date and time for which you want the report, formatted as '%m/%d/%y %H:%M:%S'.
"""
lon, lat = convert_location_to_coordinates(location)
try:
date_time = datetime.strptime(date_time)
except Exception as e:
raise ValueError("Conversion of `date_time` to datetime format failed, make sure to provide a string in format '%m/%d/%y %H:%M:%S'. Full trace:" + str(e))
temperature_celsius, risk_of_rain, wave_height = get_weather_report_at_coordinates((lon, lat), date_time)
return f"Weather report for {location}, {date_time}: Temperature will be {temperature_celsius}ยฐC, risk of rain is {risk_of_rain*100:.0f}%, wave height is {wave_height}m."
```
ไธ€่ˆฌๆฅ่ฏด๏ผŒไธบไบ†ๅ‡่ฝป LLM ็š„่ดŸๆ‹…๏ผŒ่ฆ้—ฎ่‡ชๅทฑ็š„ๅฅฝ้—ฎ้ข˜ๆ˜ฏ๏ผš"ๅฆ‚ๆžœๆˆ‘ๆ˜ฏไธ€ไธช็ฌฌไธ€ๆฌกไฝฟ็”จ่ฟ™ไธชๅทฅๅ…ท็š„ๅ‚ป็“œ๏ผŒไฝฟ็”จ่ฟ™ไธชๅทฅๅ…ท็ผ–็จ‹ๅนถ็บ ๆญฃ่‡ชๅทฑ็š„้”™่ฏฏๆœ‰ๅคšๅฎนๆ˜“๏ผŸ"ใ€‚
### ็ป™ agent ๆ›ดๅคšๅ‚ๆ•ฐ
้™คไบ†็ฎ€ๅ•็š„ไปปๅŠกๆ่ฟฐๅญ—็ฌฆไธฒๅค–๏ผŒไฝ ่ฟ˜ๅฏไปฅไฝฟ็”จ `additional_args` ๅ‚ๆ•ฐไผ ้€’ไปปไฝ•็ฑปๅž‹็š„ๅฏน่ฑก๏ผš
```py
from smolagents import CodeAgent, HfApiModel
model_id = "meta-llama/Llama-3.3-70B-Instruct"
agent = CodeAgent(tools=[], model=HfApiModel(model_id=model_id), add_base_tools=True)
agent.run(
"Why does Mike not know many people in New York?",
additional_args={"mp3_sound_file_url":'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/recording.mp3'}
)
```
ไพ‹ๅฆ‚๏ผŒไฝ ๅฏไปฅไฝฟ็”จ่ฟ™ไธช `additional_args` ๅ‚ๆ•ฐไผ ้€’ไฝ ๅธŒๆœ› agent ๅˆฉ็”จ็š„ๅ›พๅƒๆˆ–ๅญ—็ฌฆไธฒใ€‚
## ๅฆ‚ไฝ•่ฐƒ่ฏ•ไฝ ็š„ agent
### 1. ไฝฟ็”จๆ›ดๅผบๅคง็š„ LLM
ๅœจ agent ๅทฅไฝœๆตไธญ๏ผŒๆœ‰ไบ›้”™่ฏฏๆ˜ฏๅฎž้™…้”™่ฏฏ๏ผŒๆœ‰ไบ›ๅˆ™ๆ˜ฏไฝ ็š„ LLM ๅผ•ๆ“Žๆฒกๆœ‰ๆญฃ็กฎๆŽจ็†็š„็ป“ๆžœใ€‚
ไพ‹ๅฆ‚๏ผŒๅ‚่€ƒ่ฟ™ไธชๆˆ‘่ฆๆฑ‚ๅˆ›ๅปบไธ€ไธชๆฑฝ่ฝฆๅ›พ็‰‡็š„ `CodeAgent` ็š„่ฟ่กŒ่ฎฐๅฝ•๏ผš
```text
==================================================================================================== New task ====================================================================================================
Make me a cool car picture
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ New step โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
Agent is executing the code below: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
image_generator(prompt="A cool, futuristic sports car with LED headlights, aerodynamic design, and vibrant color, high-res, photorealistic")
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
Last output from code snippet: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
/var/folders/6m/9b1tts6d5w960j80wbw9tx3m0000gn/T/tmpx09qfsdd/652f0007-3ee9-44e2-94ac-90dae6bb89a4.png
Step 1:
- Time taken: 16.35 seconds
- Input tokens: 1,383
- Output tokens: 77
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ New step โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
Agent is executing the code below: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
final_answer("/var/folders/6m/9b1tts6d5w960j80wbw9tx3m0000gn/T/tmpx09qfsdd/652f0007-3ee9-44e2-94ac-90dae6bb89a4.png")
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
Print outputs:
Last output from code snippet: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
/var/folders/6m/9b1tts6d5w960j80wbw9tx3m0000gn/T/tmpx09qfsdd/652f0007-3ee9-44e2-94ac-90dae6bb89a4.png
Final answer:
/var/folders/6m/9b1tts6d5w960j80wbw9tx3m0000gn/T/tmpx09qfsdd/652f0007-3ee9-44e2-94ac-90dae6bb89a4.png
```
็”จๆˆท็œ‹ๅˆฐ็š„ๆ˜ฏ่ฟ”ๅ›žไบ†ไธ€ไธช่ทฏๅพ„๏ผŒ่€Œไธๆ˜ฏๅ›พๅƒใ€‚
่ฟ™็œ‹่ตทๆฅๅƒๆ˜ฏ็ณป็ปŸ็š„้”™่ฏฏ๏ผŒไฝ†ๅฎž้™…ไธŠ agent ็ณป็ปŸๅนถๆฒกๆœ‰ๅฏผ่‡ด้”™่ฏฏ๏ผšๅชๆ˜ฏ LLM ๅคง่„‘็Šฏไบ†ไธ€ไธช้”™่ฏฏ๏ผŒๆฒกๆœ‰ๆŠŠๅ›พๅƒ่พ“ๅ‡บ๏ผŒไฟๅญ˜ๅˆฐๅ˜้‡ไธญใ€‚
ๅ› ๆญค๏ผŒๅฎƒๆ— ๆณ•ๅ†ๆฌก่ฎฟ้—ฎๅ›พๅƒ๏ผŒๅช่ƒฝๅˆฉ็”จไฟๅญ˜ๅ›พๅƒๆ—ถ่ฎฐๅฝ•็š„่ทฏๅพ„๏ผŒๆ‰€ไปฅๅฎƒ่ฟ”ๅ›ž็š„ๆ˜ฏ่ทฏๅพ„๏ผŒ่€Œไธๆ˜ฏๅ›พๅƒใ€‚
่ฐƒ่ฏ• agent ็š„็ฌฌไธ€ๆญฅๆ˜ฏ"ไฝฟ็”จๆ›ดๅผบๅคง็š„ LLM"ใ€‚ๅƒ `Qwen2.5-72B-Instruct` ่ฟ™ๆ ท็š„ๆ›ฟไปฃๆ–นๆกˆไธไผš็Šฏ่ฟ™็ง้”™่ฏฏใ€‚
### 2. ๆไพ›ๆ›ดๅคšๆŒ‡ๅฏผ/ๆ›ดๅคšไฟกๆฏ
ไฝ ไนŸๅฏไปฅไฝฟ็”จไธๅคชๅผบๅคง็š„ๆจกๅž‹๏ผŒๅช่ฆไฝ ๆ›ดๆœ‰ๆ•ˆๅœฐๆŒ‡ๅฏผๅฎƒไปฌใ€‚
็ซ™ๅœจๆจกๅž‹็š„่ง’ๅบฆๆ€่€ƒ๏ผšๅฆ‚ๆžœไฝ ๆ˜ฏๆจกๅž‹ๅœจ่งฃๅ†ณไปปๅŠก๏ผŒไฝ ไผšๅ› ไธบ็ณป็ปŸๆ็คบ+ไปปๅŠก่กจ่ฟฐ+ๅทฅๅ…ทๆ่ฟฐไธญๆไพ›็š„ไฟกๆฏ่€ŒๆŒฃๆ‰Žๅ—๏ผŸ
ไฝ ้œ€่ฆไธ€ไบ›้ขๅค–็š„่ฏดๆ˜Žๅ—๏ผŸ
ไธบไบ†ๆไพ›้ขๅค–ไฟกๆฏ๏ผŒๆˆ‘ไปฌไธๅปบ่ฎฎ็ซ‹ๅณๆ›ดๆ”น็ณป็ปŸๆ็คบ๏ผš้ป˜่ฎค็ณป็ปŸๆ็คบๆœ‰่ฎธๅคš่ฐƒๆ•ด๏ผŒ้™ค้žไฝ ้žๅธธไบ†่งฃๆ็คบ๏ผŒๅฆๅˆ™ไฝ ๅพˆๅฎนๆ˜“็ฟป่ฝฆใ€‚
ๆ›ดๅฅฝ็š„ๆŒ‡ๅฏผ LLM ๅผ•ๆ“Ž็š„ๆ–นๆณ•ๆ˜ฏ๏ผš
- ๅฆ‚ๆžœๆ˜ฏๅ…ณไบŽ่ฆ่งฃๅ†ณ็š„ไปปๅŠก๏ผšๆŠŠๆ‰€ๆœ‰็ป†่Š‚ๆทปๅŠ ๅˆฐไปปๅŠกไธญใ€‚ไปปๅŠกๅฏไปฅๆœ‰ๅ‡ ็™พ้กต้•ฟใ€‚
- ๅฆ‚ๆžœๆ˜ฏๅ…ณไบŽๅฆ‚ไฝ•ไฝฟ็”จๅทฅๅ…ท๏ผšไฝ ็š„ๅทฅๅ…ท็š„ description ๅฑžๆ€งใ€‚
### 3. ๆ›ดๆ”น็ณป็ปŸๆ็คบ๏ผˆ้€šๅธธไธๅปบ่ฎฎ๏ผ‰
ๅฆ‚ๆžœไธŠ่ฟฐ่ฏดๆ˜ŽไธๅคŸ๏ผŒไฝ ๅฏไปฅๆ›ดๆ”น็ณป็ปŸๆ็คบใ€‚
่ฎฉๆˆ‘ไปฌ็œ‹็œ‹ๅฎƒๆ˜ฏๅฆ‚ไฝ•ๅทฅไฝœ็š„ใ€‚ไพ‹ๅฆ‚๏ผŒ่ฎฉๆˆ‘ไปฌๆฃ€ๆŸฅ [`CodeAgent`] ็š„้ป˜่ฎค็ณป็ปŸๆ็คบ๏ผˆไธ‹้ข็š„็‰ˆๆœฌ้€š่ฟ‡่ทณ่ฟ‡้›ถๆ ทๆœฌ็คบไพ‹่ฟ›่กŒไบ†็ผฉ็Ÿญ๏ผ‰ใ€‚
```python
print(agent.prompt_templates["system_prompt"])
```
ไฝ ไผšๅพ—ๅˆฐ๏ผš
```text
You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
At each step, in the 'Thought:' sequence, you should first explain your reasoning towards solving the task and the tools that you want to use.
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '<end_code>' sequence.
During each intermediate step, you can use 'print()' to save whatever important information you will then need.
These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
In the end you have to return a final answer using the `final_answer` tool.
Here are a few examples using notional tools:
---
{examples}
Above example were using notional tools that might not exist for you. On top of performing computations in the Python code snippets that you create, you only have access to these tools:
{{tool_descriptions}}
{{managed_agents_descriptions}}
Here are the rules you should always follow to solve your task:
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```<end_code>' sequence, else you will fail.
2. Use only variables that you have defined!
3. Always use the right arguments for the tools. DO NOT pass the arguments as a dict as in 'answer = wiki({'query': "What is the place where James Bond lives?"})', but use the arguments directly as in 'answer = wiki(query="What is the place where James Bond lives?")'.
4. Take care to not chain too many sequential tool calls in the same code block, especially when the output format is unpredictable. For instance, a call to search has an unpredictable return format, so do not have another tool call that depends on its output in the same block: rather output results with print() to use them in the next block.
5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters.
6. Don't name any new variable with the same name as a tool: for instance don't name a variable 'final_answer'.
7. Never create any notional variables in our code, as having these in your logs might derail you from the true variables.
8. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}
9. The state persists between code executions: so if in one step you've created variables or imported modules, these will all persist.
10. Don't give up! You're in charge of solving the task, not providing directions to solve it.
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
```
ๅฆ‚ไฝ ๆ‰€่ง๏ผŒๆœ‰ไธ€ไบ›ๅ ไฝ็ฌฆ๏ผŒๅฆ‚ `"{{tool_descriptions}}"`๏ผš่ฟ™ไบ›ๅฐ†ๅœจ agent ๅˆๅง‹ๅŒ–ๆ—ถ็”จไบŽๆ’ๅ…ฅๆŸไบ›่‡ชๅŠจ็”Ÿๆˆ็š„ๅทฅๅ…ทๆˆ–็ฎก็† agent ็š„ๆ่ฟฐใ€‚
ๅ› ๆญค๏ผŒ่™ฝ็„ถไฝ ๅฏไปฅ้€š่ฟ‡ๅฐ†่‡ชๅฎšไน‰ๆ็คบไฝœไธบๅ‚ๆ•ฐไผ ้€’็ป™ `system_prompt` ๅ‚ๆ•ฐๆฅ่ฆ†็›–ๆญค็ณป็ปŸๆ็คบๆจกๆฟ๏ผŒไฝ†ไฝ ็š„ๆ–ฐ็ณป็ปŸๆ็คบๅฟ…้กปๅŒ…ๅซไปฅไธ‹ๅ ไฝ็ฌฆ๏ผš
- `"{{tool_descriptions}}"` ็”จไบŽๆ’ๅ…ฅๅทฅๅ…ทๆ่ฟฐใ€‚
- `"{{managed_agents_description}}"` ็”จไบŽๆ’ๅ…ฅ managed agent ็š„ๆ่ฟฐ๏ผˆๅฆ‚ๆžœๆœ‰๏ผ‰ใ€‚
- ไป…้™ `CodeAgent`๏ผš`"{{authorized_imports}}"` ็”จไบŽๆ’ๅ…ฅๆŽˆๆƒๅฏผๅ…ฅๅˆ—่กจใ€‚
็„ถๅŽไฝ ๅฏไปฅๆ นๆฎๅฆ‚ไธ‹๏ผŒๆ›ดๆ”น็ณป็ปŸๆ็คบ๏ผš
```py
agent.prompt_templates["system_prompt"] = agent.prompt_templates["system_prompt"] + "\nHere you go!"
```
่ฟ™ไนŸ้€‚็”จไบŽ [`ToolCallingAgent`]ใ€‚
### 4. ้ขๅค–่ง„ๅˆ’
ๆˆ‘ไปฌๆไพ›ไบ†ไธ€ไธช็”จไบŽ่กฅๅ……่ง„ๅˆ’ๆญฅ้ชค็š„ๆจกๅž‹๏ผŒagent ๅฏไปฅๅœจๆญฃๅธธๆ“ไฝœๆญฅ้ชคไน‹้—ดๅฎšๆœŸ่ฟ่กŒใ€‚ๅœจๆญคๆญฅ้ชคไธญ๏ผŒๆฒกๆœ‰ๅทฅๅ…ท่ฐƒ็”จ๏ผŒLLM ๅชๆ˜ฏ่ขซ่ฆๆฑ‚ๆ›ดๆ–ฐๅฎƒ็Ÿฅ้“็š„ไบ‹ๅฎžๅˆ—่กจ๏ผŒๅนถๆ นๆฎ่ฟ™ไบ›ไบ‹ๅฎžๅๆŽจๅฎƒๅบ”่ฏฅ้‡‡ๅ–็š„ไธ‹ไธ€ๆญฅใ€‚
```py
from smolagents import load_tool, CodeAgent, HfApiModel, DuckDuckGoSearchTool
from dotenv import load_dotenv
load_dotenv()
# ไปŽ Hub ๅฏผๅ…ฅๅทฅๅ…ท
image_generation_tool = load_tool("m-ric/text-to-image", trust_remote_code=True)
search_tool = DuckDuckGoSearchTool()
agent = CodeAgent(
tools=[search_tool],
model=HfApiModel("Qwen/Qwen2.5-72B-Instruct"),
planning_interval=3 # ่ฟ™ๆ˜ฏไฝ ๆฟ€ๆดป่ง„ๅˆ’็š„ๅœฐๆ–น๏ผ
)
# ่ฟ่กŒๅฎƒ๏ผ
result = agent.run(
"How long would a cheetah at full speed take to run the length of Pont Alexandre III?",
)
```