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

การขอ structured output

ในการสร้าง lexical graph นั้น เราได้ดึงชื่อตัวละครและบทพูดจากข้อความโดยใช้ text splitter แต่วิธีนี้มักทำให้สูญเสียบริบทบริเวณขอบเขตที่ตัด เช่น stage direction ในข้อความสามารถนำมาใช้ระบุได้ว่าตัวละครกำลังพูดกับใครในขณะนั้น

เราจะใช้คลาส Character ภายในคลาส Line ใหม่เพื่ออธิบายบทพูด ผู้พูด และรายชื่อตัวละครที่บทพูดนั้นพูดถึง นอกจากนี้ยังจะสร้างคลาส LineOutput เพื่อดึงบทพูดหลายบรรทัดพร้อมกัน

ตัวแปร prompt ที่มีคำแนะนำสำหรับการดึงข้อมูลได้ถูกกำหนดไว้ให้แล้ว พร้อมกับ instance ของ LLM ในชื่อ llm

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

Graph RAG ด้วย LangChain และ Neo4j

ดูคอร์ส

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

  • อัปเดตคลาส LineOutput ให้รองรับรายการของออบเจกต์ Line
  • กำหนด LLM ที่สร้าง structured output โดยใช้คลาส LineOutput

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

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

class Line(BaseModel):
    text: str = Field(..., description="The text of the line")
    spoken_by: Character = Field(..., description="The character who speaks the line")
    spoken_to: Optional[list[Character]] = Field(None, description="The character who the line is spoken to")

class LineOutput(BaseModel):
    # Request more than one line from the text
    lines: ____[Line] = Field(..., description="The lines from the text")

# Return an instance of LineOutput
structured_llm = llm.____(____)

for line in structured_llm.invoke(prompt.format_messages(text=text)):
	print(line)
แก้ไขและรันโค้ด