การสร้าง structured output
Structured output มีบทบาทสำคัญในการดึงผลลัพธ์ที่สม่ำเสมอจาก Large Language Model ซึ่งจำเป็นอย่างยิ่งในหลายแอปพลิเคชัน
ในแบบฝึกหัดนี้ จะใช้ไลบรารี pydantic เพื่อกำหนด property ที่ใช้อธิบายตัวละครใน Romeo and Juliet ผ่านคลาส Character และในแบบฝึกหัดถัดไป จะนำคลาสนี้ไปใช้ดึงข้อมูลตัวละครในรูปแบบ structured output จากบทละครส่วนหนึ่ง
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Graph RAG ด้วย LangChain และ Neo4j
คำแนะนำการฝึกหัด
- สร้างคลาส
Characterที่สืบทอดมาจาก base class ของpydanticที่เหมาะสม - กำหนด property
nameของคลาส - กำหนด property
familyแบบ optional เพื่อรองรับตัวละครที่อาจไม่ทราบชื่อตระกูล
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
from pydantic import BaseModel, Field
from typing import Optional
# Inherit the correct pydantic class
class Character(____):
# Describe the name property
name: str = ____(description="The full name of the character")
# Set family to optional
____: Optional[str] = Field(description="The name of the family they belong to")