|
| 1 | +//! vLLM-SR route-decision probe client. |
| 2 | +
|
| 3 | +use reqwest::header::AUTHORIZATION; |
| 4 | +use serde_json::json; |
| 5 | + |
| 6 | +use super::constants::VLLM_SR_AUTO_MODEL; |
| 7 | +use super::route_helpers::normalize_base_url; |
| 8 | +use super::types::{WendaoModelDecision, WendaoRouteIntent}; |
| 9 | + |
| 10 | +/// OpenAI-compatible vLLM-SR route-decision probe client. |
| 11 | +#[derive(Clone)] |
| 12 | +pub struct VllmSrRouteDecisionClient { |
| 13 | + base_url: String, |
| 14 | + bearer_token: Option<String>, |
| 15 | + http: reqwest::Client, |
| 16 | +} |
| 17 | + |
| 18 | +impl VllmSrRouteDecisionClient { |
| 19 | + /// Build a route decision client. |
| 20 | + #[must_use] |
| 21 | + pub fn new(base_url: impl Into<String>) -> Self { |
| 22 | + Self::with_http_client(base_url, reqwest::Client::new()) |
| 23 | + } |
| 24 | + |
| 25 | + /// Build a route decision client with an injected HTTP client. |
| 26 | + #[must_use] |
| 27 | + pub fn with_http_client(base_url: impl Into<String>, http: reqwest::Client) -> Self { |
| 28 | + let base_url = base_url.into(); |
| 29 | + Self { |
| 30 | + base_url: normalize_base_url(base_url.as_str()), |
| 31 | + bearer_token: None, |
| 32 | + http, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// Attach an optional bearer token for vLLM-SR deployments that require it. |
| 37 | + #[must_use] |
| 38 | + pub fn with_bearer_token(mut self, bearer_token: impl Into<String>) -> Self { |
| 39 | + let bearer_token = bearer_token.into(); |
| 40 | + self.bearer_token = (!bearer_token.trim().is_empty()).then_some(bearer_token); |
| 41 | + self |
| 42 | + } |
| 43 | + |
| 44 | + /// Return the normalized vLLM-SR base URL. |
| 45 | + #[must_use] |
| 46 | + pub fn base_url(&self) -> &str { |
| 47 | + self.base_url.as_str() |
| 48 | + } |
| 49 | + |
| 50 | + /// Obtain a route decision through vLLM-SR's OpenAI-compatible data plane. |
| 51 | + /// |
| 52 | + /// # Errors |
| 53 | + /// |
| 54 | + /// Returns an error when the HTTP request fails, vLLM-SR returns a |
| 55 | + /// non-success status, or the response does not include a selected model. |
| 56 | + pub async fn decide( |
| 57 | + &self, |
| 58 | + intent: &WendaoRouteIntent, |
| 59 | + selected_provider: &str, |
| 60 | + selected_backend_profile: &str, |
| 61 | + ) -> Result<WendaoModelDecision, String> { |
| 62 | + let endpoint = format!("{}/v1/chat/completions", self.base_url); |
| 63 | + let prompt = serde_json::to_string(intent) |
| 64 | + .map_err(|error| format!("serialize Wendao route intent: {error}"))?; |
| 65 | + let payload = json!({ |
| 66 | + "model": VLLM_SR_AUTO_MODEL, |
| 67 | + "messages": [ |
| 68 | + { |
| 69 | + "role": "system", |
| 70 | + "content": "Route this Wendao task. The response body is ignored; Wendao reads vLLM-SR routing headers." |
| 71 | + }, |
| 72 | + { |
| 73 | + "role": "user", |
| 74 | + "content": prompt |
| 75 | + } |
| 76 | + ], |
| 77 | + "temperature": 0, |
| 78 | + "max_tokens": 1, |
| 79 | + "stream": false |
| 80 | + }); |
| 81 | + |
| 82 | + let mut request = self.http.post(endpoint.as_str()).json(&payload); |
| 83 | + if let Some(token) = self.bearer_token.as_deref() { |
| 84 | + request = request.header(AUTHORIZATION, format!("Bearer {token}")); |
| 85 | + } |
| 86 | + let response = request |
| 87 | + .send() |
| 88 | + .await |
| 89 | + .map_err(|error| format!("call vLLM-SR route probe `{endpoint}`: {error}"))?; |
| 90 | + let status = response.status(); |
| 91 | + let headers = response.headers().clone(); |
| 92 | + let body = response |
| 93 | + .text() |
| 94 | + .await |
| 95 | + .map_err(|error| format!("read vLLM-SR route probe response body: {error}"))?; |
| 96 | + if !status.is_success() { |
| 97 | + return Err(format!( |
| 98 | + "vLLM-SR route probe `{endpoint}` returned {status}: {body}" |
| 99 | + )); |
| 100 | + } |
| 101 | + WendaoModelDecision::from_vllm_sr_response_parts( |
| 102 | + &headers, |
| 103 | + &body, |
| 104 | + selected_provider, |
| 105 | + selected_backend_profile, |
| 106 | + ) |
| 107 | + } |
| 108 | +} |
0 commit comments