request_adapter.rs 42.4 KB
Newer Older
1
2
// Request adapter to bridge OpenAI API types with PD routing requirements

3
use super::pd_types::{Bootstrap, ChatReqInput, GenerateReqInput, SingleOrBatch};
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
use crate::openai_api_types::{
    ChatCompletionRequest, CompletionRequest, GenerateRequest, GenerationRequest, StringOrArray,
};
use serde_json::Value;

/// Adapter trait to convert OpenAI requests to PD-compatible requests
pub trait ToPdRequest {
    type Output: Bootstrap;
    fn to_pd_request(self) -> Self::Output;
}

// Helper macro to insert optional fields into a map
macro_rules! insert_if_some {
    ($map:expr, $($field:expr => $key:expr),* $(,)?) => {
        $(
            if let Some(value) = $field {
                $map.insert($key.to_string(), serde_json::to_value(value).unwrap_or(Value::Null));
            }
        )*
    };
}

// Helper macro for simple value insertions
macro_rules! insert_value {
    ($map:expr, $($field:expr => $key:expr),* $(,)?) => {
        $(
            $map.insert($key.to_string(), $field.into());
        )*
    };
}

// ============= Generate Request Adapter =============

impl ToPdRequest for GenerateRequest {
    type Output = GenerateReqInput;

    fn to_pd_request(self) -> Self::Output {
        // Build the other fields first
        let mut other = serde_json::Map::new();

        // Handle text input - check in priority order: text (SGLang), prompt (OpenAI)
        let (text, input_ids) = if let Some(text_str) = self.text {
            // SGLang native format
            (Some(SingleOrBatch::Single(text_str)), None)
        } else if let Some(prompt) = self.prompt {
            // OpenAI style prompt
            let text = match prompt {
                StringOrArray::String(s) => Some(SingleOrBatch::Single(s)),
                StringOrArray::Array(v) => Some(SingleOrBatch::Batch(v)),
            };
            (text, None)
        } else if let Some(ids) = self.input_ids {
            // Input IDs case
            let input_ids = match ids {
                crate::openai_api_types::InputIds::Single(ids) => Some(SingleOrBatch::Single(ids)),
                crate::openai_api_types::InputIds::Batch(ids) => Some(SingleOrBatch::Batch(ids)),
            };
            (None, input_ids)
        } else {
            // No input provided
            (None, None)
        };

        // Add parameters to other - handle both old and new style
        if let Some(params) = self.parameters {
            // For generate endpoint, extract max_new_tokens to top level if present
            let mut params_value = serde_json::to_value(&params).unwrap_or(Value::Null);
            if let Value::Object(ref mut params_map) = params_value {
                // Move max_new_tokens to top level if it exists
                if let Some(max_new_tokens) = params_map.remove("max_new_tokens") {
                    other.insert("max_new_tokens".to_string(), max_new_tokens);
                }
                // Move temperature to top level if it exists
                if let Some(temperature) = params_map.remove("temperature") {
                    other.insert("temperature".to_string(), temperature);
                }
            }
            // Only add parameters if there are remaining fields
            if !params_value.is_null() && params_value.as_object().map_or(false, |m| !m.is_empty())
            {
                other.insert("parameters".to_string(), params_value);
            }
        }

        // Add sampling_params if present
        if let Some(sampling_params) = self.sampling_params {
            let params_value = serde_json::to_value(&sampling_params).unwrap_or(Value::Null);
            if !params_value.is_null() {
                // Extract commonly used fields to top level
                if let Value::Object(ref params_map) = params_value {
                    if let Some(max_new_tokens) = params_map.get("max_new_tokens") {
                        other.insert("max_new_tokens".to_string(), max_new_tokens.clone());
                    }
                    if let Some(temperature) = params_map.get("temperature") {
                        other.insert("temperature".to_string(), temperature.clone());
                    }
                }
                other.insert("sampling_params".to_string(), params_value);
            }
        }

        // Add other fields
        insert_value!(other,
            self.stream => "stream",
            self.return_logprob => "return_logprob"
        );

        GenerateReqInput {
            text,
            input_ids,
            stream: self.stream,
            bootstrap_host: None,
            bootstrap_port: None,
            bootstrap_room: None,
            other: Value::Object(other),
        }
    }
}

// ============= Completion Request Adapter =============

impl ToPdRequest for CompletionRequest {
    type Output = GenerateReqInput;

