Giới thiệu về Agentic RAG

AI Agents với Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Agentic RAG: Truy xuất lặp + Suy luận

AI Agents với Hugging Face smolagents

Công cụ Stateless vs. Stateful trong smolagents

Công cụ hàm (trang trí @tool)

  • Stateless: không có bộ nhớ giữa các lần gọi
  • Không thể nhớ vector store giữa các lần gọi

Công cụ dạng lớp (kế thừa Tool)

  • Stateful: giữ tham chiếu qua các lần gọi
  • Lưu đối tượng phức tạp
AI Agents với Hugging Face smolagents

Cấu trúc của công cụ dạng lớp

from smolagents import Tool

class ToolName(Tool):
    name = "tool_name"

description = "Clear description for the agent"
inputs = { "parameter_name": {"type": "parameter_type", "description": "Parameter purpose"} } output_type = "string"
def __init__(self, custom_parameters): super().__init__() self.custom_attribute = custom_parameters
def forward(self, parameter_name): # Agent calls this method return "processed output"
AI Agents với Hugging Face smolagents

Xây dựng công cụ tìm công thức

class RecipeSearchTool(Tool):
    name = "recipe_search" 
    description = "Search cooking documentation for recipes, techniques, and meal planning information"
    inputs = {
        "query": {"type": "string", "description": "Natural language cooking query"}
    }
    output_type = "string"

def __init__(self, vectorstore, k=6): super().__init__() self.vectorstore = vectorstore self.k = k
def forward(self, query): docs = self.vectorstore.similarity_search(query, k=self.k) return "\n\n".join(doc.page_content for doc in docs) or "Nothing found."
AI Agents với Hugging Face smolagents

Trợ lý nấu ăn: Tổng hợp lại

# Initialize the retrieval tool
recipe_search = RecipeSearchTool(vector_store)

agent = CodeAgent(
    tools=[recipe_search],
    model=model,
    instructions="Search thoroughly to provide complete recipe answers. 
      If initial results seem incomplete, try different search terms.",
    verbosity_level=1,
    max_steps=8
)
AI Agents với Hugging Face smolagents

Ví dụ chạy tác tử

Truy vấn: Làm cá hồi với thảo mộc theo kỹ thuật chuyên nghiệp như thế nào?

[Bước 1] Tìm "salmon herbs cooking techniques"...

[Bước 2] Tìm "professional salmon preparation"...

[Câu trả lời cuối]

"Thấm khô phi-lê, ướp muối tiêu và thảo mộc, rồi:

  • Áp chảo mặt da xuống, rưới bơ, tỏi, thyme
  • Hoặc nướng 200°C với bơ chanh & thảo mộc trong 10–12 phút"
AI Agents với Hugging Face smolagents

Ayo berlatih!

AI Agents với Hugging Face smolagents

Preparing Video For Download...