始める無料で始める

検索と置換

検索・置換ツールは数十年前からありますが、厳密に一致する単語やフレーズを見つけて置き換えるだけの機能にとどまることが多いです。ここに、自動車に関するテキストがあります。Chat completionモデルを使って、このテキストを飛行機に関する内容に書き換えましょう。

注意:短時間に多くのリクエストを送信したり、大量のトークンを使用すると、レート制限に達し、openai.error.RateLimitErrorのエラーが表示されることがあります。その場合は、1分ほど待って容量をリセットしてから、再度リクエストを送信してください。

この演習はコースの一部です

OpenAI APIを使いこなす

コースを見る

演習の手順

  • Chat Completionsエンドポイントへのリクエストを作成し、最大トークン数を 100に設定してください。
  • 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].____.____)
コードを編集して実行