@@ -152,6 +152,33 @@ describe('validationRules', () => {
152152 expect ( validationRules . text_model ( 'true' ) ) . toBe ( false )
153153 expect ( validationRules . text_model ( 1 ) ) . toBe ( false )
154154 } )
155+
156+ it ( 'should validate repeat_last_n correctly' , ( ) => {
157+ expect ( validationRules . repeat_last_n ( 5 ) ) . toBe ( true )
158+ expect ( validationRules . repeat_last_n ( - 5 ) ) . toBe ( true )
159+ expect ( validationRules . repeat_last_n ( 0 ) ) . toBe ( true )
160+ expect ( validationRules . repeat_last_n ( 1.5 ) ) . toBe ( true )
161+ expect ( validationRules . repeat_last_n ( '5' ) ) . toBe ( false )
162+ expect ( validationRules . repeat_last_n ( null ) ) . toBe ( false )
163+ } )
164+
165+ it ( 'should validate repeat_penalty correctly' , ( ) => {
166+ expect ( validationRules . repeat_penalty ( 1.1 ) ) . toBe ( true )
167+ expect ( validationRules . repeat_penalty ( 0.9 ) ) . toBe ( true )
168+ expect ( validationRules . repeat_penalty ( 0 ) ) . toBe ( true )
169+ expect ( validationRules . repeat_penalty ( - 1 ) ) . toBe ( true )
170+ expect ( validationRules . repeat_penalty ( '1.1' ) ) . toBe ( false )
171+ expect ( validationRules . repeat_penalty ( null ) ) . toBe ( false )
172+ } )
173+
174+ it ( 'should validate min_p correctly' , ( ) => {
175+ expect ( validationRules . min_p ( 0.1 ) ) . toBe ( true )
176+ expect ( validationRules . min_p ( 0 ) ) . toBe ( true )
177+ expect ( validationRules . min_p ( - 0.1 ) ) . toBe ( true )
178+ expect ( validationRules . min_p ( 1.5 ) ) . toBe ( true )
179+ expect ( validationRules . min_p ( '0.1' ) ) . toBe ( false )
180+ expect ( validationRules . min_p ( null ) ) . toBe ( false )
181+ } )
155182} )
156183
157184it ( 'should normalize invalid values for keys not listed in validationRules' , ( ) => {
@@ -192,18 +219,125 @@ describe('normalizeValue', () => {
192219 expect ( normalizeValue ( 'cpu_threads' , '4' ) ) . toBe ( 4 )
193220 expect ( normalizeValue ( 'cpu_threads' , 0 ) ) . toBe ( 0 )
194221 } )
195- } )
196222
197- it ( 'should handle invalid values correctly by falling back to originParams' , ( ) => {
198- const modelParams = { temperature : 'invalid' , token_limit : - 1 }
199- const originParams = { temperature : 0.5 , token_limit : 100 }
200- expect ( extractInferenceParams ( modelParams as any , originParams ) ) . toEqual ( originParams )
223+ it ( 'should handle edge cases for normalization' , ( ) => {
224+ expect ( normalizeValue ( 'ctx_len' , - 5.7 ) ) . toBe ( - 6 )
225+ expect ( normalizeValue ( 'token_limit' , 'abc' ) ) . toBeNaN ( )
226+ expect ( normalizeValue ( 'max_tokens' , null ) ) . toBe ( 0 )
227+ expect ( normalizeValue ( 'ngl' , undefined ) ) . toBeNaN ( )
228+ expect ( normalizeValue ( 'n_parallel' , Infinity ) ) . toBe ( Infinity )
229+ expect ( normalizeValue ( 'cpu_threads' , - Infinity ) ) . toBe ( - Infinity )
230+ } )
231+
232+ it ( 'should not normalize non-integer parameters' , ( ) => {
233+ expect ( normalizeValue ( 'temperature' , 1.5 ) ) . toBe ( 1.5 )
234+ expect ( normalizeValue ( 'top_p' , 0.9 ) ) . toBe ( 0.9 )
235+ expect ( normalizeValue ( 'stream' , true ) ) . toBe ( true )
236+ expect ( normalizeValue ( 'prompt_template' , 'template' ) ) . toBe ( 'template' )
237+ } )
201238} )
202239
203- it ( 'should return an empty object when no modelParams are provided' , ( ) => {
204- expect ( extractModelLoadParams ( ) ) . toEqual ( { } )
240+ describe ( 'extractInferenceParams' , ( ) => {
241+ it ( 'should handle invalid values correctly by falling back to originParams' , ( ) => {
242+ const modelParams = { temperature : 'invalid' , token_limit : - 1 }
243+ const originParams = { temperature : 0.5 , token_limit : 100 }
244+ expect ( extractInferenceParams ( modelParams as any , originParams ) ) . toEqual ( originParams )
245+ } )
246+
247+ it ( 'should return an empty object when no modelParams are provided' , ( ) => {
248+ expect ( extractInferenceParams ( ) ) . toEqual ( { } )
249+ } )
250+
251+ it ( 'should extract and normalize valid inference parameters' , ( ) => {
252+ const modelParams = {
253+ temperature : 1.5 ,
254+ token_limit : 100.7 ,
255+ top_p : 0.9 ,
256+ stream : true ,
257+ max_tokens : 50.3 ,
258+ invalid_param : 'should_be_ignored'
259+ }
260+
261+ const result = extractInferenceParams ( modelParams as any )
262+ expect ( result ) . toEqual ( {
263+ temperature : 1.5 ,
264+ token_limit : 100 ,
265+ top_p : 0.9 ,
266+ stream : true ,
267+ max_tokens : 50
268+ } )
269+ } )
270+
271+ it ( 'should handle parameters without validation rules' , ( ) => {
272+ const modelParams = { engine : 'llama' }
273+ const result = extractInferenceParams ( modelParams as any )
274+ expect ( result ) . toEqual ( { engine : 'llama' } )
275+ } )
276+
277+ it ( 'should skip invalid values when no origin params provided' , ( ) => {
278+ const modelParams = { temperature : 'invalid' , top_p : 0.8 }
279+ const result = extractInferenceParams ( modelParams as any )
280+ expect ( result ) . toEqual ( { top_p : 0.8 } )
281+ } )
205282} )
206283
207- it ( 'should return an empty object when no modelParams are provided' , ( ) => {
208- expect ( extractInferenceParams ( ) ) . toEqual ( { } )
284+ describe ( 'extractModelLoadParams' , ( ) => {
285+ it ( 'should return an empty object when no modelParams are provided' , ( ) => {
286+ expect ( extractModelLoadParams ( ) ) . toEqual ( { } )
287+ } )
288+
289+ it ( 'should extract and normalize valid model load parameters' , ( ) => {
290+ const modelParams = {
291+ ctx_len : 2048.5 ,
292+ ngl : 12.7 ,
293+ embedding : true ,
294+ n_parallel : 4.2 ,
295+ cpu_threads : 8.9 ,
296+ prompt_template : 'template' ,
297+ llama_model_path : '/path/to/model' ,
298+ vision_model : false ,
299+ invalid_param : 'should_be_ignored'
300+ }
301+
302+ const result = extractModelLoadParams ( modelParams as any )
303+ expect ( result ) . toEqual ( {
304+ ctx_len : 2048 ,
305+ ngl : 12 ,
306+ embedding : true ,
307+ n_parallel : 4 ,
308+ cpu_threads : 8 ,
309+ prompt_template : 'template' ,
310+ llama_model_path : '/path/to/model' ,
311+ vision_model : false
312+ } )
313+ } )
314+
315+ it ( 'should handle parameters without validation rules' , ( ) => {
316+ const modelParams = {
317+ engine : 'llama' ,
318+ pre_prompt : 'System:' ,
319+ system_prompt : 'You are helpful' ,
320+ model_path : '/path'
321+ }
322+ const result = extractModelLoadParams ( modelParams as any )
323+ expect ( result ) . toEqual ( {
324+ engine : 'llama' ,
325+ pre_prompt : 'System:' ,
326+ system_prompt : 'You are helpful' ,
327+ model_path : '/path'
328+ } )
329+ } )
330+
331+ it ( 'should fall back to origin params for invalid values' , ( ) => {
332+ const modelParams = { ctx_len : - 1 , ngl : 'invalid' }
333+ const originParams = { ctx_len : 2048 , ngl : 12 }
334+ const result = extractModelLoadParams ( modelParams as any , originParams )
335+ expect ( result ) . toEqual ( { } )
336+ } )
337+
338+ it ( 'should skip invalid values when no origin params provided' , ( ) => {
339+ const modelParams = { ctx_len : - 1 , embedding : true }
340+ const result = extractModelLoadParams ( modelParams as any )
341+ expect ( result ) . toEqual ( { embedding : true } )
342+ } )
209343} )
0 commit comments