เขียนโค้ดผ่าน Prompt: ดอกเบี้ยทบต้น
สมมติว่าคุณออมเงินจำนวนคงที่ทุกเดือนในบัญชีที่มีดอกเบี้ยทบต้น
แทนที่จะคำนวณด้วยมือหรือใช้ spreadsheet คุณสามารถใช้ agent ให้เขียนโค้ดและตอบคำถามแทนได้
หมายเหตุ: สำหรับทุกแบบฝึกหัดในคอร์สนี้ ตัวแปร model ได้รับการกำหนดค่าเรียบร้อยแล้ว โดยเชื่อมต่อกับโมเดล OpenAI ในเบื้องหลัง ไม่ต้องตั้งค่าหรือใส่ API key ใดๆ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
AI Agents ด้วย Hugging Face smolagents
คำแนะนำการฝึกหัด
- นำเข้าคลาส
CodeAgentจากไลบรารีsmolagents - สร้าง instance ของ
CodeAgentโดยส่ง list ว่างสำหรับ tools - เรียกใช้เมธอด
.run()บน agent แล้วส่ง 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)