시작하기무료로 시작하기

상태를 설계로 담다: RAG 검색 도구 만들기

이전 레슨에서는 임베딩을 사용해 가전제품 매뉴얼의 벡터 기반 지식 베이스를 구축하고 검색하는 방법을 살펴봤어요.

이제 이 검색 로직을 감싸는 커스텀 도구를 만들어, 에이전트가 가전제품 관련 질문에 답할 수 있도록 하겠습니다.

여러분이 작성할 도구는 Tool 베이스 클래스를 상속하고, 입력 하나(가전제품 작동에 관한 질문)를 받습니다.

이미 다음에 접근할 수 있어요:

  • 미리 구축된 FAISS 인덱스를 담은 vector_store 변수
  • 검색할 준비가 된, 가전제품 매뉴얼 내용의 문서 청크와 그 임베딩

여러분의 역할은 이 지식 베이스를 에이전트가 활용할 수 있도록, 도구의 구조와 로직을 구현하는 것입니다.

이 연습은 강의의 일부입니다

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

강의 보기

연습 안내

  • __init__() 메서드에서 vector_store 매개변수를 받으세요.
  • forward() 메서드의 매개변수로 query를 추가하세요.
  • 유사도 검색에서 반환할 관련 문서 수를 설정하기 위해 self.k를 사용하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

class ApplianceSearchTool(Tool):
    name = "appliance_manual_search"
    description = "Search appliance manuals for maintenance and usage information"
    inputs = {"query": {"type": "string", "description": "Question about appliance operation"}}
    output_type = "string"

    # Pass the vector store into the constructor
    def __init__(self, ____, k=3):
        super().__init__()
        self.vector_store = vector_store
        self.k = k

    # Accept the query string as input to the forward method
    def forward(self, ____):
        # Use self.k here to specify how many chunks to return
        docs = self.vector_store.similarity_search(query, k=____)
        return "\n\n".join(doc.page_content for doc in docs) or "No relevant manual sections found."
코드 편집 및 실행