Introduction to Agentic RAG

AI Agents ด้วย Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Agentic RAG: การดึงข้อมูลและการให้เหตุผลแบบวนซ้ำ

AI Agents ด้วย Hugging Face smolagents

Stateless vs. Stateful Tools ใน smolagents

Function Tools (decorator @tool)

  • Stateless: ไม่มีหน่วยความจำระหว่างการเรียกใช้
  • จดจำ vector store ระหว่างการเรียกใช้ไม่ได้

Class-Based Tools (คลาสย่อยของ Tool)

  • Stateful: เก็บการอ้างอิงข้ามการเรียกใช้ได้
  • จัดเก็บออบเจ็กต์ที่ซับซ้อนได้
AI Agents ด้วย Hugging Face smolagents

โครงสร้างของ Class-Based Tool

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 ด้วย Hugging Face smolagents

สร้าง Recipe Search Tool

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 ด้วย Hugging Face smolagents

Cooking Assistant Agent: รวมทุกส่วนเข้าด้วยกัน

# 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 ด้วย Hugging Face smolagents

ตัวอย่างการรัน Agent

Query: วิธีปรุงปลาแซลมอนกับสมุนไพรโดยใช้เทคนิคระดับมืออาชีพ?

[Step 1] ค้นหา "salmon herbs cooking techniques"...

[Step 2] ค้นหา "professional salmon preparation"...

[Final Answer]

"ซับเนื้อปลาให้แห้ง ปรุงรสด้วยสมุนไพร แล้ว:

  • ทอดด้านหนังลงในกระทะ ราดเนย กระเทียม ไทม์
  • หรืออบที่ 200°C กับเนยมะนาวสมุนไพร 10 ถึง 12 นาที"
AI Agents ด้วย Hugging Face smolagents

มาฝึกกันเถอะ!

AI Agents ด้วย Hugging Face smolagents

Preparing Video For Download...