开始使用免费开始使用

文本摘要

使用 OpenAI 模型的一个非常常见场景是对文本进行摘要。这在商业环境中用途广泛,例如把冗长的报告压缩成简洁的一页稿或几条要点,或为不同相关方提取后续步骤和时间线。

在本练习中,您将使用对话补全模型,把一段关于金融投资的文本(finance_text)总结为两条精炼的项目符号要点。

本练习是课程的一部分

使用 OpenAI API

查看课程

练习说明

  • 使用 f-string 将 finance_text 插入到 prompt 中。
  • 创建请求,发送提供的 prompt;将最大 token 数设置为 400

交互式实操练习

通过完成这段示例代码来试试这个练习。

client = OpenAI(api_key="")

# Use an f-string to format the prompt
prompt = f"""Summarize the following text into two concise bullet points:
{____}"""

# Create a request to the Chat Completions endpoint
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "user", "content": prompt}],
  ____
)

print(response.choices[0].message.content)
编辑并运行代码