指定结构化 JSON 模式
您所在的团队正在为一个在线教育平台设计新的交互式练习,学生可以提问,答案会通过图形视图展示。该问答功能由 LLM 提供支持,但图形视图需要一个包含 Question 和 Answer 字段的 JSON 输入,才能正确显示问题与答案:
{
"Question": "...",
"Answer": "..."
}
系统提示词和示例用户问题已存储在变量 messages 中。
本练习是课程的一部分
使用 Llama 3
练习说明
- 在模式中将
"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'])