文本摘要与编辑

使用 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. 客户因密码问题与未收到重置链接而无法登录。  
2. 支持在确认已发送后重新发送了重置邮件。  
3. 客户改用 Google 登录后问题解决。
使用 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

理解 tokens

$$

  • Tokens:帮助模型理解与解析文本的最小单位

$$

句子"如何让 OpenAI API 带来业务价值?"其中每个 token 用不同颜色标注。

1 https://platform.openai.com/tokenizer
使用 OpenAI API

成本计算

 

  • 费用取决于所用模型token数量 💰

    • 按"成本/每千 token"计价
    • 输入与输出 token 价格可能不同
  • 提高 max_completion_tokens 会增加成本 📈

Screenshot 2025-03-05 at 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

Vamos praticar!

使用 OpenAI API

Preparing Video For Download...