エージェンティックRAGの概要

Hugging Face smolagents で学ぶ AI エージェント

Adel Nehme

VP of AI Curriculum, DataCamp

エージェンティックRAG:反復的な検索と推論

Hugging Face smolagents で学ぶ AI エージェント

smolagentsにおけるステートレスとステートフルのツール

関数ツール(@toolデコレーター)

  • ステートレス:呼び出し間でメモリなし
  • 呼び出し間でベクターストアを保持できない

クラスベースのツール(Toolサブクラス)

  • ステートフル:呼び出し間で参照を保持
  • 複雑なオブジェクトを保存可能
Hugging Face smolagents で学ぶ AI エージェント

クラスベースのツールの構造

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"
Hugging Face smolagents で学ぶ AI エージェント

レシピ検索ツールの構築

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."
Hugging Face smolagents で学ぶ AI エージェント

料理アシスタントエージェント:全体の統合

# 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
)
Hugging Face smolagents で学ぶ AI エージェント

エージェント実行例

クエリ:プロの技術を使ってハーブでサーモンを調理するには?

[ステップ1] 「サーモン ハーブ 調理テクニック」を検索...

[ステップ2] 「プロのサーモン調理法」を検索...

[最終回答]

「フィレをよく水気を拭き取り、ハーブで味付けし:

  • 皮面を下にしてフライパンでバター・ニンニク・タイムをかけながらソテー
  • またはレモン&ハーブバターを塗って200℃で10〜12分焼く」
Hugging Face smolagents で学ぶ AI エージェント

練習しましょう!

Hugging Face smolagents で学ぶ AI エージェント

Preparing Video For Download...