Mục Tiêu Lab
Implement một ReAct agent hoàn chỉnh không dùng framework để hiểu sâu cơ chế hoạt động.
1. Setup Environment
Cài đặt openai và python-dotenv packages.
2. Define Tools
Tạo các Tool classes với name, description và function handler. Mỗi tool cần method execute() để chạy logic.
3. System Prompt Engineering
Prompt cần define:
- Available tools và mô tả của chúng
- Response format (Thought/Action/Observation)
- Rules: Luôn bắt đầu bằng Thought, chỉ 1 action mỗi lượt
4. The ReAct Loop
- Gửi task đến LLM
- Parse response để tìm Action
- Execute tool, lấy Observation
- Đưa Observation vào context
- Repeat cho đến khi có Final Answer
5. Key Observations
- Mỗi iteration thêm context → Token usage tăng dần
- Error trong tool → Agent phải recover
- Parsing brittle → Cần structured outputs (JSON mode)
📝 Bài Tập: Thêm Tool thứ 3 là get_weather(city) và test agent với câu hỏi về thời tiết và gợi ý đồ uống.
