建立結構化輸出
結構化輸出是從大型語言模型中擷取一致結果的關鍵,對許多應用都至關重要。
在這裡,你將使用 pydantic 函式庫,透過一個 Character 類別來定義描述《羅密歐與茱麗葉》角色的屬性。下一個練習中,你會用它從劇本的一段文字中,以結構化輸出的方式擷取角色。
本練習屬於課程
使用 LangChain 與 Neo4j 的 Graph RAG
練習說明
- 建立一個
Character類別,並從合適的pydantic基底類別延伸。 - 定義此類別的
name屬性。 - 定義可選的
family屬性,以處理家族背景可能未知的角色。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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")