1
1
const z = require ( 'zod' ) ;
2
2
const { EModelEndpoint } = require ( 'librechat-data-provider' ) ;
3
3
4
- const models = [
5
- 'text-davinci-003' ,
6
- 'text-davinci-002' ,
7
- 'text-davinci-001' ,
8
- 'text-curie-001' ,
9
- 'text-babbage-001' ,
10
- 'text-ada-001' ,
11
- 'davinci' ,
12
- 'curie' ,
13
- 'babbage' ,
14
- 'ada' ,
15
- 'code-davinci-002' ,
16
- 'code-davinci-001' ,
17
- 'code-cushman-002' ,
18
- 'code-cushman-001' ,
19
- 'davinci-codex' ,
20
- 'cushman-codex' ,
21
- 'text-davinci-edit-001' ,
22
- 'code-davinci-edit-001' ,
23
- 'text-embedding-ada-002' ,
24
- 'text-similarity-davinci-001' ,
25
- 'text-similarity-curie-001' ,
26
- 'text-similarity-babbage-001' ,
27
- 'text-similarity-ada-001' ,
28
- 'text-search-davinci-doc-001' ,
29
- 'text-search-curie-doc-001' ,
30
- 'text-search-babbage-doc-001' ,
31
- 'text-search-ada-doc-001' ,
32
- 'code-search-babbage-code-001' ,
33
- 'code-search-ada-code-001' ,
34
- 'gpt2' ,
35
- 'gpt-4' ,
36
- 'gpt-4-0314' ,
37
- 'gpt-4-32k' ,
38
- 'gpt-4-32k-0314' ,
39
- 'gpt-3.5-turbo' ,
40
- 'gpt-3.5-turbo-0301' ,
41
- ] ;
42
-
43
4
const openAIModels = {
44
5
'gpt-4' : 8187 , // -5 from max
45
6
'gpt-4-0613' : 8187 , // -5 from max
@@ -49,6 +10,7 @@ const openAIModels = {
49
10
'gpt-4-1106' : 127990 , // -10 from max
50
11
'gpt-4-0125' : 127990 , // -10 from max
51
12
'gpt-4o' : 127990 , // -10 from max
13
+ 'gpt-4o-mini' : 127990 , // -10 from max
52
14
'gpt-4-turbo' : 127990 , // -10 from max
53
15
'gpt-4-vision' : 127990 , // -10 from max
54
16
'gpt-3.5-turbo' : 16375 , // -10 from max
@@ -101,7 +63,6 @@ const anthropicModels = {
101
63
102
64
const aggregateModels = { ...openAIModels , ...googleModels , ...anthropicModels , ...cohereModels } ;
103
65
104
- // Order is important here: by model series and context size (gpt-4 then gpt-3, ascending)
105
66
const maxTokensMap = {
106
67
[ EModelEndpoint . azureOpenAI ] : openAIModels ,
107
68
[ EModelEndpoint . openAI ] : aggregateModels ,
@@ -110,6 +71,24 @@ const maxTokensMap = {
110
71
[ EModelEndpoint . anthropic ] : anthropicModels ,
111
72
} ;
112
73
74
+ /**
75
+ * Finds the first matching pattern in the tokens map.
76
+ * @param {string } modelName
77
+ * @param {Record<string, number> } tokensMap
78
+ * @returns {string|null }
79
+ */
80
+ function findMatchingPattern ( modelName , tokensMap ) {
81
+ const keys = Object . keys ( tokensMap ) ;
82
+ for ( let i = keys . length - 1 ; i >= 0 ; i -- ) {
83
+ const modelKey = keys [ i ] ;
84
+ if ( modelName . includes ( modelKey ) ) {
85
+ return modelKey ;
86
+ }
87
+ }
88
+
89
+ return null ;
90
+ }
91
+
113
92
/**
114
93
* Retrieves the maximum tokens for a given model name. If the exact model name isn't found,
115
94
* it searches for partial matches within the model name, checking keys in reverse order.
@@ -143,12 +122,11 @@ function getModelMaxTokens(modelName, endpoint = EModelEndpoint.openAI, endpoint
143
122
return tokensMap [ modelName ] ;
144
123
}
145
124
146
- const keys = Object . keys ( tokensMap ) ;
147
- for ( let i = keys . length - 1 ; i >= 0 ; i -- ) {
148
- if ( modelName . includes ( keys [ i ] ) ) {
149
- const result = tokensMap [ keys [ i ] ] ;
150
- return result ?. context ?? result ;
151
- }
125
+ const matchedPattern = findMatchingPattern ( modelName , tokensMap ) ;
126
+
127
+ if ( matchedPattern ) {
128
+ const result = tokensMap [ matchedPattern ] ;
129
+ return result ?. context ?? result ;
152
130
}
153
131
154
132
return undefined ;
@@ -181,15 +159,8 @@ function matchModelName(modelName, endpoint = EModelEndpoint.openAI) {
181
159
return modelName ;
182
160
}
183
161
184
- const keys = Object . keys ( tokensMap ) ;
185
- for ( let i = keys . length - 1 ; i >= 0 ; i -- ) {
186
- const modelKey = keys [ i ] ;
187
- if ( modelName . includes ( modelKey ) ) {
188
- return modelKey ;
189
- }
190
- }
191
-
192
- return modelName ;
162
+ const matchedPattern = findMatchingPattern ( modelName , tokensMap ) ;
163
+ return matchedPattern || modelName ;
193
164
}
194
165
195
166
const modelSchema = z . object ( {
@@ -241,8 +212,47 @@ function processModelData(input) {
241
212
return tokenConfig ;
242
213
}
243
214
215
+ const tiktokenModels = new Set ( [
216
+ 'text-davinci-003' ,
217
+ 'text-davinci-002' ,
218
+ 'text-davinci-001' ,
219
+ 'text-curie-001' ,
220
+ 'text-babbage-001' ,
221
+ 'text-ada-001' ,
222
+ 'davinci' ,
223
+ 'curie' ,
224
+ 'babbage' ,
225
+ 'ada' ,
226
+ 'code-davinci-002' ,
227
+ 'code-davinci-001' ,
228
+ 'code-cushman-002' ,
229
+ 'code-cushman-001' ,
230
+ 'davinci-codex' ,
231
+ 'cushman-codex' ,
232
+ 'text-davinci-edit-001' ,
233
+ 'code-davinci-edit-001' ,
234
+ 'text-embedding-ada-002' ,
235
+ 'text-similarity-davinci-001' ,
236
+ 'text-similarity-curie-001' ,
237
+ 'text-similarity-babbage-001' ,
238
+ 'text-similarity-ada-001' ,
239
+ 'text-search-davinci-doc-001' ,
240
+ 'text-search-curie-doc-001' ,
241
+ 'text-search-babbage-doc-001' ,
242
+ 'text-search-ada-doc-001' ,
243
+ 'code-search-babbage-code-001' ,
244
+ 'code-search-ada-code-001' ,
245
+ 'gpt2' ,
246
+ 'gpt-4' ,
247
+ 'gpt-4-0314' ,
248
+ 'gpt-4-32k' ,
249
+ 'gpt-4-32k-0314' ,
250
+ 'gpt-3.5-turbo' ,
251
+ 'gpt-3.5-turbo-0301' ,
252
+ ] ) ;
253
+
244
254
module . exports = {
245
- tiktokenModels : new Set ( models ) ,
255
+ tiktokenModels,
246
256
maxTokensMap,
247
257
inputSchema,
248
258
modelSchema,
0 commit comments