MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / getImageToken

Function getImageToken

service/token_counter.go:75–153  ·  view source on GitHub ↗
(info *relaycommon.RelayInfo, imageUrl *dto.MessageImageUrl, model string, stream bool)

Source from the content-addressed store, hash-verified

73}
74
75func getImageToken(info *relaycommon.RelayInfo, imageUrl *dto.MessageImageUrl, model string, stream bool) (int, error) {
76 if imageUrl == nil {
77 return 0, fmt.Errorf("image_url_is_nil")
78 }
79 baseTokens := 85
80 if model == "glm-4v" {
81 return 1047, nil
82 }
83 if imageUrl.Detail == "low" {
84 return baseTokens, nil
85 }
86 if !constant.GetMediaTokenNotStream && !stream {
87 return 3 * baseTokens, nil
88 }
89
90 // 同步One API的图片计费逻辑
91 if imageUrl.Detail == "auto" || imageUrl.Detail == "" {
92 imageUrl.Detail = "high"
93 }
94
95 tileTokens := 170
96 if strings.HasPrefix(model, "gpt-4o-mini") {
97 tileTokens = 5667
98 baseTokens = 2833
99 }
100 // 是否统计图片token
101 if !constant.GetMediaToken {
102 return 3 * baseTokens, nil
103 }
104 if info.ChannelType == constant.ChannelTypeGemini || info.ChannelType == constant.ChannelTypeVertexAi || info.ChannelType == constant.ChannelTypeAnthropic {
105 return 3 * baseTokens, nil
106 }
107 var config image.Config
108 var err error
109 var format string
110 var b64str string
111 if strings.HasPrefix(imageUrl.Url, "http") {
112 config, format, err = DecodeUrlImageData(imageUrl.Url)
113 } else {
114 common.SysLog(fmt.Sprintf("decoding image"))
115 config, format, b64str, err = DecodeBase64ImageData(imageUrl.Url)
116 }
117 if err != nil {
118 return 0, err
119 }
120 imageUrl.MimeType = format
121
122 if config.Width == 0 || config.Height == 0 {
123 // not an image
124 if format != "" && b64str != "" {
125 // file type
126 return 3 * baseTokens, nil
127 }
128 return 0, errors.New(fmt.Sprintf("fail to decode base64 config: %s", imageUrl.Url))
129 }
130
131 shortSide := config.Width
132 otherSide := config.Height

Callers 1

CountTokenMessagesFunction · 0.85

Calls 2

DecodeUrlImageDataFunction · 0.85
DecodeBase64ImageDataFunction · 0.85

Tested by

no test coverage detected