File size: 13,050 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { EModelEndpoint, Constants } from 'librechat-data-provider';
import type { TConversation, TPreset } from 'librechat-data-provider';
import createChatSearchParams from './createChatSearchParams';

describe('createChatSearchParams', () => {
  describe('conversation inputs', () => {
    it('handles basic conversation properties', () => {
      const conversation: Partial<TConversation> = {
        endpoint: EModelEndpoint.openAI,
        model: 'gpt-4',
        temperature: 0.7,
      };

      const result = createChatSearchParams(conversation as TConversation);
      expect(result.get('endpoint')).toBe(EModelEndpoint.openAI);
      expect(result.get('model')).toBe('gpt-4');
      expect(result.get('temperature')).toBe('0.7');
    });

    it('applies only the endpoint property when other conversation fields are absent', () => {
      const endpointOnly = createChatSearchParams({
        endpoint: EModelEndpoint.openAI,
      } as TConversation);
      expect(endpointOnly.get('endpoint')).toBe(EModelEndpoint.openAI);
      expect(endpointOnly.has('model')).toBe(false);
      expect(endpointOnly.has('endpoint')).toBe(true);
    });

    it('applies only the model property when other conversation fields are absent', () => {
      const modelOnly = createChatSearchParams({ model: 'gpt-4' } as TConversation);
      expect(modelOnly.has('endpoint')).toBe(false);
      expect(modelOnly.get('model')).toBe('gpt-4');
      expect(modelOnly.has('model')).toBe(true);
    });

    it('includes assistant_id when endpoint is assistants', () => {
      const withAssistantId = createChatSearchParams({
        endpoint: EModelEndpoint.assistants,
        model: 'gpt-4',
        assistant_id: 'asst_123',
        temperature: 0.7,
      } as TConversation);

      expect(withAssistantId.get('assistant_id')).toBe('asst_123');
      expect(withAssistantId.has('endpoint')).toBe(false);
      expect(withAssistantId.has('model')).toBe(false);
      expect(withAssistantId.has('temperature')).toBe(false);
    });

    it('includes agent_id when endpoint is agents', () => {
      const withAgentId = createChatSearchParams({
        endpoint: EModelEndpoint.agents,
        model: 'gpt-4',
        agent_id: 'agent_123',
        temperature: 0.7,
      } as TConversation);

      expect(withAgentId.get('agent_id')).toBe('agent_123');
      expect(withAgentId.has('endpoint')).toBe(false);
      expect(withAgentId.has('model')).toBe(false);
      expect(withAgentId.has('temperature')).toBe(false);
    });

    it('excludes all parameters except assistant_id when endpoint is assistants', () => {
      const withAssistantId = createChatSearchParams({
        endpoint: EModelEndpoint.assistants,
        model: 'gpt-4',
        assistant_id: 'asst_123',
        temperature: 0.7,
      } as TConversation);

      expect(withAssistantId.get('assistant_id')).toBe('asst_123');
      expect(withAssistantId.has('endpoint')).toBe(false);
      expect(withAssistantId.has('model')).toBe(false);
      expect(withAssistantId.has('temperature')).toBe(false);
      expect([...withAssistantId.entries()].length).toBe(1);
    });

    it('excludes all parameters except agent_id when endpoint is agents', () => {
      const withAgentId = createChatSearchParams({
        endpoint: EModelEndpoint.agents,
        model: 'gpt-4',
        agent_id: 'agent_123',
        temperature: 0.7,
      } as TConversation);

      expect(withAgentId.get('agent_id')).toBe('agent_123');
      expect(withAgentId.has('endpoint')).toBe(false);
      expect(withAgentId.has('model')).toBe(false);
      expect(withAgentId.has('temperature')).toBe(false);
      expect([...withAgentId.entries()].length).toBe(1);
    });

    it('returns empty params when agent endpoint has no agent_id', () => {
      const result = createChatSearchParams({
        endpoint: EModelEndpoint.agents,
        model: 'gpt-4',
        temperature: 0.7,
      } as TConversation);

      expect(result.toString()).toBe('');
      expect([...result.entries()].length).toBe(0);
    });

    it('returns empty params when assistants endpoint has no assistant_id', () => {
      const result = createChatSearchParams({
        endpoint: EModelEndpoint.assistants,
        model: 'gpt-4',
        temperature: 0.7,
      } as TConversation);

      expect(result.toString()).toBe('');
      expect([...result.entries()].length).toBe(0);
    });

    it('ignores agent_id when it matches EPHEMERAL_AGENT_ID', () => {
      const result = createChatSearchParams({
        endpoint: EModelEndpoint.agents,
        model: 'gpt-4',
        agent_id: Constants.EPHEMERAL_AGENT_ID,
        temperature: 0.7,
      } as TConversation);

      // The agent_id is ignored but other params are still included
      expect(result.has('agent_id')).toBe(false);
      expect(result.get('endpoint')).toBe(EModelEndpoint.agents);
      expect(result.get('model')).toBe('gpt-4');
      expect(result.get('temperature')).toBe('0.7');
    });

    it('handles stop arrays correctly by joining with commas', () => {
      const withStopArray = createChatSearchParams({
        endpoint: EModelEndpoint.openAI,
        model: 'gpt-4',
        stop: ['stop1', 'stop2'],
      } as TConversation);

      expect(withStopArray.get('endpoint')).toBe(EModelEndpoint.openAI);
      expect(withStopArray.get('model')).toBe('gpt-4');
      expect(withStopArray.get('stop')).toBe('stop1,stop2');
    });

    it('filters out non-supported array properties', () => {
      const withOtherArray = createChatSearchParams({
        endpoint: EModelEndpoint.openAI,
        model: 'gpt-4',
        otherArrayProp: ['value1', 'value2'],
      } as any);

      expect(withOtherArray.get('endpoint')).toBe(EModelEndpoint.openAI);
      expect(withOtherArray.get('model')).toBe('gpt-4');
      expect(withOtherArray.has('otherArrayProp')).toBe(false);
    });

    it('includes empty arrays in output params', () => {
      const result = createChatSearchParams({
        endpoint: EModelEndpoint.openAI,
        stop: [],
      });

      expect(result.get('endpoint')).toBe(EModelEndpoint.openAI);
      expect(result.has('stop')).toBe(true);
      expect(result.get('stop')).toBe('');
    });

    it('handles non-stop arrays correctly in paramMap', () => {
      const conversation: any = {
        endpoint: EModelEndpoint.openAI,
        model: 'gpt-4',
        top_p: ['0.7', '0.8'],
      };

      const result = createChatSearchParams(conversation);

      const expectedJson = JSON.stringify(['0.7', '0.8']);
      expect(result.get('top_p')).toBe(expectedJson);
      expect(result.get('endpoint')).toBe(EModelEndpoint.openAI);
      expect(result.get('model')).toBe('gpt-4');
    });

    it('includes empty non-stop arrays as serialized empty arrays', () => {
      const result = createChatSearchParams({
        endpoint: EModelEndpoint.openAI,
        model: 'gpt-4',
        temperature: 0.7,
        top_p: [],
      } as any);

      expect(result.get('endpoint')).toBe(EModelEndpoint.openAI);
      expect(result.get('model')).toBe('gpt-4');
      expect(result.get('temperature')).toBe('0.7');
      expect(result.has('top_p')).toBe(true);
      expect(result.get('top_p')).toBe('[]');
    });

    it('excludes parameters with null or undefined values from the output', () => {
      const result = createChatSearchParams({
        endpoint: EModelEndpoint.openAI,
        model: 'gpt-4',
        temperature: 0.7,
        top_p: undefined,
        presence_penalty: undefined,
        frequency_penalty: null,
      } as any);

      expect(result.get('endpoint')).toBe(EModelEndpoint.openAI);
      expect(result.get('model')).toBe('gpt-4');
      expect(result.get('temperature')).toBe('0.7');
      expect(result.has('top_p')).toBe(false);
      expect(result.has('presence_penalty')).toBe(false);
      expect(result.has('frequency_penalty')).toBe(false);
      expect(result).toBeDefined();
    });

    it('handles float parameter values correctly', () => {
      const result = createChatSearchParams({
        endpoint: EModelEndpoint.google,
        model: 'gemini-pro',
        frequency_penalty: 0.25,
        temperature: 0.75,
      });

      expect(result.get('endpoint')).toBe(EModelEndpoint.google);
      expect(result.get('model')).toBe('gemini-pro');
      expect(result.get('frequency_penalty')).toBe('0.25');
      expect(result.get('temperature')).toBe('0.75');
    });

    it('handles integer parameter values correctly', () => {
      const result = createChatSearchParams({
        endpoint: EModelEndpoint.google,
        model: 'gemini-pro',
        topK: 40,
        maxOutputTokens: 2048,
      });

      expect(result.get('endpoint')).toBe(EModelEndpoint.google);
      expect(result.get('model')).toBe('gemini-pro');
      expect(result.get('topK')).toBe('40');
      expect(result.get('maxOutputTokens')).toBe('2048');
    });
  });

  describe('preset inputs', () => {
    it('handles preset objects correctly', () => {
      const preset: Partial<TPreset> = {
        endpoint: EModelEndpoint.google,
        model: 'gemini-pro',
        temperature: 0.5,
        topP: 0.8,
      };

      const result = createChatSearchParams(preset as TPreset);
      expect(result.get('endpoint')).toBe(EModelEndpoint.google);
      expect(result.get('model')).toBe('gemini-pro');
      expect(result.get('temperature')).toBe('0.5');
      expect(result.get('topP')).toBe('0.8');
    });

    it('returns only spec param when spec property is present', () => {
      const preset: Partial<TPreset> = {
        endpoint: EModelEndpoint.google,
        model: 'gemini-pro',
        temperature: 0.5,
        spec: 'special_spec',
      };

      const result = createChatSearchParams(preset as TPreset);
      expect(result.get('spec')).toBe('special_spec');
      expect(result.has('endpoint')).toBe(false);
      expect(result.has('model')).toBe(false);
      expect(result.has('temperature')).toBe(false);
      expect([...result.entries()].length).toBe(1);
    });
  });

  describe('record inputs', () => {
    it('includes allowed parameters from Record inputs', () => {
      const record: Record<string, any> = {
        endpoint: EModelEndpoint.anthropic,
        model: 'claude-2',
        temperature: '0.8',
        top_p: '0.95',
        extraParam: 'should-not-be-included',
        invalidParam1: 'value1',
        invalidParam2: 'value2',
      };

      const result = createChatSearchParams(record);
      expect(result.get('endpoint')).toBe(EModelEndpoint.anthropic);
      expect(result.get('model')).toBe('claude-2');
      expect(result.get('temperature')).toBe('0.8');
      expect(result.get('top_p')).toBe('0.95');
    });

    it('excludes disallowed parameters from Record inputs', () => {
      const record: Record<string, any> = {
        endpoint: EModelEndpoint.anthropic,
        model: 'claude-2',
        extraParam: 'should-not-be-included',
        invalidParam1: 'value1',
        invalidParam2: 'value2',
      };

      const result = createChatSearchParams(record);
      expect(result.has('extraParam')).toBe(false);
      expect(result.has('invalidParam1')).toBe(false);
      expect(result.has('invalidParam2')).toBe(false);
      expect(result.toString().includes('invalidParam')).toBe(false);
      expect(result.toString().includes('extraParam')).toBe(false);
    });

    it('includes valid values from Record inputs', () => {
      const record: Record<string, any> = {
        temperature: '0.7',
        top_p: null,
        frequency_penalty: undefined,
      };

      const result = createChatSearchParams(record);
      expect(result.get('temperature')).toBe('0.7');
    });

    it('excludes null or undefined values from Record inputs', () => {
      const record: Record<string, any> = {
        temperature: '0.7',
        top_p: null,
        frequency_penalty: undefined,
      };

      const result = createChatSearchParams(record);
      expect(result.has('top_p')).toBe(false);
      expect(result.has('frequency_penalty')).toBe(false);
    });

    it('handles generic object without endpoint or model properties', () => {
      const customObject = {
        temperature: '0.5',
        top_p: '0.7',
        customProperty: 'value',
      };

      const result = createChatSearchParams(customObject);
      expect(result.get('temperature')).toBe('0.5');
      expect(result.get('top_p')).toBe('0.7');
      expect(result.has('customProperty')).toBe(false);
    });
  });

  describe('edge cases', () => {
    it('returns an empty URLSearchParams instance when input is null', () => {
      const result = createChatSearchParams(null);
      expect(result.toString()).toBe('');
      expect(result instanceof URLSearchParams).toBe(true);
    });

    it('returns an empty URLSearchParams instance for an empty object input', () => {
      const result = createChatSearchParams({});
      expect(result.toString()).toBe('');
      expect(result instanceof URLSearchParams).toBe(true);
    });
  });
});