开始使用免费开始使用

查找并替换

查找替换工具已经存在数十年,但它们通常只能识别并替换完全匹配的单词或短语。现在您拿到了一段关于汽车的文本,您将使用聊天补全模型把它改写为关于飞机的文本,并做出相应的上下文调整。

警告:如果您在短时间内发送大量请求或使用了很多 token,可能会触发速率限制并看到 openai.error.RateLimitError。如果出现该错误,请等待一分钟让配额重置,然后您就可以继续发送更多请求。

本练习是课程的一部分

使用 OpenAI API

查看课程

练习说明

  • 向 Chat Completions 端点创建一个请求;最多使用 100 个 token。
  • 从 API 中提取并打印文本回复。

交互式实操练习

通过完成这段示例代码来试试这个练习。

client = OpenAI(api_key="")

prompt="""Replace car with plane and adjust phrase:
A car is a vehicle that is typically powered by an internal combustion engine or an electric motor. It has four wheels, and is designed to carry passengers and/or cargo on roads or highways. Cars have become a ubiquitous part of modern society, and are used for a wide variety of purposes, such as commuting, travel, and transportation of goods. Cars are often associated with freedom, independence, and mobility."""

# Create a request to the Chat Completions endpoint
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "user", "____": ____}],
  ____
)

# Extract and print the response text
print(response.choices[0].____.____)
编辑并运行代码