smg-grpc-client 1.6.0

gRPC clients for vLLM, TensorRT-LLM, MLX, TokenSpeed, and SGLang backends
Documentation
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
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
use std::{collections::HashMap, future::Future, pin::Pin};

use openai_protocol::{
    chat::ChatCompletionRequest, completion::CompletionRequest, generate::GenerateRequest,
    messages::CreateMessageRequest, responses::ResponsesRequest,
    sampling_params::SamplingParams as GenerateSamplingParams,
};
use tonic::{transport::Channel, Request};
use tracing::{debug, warn};

use crate::{AbortOnDropClient, BoxedTraceInjector};

// Include the generated protobuf code
#[expect(clippy::allow_attributes)]
pub mod proto {
    #![allow(clippy::all, clippy::absolute_paths, unused_qualifications)]
    tonic::include_proto!("mlx.grpc.engine");
}

/// Streaming `generate()` response that auto-aborts on drop. Concrete
/// alias for the generic `crate::AbortOnDropStream`.
pub type AbortOnDropStream = crate::AbortOnDropStream<proto::GenerateResponse, MlxEngineClient>;

/// gRPC client for MLX engine
#[derive(Clone)]
pub struct MlxEngineClient {
    client: proto::mlx_engine_client::MlxEngineClient<Channel>,
    trace_injector: BoxedTraceInjector,
}

impl AbortOnDropClient for MlxEngineClient {
    fn abort_for_drop(
        self,
        request_id: String,
    ) -> Pin<Box<dyn Future<Output = Result<(), tonic::Status>> + Send>> {
        Box::pin(async move {
            self.abort_request(request_id, "Stream dropped".to_string())
                .await
        })
    }
}

impl MlxEngineClient {
    crate::impl_engine_client_basics!(proto::mlx_engine_client::MlxEngineClient<Channel>, "MLX");

    /// Submit a generation request (returns auto-aborting streaming response)
    pub async fn generate(
        &self,
        req: proto::GenerateRequest,
    ) -> Result<AbortOnDropStream, tonic::Status> {
        let request_id = req.request_id.clone();
        let mut client = self.client.clone();
        let mut request = Request::new(req);

        if let Err(e) = self.trace_injector.inject(request.metadata_mut()) {
            warn!("Failed to inject trace context: {}", e);
        }

        let response = client.generate(request).await?;

        Ok(AbortOnDropStream::new(
            response.into_inner(),
            request_id,
            self.clone(),
        ))
    }

    /// Abort a request
    pub async fn abort_request(
        &self,
        request_id: String,
        _reason: String,
    ) -> Result<(), tonic::Status> {
        debug!("Sending abort request for {}", request_id);
        let request = Request::new(proto::AbortRequest {
            request_ids: vec![request_id.clone()],
        });

        let mut client = self.client.clone();
        let _response = client.abort(request).await?;
        debug!("Abort response received for {}", request_id);
        Ok(())
    }

    crate::impl_get_tokenizer!();

    // ── Request builders ────────────────────────────────────────────────

    // ── Unsupported feature validation ────────────────────────────────
    //
    // TODO(mlx): Gaps preventing feature parity with vLLM/SGLang:
    //
    // mlx-lm engine limitations:
    //   - Constrained decoding (json_schema, regex, grammar, structural_tag)
    //     — needs outlines/xgrammar integration in mlx-lm
    //   - Parallel samples (n > 1) — mlx-lm server doesn't expose this
    //   - response_format — same as constrained decoding
    //
    // Servicer limitations (fixable without mlx-lm changes):
    //   - TODO(mlx): String stop sequences — mlx-lm supports this via
    //     tokenizer.encode() → SequenceStateMachine. Fix by converting stop
    //     strings to token IDs in the preparation stage (which already has the
    //     Rust tokenizer) and passing them as stop_token_ids in the proto.
    //
    // Track upstream: https://github.com/ml-explore/mlx-lm