    fn to_pd_request(self) -> Self::Output {
        // Convert CompletionRequest to GenerateReqInput
        let text = match self.prompt {
            StringOrArray::String(s) => Some(SingleOrBatch::Single(s)),
            StringOrArray::Array(v) => Some(SingleOrBatch::Batch(v)),
        };

        // Map OpenAI parameters to generate parameters
        let mut other = serde_json::Map::new();

        // Create parameters object
        let mut params = serde_json::Map::new();

        // Map OpenAI fields to internal parameter names
        insert_if_some!(params,
            self.max_tokens => "max_new_tokens",
            self.temperature => "temperature",
            self.top_p => "top_p",
            self.n => "best_of",
            self.logprobs => "top_n_tokens",
            self.seed => "seed"
        );

        // Special handling for fields that need transformation
        if let Some(presence_penalty) = self.presence_penalty {
            params.insert(
                "repetition_penalty".to_string(),
                (1.0 + presence_penalty).into(),
            );
        }

        if let Some(stop) = self.stop {
            let stop_sequences = match stop {
                StringOrArray::String(s) => vec![s],
                StringOrArray::Array(v) => v,
            };
            params.insert("stop".to_string(), stop_sequences.into());
        }

        if self.echo {
            params.insert("return_full_text".to_string(), true.into());
        }

        other.insert("parameters".to_string(), Value::Object(params));

        // Store original model and stream flag
        insert_value!(other,
            self.model => "model",
            self.stream => "stream"
        );

        GenerateReqInput {
            text,
            input_ids: None,
            stream: self.stream,
            bootstrap_host: None,
            bootstrap_port: None,
            bootstrap_room: None,
            other: Value::Object(other),
        }
    }
}

// ============= Chat Completion Request Adapter =============

impl ToPdRequest for ChatCompletionRequest {
    type Output = ChatReqInput;

    fn to_pd_request(self) -> Self::Output {
        let mut other = serde_json::Map::new();

        // Add required fields
        insert_if_some!(other,
            Some(&self.messages) => "messages"
        );

        insert_value!(other,
            self.model => "model",
            self.stream => "stream"
        );

        // Add all optional fields
        insert_if_some!(other,
            self.temperature => "temperature",
            self.top_p => "top_p",
            self.n => "n",
214
            self.stream_options => "stream_options",
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
            self.stop => "stop",
            self.max_tokens => "max_tokens",
            self.max_completion_tokens => "max_completion_tokens",
            self.presence_penalty => "presence_penalty",
            self.frequency_penalty => "frequency_penalty",
            self.logit_bias => "logit_bias",
            self.user => "user",
            self.seed => "seed",
            self.top_logprobs => "top_logprobs",
            self.response_format => "response_format",
            self.tools => "tools",
            self.tool_choice => "tool_choice",
            self.parallel_tool_calls => "parallel_tool_calls",
            self.functions => "functions",
            self.function_call => "function_call"
        );

        // Handle boolean logprobs flag
        if self.logprobs {
            other.insert("logprobs".to_string(), true.into());
        }

        ChatReqInput {
            stream: self.stream,
            bootstrap_host: None,
            bootstrap_port: None,
            bootstrap_room: None,
            other: Value::Object(other),
        }
    }
}

// ============= Direct routing support for regular router =============

/// Extension trait for routing without PD conversion
pub trait RouteableRequest: GenerationRequest + serde::Serialize + Clone {
    /// Convert to JSON for sending to backend
    fn to_json(&self) -> Result<Value, serde_json::Error> {
        serde_json::to_value(self)
    }

    /// Convert to bytes for legacy routing
    fn to_bytes(&self) -> Result<bytes::Bytes, serde_json::Error> {
        let json = serde_json::to_vec(self)?;
        Ok(bytes::Bytes::from(json))
    }
}

impl RouteableRequest for GenerateRequest {}
impl RouteableRequest for CompletionRequest {}
impl RouteableRequest for ChatCompletionRequest {}
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

#[cfg(test)]
mod tests {
    use super::*;
    use crate::openai_api_types::*;
    use serde_json::json;
    use std::collections::HashMap;

    // ============= GenerateRequest to_pd_request Tests =============

