openai-api - 在 api 中发送对话历史记录时,chatgpt-3.5-turbo API 是否会重新计算计费中的 token

标签 openai-api chatgpt-api

使用 chatgpt-3.5-turbo 模型创建聊天应用程序时。每当我重新发送 API 请求并在对话中附加新消息时,API 是否会在计费中考虑整个 token (包括辅助消息和旧消息集),或者仅将用户的最后一条消息计入计费中?

例如:

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})

最佳答案

OpenAI document 中所述:

The total number of tokens in an API call affects how much your API call costs, as you pay per token
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.

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

response['usage']['total_tokens']

每次您附加以前的聊天到消息时,total_token 的数量都会增加。因此之前消息的所有代币都将被纳入账单中。

关于openai-api - 在 api 中发送对话历史记录时,chatgpt-3.5-turbo API 是否会重新计算计费中的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75682331/

相关文章:

python - 如何使用 python 修复字符串中不正确的 html 标签?

python - 如何在Python中循环嵌套(二维?)数组

python - 如何向 RetrievalQA.from_chain_type 添加内存?或者,如何向 ConversationalRetrievalChain 添加自定义提示?

javascript - 循环等待。 (再次)

python - 如何离线设置 ChatGPT 进行开发

python - OpenAI API 错误 : "Module ' openai' has no attribute 'ChatCompletion' , 您的意思是 'Completion' 吗?”

python - 将聊天响应放入 GPT API 中的列表中

node.js - openAI api - 是否可以通过 api 保存聊天状态\历史记录(无需重新发送)?

javascript - 如何使用 JavaScript 从 OpenAI API 获取数据并将其显示在我的网站上

python - 将 OpenAI API 与聊天机器人的 WhatsApp Business API 集成