Skip to content

Commit fd3ac24

Browse files
committed
count token
1 parent 0e1ea12 commit fd3ac24

File tree

3 files changed

+87
-46
lines changed

3 files changed

+87
-46
lines changed

README.md

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,19 @@ OpenAI ChatGPT 的SDK。觉得不错请右上角Star
2020

2121
# 功能特性
2222

23-
| 功能 | 特性 |
24-
|:-----------:| :------: |
25-
| GPT 3.5 | 支持 |
26-
| GPT 4.0 | 支持 |
27-
| 函数调用 | 支持 |
28-
| 流式对话 | 支持 |
29-
| 阻塞式对话 | 支持 |
30-
| 前端 ||
31-
| 上下文 | 支持 |
32-
| 计算Token | [用jtokkit](https://github.com/knuddelsgmbh/jtokkit) |
33-
| 多KEY轮询 | 支持 |
34-
| 代理 | 支持 |
35-
| 反向代理 | 支持 |
36-
37-
38-
39-
40-
41-
![image](https://user-images.githubusercontent.com/15922823/206353660-47d99158-a664-4ade-b2f1-e2cc8ac68b74.png)
42-
43-
![image](https://user-images.githubusercontent.com/15922823/206615422-23c5e587-d29a-4f04-8d0d-f8dd7c19da37.png)
44-
23+
| 功能 | 特性 |
24+
|:-----------:|:---:|
25+
| GPT 3.5 | 支持 |
26+
| GPT 4.0 | 支持 |
27+
| GPT 4o | 支持 |
28+
| GPT 4o-mini | 支持 |
29+
| 函数调用 | 支持 |
30+
| 流式对话 | 支持 |
31+
| 上下文 | 支持 |
32+
| 计算Token | 支持 |
33+
| 多KEY轮询 | 支持 |
34+
| 代理 | 支持 |
35+
| 反向代理 | 支持 |
4536

4637

4738
## 使用指南
@@ -116,6 +107,33 @@ implementation group: 'com.github.plexpt', name: 'chatgpt', version: '5.1.0'
116107
System.out.println(response.toPlainString());
117108

118109
```
110+
111+
112+
### 计算token数
113+
114+
115+
```java
116+
Message system = Message.ofSystem("你现在是一个诗人,专门写七言绝句");
117+
Message message = Message.of("写一段七言绝句诗,题目是:火锅!");
118+
119+
ChatCompletion chatCompletion1 = ChatCompletion.builder()
120+
.model(ChatCompletion.Model.GPT_3_5_TURBO)
121+
.messages(Arrays.asList(system, message))
122+
.maxTokens(3000)
123+
.temperature(0.9)
124+
.build();
125+
ChatCompletion chatCompletion2 = ChatCompletion.builder()
126+
.model(ChatCompletion.Model.GPT4)
127+
.messages(Arrays.asList(system, message))
128+
.maxTokens(3000)
129+
.temperature(0.9)
130+
.build();
131+
132+
log.info("{} tokens: {}", chatCompletion1.getModel(), chatCompletion1.countTokens());
133+
log.info("{} tokens: {}", chatCompletion2.getModel(), chatCompletion2.countTokens());
134+
135+
```
136+
119137
### 函数调用(Function Call)
120138

121139
```java
@@ -299,24 +317,24 @@ chatGPT = ChatGPT.builder()
299317

300318
# 常见问题
301319

302-
|| |
303-
| :----------------------------------------------------------: | :----------------------------------------------------------: |
304-
| KEY从哪来? | 手动注册生成:openai.com(需要海外手机号)、或者成品独享帐号:[购买](https://fk.fq.mk/?code=YT0xJmI9Mg%3D%3D) |
305-
| 哪些地区不能用 | **以下国家IP不支持使用:中国(包含港澳台) 俄罗斯 乌克兰 阿富汗 白俄罗斯 委内瑞拉 伊朗 埃及!!** |
306-
| 有封号风险吗 | 使用代理有一定的风险。 |
307-
| 我是尊贵的Plus会员,能用吗 | PLUS是网页端,调用API没啥区别 |
308-
| GPT4.0 怎么用 | 目前需要充值 |
309-
| api.openai.com ping不通? | 禁ping,用curl测试连通性 |
310-
| 显示超时? | IP不好,换个IP |
311-
| 显示`Your access was terminated due to violation of our policies`... | 你号没了,下一个 |
312-
| 显示`That model is currently overloaded with other requests. You can retry your request` | 模型过载,官方炸了,重试 |
313-
| 生成的图片不能用? | 图片是它瞎编的,洗洗睡吧 |
314-
| 如何充值? | 用国外信用卡,国内的不行 |
315-
| 没有国外信用卡怎么办? | 暂时没有特别好的办法待定 |
316-
| 返回http 401 | API 密钥写错了/没写 |
317-
| 返回http 429 | 请求超速了,或者官方超载了。充钱可解决 |
318-
| 返回http 500 | 服务器炸了 |
319-
| | |
320+
|| |
321+
| :----------------------------------------------------------: |:----------------------------------------------------------------------------------------------------:|
322+
| KEY从哪来? | 手动注册生成:openai.com(需要海外手机号)、或者[GPT API 额度购买(微信)](https://work.weixin.qq.com/kfid/kfc6913bb4906e0e597) |
323+
| 哪些地区不能用 | **以下国家IP不支持使用:中国(包含港澳台) 俄罗斯 乌克兰 阿富汗 白俄罗斯 委内瑞拉 伊朗 埃及!!** |
324+
| 有封号风险吗 | 使用代理有一定的风险。 |
325+
| 我是尊贵的Plus会员,能用吗 | PLUS是网页端,和API不是一个 |
326+
| GPT4.0 怎么用 | 目前需要充值 |
327+
| api.openai.com ping不通? | 禁ping,用curl测试连通性 |
328+
| 显示超时? | IP不好,换个IP |
329+
| 显示`Your access was terminated due to violation of our policies`... | 你号没了,下一个 |
330+
| 显示`That model is currently overloaded with other requests. You can retry your request` | 模型过载,官方炸了,重试 |
331+
| 生成的图片不能用? | 图片是它瞎编的,洗洗睡吧 |
332+
| 如何充值? | 用国外信用卡,国内的不行 |
333+
| 没有国外信用卡怎么办? | 暂时没有特别好的办法待定 |
334+
| 返回http 401 | API 密钥写错了/没写 |
335+
| 返回http 429 | 请求超速了,或者官方超载了。充钱可解决 |
336+
| 返回http 500 | 服务器炸了 |
337+
| | |
320338

321339
---
322340

src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletion.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ public interface Model {
178178

179179
}
180180

181+
/**
182+
* 计算token
183+
*
184+
* @return
185+
*/
181186
public int countTokens() {
182187
return TokensUtil.tokens(this.model, this.messages);
183188
}

src/main/java/com/plexpt/chatgpt/util/TokensUtil.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class TokensUtil {
1818

1919
public static EncodingRegistry registry = Encodings.newDefaultEncodingRegistry();
2020
public static Encoding encoding = registry.getEncoding(EncodingType.CL100K_BASE);
21+
public static Encoding encoding4o = registry.getEncoding(EncodingType.O200K_BASE);
2122

2223

2324
/**
@@ -27,7 +28,21 @@ public class TokensUtil {
2728
* @return
2829
*/
2930
public static int countTextTokens(String text) {
30-
return encoding.countTokens(text);
31+
return encoding4o.countTokens(text);
32+
}
33+
34+
/**
35+
* 计算text信息的tokens
36+
*
37+
* @param text
38+
* @return
39+
*/
40+
public static int countTextTokens(String text, String model) {
41+
if (StringUtils.startsWithIgnoreCase(model, ModelType.GPT_3_5_TURBO.getName())) {
42+
return encoding.countTokens(text);
43+
}
44+
45+
return encoding4o.countTokens(text);
3146
}
3247

3348

@@ -37,7 +52,7 @@ public static int countTextTokens(String text) {
3752
* @param name
3853
* @return
3954
*/
40-
private static ModelType getModelTypeByName(String name) {
55+
public static ModelType getModelTypeByName(String name) {
4156
Optional<ModelType> optional = ModelType.fromName(name);
4257

4358
return optional.orElse(ModelType.GPT_3_5_TURBO);
@@ -55,7 +70,7 @@ public static int tokens(List<Message> messages, String model) {
5570
if (CollectionUtils.isEmpty(messages)) {
5671
return 0;
5772
}
58-
73+
Encoding encodingUsed = encoding;
5974
//"gpt-3.5-turbo"
6075
// every message follows <|start|>{role/name}\n{content}<|end|>\n
6176
int tokensPerMessage = 4;
@@ -66,14 +81,17 @@ public static int tokens(List<Message> messages, String model) {
6681
tokensPerMessage = 3;
6782
tokensPerName = 1;
6883
}
84+
if (StringUtils.startsWithIgnoreCase(model, ModelType.GPT_4O.getName())) {
85+
encodingUsed = encoding4o;
86+
}
6987

7088
int sum = 0;
7189
for (final Message message : messages) {
7290
sum += tokensPerMessage;
73-
sum += encoding.countTokens(message.getContent());
74-
sum += encoding.countTokens(message.getRole());
91+
sum += encodingUsed.countTokens(message.getContent());
92+
sum += encodingUsed.countTokens(message.getRole());
7593
if (!StringUtils.isEmpty(message.getName())) {
76-
sum += encoding.countTokens(message.getName());
94+
sum += encodingUsed.countTokens(message.getName());
7795
sum += tokensPerName;
7896
}
7997
}

0 commit comments

Comments
 (0)