开始使用免费开始使用

Writing Code Through Prompts: Compound Interest

You're setting aside a fixed amount of money each month into a savings account that earns interest.

Rather than calculating compound interest manually or in a spreadsheet, you decide to use an agent to write the code for you and answer your queries.

Note: For all exercises in this course, a model variable has already been initialized for you. It provides access to an OpenAI model behind the scenes. No setup or API keys required.

本练习是课程的一部分

AI Agents with Hugging Face smolagents

查看课程

练习说明

  • Import the CodeAgent class from the smolagents library.
  • Create an instance of CodeAgent, passing it an empty list for tools.
  • Call the .run() method on your agent and pass in your task string.

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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)
编辑并运行代码