-
Notifications
You must be signed in to change notification settings - Fork 8
/
TikTokenUtils.java
412 lines (377 loc) · 16.8 KB
/
TikTokenUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package xyz.felh.openai.jtokkit.utils;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SerializationUtils;
import xyz.felh.openai.chat.ChatCompletion;
import xyz.felh.openai.chat.ChatMessage;
import xyz.felh.openai.chat.ChatMessageRole;
import xyz.felh.openai.chat.CreateChatCompletionRequest;
import xyz.felh.openai.chat.tool.Tool;
import xyz.felh.openai.chat.tool.ToolCall;
import xyz.felh.openai.chat.tool.ToolChoice;
import xyz.felh.openai.chat.tool.Type;
import xyz.felh.openai.jtokkit.Encodings;
import xyz.felh.openai.jtokkit.api.Encoding;
import xyz.felh.openai.jtokkit.api.EncodingRegistry;
import xyz.felh.openai.jtokkit.api.EncodingType;
import xyz.felh.openai.jtokkit.api.ModelType;
import xyz.felh.openai.utils.ListUtils;
import xyz.felh.openai.utils.Preconditions;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.*;
@Slf4j
public class TikTokenUtils {
/**
* 模型名称对应Encoding
*/
private static final Map<String, Encoding> modelMap = new HashMap<>();
/**
* registry实例
*/
private static final EncodingRegistry registry = Encodings.newDefaultEncodingRegistry();
static {
for (ModelType modelType : ModelType.values()) {
modelMap.put(modelType.getName(), registry.getEncodingForModel(modelType));
}
modelMap.put(ChatCompletion.Model.GPT_3_5_TURBO_1106.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO_0125));
modelMap.put(ChatCompletion.Model.GPT_3_5_TURBO_INSTRUCT.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO_0125));
modelMap.put(ChatCompletion.Model.GPT_3_5_TURBO_0125.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO_0125));
modelMap.put(ChatCompletion.Model.GPT_4_32K.getName(), registry.getEncodingForModel(ModelType.GPT_4));
modelMap.put(ChatCompletion.Model.GPT_4_1106_PREVIEW.getName(), registry.getEncodingForModel(ModelType.GPT_4));
modelMap.put(ChatCompletion.Model.GPT_4_VISION_PREVIEW.getName(), registry.getEncodingForModel(ModelType.GPT_4));
modelMap.put(ChatCompletion.Model.GPT_4_0125_PREVIEW.getName(), registry.getEncodingForModel(ModelType.GPT_4));
modelMap.put(ChatCompletion.Model.GPT_4_TURBO_20240409.getName(), registry.getEncodingForModel(ModelType.GPT_4));
modelMap.put(ChatCompletion.Model.GPT_4_O_20240513.getName(), registry.getEncodingForModel(ModelType.GPT_4_O_2024_05_13));
}
/**
* 通过Encoding和text获取编码数组
*
* @param enc Encoding类型
* @param text 文本信息
* @return 编码数组
*/
public static List<Integer> encode(Encoding enc, String text) {
return isBlank(text) ? new ArrayList<>() : enc.encode(text);
}
/**
* 通过Encoding计算text信息的tokens
*
* @param enc Encoding类型
* @param text 文本信息
* @return tokens数量
*/
public static int tokens(Encoding enc, String text) {
return encode(enc, text).size();
}
/**
* 通过Encoding和encoded数组反推text信息
*
* @param enc Encoding
* @param encoded 编码数组
* @return 编码数组对应的文本信息
*/
public static String decode(Encoding enc, List<Integer> encoded) {
return enc.decode(encoded);
}
/**
* 获取一个Encoding对象,通过Encoding类型
*
* @param encodingType encodingType
* @return Encoding
*/
public static Encoding getEncoding(EncodingType encodingType) {
return registry.getEncoding(encodingType);
}
/**
* 获取encode的编码数组
*
* @param text 文本信息
* @return 编码数组
*/
public static List<Integer> encode(EncodingType encodingType, String text) {
if (isBlank(text)) {
return new ArrayList<>();
}
Encoding enc = getEncoding(encodingType);
return enc.encode(text);
}
/**
* 计算指定字符串的tokens,通过EncodingType
*
* @param encodingType encodingType
* @param text 文本信息
* @return tokens数量
*/
public static int tokens(EncodingType encodingType, String text) {
return encode(encodingType, text).size();
}
/**
* 通过EncodingType和encoded编码数组,反推字符串文本
*
* @param encodingType encodingType
* @param encoded 编码数组
* @return 编码数组对应的字符串
*/
public static String decode(EncodingType encodingType, List<Integer> encoded) {
Encoding enc = getEncoding(encodingType);
return enc.decode(encoded);
}
/**
* 获取一个Encoding对象,通过模型名称
*
* @param modelName 模型名称
* @return Encoding
*/
public static Encoding getEncoding(String modelName) {
Encoding encoding = modelMap.get(modelName);
if (Preconditions.isBlank(encoding)) {
if (modelName.toLowerCase().startsWith("ft:")) {
String baseModel = modelName.split(":")[1];
encoding = modelMap.get(baseModel);
if (Preconditions.isBlank(encoding)) {
if (baseModel.toLowerCase().startsWith("gpt-3.5")) {
encoding = modelMap.get(ModelType.GPT_3_5_TURBO_0125.getName());
}
if (baseModel.toLowerCase().startsWith("gpt-4")) {
encoding = modelMap.get(ModelType.GPT_4.getName());
}
}
}
}
return encoding;
}
/**
* 获取encode的编码数组,通过模型名称
*
* @param text 文本信息
* @return 编码数组
*/
public static List<Integer> encode(String modelName, String text) {
if (isBlank(text)) {
return new ArrayList<>();
}
Encoding enc = getEncoding(modelName);
if (Objects.isNull(enc)) {
log.warn("[{}]模型不存在或者暂不支持计算tokens,直接返回tokens==0", modelName);
return new ArrayList<>();
}
return enc.encode(text);
}
/**
* 通过模型名称, 计算指定字符串的tokens
*
* @param modelName 模型名称
* @param text 文本信息
* @return tokens数量
*/
public static int tokens(String modelName, String text) {
return encode(modelName, text).size();
}
/**
* 计算request的token数量
*
* @param request CreateChatCompletionRequest
* @return tokens count
*/
public static int estimateTokens(CreateChatCompletionRequest request) {
List<ChatMessage> messages = request.getMessages();
List<Tool> tools = request.getTools();
Object toolChoice = request.getToolChoice();
String chatModel = request.getModel();
int tokens = 0;
tokens += estimateTokensInMessages(chatModel, messages, tools);
// If there are tools, add the function definitions as they count towards token usage
if (Preconditions.isNotBlank(tools)) {
tokens += estimateTokensInTools(chatModel, tools);
}
// If there's a system message and tools are present, subtract four tokens
if (Preconditions.isNotBlank(tools) && messages.stream().anyMatch(it -> it.getRole() == ChatMessageRole.SYSTEM)) {
tokens -= 4;
}
// If function_call is 'none', add one token.
// If it's a OpenAIFunctionCall object, add 4 + the number of tokens in the function name.
// If it's undefined or 'auto', don't add anything.
if (Preconditions.isNotBlank(toolChoice) && !"auto".equals(toolChoice.toString())) {
if ("none".equals(toolChoice.toString())) {
tokens += 1;
} else {
if (toolChoice instanceof ToolChoice tc) {
if (Preconditions.isNotBlank(tc.getFunction().getName())) {
tokens += tokens(chatModel, tc.getFunction().getName()) + 4;
}
}
}
}
return tokens;
}
public static int estimateTokensInTools(String modelName, List<Tool> tools) {
Encoding encoding = getEncoding(modelName);
int tokens = tokens(encoding, FunctionFormat.formatFunctionDefinitions(tools));
tokens += 9; // Additional tokens for function definition of tools
return tokens;
}
public static int estimateTokensInMessages(String modelName, List<ChatMessage> messages) {
return estimateTokensInMessages(modelName, messages, null);
}
public static int estimateTokensInMessages(String modelName, List<ChatMessage> messages, List<Tool> tools) {
int tokens = 0;
int toolMessageSize = (int) messages.stream().filter(it -> it.getRole() == ChatMessageRole.TOOL).count();
if (toolMessageSize > 1) {
tokens += toolMessageSize * 2 + 1;
int jsonContentToolSize = (int) messages.stream().filter(it -> it.getRole() == ChatMessageRole.TOOL
&& ToolContentFormat.isJSONString(it.getContent().toString())).count();
if (jsonContentToolSize > 0) {
tokens += 1 - jsonContentToolSize;
}
}
boolean paddedSystem = false;
for (ChatMessage message : messages) {
ChatMessage msg = SerializationUtils.clone(message);
if (msg.getRole() == ChatMessageRole.SYSTEM && Preconditions.isNotBlank(tools) && !paddedSystem) {
if (Preconditions.isNotBlank(msg.getContent()) && msg.getContent() instanceof String) {
msg.setContent(msg.getContent() + "\n");
}
paddedSystem = true;
}
tokens += estimateTokensInMessage(modelName, msg, toolMessageSize);
}
// Each completion (vs message) seems to carry a 3-token overhead
tokens += 3;
return tokens;
}
/**
* 通过模型名称计算messages获取编码数组
* 参考官方的处理逻辑:<a href=https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb>https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb</a>
*
* @param modelName 模型名称
* @param message 消息体
* @return tokens数量
*/
public static int estimateTokensInMessage(String modelName, ChatMessage message, int toolMessageSize) {
Encoding encoding = getEncoding(modelName);
int tokens = 0;
// role
tokens += tokens(encoding, message.getRole().value());
// content
if (message.getRole() == ChatMessageRole.TOOL) {
if (toolMessageSize == 1) {
tokens += tokens(encoding, message.getContent().toString());
} else {
tokens += tokens(encoding, ToolContentFormat.format(message.getContent()));
JSONObject contentJSON = ToolContentFormat.tryFormat(message.getContent().toString());
if (Preconditions.isNotBlank(contentJSON)) {
tokens -= contentJSON.keySet().size();
}
}
} else {
if (message.getContent() instanceof String) {
tokens += tokens(encoding, message.getContent().toString());
} else {
List<ChatMessage.ContentItem> items = ListUtils.castList(message.getContent(), ChatMessage.ContentItem.class);
if (Preconditions.isNotBlank(items)) {
for (ChatMessage.ContentItem item : items) {
if (item.getType() == ChatMessage.ContentType.TEXT) {
// 不需要计算type
tokens += tokens(encoding, item.getText());
} else if (item.getType() == ChatMessage.ContentType.IMAGE_URL) {
ChatMessage.ImageUrl imageUrl = item.getImageUrl();
// https://openai.com/pricing
if (imageUrl.getDetail() == ChatMessage.ImageUrlDetail.LOW) {
tokens += 85;
} else if (imageUrl.getDetail() == ChatMessage.ImageUrlDetail.HIGH) {
tokens += 85;
int width = 0;
int height = 0;
if (imageUrl.getUrl().startsWith("f")) {
// base64
Base64.Decoder decoder = Base64.getDecoder();
try {
String b64 = imageUrl.getUrl();
b64 = b64.substring(b64.indexOf(";base64,") + 8);
b64 = b64.substring(0, b64.length() - 1);
byte[] bytes = decoder.decode(b64);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
BufferedImage bi = ImageIO.read(inputStream);
if (Preconditions.isNotBlank(inputStream)) {
inputStream.close();
}
width = bi.getWidth();
height = bi.getHeight();
} catch (Exception e) {
log.error("image to base64 error", e);
}
} else {
// image url
try {
BufferedImage bi = ImageIO.read(new URL(imageUrl.getUrl()));
width = bi.getWidth();
height = bi.getHeight();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// 1 per 512x512
int tiles = (int) Math.ceil(width / 512.0) * (int) Math.ceil(height / 512.0);
tokens += 170 * tiles;
}
}
}
}
}
}
// name 如果是 tool的时候不计算 name
if (Preconditions.isNotBlank(message.getName()) && message.getRole() != ChatMessageRole.TOOL) {
tokens += tokens(encoding, message.getName()) + 1; // +1 for the name
}
if (message.getRole() == ChatMessageRole.ASSISTANT && Preconditions.isNotBlank(message.getToolCalls())) {
for (ToolCall toolCall : message.getToolCalls()) {
tokens += 3;
tokens += tokens(encoding, toolCall.getType().value());
if (toolCall.getType() == Type.FUNCTION) {
if (Preconditions.isNotBlank(toolCall.getFunction().getName())) { // name is required
int nameToken = tokens(encoding, toolCall.getFunction().getName());
tokens += nameToken * 2;
}
if (Preconditions.isNotBlank(toolCall.getFunction().getArguments())) {
tokens += tokens(encoding, ArgumentFormat.formatArguments(toolCall.getFunction().getArguments()));
}
}
}
if (message.getToolCalls().size() > 1) {
// s1, add delta when multi tools is added
tokens += 15;
// s2
tokens -= message.getToolCalls().size() * 5 - 6;
} else {
// s1
// s2
tokens -= 2;
}
}
if (message.getRole() == ChatMessageRole.TOOL) {
tokens += 2; // add 2 if role is "tool"
} else {
tokens += 3; // Add three per message
}
return tokens;
}
public static int tokens(String modelName, Object functionCall, List<Tool> tools) {
Encoding encoding = getEncoding(modelName);
int sum = 0;
if (Preconditions.isNotBlank(functionCall)) {
if (functionCall instanceof JSONObject) {
sum += tokens(encoding, functionCall.toString());
}
}
sum += tokens(encoding, FunctionFormat.formatFunctionDefinitions(tools));
sum += 9; // Additional tokens for function definition
return sum;
}
public static boolean isBlank(CharSequence str) {
return str == null || "".contentEquals(str);
}
}