+
80
-

chatgpt4o api流式输出如何计算消耗的tokens数量?

chatgpt4o api流式输出如何计算消耗的tokens数量?

如何不是流式输出会输出总的消耗token数量,那么流式模式下呢?

这是非流式模式下输出tokens数量:

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

流式模式下api接口没有统计消耗的tokens

{
  "id":"chatcmpl76y3gsGmxYfioodOWIFgxxxxxV",
  "object":"chat.completion.chunk",
  "created":1681895111,
  "model":"gpt-3.5-turbo-0301",
  "choices":[
    {
      "delta":{"content":"xx""},
      "index":0,
      "finish_reason":null
    }
  ]
}


网友回复

+
0
-

openai推出了tiktoken工具用于统计输出的tokens数量,只要在最后一个tokens输出后将前面的tokens累积交给tiktokoen计算即可。

# -*- coding: utf-8 -*-
import tiktoken


def num_tokens_from_string(string: str, encoding_name: str) -> int:
    # Return...

点击查看剩余70%

我知道答案,我要回答