ๆๅปบๅฅฝ็จ็ agent
[[open-in-colab]]
่ฝ่ฏๅฅฝๅทฅไฝ็ agent ๅไธ่ฝๅทฅไฝ็ agent ไน้ด๏ผๆๅคฉๅฃคไนๅซใ ๆไปฌๆไนๆ ทๆ่ฝๆๅปบๅบๅฑไบๅ่ ็ agent ๅข๏ผ ๅจๆฌๆๅไธญ๏ผๆไปฌๅฐ็ๅฐๆๅปบ agent ็ๆไฝณๅฎ่ทตใ
ๅฆๆไฝ ๆฏ agent ๆๅปบ็ๆฐๆ๏ผ่ฏท็กฎไฟ้ฆๅ ้ ่ฏป agent ไป็ป ๅ smolagents ๅฏผ่งใ
ๆๅฅฝ็ agent ็ณป็ปๆฏๆ็ฎๅ็๏ผๅฐฝๅฏ่ฝ็ฎๅๅทฅไฝๆต
ๅจไฝ ็ๅทฅไฝๆตไธญ่ตไบ LLM ไธไบ่ชไธปๆ๏ผไผๅผๅ ฅไธไบ้่ฏฏ้ฃ้ฉใ
็ป่ฟ่ฏๅฅฝ็ผ็จ็ agent ็ณป็ป๏ผ้ๅธธๅ ทๆ่ฏๅฅฝ็้่ฏฏๆฅๅฟ่ฎฐๅฝๅ้่ฏๆบๅถ๏ผๅ ๆญค LLM ๅผๆๆๆบไผ่ชๆ็บ ้ใไฝไธบไบๆๅคง้ๅบฆๅฐ้ไฝ LLM ้่ฏฏ็้ฃ้ฉ๏ผไฝ ๅบ่ฏฅ็ฎๅไฝ ็ๅทฅไฝๆต๏ผ
่ฎฉๆไปฌๅ้กพไธไธ agent ไป็ป ไธญ็ไพๅญ๏ผไธไธชไธบๅฒๆตชๆ ่กๅ ฌๅธๅ็ญ็จๆทๅจ่ฏข็ๆบๅจไบบใ ไธๅ ถ่ฎฉ agent ๆฏๆฌก่ขซ้ฎๅๆฐ็ๅฒๆตชๅฐ็นๆถ๏ผ้ฝๅๅซ่ฐ็จ "ๆ ่ก่ท็ฆป API" ๅ "ๅคฉๆฐ API"๏ผไฝ ๅฏไปฅๅชๅๅปบไธไธช็ปไธ็ๅทฅๅ ท "return_spot_information"๏ผไธไธชๅๆถ่ฐ็จ่ฟไธคไธช API๏ผๅนถ่ฟๅๅฎไปฌ่ฟๆฅ่พๅบ็ๅฝๆฐใ
่ฟๅฏไปฅ้ไฝๆๆฌใๅปถ่ฟๅ้่ฏฏ้ฃ้ฉ๏ผ
ไธป่ฆ็ๆๅฏผๅๅๆฏ๏ผๅฐฝๅฏ่ฝๅๅฐ LLM ่ฐ็จ็ๆฌกๆฐใ
่ฟๅฏไปฅๅธฆๆฅไธไบๅฏๅ๏ผ
- ๅฐฝๅฏ่ฝๆไธคไธชๅทฅๅ ทๅๅนถไธบไธไธช๏ผๅฐฑๅๆไปฌไธคไธช API ็ไพๅญใ
- ๅฐฝๅฏ่ฝๅบไบ็กฎๅฎๆงๅฝๆฐ๏ผ่ไธๆฏ agent ๅณ็ญ๏ผๆฅๅฎ็ฐ้ป่พใ
ๆนๅๆตๅ LLM ๅผๆ็ไฟกๆฏๆต
่ฎฐไฝ๏ผไฝ ็ LLM ๅผๆๅฐฑๅไธไธช ๆบ่ฝ ๆบๅจไบบ๏ผ่ขซๅ
ณๅจไธไธชๆฟ้ด้๏ผไธๅค็ๅฏไธ็ไบคๆตๆนๅผๆฏ้่ฟ้จ็ผไผ ้็็บธๆกใ
ๅฆๆไฝ ๆฒกๆๆ็กฎๅฐๅฐไฟกๆฏๆพๅ ฅๅ ถๆ็คบไธญ๏ผๅฎๅฐไธ็ฅ้ๅ็็ไปปไฝไบๆ ใ
ๆไปฅ้ฆๅ ่ฆ่ฎฉไฝ ็ไปปๅก้ๅธธๆธ ๆฐ๏ผ ็ฑไบ agent ็ฑ LLM ้ฉฑๅจ๏ผไปปๅก่กจ่ฟฐ็ๅพฎๅฐๅๅๅฏ่ฝไผไบง็ๅฎๅ จไธๅ็็ปๆใ
็ถๅ๏ผๆนๅๅทฅๅ ทไฝฟ็จไธญๆตๅ agent ็ไฟกๆฏๆตใ
้่ฆ้ตๅพช็ๅ ทไฝๆๅ๏ผ
- ๆฏไธชๅทฅๅ
ท้ฝๅบ่ฏฅ่ฎฐๅฝ๏ผๅช้ๅจๅทฅๅ
ท็
forward
ๆนๆณไธญไฝฟ็จprint
่ฏญๅฅ๏ผๅฏน LLM ๅผๆๅฏ่ฝๆ็จ็ๆๆไฟกๆฏใ- ็นๅซๆฏ๏ผ่ฎฐๅฝๅทฅๅ ทๆง่ก้่ฏฏ็่ฏฆ็ปไฟกๆฏไผๅพๆๅธฎๅฉ๏ผ
ไพๅฆ๏ผ่ฟ้ๆไธไธชๆ นๆฎไฝ็ฝฎๅๆฅๆๆถ้ดๆฃ็ดขๅคฉๆฐๆฐๆฎ็ๅทฅๅ ท๏ผ
้ฆๅ ๏ผ่ฟๆฏไธไธช็ณ็ณ็็ๆฌ๏ผ
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 ้ๅๅทฅ็จๅทฅๅ ทๆฅไฟฎๅค้่ฏฏใไฝไธบไปไน่ฆ่ฎฉๅฎๅ่ฟไนๅค็น้็ๅทฅไฝๅข๏ผ
ๆๅปบ่ฟไธชๅทฅๅ ท็ๆดๅฅฝๆนๅผๅฆไธ๏ผ
@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
ๅๆฐไผ ้ไปปไฝ็ฑปๅ็ๅฏน่ฑก๏ผ
from smolagents import CodeAgent, InferenceClientModel
model_id = "meta-llama/Llama-3.3-70B-Instruct"
agent = CodeAgent(tools=[], model=InferenceClientModel(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
็่ฟ่ก่ฎฐๅฝ๏ผ
==================================================================================================== 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
] ็้ป่ฎค็ณป็ปๆ็คบ๏ผไธ้ข็็ๆฌ้่ฟ่ทณ่ฟ้ถๆ ทๆฌ็คบไพ่ฟ่กไบ็ผฉ็ญ๏ผใ
print(agent.prompt_templates["system_prompt"])
ไฝ ไผๅพๅฐ๏ผ
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:
---
Task: "Generate an image of the oldest person in this document."
Thought: I will proceed step by step and use the following tools: `document_qa` to find the oldest person in the document, then `image_generator` to generate an image according to the answer.
Code:
```py
answer = document_qa(document=document, question="Who is the oldest person mentioned?")
print(answer)
```<end_code>
Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."
Thought: I will now generate an image showcasing the oldest person.
Code:
```py
image = image_generator("A portrait of John Doe, a 55-year-old man living in Canada.")
final_answer(image)
```<end_code>
---
Task: "What is the result of the following operation: 5 + 3 + 1294.678?"
Thought: I will use python code to compute the result of the operation and then return the final answer using the `final_answer` tool
Code:
```py
result = 5 + 3 + 1294.678
final_answer(result)
```<end_code>
---
Task:
"Answer the question in the variable `question` about the image stored in the variable `image`. The question is in French.
You have been provided with these additional arguments, that you can access using the keys as variables in your python code:
{'question': 'Quel est l'animal sur l'image?', 'image': 'path/to/image.jpg'}"
Thought: I will use the following tools: `translator` to translate the question into English and then `image_qa` to answer the question on the input image.
Code:
```py
translated_question = translator(question=question, src_lang="French", tgt_lang="English")
print(f"The translated question is {translated_question}.")
answer = image_qa(image=image, question=translated_question)
final_answer(f"The answer is {answer}")
```<end_code>
---
Task:
In a 1979 interview, Stanislaus Ulam discusses with Martin Sherwin about other great physicists of his time, including Oppenheimer.
What does he say was the consequence of Einstein learning too much math on his creativity, in one word?
Thought: I need to find and read the 1979 interview of Stanislaus Ulam with Martin Sherwin.
Code:
```py
pages = search(query="1979 interview Stanislaus Ulam Martin Sherwin physicists Einstein")
print(pages)
```<end_code>
Observation:
No result found for query "1979 interview Stanislaus Ulam Martin Sherwin physicists Einstein".
Thought: The query was maybe too restrictive and did not find any results. Let's try again with a broader query.
Code:
```py
pages = search(query="1979 interview Stanislaus Ulam")
print(pages)
```<end_code>
Observation:
Found 6 pages:
[Stanislaus Ulam 1979 interview](https://ahf.nuclearmuseum.org/voices/oral-histories/stanislaus-ulams-interview-1979/)
[Ulam discusses Manhattan Project](https://ahf.nuclearmuseum.org/manhattan-project/ulam-manhattan-project/)
(truncated)
Thought: I will read the first 2 pages to know more.
Code:
```py
for url in ["https://ahf.nuclearmuseum.org/voices/oral-histories/stanislaus-ulams-interview-1979/", "https://ahf.nuclearmuseum.org/manhattan-project/ulam-manhattan-project/"]:
whole_page = visit_webpage(url)
print(whole_page)
print("\n" + "="*80 + "\n") # Print separator between pages
```<end_code>
Observation:
Manhattan Project Locations:
Los Alamos, NM
Stanislaus Ulam was a Polish-American mathematician. He worked on the Manhattan Project at Los Alamos and later helped design the hydrogen bomb. In this interview, he discusses his work at
(truncated)
Thought: I now have the final answer: from the webpages visited, Stanislaus Ulam says of Einstein: "He learned too much mathematics and sort of diminished, it seems to me personally, it seems to me his purely physics creativity." Let's answer in one word.
Code:
```py
final_answer("diminished")
```<end_code>
---
Task: "Which city has the highest population: Guangzhou or Shanghai?"
Thought: I need to get the populations for both cities and compare them: I will use the tool `search` to get the population of both cities.
Code:
```py
for city in ["Guangzhou", "Shanghai"]:
print(f"Population {city}:", search(f"{city} population")
```<end_code>
Observation:
Population Guangzhou: ['Guangzhou has a population of 15 million inhabitants as of 2021.']
Population Shanghai: '26 million (2019)'
Thought: Now I know that Shanghai has the highest population.
Code:
```py
final_answer("Shanghai")
```<end_code>
---
Task: "What is the current age of the pope, raised to the power 0.36?"
Thought: I will use the tool `wiki` to get the age of the pope, and confirm that with a web search.
Code:
```py
pope_age_wiki = wiki(query="current pope age")
print("Pope age as per wikipedia:", pope_age_wiki)
pope_age_search = web_search(query="current pope age")
print("Pope age as per google search:", pope_age_search)
```<end_code>
Observation:
Pope age: "The pope Francis is currently 88 years old."
Thought: I know that the pope is 88 years old. Let's compute the result using python code.
Code:
```py
pope_current_age = 88 ** 0.36
final_answer(pope_current_age)
```<end_code>
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:
{%- for tool in tools.values() %}
- {{ tool.name }}: {{ tool.description }}
Takes inputs: {{tool.inputs}}
Returns an output of type: {{tool.output_type}}
{%- endfor %}
{%- if managed_agents and managed_agents.values() | list %}
You can also give tasks to team members.
Calling a team member works similarly to calling a tool: provide the task description as the 'task' argument. Since this team member is a real human, be as detailed and verbose as necessary in your task description.
You can also include any relevant variables or context using the 'additional_args' argument.
Here is a list of the team members that you can call:
{%- for agent in managed_agents.values() %}
- {{ agent.name }}: {{ agent.description }}
{%- endfor %}
{%- endif %}
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 will 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.description }}"
๏ผ่ฟไบๅฐๅจ agent ๅๅงๅๆถ็จไบๆๅ
ฅๆไบ่ชๅจ็ๆ็ๅทฅๅ
ทๆ็ฎก็ agent ็ๆ่ฟฐใ
ๅ ๆญค๏ผ่ฝ็ถไฝ ๅฏไปฅ้่ฟๅฐ่ชๅฎไนๆ็คบไฝไธบๅๆฐไผ ้็ป system_prompt
ๅๆฐๆฅ่ฆ็ๆญค็ณป็ปๆ็คบๆจกๆฟ๏ผไฝไฝ ็ๆฐ็ณป็ปๆ็คบๅฟ
้กปๅ
ๅซไปฅไธๅ ไฝ็ฌฆ๏ผ
- ็จไบๆๅ
ฅๅทฅๅ
ทๆ่ฟฐใ
{%- for tool in tools.values() %} - {{ tool.name }}: {{ tool.description }} Takes inputs: {{tool.inputs}} Returns an output of type: {{tool.output_type}} {%- endfor %}
- ็จไบๆๅ
ฅ managed agent ็ๆ่ฟฐ๏ผๅฆๆๆ๏ผใ
{%- if managed_agents and managed_agents.values() | list %} You can also give tasks to team members. Calling a team member works similarly to calling a tool: provide the task description as the 'task' argument. Since this team member is a real human, be as detailed and verbose as necessary in your task description. You can also include any relevant variables or context using the 'additional_args' argument. Here is a list of the team members that you can call: {%- for agent in managed_agents.values() %} - {{ agent.name }}: {{ agent.description }} {%- endfor %} {%- endif %}
- ไป
้
CodeAgent
๏ผ"{{authorized_imports}}"
็จไบๆๅ ฅๆๆๅฏผๅ ฅๅ่กจใ
็ถๅไฝ ๅฏไปฅๆ นๆฎๅฆไธ๏ผๆดๆน็ณป็ปๆ็คบ๏ผ
agent.prompt_templates["system_prompt"] = agent.prompt_templates["system_prompt"] + "\nHere you go!"
่ฟไน้็จไบ [ToolCallingAgent
]ใ
4. ้ขๅค่งๅ
ๆไปฌๆไพไบไธไธช็จไบ่กฅๅ ่งๅๆญฅ้ชค็ๆจกๅ๏ผagent ๅฏไปฅๅจๆญฃๅธธๆไฝๆญฅ้ชคไน้ดๅฎๆ่ฟ่กใๅจๆญคๆญฅ้ชคไธญ๏ผๆฒกๆๅทฅๅ ท่ฐ็จ๏ผLLM ๅชๆฏ่ขซ่ฆๆฑๆดๆฐๅฎ็ฅ้็ไบๅฎๅ่กจ๏ผๅนถๆ นๆฎ่ฟไบไบๅฎๅๆจๅฎๅบ่ฏฅ้ๅ็ไธไธๆญฅใ
from smolagents import load_tool, CodeAgent, InferenceClientModel, WebSearchTool
from dotenv import load_dotenv
load_dotenv()
# ไป Hub ๅฏผๅ
ฅๅทฅๅ
ท
image_generation_tool = load_tool("m-ric/text-to-image", trust_remote_code=True)
search_tool = WebSearchTool()
agent = CodeAgent(
tools=[search_tool],
model=InferenceClientModel(model_id="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?",
)