Giới thiệu
Agent cơ bản hoạt động theo kiểu "nghĩ 1 bước, làm 1 bước". Agent nâng cao cần biết lên kế hoạch, tự đánh giá, và tự sửa lỗi. Bài này cover các design patterns quan trọng nhất.
1. Plan-and-Execute Pattern
User Request → Planner (tạo plan) → Executor (thực hiện từng step) → Re-planner (adjust)
2. Self-Reflection / Critic Loop
def reflect_and_improve(agent_output, original_task):
critic_prompt = f"""
Task: {original_task}
Agent Output: {agent_output}
Evaluate:
1. Does it fully answer the task? (completeness)
2. Is the information accurate? (accuracy)
3. Is it well-structured? (quality)
If issues found, provide specific improvements.
"""
feedback = call_llm(critic_prompt)
if "APPROVED" in feedback:
return agent_output
else:
improved = call_llm(f"Improve based on feedback: {feedback}")
return improved
3. Tree-of-Thought
Thay vì 1 chain of thought, explore nhiều nhánh suy nghĩ song song, rồi chọn nhánh tốt nhất.
Tóm tắt
- Plan-and-Execute: lên kế hoạch trước, thực hiện sau
- Self-Reflection: critic loop cải thiện output quality
- Tree-of-Thought: explore nhiều reasoning paths
- Combine patterns cho agent mạnh nhất
Bài tập
- Implement Plan-and-Execute agent
- Thêm self-reflection loop (max 3 iterations)
- So sánh output quality: with vs without reflection
- Implement Tree-of-Thought cho math problem solving