通过示例对话提供上下文
假设有一家名为 MyPersonalDelivery 的配送服务公司,提供各类物品的多种配送选项。您希望创建一个客服聊天机器人,能为客户提供他们所需的一切支持。为此,您将通过一段先前的对话,提供公司可配送物品的 context_question 和 context_answer,然后通过新的用户提示来测试模型是否识别到这些上下文。
OpenAI 包以及 context_question 和 context_answer 字符串已为您预先加载。
本练习是课程的一部分
使用 OpenAI API 的 Prompt Engineering
练习说明
- 定义一个
system_prompt,用于说明聊天机器人的用途,并引导其以温和的方式回答问题。 - 使用
system_prompt、context_question和context_answer来构建一段对话,让聊天机器人据此作为上下文来回答新的用户问题。
交互式实操练习
通过完成这段示例代码来试试这个练习。
client = OpenAI(api_key="")
# Define the system prompt
system_prompt = "____"
context_question = "What types of items can be delivered using MyPersonalDelivery?"
context_answer = "We deliver everything from everyday essentials such as groceries, medications, and documents to larger items like electronics, clothing, and furniture. However, please note that we currently do not offer delivery for hazardous materials or extremely fragile items requiring special handling."
# Add the context to the model
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "____", "content": ____},
{"role": "____", "content": ____},
{"role": "____", "content": ____ },
{"role": "user", "content": "Do you deliver furniture?"}])
response = response.choices[0].message.content
print(response)