    fn reject_constraint(constraint: Option<&(String, String)>) -> Result<(), String> {
        if let Some((kind, _)) = constraint {
            return Err(format!(
                "MLX backend does not support structured output constraint: {kind}"
            ));
        }
        Ok(())
    }

    fn reject_n(n: Option<u32>) -> Result<(), String> {
        if n.is_some_and(|n| n > 1) {
            return Err("MLX backend does not support n > 1 (parallel samples)".to_string());
        }
        Ok(())
    }

    fn reject_stop_strings(has_stop_strings: bool) -> Result<(), String> {
        if has_stop_strings {
            return Err("MLX backend does not support string stop sequences".to_string());
        }
        Ok(())
    }

    fn reject_response_format(has_response_format: bool) -> Result<(), String> {
        if has_response_format {
            return Err(
                "MLX backend does not support response_format (structured outputs)".to_string(),
            );
        }
        Ok(())
    }

    fn reject_if_any_constraint(
        json_schema: Option<&String>,
        regex: Option<&String>,
        ebnf: Option<&String>,
    ) -> Result<(), String> {
        if json_schema.is_some() || regex.is_some() || ebnf.is_some() {
            return Err("MLX backend does not support structured output constraints".to_string());
        }
        Ok(())
    }

    // ── Public request builders ────────────────────────────────────────