    #[test]
    fn test_generate_to_pd_request_with_text_only() {
        let req = GenerateRequest {
            text: Some("Hello world".to_string()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();

        // Check text field conversion
        assert!(matches!(pd_req.text, Some(SingleOrBatch::Single(ref s)) if s == "Hello world"));
        assert!(pd_req.input_ids.is_none());

        // Check bootstrap fields are None
        assert!(pd_req.bootstrap_host.is_none());
        assert!(pd_req.bootstrap_port.is_none());
        assert!(pd_req.bootstrap_room.is_none());

        // Check stream flag
        assert_eq!(pd_req.stream, false);

        // Check other fields
        let other = pd_req.other.as_object().unwrap();
        assert_eq!(other.get("stream"), Some(&json!(false)));
        assert_eq!(other.get("return_logprob"), Some(&json!(false)));
    }

    #[test]
    fn test_generate_to_pd_request_with_prompt_string() {
        let req = GenerateRequest {
            text: None,
            prompt: Some(StringOrArray::String("Test prompt".to_string())),
            input_ids: None,
            stream: true,
            parameters: None,
            sampling_params: None,
            return_logprob: true,
        };

        let pd_req = req.to_pd_request();

        assert!(matches!(pd_req.text, Some(SingleOrBatch::Single(ref s)) if s == "Test prompt"));
        assert!(pd_req.input_ids.is_none());
        assert_eq!(pd_req.stream, true);

        let other = pd_req.other.as_object().unwrap();
        assert_eq!(other.get("stream"), Some(&json!(true)));
        assert_eq!(other.get("return_logprob"), Some(&json!(true)));
    }

    #[test]
    fn test_generate_to_pd_request_with_prompt_array() {
        let req = GenerateRequest {
            text: None,
            prompt: Some(StringOrArray::Array(vec![
                "Prompt 1".to_string(),
                "Prompt 2".to_string(),
                "Prompt 3".to_string(),
            ])),
            input_ids: None,
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();

        match pd_req.text {
            Some(SingleOrBatch::Batch(ref batch)) => {
                assert_eq!(batch.len(), 3);
                assert_eq!(batch[0], "Prompt 1");
                assert_eq!(batch[1], "Prompt 2");
                assert_eq!(batch[2], "Prompt 3");
            }
            _ => panic!("Expected batch text"),
        }
    }

    #[test]
    fn test_generate_to_pd_request_with_single_input_ids() {
        let req = GenerateRequest {
            text: None,
            prompt: None,
            input_ids: Some(InputIds::Single(vec![100, 200, 300, 400])),
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();

        assert!(pd_req.text.is_none());
        assert!(matches!(
            pd_req.input_ids,
            Some(SingleOrBatch::Single(ref ids)) if ids == &vec![100, 200, 300, 400]
        ));
    }

    #[test]
    fn test_generate_to_pd_request_with_batch_input_ids() {
        let req = GenerateRequest {
            text: None,
            prompt: None,
            input_ids: Some(InputIds::Batch(vec![
                vec![1, 2, 3],
                vec![4, 5, 6, 7],
                vec![8, 9],
            ])),
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();

        match pd_req.input_ids {
            Some(SingleOrBatch::Batch(ref batch)) => {
                assert_eq!(batch.len(), 3);
                assert_eq!(batch[0], vec![1, 2, 3]);
                assert_eq!(batch[1], vec![4, 5, 6, 7]);
                assert_eq!(batch[2], vec![8, 9]);
            }
            _ => panic!("Expected batch input_ids"),
        }
    }

    #[test]
    fn test_generate_to_pd_request_priority_text_over_prompt() {
        let req = GenerateRequest {
            text: Some("SGLang text".to_string()),
            prompt: Some(StringOrArray::String("OpenAI prompt".to_string())),
            input_ids: Some(InputIds::Single(vec![1, 2, 3])),
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();

        // text should take priority
        assert!(matches!(pd_req.text, Some(SingleOrBatch::Single(ref s)) if s == "SGLang text"));
        assert!(pd_req.input_ids.is_none());
    }

    #[test]
    fn test_generate_to_pd_request_priority_prompt_over_input_ids() {
        let req = GenerateRequest {
            text: None,
            prompt: Some(StringOrArray::String("OpenAI prompt".to_string())),
            input_ids: Some(InputIds::Single(vec![1, 2, 3])),
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();

        // prompt should take priority over input_ids
        assert!(matches!(pd_req.text, Some(SingleOrBatch::Single(ref s)) if s == "OpenAI prompt"));
        assert!(pd_req.input_ids.is_none());
    }

    #[test]
    fn test_generate_to_pd_request_with_parameters() {
        let params = GenerateParameters {
            max_new_tokens: Some(100),
            temperature: Some(0.8),
            top_p: Some(0.95),
            seed: Some(12345),
            stop: Some(vec!["END".to_string(), "STOP".to_string()]),
            repetition_penalty: Some(1.1),
            ..Default::default()
        };

        let req = GenerateRequest {
            text: Some("test".to_string()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: Some(params),
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        // Check that max_new_tokens and temperature were extracted to top level
        assert_eq!(other.get("max_new_tokens"), Some(&json!(100)));
        assert!(other.get("temperature").unwrap().as_f64().unwrap() - 0.8 < 0.0001);

        // Check that other parameters remain under "parameters"
        let params = other.get("parameters").unwrap().as_object().unwrap();
        assert!(params.get("top_p").unwrap().as_f64().unwrap() - 0.95 < 0.0001);
        assert_eq!(params.get("seed"), Some(&json!(12345)));
        assert_eq!(params.get("stop"), Some(&json!(vec!["END", "STOP"])));
        assert!(params.get("repetition_penalty").unwrap().as_f64().unwrap() - 1.1 < 0.0001);
    }

    #[test]
    fn test_generate_to_pd_request_with_sampling_params() {
        let sampling = SamplingParams {
            max_new_tokens: Some(200),
            temperature: Some(0.7),
            top_p: Some(0.9),
            top_k: Some(50),
            frequency_penalty: Some(0.1),
            presence_penalty: Some(0.2),
            repetition_penalty: Some(1.05),
            ..Default::default()
        };

        let req = GenerateRequest {
            text: Some("test".to_string()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: None,
            sampling_params: Some(sampling),
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        // Check extracted top-level fields
        assert_eq!(other.get("max_new_tokens"), Some(&json!(200)));
        assert!(other.get("temperature").unwrap().as_f64().unwrap() - 0.7 < 0.0001);

        // Check full sampling_params is preserved
        let sampling = other.get("sampling_params").unwrap().as_object().unwrap();
        assert_eq!(sampling.get("max_new_tokens"), Some(&json!(200)));
        assert!(sampling.get("temperature").unwrap().as_f64().unwrap() - 0.7 < 0.0001);
        assert!(sampling.get("top_p").unwrap().as_f64().unwrap() - 0.9 < 0.0001);
        assert_eq!(sampling.get("top_k"), Some(&json!(50)));
        assert!(sampling.get("frequency_penalty").unwrap().as_f64().unwrap() - 0.1 < 0.0001);
        assert!(sampling.get("presence_penalty").unwrap().as_f64().unwrap() - 0.2 < 0.0001);
    }

    #[test]
    fn test_generate_to_pd_request_sampling_params_override_parameters() {
        // When both parameters and sampling_params have max_new_tokens/temperature,
        // sampling_params should take precedence (processed last)
        let params = GenerateParameters {
            max_new_tokens: Some(100),
            temperature: Some(0.5),
            ..Default::default()
        };

        let sampling = SamplingParams {
            max_new_tokens: Some(200),
            temperature: Some(0.9),
            ..Default::default()
        };

        let req = GenerateRequest {
            text: Some("test".to_string()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: Some(params),
            sampling_params: Some(sampling),
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        // Should use values from sampling_params since they're processed last
        assert_eq!(other.get("max_new_tokens"), Some(&json!(200)));
        assert!(other.get("temperature").unwrap().as_f64().unwrap() - 0.9 < 0.0001);
    }

    #[test]
    fn test_generate_to_pd_request_empty_parameters() {
        let params = GenerateParameters::default();

        let req = GenerateRequest {
            text: Some("test".to_string()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: Some(params),
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        // Should not have parameters field if all values are None/default
        assert!(!other.contains_key("parameters"));
        assert!(!other.contains_key("max_new_tokens"));
        assert!(!other.contains_key("temperature"));
    }

    #[test]
    fn test_generate_to_pd_request_all_fields() {
        let params = GenerateParameters {
            max_new_tokens: Some(150),
            temperature: Some(0.6),
            top_k: Some(40),
            ..Default::default()
        };

        let sampling = SamplingParams {
            max_new_tokens: Some(250), // Will override parameters
            temperature: Some(0.8),    // Will override parameters
            presence_penalty: Some(0.1),
            ..Default::default()
        };

        let req = GenerateRequest {
            text: Some("Complex test".to_string()),
            prompt: Some(StringOrArray::String("Ignored prompt".to_string())),
            input_ids: None,
            stream: true,
            parameters: Some(params),
            sampling_params: Some(sampling),
            return_logprob: true,
        };

        let pd_req = req.to_pd_request();

        // Verify all fields
        assert!(matches!(pd_req.text, Some(SingleOrBatch::Single(ref s)) if s == "Complex test"));
        assert!(pd_req.input_ids.is_none());
        assert_eq!(pd_req.stream, true);
        assert!(pd_req.bootstrap_host.is_none());
        assert!(pd_req.bootstrap_port.is_none());
        assert!(pd_req.bootstrap_room.is_none());

        let other = pd_req.other.as_object().unwrap();
        assert_eq!(other.get("stream"), Some(&json!(true)));
        assert_eq!(other.get("return_logprob"), Some(&json!(true)));
        // Sampling params override parameters
        assert_eq!(other.get("max_new_tokens"), Some(&json!(250)));
        assert!(other.get("temperature").unwrap().as_f64().unwrap() - 0.8 < 0.0001);
        assert!(other.contains_key("parameters"));
        assert!(other.contains_key("sampling_params"));
    }

    // ============= CompletionRequest to_pd_request Tests =============

    #[test]
    fn test_completion_to_pd_request_basic() {
        let req = CompletionRequest {
            model: "gpt-3.5-turbo".to_string(),
            prompt: StringOrArray::String("Complete this sentence".to_string()),
            max_tokens: None,
            temperature: None,
            top_p: None,
            n: None,
            stream: false,
            stream_options: None,
            logprobs: None,
            echo: false,
            stop: None,
            presence_penalty: None,
            frequency_penalty: None,
            best_of: None,
            logit_bias: None,
            user: None,
            seed: None,
            suffix: None,
651
            other: serde_json::Map::new(),
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
        };

        let pd_req = req.to_pd_request();

        assert!(
            matches!(pd_req.text, Some(SingleOrBatch::Single(ref s)) if s == "Complete this sentence")
        );
        assert!(pd_req.input_ids.is_none());
        assert_eq!(pd_req.stream, false);

        let other = pd_req.other.as_object().unwrap();
        assert_eq!(other.get("model"), Some(&json!("gpt-3.5-turbo")));
        assert_eq!(other.get("stream"), Some(&json!(false)));
    }

    #[test]
    fn test_completion_to_pd_request_array_prompt() {
        let req = CompletionRequest {
            model: "test".to_string(),
            prompt: StringOrArray::Array(vec![
                "First prompt".to_string(),
                "Second prompt".to_string(),
            ]),
            max_tokens: None,
            temperature: None,
            top_p: None,
            n: None,
            stream: false,
            stream_options: None,
            logprobs: None,
            echo: false,
            stop: None,
            presence_penalty: None,
            frequency_penalty: None,
            best_of: None,
            logit_bias: None,
            user: None,
            seed: None,
            suffix: None,
691
            other: serde_json::Map::new(),
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
        };

        let pd_req = req.to_pd_request();

        match pd_req.text {
            Some(SingleOrBatch::Batch(ref batch)) => {
                assert_eq!(batch.len(), 2);
                assert_eq!(batch[0], "First prompt");
                assert_eq!(batch[1], "Second prompt");
            }
            _ => panic!("Expected batch text"),
        }
    }

    #[test]
    fn test_completion_to_pd_request_parameter_mapping() {
        let req = CompletionRequest {
            model: "test".to_string(),
            prompt: StringOrArray::String("test".to_string()),
            max_tokens: Some(150), // -> max_new_tokens
            temperature: Some(0.75),
            top_p: Some(0.92),
            n: Some(3), // -> best_of
            stream: true,
            stream_options: None,
            logprobs: Some(10), // -> top_n_tokens
            echo: true,         // -> return_full_text
            stop: Some(StringOrArray::Array(vec![
                "\\n".to_string(),
                "END".to_string(),
            ])),
            presence_penalty: Some(0.5), // -> repetition_penalty = 1.5
            frequency_penalty: Some(0.2),
            best_of: Some(5),
            logit_bias: None,
            user: Some("user123".to_string()),
            seed: Some(42),
            suffix: Some("...".to_string()),
730
            other: serde_json::Map::new(),
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();
        let params = other.get("parameters").unwrap().as_object().unwrap();

        // Check parameter mappings
        assert_eq!(params.get("max_new_tokens"), Some(&json!(150)));
        assert!(params.get("temperature").unwrap().as_f64().unwrap() - 0.75 < 0.0001);
        assert!(params.get("top_p").unwrap().as_f64().unwrap() - 0.92 < 0.0001);
        assert_eq!(params.get("best_of"), Some(&json!(3)));
        assert_eq!(params.get("top_n_tokens"), Some(&json!(10)));
        assert_eq!(params.get("return_full_text"), Some(&json!(true)));
        assert_eq!(params.get("stop"), Some(&json!(vec!["\\n", "END"])));
        assert!(params.get("repetition_penalty").unwrap().as_f64().unwrap() - 1.5 < 0.0001);
        assert_eq!(params.get("seed"), Some(&json!(42)));

        // Check other fields
        assert_eq!(other.get("model"), Some(&json!("test")));
        assert_eq!(other.get("stream"), Some(&json!(true)));
    }

    #[test]
    fn test_completion_to_pd_request_stop_string() {
        let req = CompletionRequest {
            model: "test".to_string(),
            prompt: StringOrArray::String("test".to_string()),
            stop: Some(StringOrArray::String("STOP".to_string())),
            max_tokens: None,
            temperature: None,
            top_p: None,
            n: None,
            stream: false,
            stream_options: None,
            logprobs: None,
            echo: false,
            presence_penalty: None,
            frequency_penalty: None,
            best_of: None,
            logit_bias: None,
            user: None,
            seed: None,
            suffix: None,
774
            other: serde_json::Map::new(),
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();
        let params = other.get("parameters").unwrap().as_object().unwrap();

        // Single string stop should be converted to array
        assert_eq!(params.get("stop"), Some(&json!(vec!["STOP"])));
    }

    #[test]
    fn test_completion_to_pd_request_no_presence_penalty() {
        let req = CompletionRequest {
            model: "test".to_string(),
            prompt: StringOrArray::String("test".to_string()),
            presence_penalty: None,
            max_tokens: None,
            temperature: None,
            top_p: None,
            n: None,
            stream: false,
            stream_options: None,
            logprobs: None,
            echo: false,
            stop: None,
            frequency_penalty: None,
            best_of: None,
            logit_bias: None,
            user: None,
            seed: None,
            suffix: None,
806
            other: serde_json::Map::new(),
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();
        let params = other.get("parameters").unwrap().as_object().unwrap();

        // Should not have repetition_penalty if presence_penalty is None
        assert!(!params.contains_key("repetition_penalty"));
    }

    // ============= ChatCompletionRequest to_pd_request Tests =============

    #[test]
    fn test_chat_to_pd_request_basic() {
        let messages = vec![
            ChatMessage::System {
                role: "system".to_string(),
                content: "You are a helpful assistant".to_string(),
                name: None,
            },
            ChatMessage::User {
                role: "user".to_string(),
                content: UserMessageContent::Text("Hello!".to_string()),
                name: None,
            },
        ];

        let req = ChatCompletionRequest {
            messages,
            model: "gpt-4".to_string(),
            temperature: None,
            top_p: None,
            n: None,
            stream: false,
            stream_options: None,
            stop: None,
            max_tokens: None,
            max_completion_tokens: None,
            presence_penalty: None,
            frequency_penalty: None,
            logit_bias: None,
            logprobs: false,
            top_logprobs: None,
            user: None,
            seed: None,
            response_format: None,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            functions: None,
            function_call: None,
        };

        let pd_req = req.to_pd_request();

        assert_eq!(pd_req.stream, false);
        assert!(pd_req.bootstrap_host.is_none());
        assert!(pd_req.bootstrap_port.is_none());
        assert!(pd_req.bootstrap_room.is_none());

        let other = pd_req.other.as_object().unwrap();
        assert!(other.contains_key("messages"));
        assert_eq!(other.get("model"), Some(&json!("gpt-4")));
        assert_eq!(other.get("stream"), Some(&json!(false)));

        // Check messages are preserved
        let messages = other.get("messages").unwrap().as_array().unwrap();
        assert_eq!(messages.len(), 2);
    }

    #[test]
    fn test_chat_to_pd_request_with_all_optional_fields() {
        let messages = vec![ChatMessage::User {
            role: "user".to_string(),
            content: UserMessageContent::Text("Test".to_string()),
            name: Some("test_user".to_string()),
        }];

        let mut logit_bias = HashMap::new();
        logit_bias.insert("50256".to_string(), -100);

        let tool = Tool {
            tool_type: "function".to_string(),
            function: Function {
                name: "get_weather".to_string(),
                description: Some("Get weather info".to_string()),
                parameters: json!({"type": "object"}),
            },
        };

        let req = ChatCompletionRequest {
            messages,
            model: "gpt-4".to_string(),
            temperature: Some(0.8),
            top_p: Some(0.95),
            n: Some(2),
            stream: true,
            stream_options: Some(StreamOptions {
                include_usage: Some(true),
            }),
            stop: Some(StringOrArray::String("\\n\\n".to_string())),
            max_tokens: Some(200),
            max_completion_tokens: Some(150),
            presence_penalty: Some(0.1),
            frequency_penalty: Some(0.2),
            logit_bias: Some(logit_bias),
            logprobs: true,
            top_logprobs: Some(5),
            user: Some("user456".to_string()),
            seed: Some(12345),
            response_format: Some(ResponseFormat::JsonObject),
            tools: Some(vec![tool]),
            tool_choice: Some(ToolChoice::Auto),
            parallel_tool_calls: Some(false),
            functions: None,
            function_call: None,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        // Check all fields are preserved
        assert!(other.get("temperature").unwrap().as_f64().unwrap() - 0.8 < 0.0001);
        assert!(other.get("top_p").unwrap().as_f64().unwrap() - 0.95 < 0.0001);
        assert_eq!(other.get("n"), Some(&json!(2)));
        assert_eq!(other.get("stream"), Some(&json!(true)));
        assert!(other.contains_key("stream_options"));
        assert!(other.contains_key("stop"));
        assert_eq!(other.get("max_tokens"), Some(&json!(200)));
        assert_eq!(other.get("max_completion_tokens"), Some(&json!(150)));
        assert!(other.get("presence_penalty").unwrap().as_f64().unwrap() - 0.1 < 0.0001);
        assert!(other.get("frequency_penalty").unwrap().as_f64().unwrap() - 0.2 < 0.0001);
        assert!(other.contains_key("logit_bias"));
        assert_eq!(other.get("logprobs"), Some(&json!(true)));
        assert_eq!(other.get("top_logprobs"), Some(&json!(5)));
        assert_eq!(other.get("user"), Some(&json!("user456")));
        assert_eq!(other.get("seed"), Some(&json!(12345)));
        assert!(other.contains_key("response_format"));
        assert!(other.contains_key("tools"));
        assert!(other.contains_key("tool_choice"));
        assert_eq!(other.get("parallel_tool_calls"), Some(&json!(false)));
    }

    #[test]
    fn test_chat_to_pd_request_multimodal_content() {
        let messages = vec![ChatMessage::User {
            role: "user".to_string(),
            content: UserMessageContent::Parts(vec![
                ContentPart::Text {
                    text: "What's in this image?".to_string(),
                },
                ContentPart::ImageUrl {
                    image_url: ImageUrl {
                        url: "https://example.com/image.jpg".to_string(),
                        detail: Some("high".to_string()),
                    },
                },
            ]),
            name: None,
        }];

        let req = ChatCompletionRequest {
            messages,
            model: "gpt-4-vision".to_string(),
            temperature: None,
            top_p: None,
            n: None,
            stream: false,
            stream_options: None,
            stop: None,
            max_tokens: None,
            max_completion_tokens: None,
            presence_penalty: None,
            frequency_penalty: None,
            logit_bias: None,
            logprobs: false,
            top_logprobs: None,
            user: None,
            seed: None,
            response_format: None,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            functions: None,
            function_call: None,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        // Messages with multimodal content should be preserved
        assert!(other.contains_key("messages"));
        let messages = other.get("messages").unwrap().as_array().unwrap();
        assert_eq!(messages.len(), 1);

        // Verify the message structure is preserved
        let msg = &messages[0];
        assert_eq!(msg["role"], "user");
        assert!(msg["content"].is_array());
    }

    #[test]
    fn test_chat_to_pd_request_logprobs_boolean() {
        let messages = vec![ChatMessage::User {
            role: "user".to_string(),
            content: UserMessageContent::Text("Test".to_string()),
            name: None,
        }];

        let req = ChatCompletionRequest {
            messages,
            model: "test".to_string(),
            logprobs: true, // Boolean logprobs flag
            top_logprobs: Some(3),
            temperature: None,
            top_p: None,
            n: None,
            stream: false,
            stream_options: None,
            stop: None,
            max_tokens: None,
            max_completion_tokens: None,
            presence_penalty: None,
            frequency_penalty: None,
            logit_bias: None,
            user: None,
            seed: None,
            response_format: None,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            functions: None,
            function_call: None,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        assert_eq!(other.get("logprobs"), Some(&json!(true)));
        assert_eq!(other.get("top_logprobs"), Some(&json!(3)));
    }

    #[test]
    fn test_chat_to_pd_request_minimal_fields() {
        let messages = vec![ChatMessage::Assistant {
            role: "assistant".to_string(),
            content: Some("I can help with that.".to_string()),
            name: None,
            tool_calls: None,
            function_call: None,
        }];

        let req = ChatCompletionRequest {
            messages,
            model: "gpt-3.5-turbo".to_string(),
            temperature: None,
            top_p: None,
            n: None,
            stream: false,
            stream_options: None,
            stop: None,
            max_tokens: None,
            max_completion_tokens: None,
            presence_penalty: None,
            frequency_penalty: None,
            logit_bias: None,
            logprobs: false,
            top_logprobs: None,
            user: None,
            seed: None,
            response_format: None,
            tools: None,
            tool_choice: None,
            parallel_tool_calls: None,
            functions: None,
            function_call: None,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        // Should only have required fields
        assert!(other.contains_key("messages"));
        assert!(other.contains_key("model"));
        assert!(other.contains_key("stream"));

        // Optional fields should not be present
        assert!(!other.contains_key("temperature"));
        assert!(!other.contains_key("top_p"));
        assert!(!other.contains_key("max_tokens"));
        assert!(!other.contains_key("stop"));
    }

    #[test]
    fn test_routeable_request_to_json() {
        let req = GenerateRequest {
            text: Some("test".to_string()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let json = req.to_json().unwrap();
        assert_eq!(json["text"], "test");
        assert_eq!(json["stream"], false);
    }

    // ============= Macro Tests =============

    #[test]
    fn test_insert_if_some_macro() {
        let mut map = serde_json::Map::new();

        let some_value: Option<i32> = Some(42);
        let none_value: Option<i32> = None;

        insert_if_some!(map,
            some_value => "present",
            none_value => "absent"
        );

        assert_eq!(map.get("present"), Some(&json!(42)));
        assert!(!map.contains_key("absent"));
    }

    #[test]
    fn test_insert_value_macro() {
        let mut map = serde_json::Map::new();

        let value1 = "test";
        let value2 = 42;

        insert_value!(map,
            value1 => "string_field",
            value2 => "int_field"
        );

        assert_eq!(map.get("string_field"), Some(&json!("test")));
        assert_eq!(map.get("int_field"), Some(&json!(42)));
    }

    // ============= Edge Cases and Error Handling =============

    #[test]
    fn test_null_value_handling() {
        let params = GenerateParameters {
            max_new_tokens: None,
            temperature: None,
            ..Default::default()
        };

        let req = GenerateRequest {
            text: Some("test".to_string()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: Some(params),
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        // Should not have parameters field if all fields are None
        assert!(!other.contains_key("parameters"));
    }

    #[test]
    fn test_large_batch_conversion() {
        let large_batch: Vec<String> = (0..1000).map(|i| format!("item_{}", i)).collect();

        let req = GenerateRequest {
            text: None,
            prompt: Some(StringOrArray::Array(large_batch.clone())),
            input_ids: None,
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();

        if let Some(SingleOrBatch::Batch(batch)) = pd_req.text {
            assert_eq!(batch.len(), 1000);
            assert_eq!(batch[0], "item_0");
            assert_eq!(batch[999], "item_999");
        } else {
            panic!("Expected batch text");
        }
    }

    #[test]
    fn test_unicode_string_handling() {
        let unicode_text = "Hello 世界 🌍 नमस्ते мир".to_string();

        let req = GenerateRequest {
            text: Some(unicode_text.clone()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();

        if let Some(SingleOrBatch::Single(text)) = pd_req.text {
            assert_eq!(text, unicode_text);
        } else {
            panic!("Expected single text");
        }
    }

    #[test]
    fn test_deeply_nested_parameters() {
        let mut nested_params = serde_json::Map::new();
        nested_params.insert(
            "nested".to_string(),
            json!({
                "level1": {
                    "level2": {
                        "level3": "value"
                    }
                }
            }),
        );

        let params = GenerateParameters {
            max_new_tokens: Some(100),
            ..Default::default()
        };

        let req = GenerateRequest {
            text: Some("test".to_string()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: Some(params),
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();
        let other = pd_req.other.as_object().unwrap();

        // Parameters should be preserved even with nested structures
        assert!(other.contains_key("max_new_tokens"));
    }

    // ============= Bootstrap Field Tests =============

    #[test]
    fn test_bootstrap_fields_none() {
        let req = GenerateRequest {
            text: Some("test".to_string()),
            prompt: None,
            input_ids: None,
            stream: false,
            parameters: None,
            sampling_params: None,
            return_logprob: false,
        };

        let pd_req = req.to_pd_request();

        assert_eq!(pd_req.bootstrap_host, None);
        assert_eq!(pd_req.bootstrap_port, None);
        assert_eq!(pd_req.bootstrap_room, None);
    }
}