零样本提示:基于评论的分类
除了回答问题、转换文本和生成新文本,OpenAI 的模型还可用于分类任务,例如归类与情感分析。
在本练习中,您将使用一家名为 Toe-Tally Comfortable 的线上鞋店的评论,探索如何用 OpenAI 的对话模型进行情感分类。
本练习是课程的一部分
使用 OpenAI API
练习说明
- 定义一个
prompt,使用数字1到5对给定语句的情感进行分类(正向到负向)。 - 创建一个对 Chat Completions 端点的请求,将该提示发送给
gpt-4o-mini。
交互式实操练习
通过完成这段示例代码来试试这个练习。
client = OpenAI(api_key="")
# Define a multi-line prompt to classify sentiment
prompt = """____:
1. Unbelievably good!
2. Shoes fell apart on the second use.
3. The shoes look nice, but they aren't very comfortable.
4. Can't wait to show them off!"""
# Create a request to the Chat Completions endpoint
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": ____}],
max_completion_tokens=100
)
print(response.choices[0].message.content)