completions
Migrate to the Responses API
https://platform.openai.com/docs/guides/migrate-to-responses
Chat Completions API
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-5",
messages=[
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
)
print(completion.choices[0].message.content)
Responses API
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5",
input="Write a one-sentence bedtime story about a unicorn."
)
print(response.output_text
https://www.devze.com/dev/1748325Inq6YxO0o
- 总结
chat/completions 和 completions 是 OpenAI API 中的两个不同的端点,它们提供了不同的功能和交互模式。以下是它们的主要区别:
completions 端点
用途:
- 主要用于生成文本补全。你提android供一个提示(prompt),模型会基于这个提示生成后续的文本。
交互模式:
- 单次请求-响应模式。你发送一个提示,模型返回一个补全结果。
适用场景:
- 适用于需要连续生成文本的场景,如编写文章、代码补全、生成故事等。
示例请求:
{
"model": "text-davinci-003",
"prompt": "Once upon a time, in a land far, far away,",
"max_tokens": 100
}
示例响应:
{
"id": "cmpl-5eU3oZz1w9Q8Jt3B3o5Q5Z5Z1",
"object": "text_completion",
"created": 1609459200,
"model": js"text-davinci-003",
"choices": [
{
"text": " there lived a wise old owl who knew all the secrets of the forest...",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 100,
"total_tokens": 110
}
}
chat/completions 端点
用途:
- 主要用于对话生成。你提供一系列对话消息,模型会基于这些消息生成下一条回复。
交互模式:
- 多轮对话模式。你可以提供一个包含多轮对话的消息列表,模型会基于整个对话上下文生成回复。
适用场景:
- 适用于需要多轮对话的场景,如聊天机器人、客户服务、对话系统等。
示例请求:
{
"model": "gpt-4",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
}
示例响应:
{
"id": "chatcmpl-5eU3oZz1w9Q8Jt3B3编程o5Q5Z5Z1",
"object": "chat.completion",
"created": 1609459200,
"model": "gpt-4",
"choices": [
{
"message": {
"role": "assandroidistant",
"content": "The 2020 World Series was played at Globe Life Field in Arlington, Texas."
},
"index": 0,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 50,
"completion_tokens": 20,
"total_tokens": 70
}
}
总结
completions端点适用于单次文本补全任务,通常用于连续文本生成。chat/completions端点适用于多轮对话生成任务,提供更自然的对话体验。
选择哪个端点取决于你的具体需求。
- 如果你需要生成连续的文本,
completions端点可能更合适。 - 如果你需要处理多轮对话,
chat/completions端点会更适合。
https://github.com/datawhalechina/hello-agents/blob/main/code/chapter11/01_dataset_loading.py
def load_sft_dataset():
"""
使用RLTrainingTool加载SFT格式的GSM8K数据集
SFT数据格式:
{
"prompt": "Question: ...\n\nLet's solve this step by step:\n",
"completion": "Step 1: ...\nFinal Answer: 42",
"text": "Question: ...\n\nLet's solve this step by step:\nStep 1: ...\nFinal Answer: 42"
}
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

浙公网安备 33010602011771号