# Note: you need to be using OpenAI Python v0.27.0 for the code below to workimport openaiopenai.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?"}])
语言模型以称为词元的块读取文本。在英语中,一个标记可以短至一个字符或长至一个单词(例如 a 或 apple),而在某些语言中,词元甚至可以比一个字符更短或比一个单词更长。
例如,“ChatGPT is great!”字符串被编码成六个词元:[“Chat”,“G”,“PT”,“ is”,“ great”,“!”]。 API 调用中的总词元数会影响以下内容:您支付每个词元的费用;写入更多词元需要更多时间;总词元必须低于模型的最大限制(gpt-3.5-turbo-0301 的最大限制为 4096 个词元)。
输入和输出词元都计入这些数量。例如,如果您的 API 调用在消息输入中使用了 10 个词元,并且您收到了消息输出中的 20 个词元,则将向您收取30个代币。
要查看 API 调用使用了多少代币,请检查 API 响应中的 usage 字段(例如 response['usage']['total_tokens'])。
{
'id': 'chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve',
'object': 'chat.completion',
'created': 1677649420,
'model': 'gpt-3.5-turbo',
'usage': {'prompt_tokens': 56, 'completion_tokens': 31, 'total_tokens': 87},
'choices': [
{
'message': {
'role': 'assistant',
'content': 'The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers.'},
'finish_reason': 'stop',
'index': 0
}
]
}