    /// Build a GenerateRequest from OpenAI ChatCompletionRequest
    #[expect(
        clippy::unused_self,
        reason = "method receiver kept for consistent public API across gRPC backends"
    )]
    pub fn build_generate_request_from_chat(
        &self,
        request_id: String,
        body: &ChatCompletionRequest,
        processed_text: String,
        token_ids: Vec<u32>,
        constraint: Option<(String, String)>,
    ) -> Result<proto::GenerateRequest, String> {
        Self::reject_constraint(constraint.as_ref())?;
        Self::reject_n(body.n)?;
        Self::reject_stop_strings(body.stop.as_ref().is_some_and(|s| !s.is_empty()))?;
        Self::reject_response_format(body.response_format.is_some())?;

        let sampling_params = Self::build_sampling_params_from_chat(body);
        Ok(Self::make_generate_request(
            request_id,
            processed_text,
            token_ids,
            sampling_params,
            body.stream,
        ))
    }

    /// Build a GenerateRequest from CompletionRequest (`/v1/completions`)
    #[expect(
        clippy::unused_self,
        reason = "method receiver kept for consistent public API"
    )]
    pub fn build_generate_request_from_completion(
        &self,
        request_id: String,
        body: &CompletionRequest,
        original_text: String,
        token_ids: Vec<u32>,
    ) -> Result<proto::GenerateRequest, String> {
        Self::reject_n(body.n)?;
        Self::reject_stop_strings(body.stop.as_ref().is_some_and(|s| !s.is_empty()))?;
        Self::reject_if_any_constraint(
            body.json_schema.as_ref(),
            body.regex.as_ref(),
            body.ebnf.as_ref(),
        )?;

        let sampling_params = Self::build_sampling_params_from_completion(body);
        Ok(Self::make_generate_request(
            request_id,
            original_text,
            token_ids,
            sampling_params,
            body.stream,
        ))
    }

    /// Build a GenerateRequest from CreateMessageRequest (Anthropic Messages API)
    #[expect(
        clippy::unused_self,
        reason = "method receiver kept for consistent public API across gRPC backends"
    )]
    pub fn build_generate_request_from_messages(
        &self,
        request_id: String,
        body: &CreateMessageRequest,
        processed_text: String,
        token_ids: Vec<u32>,
        constraint: Option<(String, String)>,
    ) -> Result<proto::GenerateRequest, String> {
        Self::reject_constraint(constraint.as_ref())?;
        Self::reject_stop_strings(body.stop_sequences.as_ref().is_some_and(|s| !s.is_empty()))?;

        let sampling_params = Self::build_sampling_params_from_messages(body);
        Ok(Self::make_generate_request(
            request_id,
            processed_text,
            token_ids,
            sampling_params,
            body.stream.unwrap_or(false),
        ))
    }

    /// Build a basic GenerateRequest from the native GenerateRequest
    #[expect(
        clippy::unused_self,
        reason = "method receiver kept for consistent public API across gRPC backends"
    )]
    pub fn build_plain_generate_request(
        &self,
        request_id: String,
        body: &GenerateRequest,
        original_text: Option<String>,
        token_ids: Vec<u32>,
    ) -> Result<proto::GenerateRequest, String> {
        if let Some(ref sp) = body.sampling_params {
            Self::reject_n(sp.n)?;
            Self::reject_stop_strings(sp.stop.as_ref().is_some_and(|s| !s.is_empty()))?;
            Self::reject_if_any_constraint(
                sp.json_schema.as_ref(),
                sp.regex.as_ref(),
                sp.ebnf.as_ref(),
            )?;
        }

        let sampling_params = Self::build_sampling_params_from_plain(body.sampling_params.as_ref());
        Ok(Self::make_generate_request(
            request_id,
            original_text.unwrap_or_default(),
            token_ids,
            sampling_params,
            body.stream,
        ))
    }

    /// Build a GenerateRequest from ResponsesRequest (OpenAI Responses API)
    #[expect(
        clippy::unused_self,
        reason = "method receiver kept for consistent public API across gRPC backends"
    )]
    pub fn build_generate_request_from_responses(
        &self,
        request_id: String,
        body: &ResponsesRequest,
        processed_text: String,
        token_ids: Vec<u32>,
        constraint: Option<(String, String)>,
    ) -> Result<proto::GenerateRequest, String> {
        Self::reject_constraint(constraint.as_ref())?;
        Self::reject_stop_strings(body.stop.as_ref().is_some_and(|s| !s.is_empty()))?;

        let sampling_params = Self::build_sampling_params_from_responses(body);
        Ok(Self::make_generate_request(
            request_id,
            processed_text,
            token_ids,
            sampling_params,
            body.stream.unwrap_or(false),
        ))
    }

    /// Shared helper to construct the proto GenerateRequest.
    fn make_generate_request(
        request_id: String,
        text: String,
        token_ids: Vec<u32>,
        sampling_params: proto::SamplingParams,
        stream: bool,
    ) -> proto::GenerateRequest {
        proto::GenerateRequest {
            request_id,
            input: Some(proto::generate_request::Input::Tokenized(
                proto::TokenizedInput {
                    original_text: text,
                    input_ids: token_ids,
                },
            )),
            sampling_params: Some(sampling_params),
            stream,
        }
    }

    // ── Private sampling param builders ─────────────────────────────────

    #[expect(deprecated, reason = "seed is legacy but still forwarded to backends")]
    fn build_sampling_params_from_chat(request: &ChatCompletionRequest) -> proto::SamplingParams {
        let logprobs = if request.logprobs {
            Some(request.top_logprobs.unwrap_or(1).min(20) as i32)
        } else {
            None
        };

        proto::SamplingParams {
            temperature: request.temperature,
            top_p: request.top_p.unwrap_or(1.0),
            top_k: request.top_k.map(|v| v.max(0) as u32).unwrap_or(0),
            min_p: request.min_p.unwrap_or(0.0),
            frequency_penalty: request.frequency_penalty.unwrap_or(0.0),
            presence_penalty: request.presence_penalty.unwrap_or(0.0),
            repetition_penalty: request.repetition_penalty.unwrap_or(1.0),
            max_tokens: request.max_completion_tokens,
            stop_token_ids: request.stop_token_ids.clone().unwrap_or_default(),
            ignore_eos: request.ignore_eos,
            logprobs,
            logit_bias: convert_logit_bias(request.logit_bias.as_ref()),
            seed: request.seed.and_then(|s| i32::try_from(s).ok()),
        }
    }

    fn build_sampling_params_from_completion(request: &CompletionRequest) -> proto::SamplingParams {
        let logprobs = request.logprobs.map(|v| v.min(5) as i32);

        proto::SamplingParams {
            temperature: request.temperature,
            top_p: request.top_p.unwrap_or(1.0),
            top_k: request.top_k.map(|v| v.max(0) as u32).unwrap_or(0),
            min_p: request.min_p.unwrap_or(0.0),
            frequency_penalty: request.frequency_penalty.unwrap_or(0.0),
            presence_penalty: request.presence_penalty.unwrap_or(0.0),
            repetition_penalty: request.repetition_penalty.unwrap_or(1.0),
            max_tokens: request.max_tokens,
            stop_token_ids: request.stop_token_ids.clone().unwrap_or_default(),
            ignore_eos: request.ignore_eos,
            logprobs,
            logit_bias: convert_logit_bias(request.logit_bias.as_ref()),
            seed: request.seed.and_then(|s| i32::try_from(s).ok()),
        }
    }

    fn build_sampling_params_from_messages(
        request: &CreateMessageRequest,
    ) -> proto::SamplingParams {
        proto::SamplingParams {
            temperature: Some(request.temperature.unwrap_or(1.0) as f32),
            top_p: request.top_p.unwrap_or(1.0) as f32,
            top_k: request.top_k.unwrap_or(0),
            max_tokens: Some(request.max_tokens),
            repetition_penalty: 1.0, // 1.0 = no penalty (0.0 would penalize everything)
            ..Default::default()
        }
    }

    fn build_sampling_params_from_plain(
        params: Option<&GenerateSamplingParams>,
    ) -> proto::SamplingParams {
        let mut sampling = proto::SamplingParams {
            temperature: Some(1.0),
            top_p: 1.0,
            repetition_penalty: 1.0, // 1.0 = no penalty
            ..Default::default()
        };

        let Some(p) = params else {
            return sampling;
        };

        if let Some(val) = p.temperature {
            sampling.temperature = Some(val);
        }
        if let Some(val) = p.top_p {
            sampling.top_p = val;
        }
        if let Some(val) = p.top_k {
            sampling.top_k = val.max(0) as u32;
        }
        if let Some(val) = p.frequency_penalty {
            sampling.frequency_penalty = val;
        }
        if let Some(val) = p.presence_penalty {
            sampling.presence_penalty = val;
        }
        if let Some(val) = p.repetition_penalty {
            sampling.repetition_penalty = val;
        }
        if let Some(val) = p.min_p {
            sampling.min_p = val;
        }
        if let Some(val) = p.ignore_eos {
            sampling.ignore_eos = val;
        }
        if let Some(stop_token_ids) = &p.stop_token_ids {
            sampling.stop_token_ids.clone_from(stop_token_ids);
        }
        if let Some(max_new_tokens) = p.max_new_tokens {
            sampling.max_tokens = Some(max_new_tokens);
        }
        if let Some(seed) = p.sampling_seed {
            sampling.seed = i32::try_from(seed).ok();
        }

        sampling
    }

    fn build_sampling_params_from_responses(request: &ResponsesRequest) -> proto::SamplingParams {
        proto::SamplingParams {
            temperature: request.temperature,
            top_p: request.top_p.unwrap_or(1.0),
            top_k: request.top_k.max(0) as u32,
            min_p: request.min_p,
            repetition_penalty: request.repetition_penalty,
            frequency_penalty: request.frequency_penalty.unwrap_or(0.0),
            presence_penalty: request.presence_penalty.unwrap_or(0.0),
            logit_bias: Default::default(),
            max_tokens: request.max_output_tokens,
            stop_token_ids: vec![],
            ignore_eos: false,
            logprobs: request.top_logprobs.map(|v| v as i32),
            seed: None,
        }
    }
}

/// Convert OpenAI-style logit_bias (String keys) to proto logit_bias (i32 keys).
fn convert_logit_bias(bias: Option<&HashMap<String, f32>>) -> HashMap<i32, f32> {
    match bias {
        Some(map) => map
            .iter()
            .filter_map(|(k, v)| k.parse::<i32>().ok().map(|id| (id, *v)))
            .collect(),
        None => HashMap::new(),
    }
}