開始使用免費開始

用提示寫程式碼:複利計算

你每個月固定存一筆金額到有利息的儲蓄帳戶。

與其手動或用試算表計算複利,你決定用代理替你撰寫程式碼並回答你的問題。

注意:在本課程的所有練習中,已經為你初始化了一個 model 變數。它在背景中提供對 OpenAI 模型的存取。不需要任何設定或 API 金鑰。

本練習屬於課程

使用 Hugging Face smolagents 的 AI 代理

檢視課程

練習說明

  • smolagents 函式庫匯入 CodeAgent 類別。
  • 建立一個 CodeAgent 執行個體,並傳入空的工具清單。
  • 在你的代理上呼叫 .run() 方法,並傳入你的任務字串。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import the CodeAgent class
from smolagents import ____

# Create a basic agent without tools
agent = CodeAgent(tools=____, model=model)

task = "I deposit $100 every month into an account that pays 5% annual interest, compounded monthly. Calculate the total balance after 10 years."

# Run the agent
result = agent.____(task)
print(result)
編輯並執行程式碼