เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

ออกแบบให้มีสถานะ: สร้าง RAG Search Tool

ในบทเรียนก่อนหน้า คุณได้เห็นวิธีสร้างและค้นหาฐานความรู้ที่ใช้เวกเตอร์จากคู่มือเครื่องใช้ไฟฟ้าด้วย embedding แล้ว

คราวนี้จะมาสร้าง tool แบบกำหนดเองที่ห่อหุ้มตรรกะการค้นหานี้ไว้ เพื่อให้ agent สามารถนำไปใช้ตอบคำถามเกี่ยวกับเครื่องใช้ไฟฟ้าได้

Tool ที่จะสร้างนี้จะ subclass มาจากคลาสพื้นฐาน Tool และรับ input หนึ่งอย่าง ได้แก่ คำถามเกี่ยวกับการใช้งานเครื่องใช้ไฟฟ้า

สิ่งที่มีอยู่แล้ว:

  • ตัวแปร vector_store ซึ่งเก็บ FAISS index ที่สร้างไว้ล่วงหน้า
  • ชิ้นส่วนเอกสารที่มีเนื้อหาจากคู่มือเครื่องใช้ไฟฟ้า ผ่านการทำ embedding และพร้อมสำหรับการค้นหา

หน้าที่ของคุณคือกำหนดโครงสร้างและตรรกะของ tool เพื่อให้ agent เข้าถึงฐานความรู้นี้ได้

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

AI Agents ด้วย Hugging Face smolagents

ดูคอร์ส

คำแนะนำการฝึกหัด

  • รับพารามิเตอร์ vector_store ในเมธอด __init__()
  • เพิ่ม query เป็นพารามิเตอร์ของเมธอด forward()
  • ใช้ self.k เพื่อกำหนดจำนวนเอกสารที่เกี่ยวข้องซึ่งจะดึงมาจาก similarity search

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

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."
แก้ไขและรันโค้ด