指定結構化的 JSON 結構(schema)
你是線上教育平台團隊的一員,正在設計新的互動式練習。學生可以提出問題,答案會以圖形化視覺呈現。這個問答功能由 LLM 提供,但圖形化視覺需要一個包含 Question 與 Answer 欄位的 JSON 輸入,才能正確顯示問題與答案:
{
"Question": "...",
"Answer": "..."
}
系統提示詞(system prompt)與使用者範例問題已經儲存在 messages 變數中。
本練習屬於課程
使用 Llama 3
練習說明
- 在結構(schema)中,將
"Question"與"Answer"欄位指定為"string"型別。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
output = llm.create_chat_completion(
messages=messages,
response_format={
"type": "json_object",
"schema": {
"type": "object",
# Set the properties of the JSON fields and their data types
"properties": {"____": {"type": "____"}, "____": {"type": "____"}}
}
}
)
print(output['choices'][0]['message']['content'])