chatgpt-3.5-turbo API在将对话历史发送到API时是否重新计算计费中的标记?

huangapple go评论49阅读模式
英文:

Does chatgpt-3.5-turbo API recounts the tokens in billing when sending the conversation history in api

问题

当使用chatgpt-3.5-turbo模型创建聊天应用时,API是否会考虑整个令牌(包括助手消息和旧消息集)来计费,还是只有用户的最后一条消息在每次重新发送API请求时计费?

例如:

messages = [
    {"role": "system", "content": "You are a kind helpful assistant."},
]

while True:
    message = input("User: ")
    if message:
        messages.append(
            {"role": "user", "content": message},
        )
        chat = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", messages=messages
        )

    reply = chat.choices[0].message.content
    print(f"ChatGPT: {reply}")
    messages.append({"role": "assistant", "content": reply})
英文:

When creating a chat app using chatgpt-3.5-turbo model. Does the API consider the whole tokens (including the assistant messages and old set of messages) in billing or just the last message from the user is counted in billing whenever I resend the API request with a new message appended to the conversation?

For eg:

messages = [
    {"role": "system", "content": "You are a kind helpful assistant."},
]
     

while True:
    message = input("User : ")
    if message:
        messages.append(
            {"role": "user", "content": message},
        )
        chat = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", messages=messages
        )
    
    reply = chat.choices[0].message.content
    print(f"ChatGPT: {reply}")
    messages.append({"role": "assistant", "content": reply})

</details>


# 答案1
**得分**: 2

如[OpenAI文档][1]中提到的:

> API调用中的总标记数会影响API调用的成本,因为您按标记付费 &lt;br&gt;
输入和输出标记都计入这些数量。例如,如果您的API调用在消息输入中使用了10个标记,并在消息输出中收到了20个标记,您将被收费30个标记。

要查看API调用使用了多少标记,请检查API响应中的usage字段

    response[&#39;usage&#39;][&#39;total_tokens&#39;]

每次您将以前的聊天附加到消息中的`append`时,`total_token`的数量都会增加。因此,以前消息的所有标记都将计入账单。

[1]: https://platform.openai.com/docs/guides/chat/introduction

<details>
<summary>英文:</summary>

As mentioned in [OpenAI document][1]:

&gt; The total number of tokens in an API call affects how much your API call costs, as you pay per token &lt;br&gt;
Both input and output tokens count toward these quantities. For example, if your API call used 10 tokens in the message input and you received 20 tokens in the message output, you would be billed for 30 tokens.

To see how many tokens are used by an API call, check the usage field in the API response

    response[&#39;usage&#39;][&#39;total_tokens&#39;]



Each time you `append` previous chats to `messages`, the number of `total_token` will increases. So all tokens of previous messages will be considered in the bill.


  [1]: https://platform.openai.com/docs/guides/chat/introduction

</details>



huangapple
  • 本文由 发表于 2023年3月9日 16:57:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定