From 09af0a7b5a8d7644ff41653e17310028da09731d Mon Sep 17 00:00:00 2001 From: "rongfu.leng" Date: Mon, 27 Oct 2025 12:14:33 +0800 Subject: [PATCH] =?UTF-8?q?[sgl-route]=20Optimize=20the=20use=20of=20const?= =?UTF-8?q?ant=20slices=20and=20retain=20to=20simplif=E2=80=A6=20(#12159)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: rongfu.leng --- sgl-router/src/routers/openai/router.rs | 77 ++++++++++--------------- 1 file changed, 30 insertions(+), 47 deletions(-) diff --git a/sgl-router/src/routers/openai/router.rs b/sgl-router/src/routers/openai/router.rs index c0de05af7..51126e4be 100644 --- a/sgl-router/src/routers/openai/router.rs +++ b/sgl-router/src/routers/openai/router.rs @@ -2,6 +2,7 @@ use std::{ any::Any, + collections::HashSet, sync::{atomic::AtomicBool, Arc}, time::{Duration, Instant}, }; @@ -15,6 +16,7 @@ use axum::{ }; use dashmap::DashMap; use futures_util::StreamExt; +use once_cell::sync::Lazy; use serde_json::{json, to_value, Value}; use tokio::sync::mpsc; use tokio_stream::wrappers::UnboundedReceiverStream; @@ -61,6 +63,32 @@ use crate::{ // OpenAIRouter Struct // ============================================================================ +/// Fields specific to SGLang that should be stripped when forwarding to OpenAI-compatible endpoints +static SGLANG_FIELDS: Lazy> = Lazy::new(|| { + HashSet::from([ + "request_id", + "priority", + "top_k", + "min_p", + "min_tokens", + "regex", + "ebnf", + "stop_token_ids", + "no_stop_trim", + "ignore_eos", + "continue_final_message", + "skip_special_tokens", + "lora_path", + "session_params", + "separate_reasoning", + "stream_reasoning", + "chat_template_kwargs", + "return_hidden_states", + "repetition_penalty", + "sampling_seed", + ]) +}); + /// Cached endpoint information #[derive(Clone, Debug)] struct CachedEndpoint { @@ -547,29 +575,7 @@ impl crate::routers::RouterTrait for OpenAIRouter { }; if let Some(obj) = payload.as_object_mut() { // Always remove SGLang-specific fields (unsupported by OpenAI) - for key in [ - "top_k", - "min_p", - "min_tokens", - "regex", - "ebnf", - "stop_token_ids", - "no_stop_trim", - "ignore_eos", - "continue_final_message", - "skip_special_tokens", - "lora_path", - "session_params", - "separate_reasoning", - "stream_reasoning", - "chat_template_kwargs", - "return_hidden_states", - "repetition_penalty", - "sampling_seed", - ] { - obj.remove(key); - } - + obj.retain(|k, _| !SGLANG_FIELDS.contains(&k.as_str())); // Remove logprobs if false (Gemini don't accept it) if obj.get("logprobs").and_then(|v| v.as_bool()) == Some(false) { obj.remove("logprobs"); @@ -899,30 +905,7 @@ impl crate::routers::RouterTrait for OpenAIRouter { // Remove SGLang-specific fields only if let Some(obj) = payload.as_object_mut() { // Remove SGLang-specific fields (not part of OpenAI API) - for key in [ - "request_id", - "priority", - "top_k", - "min_p", - "min_tokens", - "regex", - "ebnf", - "stop_token_ids", - "no_stop_trim", - "ignore_eos", - "continue_final_message", - "skip_special_tokens", - "lora_path", - "session_params", - "separate_reasoning", - "stream_reasoning", - "chat_template_kwargs", - "return_hidden_states", - "repetition_penalty", - "sampling_seed", - ] { - obj.remove(key); - } + obj.retain(|k, _| !SGLANG_FIELDS.contains(&k.as_str())); // XAI (Grok models) requires special handling of input items // Check if model is a Grok model let is_grok_model = obj