N3xtchen 的数字花园

Search

Search IconIcon to open search

OpenAI API: 如何编写 message?

上次更新于 Mar 21, 2023 编辑源文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  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?"}
    ]
)

上面的这个例子,进行第二轮的问答,通过传如上一轮的问题,让助手能够感知上下文

通常情况,一个对话遵循的模式是一个 system (系统) 信息开头,接着是 user (用户)和 assistant (助手)交替出现:

gpt-3.5-turbo-0301 并不会永远强关注 system 信息。后续的模型将会改进。

# 对比 Text Completions

messages 的虽然比 prompts 显得冗长了很多,但是书写结构化好了很多。使用 prompts 估计模型无法区分内容的产生于谁,注意力机制的关注点会有所发散,助手应该更加关注用户输入的内容,这点上 messages 就能够表现的更好了,至少可以对录入的内容进行加权(这个是我猜测的)。

下面是官方将 prompt 转化成 messages 的例子:

prompt:

1
Translate the following English text to French: "{text}"

messages:

1
2
3
4
[
  {"role": "system", "content": "You are a helpful assistant that translates English to French."},
  {"role": "user", "content": 'Translate the following English text to French: "{text}"'}
]

# 对比下 API 参数的差异

参数Text CompletionsChat Completions
modelYY
promptYN
messagesNY
max_tokensYY
temperatureYY
top_nYY
frequency_penaltyYY
presence_penaltyYY
stopYY
logit_biasYY
best_ofYN
suffixYN
streamYY
echoYN
nYY
logprobsYN

具体参数说明见:openAI api - Text Completions