Unverified Commit 37373ef2 authored by Michael Feil's avatar Michael Feil Committed by GitHub
Browse files

sgl-router - issues on routing and project build. (#3870) (#3948)

parent 61261b39
This diff is collapsed.
......@@ -19,7 +19,6 @@ reqwest = { version = "0.12.8", features = ["stream", "blocking"] }
futures-util = "0.3"
serde_json = "1.0"
pyo3 = { version = "0.22.5", features = ["extension-module"] }
tokenizers = { version = "0.20.3", features = ["http"] }
dashmap = "6.1.0"
http = "1.1.0"
env_logger = "0.11.5"
......
......@@ -9,7 +9,7 @@ description = "SGLang router is a standalone module implemented in Rust to achie
authors = [{name = "Byron Hsu", email = "byronhsu1230@gmail.com"}]
requires-python = ">=3.8"
readme = "README.md"
license = { file = "LICENSE" }
license = { text = "Apache-2.0" }
classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Rust",
......
......@@ -4,6 +4,7 @@ use actix_web::{HttpRequest, HttpResponse};
use bytes::Bytes;
use futures_util::{StreamExt, TryStreamExt};
use log::{debug, error, info, warn};
use serde_json::Value;
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::atomic::AtomicUsize;
......@@ -403,25 +404,42 @@ impl Router {
}
fn get_text_from_request(&self, body: &Bytes, route: &str) -> String {
// convert body to json
let json = serde_json::from_slice::<serde_json::Value>(body).unwrap();
if route == "generate" {
// get the "text" field
let text = json.get("text").and_then(|t| t.as_str()).unwrap_or("");
return text.to_string();
} else if route == "v1/chat/completions" {
// get the messages field as raw text
if let Some(messages) = json.get("messages") {
// Convert messages back to a string, preserving all JSON formatting
return serde_json::to_string(messages).unwrap_or_default();
// Convert body to JSON
let json: Value = match serde_json::from_slice(body) {
Ok(j) => j,
Err(_) => {
warn!("Failed to parse JSON from request body.");
return String::new();
}
} else if route == "v1/completions" {
let prompt = json.get("prompt").and_then(|t| t.as_str()).unwrap_or("");
return prompt.to_string();
}
};
return "".to_string();
match route {
"/generate" => {
// For /generate, always use the "text" field.
match json.get("text").and_then(Value::as_str) {
Some(text) => text.to_string(),
None => {
warn!("No 'text' field found in request body for route /generate.");
String::new()
}
}
}
"/v1/chat/completions" | "/v1/completions" => {
// For these routes, try "messages", then "prompt", then "text".
if let Some(messages) = json.get("messages") {
serde_json::to_string(messages).unwrap_or_default()
} else if let Some(prompt) = json.get("prompt").and_then(Value::as_str) {
prompt.to_string()
} else {
warn!("Failed to find 'messages', 'prompt' in request body.");
String::new()
}
}
_ => {
warn!("Unknown route: {} - defaulting to fallback string", route);
String::new()
}
}
}
// TODO: return Result<String, String> instead of panicking
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment