forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseModels.test.ts
290 lines (249 loc) · 9.36 KB
/
parseModels.test.ts
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
import { describe, expect, it } from 'vitest';
import { LOBE_DEFAULT_MODEL_LIST, OpenAIProviderCard } from '@/config/modelProviders';
import { ChatModelCard } from '@/types/llm';
import { parseModelString, transformToChatModelCards } from './parseModels';
describe('parseModelString', () => {
it('custom deletion, addition, and renaming of models', () => {
const result = parseModelString(
'-all,+llama,+claude-2,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo,gpt-4-1106-preview=gpt-4-32k',
);
expect(result).toMatchSnapshot();
});
it('duplicate naming model', () => {
const result = parseModelString('gpt-4-1106-preview=gpt-4-turbo,gpt-4-1106-preview=gpt-4-32k');
expect(result).toMatchSnapshot();
});
it('only add the model', () => {
const result = parseModelString('model1,model2,model3,model4');
expect(result).toMatchSnapshot();
});
describe('extension capabilities', () => {
it('with token', () => {
const result = parseModelString('chatglm-6b=ChatGLM 6B<4096>');
expect(result.add[0]).toEqual({
displayName: 'ChatGLM 6B',
id: 'chatglm-6b',
tokens: 4096,
});
});
it('token and function calling', () => {
const result = parseModelString('spark-v3.5=讯飞星火 v3.5<8192:fc>');
expect(result.add[0]).toEqual({
displayName: '讯飞星火 v3.5',
functionCall: true,
id: 'spark-v3.5',
tokens: 8192,
});
});
it('multi models', () => {
const result = parseModelString(
'gemini-1.5-flash-latest=Gemini 1.5 Flash<16000:vision>,gpt-4-all=ChatGPT Plus<128000:fc:vision:file>',
);
expect(result.add).toEqual([
{
displayName: 'Gemini 1.5 Flash',
vision: true,
id: 'gemini-1.5-flash-latest',
tokens: 16000,
},
{
displayName: 'ChatGPT Plus',
vision: true,
functionCall: true,
files: true,
id: 'gpt-4-all',
tokens: 128000,
},
]);
});
it('should have file with builtin models like gpt-4-0125-preview', () => {
const result = parseModelString(
'-all,+gpt-4-0125-preview=ChatGPT-4<128000:fc:file>,+gpt-4-turbo-2024-04-09=ChatGPT-4 Vision<128000:fc:vision:file>',
);
expect(result.add).toEqual([
{
displayName: 'ChatGPT-4',
files: true,
functionCall: true,
id: 'gpt-4-0125-preview',
tokens: 128000,
},
{
displayName: 'ChatGPT-4 Vision',
files: true,
functionCall: true,
id: 'gpt-4-turbo-2024-04-09',
tokens: 128000,
vision: true,
},
]);
});
it('should handle empty extension capability value', () => {
const result = parseModelString('model1<1024:>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024 });
});
it('should handle empty extension capability name', () => {
const result = parseModelString('model1<1024::file>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024, files: true });
});
it('should handle duplicate extension capabilities', () => {
const result = parseModelString('model1<1024:vision:vision>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024, vision: true });
});
it('should handle case-sensitive extension capability names', () => {
const result = parseModelString('model1<1024:VISION:FC:file>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024, files: true });
});
it('should handle case-sensitive extension capability values', () => {
const result = parseModelString('model1<1024:vision:Fc:File>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024, vision: true });
});
it('should handle empty angle brackets', () => {
const result = parseModelString('model1<>');
expect(result.add[0]).toEqual({ id: 'model1' });
});
it('should handle not close angle brackets', () => {
const result = parseModelString('model1<,model2');
expect(result.add).toEqual([{ id: 'model1' }, { id: 'model2' }]);
});
it('should handle multi close angle brackets', () => {
const result = parseModelString('model1<>>,model2');
expect(result.add).toEqual([{ id: 'model1' }, { id: 'model2' }]);
});
it('should handle only colon inside angle brackets', () => {
const result = parseModelString('model1<:>');
expect(result.add[0]).toEqual({ id: 'model1' });
});
it('should handle only non-digit characters inside angle brackets', () => {
const result = parseModelString('model1<abc>');
expect(result.add[0]).toEqual({ id: 'model1' });
});
it('should handle non-digit characters followed by digits inside angle brackets', () => {
const result = parseModelString('model1<abc123>');
expect(result.add[0]).toEqual({ id: 'model1' });
});
it('should handle digits followed by non-colon characters inside angle brackets', () => {
const result = parseModelString('model1<1024abc>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024 });
});
it('should handle digits followed by multiple colons inside angle brackets', () => {
const result = parseModelString('model1<1024::>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024 });
});
it('should handle digits followed by a colon and non-letter characters inside angle brackets', () => {
const result = parseModelString('model1<1024:123>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024 });
});
it('should handle digits followed by a colon and spaces inside angle brackets', () => {
const result = parseModelString('model1<1024: vision>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024 });
});
it('should handle digits followed by multiple colons and spaces inside angle brackets', () => {
const result = parseModelString('model1<1024: : vision>');
expect(result.add[0]).toEqual({ id: 'model1', tokens: 1024 });
});
});
describe('deployment name', () => {
it('should have same deployment name as id', () => {
const result = parseModelString('model1=Model 1', true);
expect(result.add[0]).toEqual({
id: 'model1',
displayName: 'Model 1',
deploymentName: 'model1',
});
});
it('should have diff deployment name as id', () => {
const result = parseModelString('gpt-35-turbo->my-deploy=GPT 3.5 Turbo', true);
expect(result.add[0]).toEqual({
id: 'gpt-35-turbo',
displayName: 'GPT 3.5 Turbo',
deploymentName: 'my-deploy',
});
});
});
});
describe('transformToChatModelCards', () => {
const defaultChatModels: ChatModelCard[] = [
{ id: 'model1', displayName: 'Model 1', enabled: true },
{ id: 'model2', displayName: 'Model 2', enabled: false },
];
it('should return undefined when modelString is empty', () => {
const result = transformToChatModelCards({
modelString: '',
defaultChatModels,
});
expect(result).toBeUndefined();
});
it('should remove all models when removeAll is true', () => {
const result = transformToChatModelCards({
modelString: '-all',
defaultChatModels,
});
expect(result).toEqual([]);
});
it('should remove specified models', () => {
const result = transformToChatModelCards({
modelString: '-model1',
defaultChatModels,
});
expect(result).toEqual([{ id: 'model2', displayName: 'Model 2', enabled: false }]);
});
it('should add a new known model', () => {
const knownModel = LOBE_DEFAULT_MODEL_LIST[0];
const result = transformToChatModelCards({
modelString: `${knownModel.id}`,
defaultChatModels,
});
expect(result).toContainEqual({
...knownModel,
displayName: knownModel.displayName || knownModel.id,
enabled: true,
});
});
it('should update an existing known model', () => {
const knownModel = LOBE_DEFAULT_MODEL_LIST[0];
const result = transformToChatModelCards({
modelString: `+${knownModel.id}=Updated Model`,
defaultChatModels: [knownModel],
});
expect(result![0]).toEqual({ ...knownModel, displayName: 'Updated Model', enabled: true });
});
it('should add a new custom model', () => {
const result = transformToChatModelCards({
modelString: '+custom_model=Custom Model',
defaultChatModels,
});
expect(result).toContainEqual({
id: 'custom_model',
displayName: 'Custom Model',
enabled: true,
});
});
it('should have file with builtin models like gpt-4-0125-preview', () => {
const result = transformToChatModelCards({
modelString:
'-all,+gpt-4-0125-preview=ChatGPT-4<128000:fc:file>,+gpt-4-turbo-2024-04-09=ChatGPT-4 Vision<128000:fc:vision:file>',
defaultChatModels: OpenAIProviderCard.chatModels,
});
expect(result).toEqual([
{
displayName: 'ChatGPT-4',
files: true,
functionCall: true,
enabled: true,
id: 'gpt-4-0125-preview',
tokens: 128000,
},
{
description: 'GPT-4 Turbo 视觉版 (240409)',
displayName: 'ChatGPT-4 Vision',
files: true,
functionCall: true,
enabled: true,
id: 'gpt-4-turbo-2024-04-09',
tokens: 128000,
vision: true,
},
]);
});
});