使用 gpt4o,如何计算图片 token? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
934831065ldc
V2EX    OpenAI

使用 gpt4o,如何计算图片 token?

  •  
  •   934831065ldc 2024-05-28 14:43:26 +08:00 2538 次点击
    这是一个创建于 501 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 gpt4 的接口的时候,用 stream 模式,没有返回 usage ,如何计算带图片消息的 token ?

    3 条回复    2024-05-29 17:36:53 +08:00
    Jonney
        1
    Jonney  
       2024-05-28 15:09:09 +08:00
    ```
    def chat(client,messages,model):
    print()
    start=time.time()
    answer=[]
    for chunk in client.chat.completions.create(
    model=model,
    messages=messages,
    stream=True,
    stream_optiOns={'include_usage':True},
    temperature=0,
    ):
    if not chunk.choices:
    continue
    stream=chunk.choices[0].delta.content or ''
    if answer or stream.strip():
    answer.append(stream)
    print(stream,end='',flush=True)
    end=time.time()
    count=chunk.usage.total_tokens
    print(f'\n\n{end-start:.2f} secs, {count} tokens')
    return ''.join(answer)
    ```
    最新版客户端,加上参数 stream_optiOns={'include_usage':True},那么在返回的最后一个 chunk 带 token 消耗数量
    vacuitym
        2
    vacuitym  
       2024-05-28 15:29:34 +08:00
    这边有写:

    https://platform.openai.com/docs/guides/vision/calculating-costs

    Image inputs are metered and charged in tokens, just as text inputs are. The token cost of a given image is determined by two factors: its size, and the detail option on each image_url block. All images with detail: low cost 85 tokens each. detail: high images are first scaled to fit within a 2048 x 2048 square, maintaining their aspect ratio. Then, they are scaled such that the shortest side of the image is 768px long. Finally, we count how many 512px squares the image consists of. Each of those squares costs 170 tokens. Another 85 tokens are always added to the final total.

    Here are some examples demonstrating the above.

    A 1024 x 1024 square image in detail: high mode costs 765 tokens
    1024 is less than 2048, so there is no initial resize.
    The shortest side is 1024, so we scale the image down to 768 x 768.
    4 512px square tiles are needed to represent the image, so the final token cost is 170 * 4 + 85 = 765.
    A 2048 x 4096 image in detail: high mode costs 1105 tokens
    We scale down the image to 1024 x 2048 to fit within the 2048 square.
    The shortest side is 1024, so we further scale down to 768 x 1536.
    6 512px tiles are needed, so the final token cost is 170 * 6 + 85 = 1105.
    A 4096 x 8192 image in detail: low most costs 85 tokens
    Regardless of input size, low detail images are a fixed cost.
    feirisu
        3
    feirisu  
       2024-05-29 17:36:53 +08:00
    c#代码,就是 auto 没测试是怎么自动 low 和 high 的


    public static int CalculateImageTokens(int width, int height, ImageDetailMode detailMode)
    {
    const int lowDetailCost = 85;
    const int highDetailCostPerSquare = 170;
    const int highDetailBaseCost = 85;
    const int maxDimension = 2048;
    const int targetShortSide = 768;
    const int squareSize = 512;


    if (detailMode == ImageDetailMode.None)
    {
    detailMode = ImageDetailMode.High; //
    }

    if (detailMode == ImageDetailMode.Low)
    {
    return lowDetailCost;
    }
    else if (detailMode == ImageDetailMode.High)
    {
    bool scaledToMax = false;

    // Scale down the image if either dimension exceeds the maximum allowed.
    if (width > maxDimension || height > maxDimension)
    {
    double scaleFactor = Math.Min((double)maxDimension / width, (double)maxDimension / height);
    width = (int)(width * scaleFactor);
    height = (int)(height * scaleFactor);
    scaledToMax = true;
    }

    // Further scale down the image only if it has been scaled in the previous step.
    if (scaledToMax)
    {
    double scaleToShortestSideFactor = (double)targetShortSide / Math.Min(width, height);
    width = (int)(width * scaleToShortestSideFactor);
    height = (int)(height * scaleToShortestSideFactor);
    }

    // Calculate how many 512px squares are needed to cover the image.
    int squaresAcross = (int)Math.Ceiling((double)width / squareSize);
    int squaresDown = (int)Math.Ceiling((double)height / squareSize);
    int totalSquares = squaresAcross * squaresDown;

    // Calculate final token cost for high detail images.
    return highDetailCostPerSquare * totalSquares + highDetailBaseCost;
    }

    return lowDetailCost;
    }
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     985 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 23:14 PVG 07:14 LAX 16:14 JFK 19:14
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86