テキストの 要約と編集

OpenAI APIを使いこなす

James Chapman

Curriculum Manager, DataCamp

ここまでの振り返り

  • 質問&回答
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "user", "content": "How many days are in October?"}]
)

print(response.choices[0].message.content)
October has 31 days.
OpenAI APIを使いこなす

テキスト編集

  • :名前、代名詞、役職名の編集

$$

prompt = """
Update name to Maarten, pronouns to he/him, and job title to Senior Content Developer
in the following text:

Joanne is a Content Developer at DataCamp. Her favorite programming language is R,
which she uses for her statistical analyses.
"""
OpenAI APIを使いこなす

テキスト編集

response = client.chat.completions.create(
    model="gpt-4o-mini",

messages=[{"role": "user", "content": prompt}]
) print(response.choices[0].message.content)
Maarten is a Senior Content Developer at DataCamp. His favorite programming language
is R, which he uses for his statistical analyses.
OpenAI APIを使いこなす

テキスト要約

  • :カスタマーサポートチャットの要約

カスタマーサポートチーム

text = """
Customer: Hi, I'm trying to log into 
my account, but it keeps saying 
my password is incorrect. I'm sure 
I'm entering the right one.  

Support: I'm sorry to hear that! 
Have you tried resetting your password?  
...
"""
OpenAI APIを使いこなす

テキスト要約

prompt = f"""Summarize the customer support chat 
             in three concise key points: {text}"""


response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": prompt}] ) print(response.choices[0].message.content)
1. Customer couldn't log in due to a password issue and missing reset link.  
2. Support resent the reset email after confirming it was sent.  
3. Customer resolved the issue by using Google sign-in.
OpenAI APIを使いこなす

回答の長さを調節する

  • max_completion_tokens = 5
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "user", 
  "content": "Write a haiku about AI."}],
  max_completion_tokens=5
)

AI so powerful
Computers
  • max_completion_tokens = 30
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "user", 
  "content": "Write a haiku about AI."}],
  max_completion_tokens=30
)

A machine mind thinks
Logic dictates its choices
Mankind ponders anew
OpenAI APIを使いこなす

トークンとは

$$

  • トークン:AIがテキストを理解するための単位

$$

文「OpenAI APIはどのようにビジネス価値を提供できるか?」が、各トークンごとに異なる色で強調表示されている。

1 https://platform.openai.com/tokenizer
OpenAI APIを使いこなす

コストの計算

  • モデルトークン数に応じた費用💰

    • コスト/各トークンで価格設定
    • 入力トークンと出力トークンは異なる場合がある
  • max_completion_tokensが増えるとコスト増 📈

スクリーンショット 2025-03-05 11.46.54.png

OpenAI APIを使いこなす

コストの計算

prompt = f"""Summarize the customer support chat 
             in three concise key points: {text}"""

max_completion_tokens = 500

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "user", "content": prompt}], 
  max_completion_tokens=max_completion_tokens
  )

OpenAI APIを使いこなす

コストの計算

# Define price per token
input_token_price = 0.15 / 1_000_000
output_token_price = 0.6 / 1_000_000

# Extract token usage input_tokens = response.usage.prompt_tokens
output_tokens = max_completion_tokens
# Calculate cost cost = (input_tokens * input_token_price + output_tokens * output_token_price) print(f"Estimated cost: ${cost}")
Estimated cost: $0.00124
1 https://openai.com/pricing
OpenAI APIを使いこなす

練習しましょう!

OpenAI APIを使いこなす

Preparing Video For Download...