尋找並取代
「尋找並取代」工具已經出現好幾十年,但通常只限於比對並替換完全相同的單字或片語。現在你拿到一段討論汽車的文字,你將使用 chat completion 模型把內容改寫成討論飛機,同時適當地更新文字。
警告:如果你在短時間內送出大量請求或使用很多 tokens,可能會觸發頻率限制並看到 openai.error.RateLimitError。如果出現這個錯誤,請等待約 1 分鐘讓配額重設,之後就能繼續送出更多請求。
本練習屬於課程
Working with the OpenAI API
練習說明
- 建立一個對 Chat Completions 端點的請求;最大使用
100個 tokens。 - 從 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].____.____)