始める無料で始める

検索と置換

検索と置換のツールは何十年も前から存在していますが、たいていは完全に一致する単語やフレーズを特定して置き換えることしかできません。ここでは、車について書かれた文章が用意されています。チャット completion モデルを使って、この文章を飛行機についての内容に適切に書き換えてみましょう。

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

Python で DeepSeek を使いこなす

コースを見る

演習の手順

  • prompt を deepseek-ai/DeepSeek-V4.1-Flash に送信するリクエストを作成し、車についての文章を飛行機についての内容に変更します。トークンの上限は 100 とします。
  • API からのテキストレスポンスを抽出して出力します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")

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 model
response = client.chat.completions.create(
  model="deepseek-ai/DeepSeek-V4.1-Flash",
  messages=[{"role": "user", "____": ____}],
  ____
)

# Extract and print the response text
print(response.choices[0].____.____)
コードを編集して実行