에이전트형 RAG 소개

Hugging Face smolagents로 AI 에이전트 만들기

Adel Nehme

VP of AI Curriculum, DataCamp

에이전트형 RAG: 반복 검색 + 추론

Hugging Face smolagents로 AI 에이전트 만들기

smolagents의 무상태 vs 상태 유지 도구

함수형 도구(@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단계] "salmon herbs cooking techniques" 검색...

[2단계] "professional salmon preparation" 검색...

[최종 답변]

"필레의 물기를 닦고, 허브로 간한 뒤:

  • 껍질 쪽을 아래로 팬 시어링하고 버터·마늘·타임으로 베이스트
  • 또는 레몬·허브 버터와 함께 200°C에서 10~12분 굽기"
Hugging Face smolagents로 AI 에이전트 만들기

연습해 봅시다!

Hugging Face smolagents로 AI 에이전트 만들기

Preparing Video For Download...