4
4
import com .knuddels .jtokkit .api .Encoding ;
5
5
import com .knuddels .jtokkit .api .EncodingRegistry ;
6
6
import com .knuddels .jtokkit .api .EncodingType ;
7
+ import com .knuddels .jtokkit .api .IntArrayList ;
7
8
import com .knuddels .jtokkit .api .ModelType ;
8
9
import com .launchableinc .openai .completion .chat .ChatMessage ;
9
10
import lombok .AllArgsConstructor ;
@@ -46,7 +47,7 @@ public class TikTokensUtil {
46
47
* @return Encoding array
47
48
*/
48
49
public static List <Integer > encode (Encoding enc , String text ) {
49
- return isBlank (text ) ? new ArrayList <>() : enc .encode (text );
50
+ return isBlank (text ) ? new ArrayList <>() : enc .encode (text ). boxed () ;
50
51
}
51
52
52
53
/**
@@ -69,7 +70,7 @@ public static int tokens(Encoding enc, String text) {
69
70
* @return Text information corresponding to the encoding array.
70
71
*/
71
72
public static String decode (Encoding enc , List <Integer > encoded ) {
72
- return enc .decode (encoded );
73
+ return enc .decode (toIntArrayList ( encoded ) );
73
74
}
74
75
75
76
/**
@@ -94,7 +95,7 @@ public static List<Integer> encode(EncodingType encodingType, String text) {
94
95
return new ArrayList <>();
95
96
}
96
97
Encoding enc = getEncoding (encodingType );
97
- List <Integer > encoded = enc .encode (text );
98
+ List <Integer > encoded = enc .encode (text ). boxed () ;
98
99
return encoded ;
99
100
}
100
101
@@ -119,7 +120,7 @@ public static int tokens(EncodingType encodingType, String text) {
119
120
*/
120
121
public static String decode (EncodingType encodingType , List <Integer > encoded ) {
121
122
Encoding enc = getEncoding (encodingType );
122
- return enc .decode (encoded );
123
+ return enc .decode (toIntArrayList ( encoded ) );
123
124
}
124
125
125
126
@@ -147,7 +148,7 @@ public static List<Integer> encode(String modelName, String text) {
147
148
if (Objects .isNull (enc )) {
148
149
return new ArrayList <>();
149
150
}
150
- List <Integer > encoded = enc .encode (text );
151
+ List <Integer > encoded = enc .encode (text ). boxed () ;
151
152
return encoded ;
152
153
}
153
154
@@ -209,7 +210,16 @@ public static int tokens(String modelName, List<ChatMessage> messages) {
209
210
*/
210
211
public static String decode (String modelName , List <Integer > encoded ) {
211
212
Encoding enc = getEncoding (modelName );
212
- return enc .decode (encoded );
213
+ return enc .decode (toIntArrayList (encoded ));
214
+ }
215
+
216
+ private static IntArrayList toIntArrayList (List <Integer > encoded ) {
217
+ IntArrayList intArrayList = new IntArrayList (encoded .size ());
218
+ for (Integer e : encoded ) {
219
+ intArrayList .add (e );
220
+ }
221
+
222
+ return intArrayList ;
213
223
}
214
224
215
225
0 commit comments