開始使用免費開始

提供 few-shot 範例

雖然結構化輸出可以描述你希望資料如何回傳,但在「應該回傳哪些內容」上仍有不少解讀空間。

Field 類別允許你提供範例值,讓 LLM 知道良好擷取的樣子。這就是為了結構化輸出所做的 few-shot 提示範例。

你將擴充 Character 類別,加入用來唯一識別角色的 id 欄位。你想提供模型 2 個可參考的範例值:"romeo-montague""lady-capulet"

本練習屬於課程

使用 LangChain 與 Neo4j 的 Graph RAG

檢視課程

練習說明

  • 指定關鍵字,為 id 屬性提供範例值。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

class Character(BaseModel):
	id: str = Field(
		description="A unique identifer consisting of the Character's given name and their family name in slug case.", 
		# Provide example values of an ID
		____=["romeo-montague", "lady-capulet"]
	)
	name: str = Field(description="The name of the character")

chain = prompt | llm.with_structured_output(Character)

chain.invoke({"text": """JULIET.
O Romeo, Romeo, wherefore art thou Romeo? Deny thy father and refuse thy name. Or if thou wilt not, be but sworn my love, And I’ll no longer be a Capulet."""})
編輯並執行程式碼