mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-20 00:11:50 +00:00
refactor(rust): rename providers to llms and move non-OCR provider code out of core
litellm-providers becomes litellm-llms, mirroring litellm/llms. The Anthropic batches, count_tokens, Messages stream iterator and chat stream handler, the OpenAI Responses websocket config and its base trait (with URL and model helpers), and the StreamTransformer base iterator now live at their Python paths in that crate. Anthropic stream decode errors move with the iterator, and core drops its duplicate OAuth prefix constant and the unused framing dependency. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
904c679595
commit
49c50739d7
61 changed files with 257 additions and 253 deletions
16
litellm-rust/Cargo.lock
generated
16
litellm-rust/Cargo.lock
generated
|
|
@ -2025,8 +2025,6 @@ dependencies = [
|
|||
name = "litellm-core"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aws-smithy-eventstream",
|
||||
"aws-smithy-types",
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
"data-url",
|
||||
|
|
@ -2037,8 +2035,7 @@ dependencies = [
|
|||
"litellm-auth-gcp",
|
||||
"litellm-callbacks",
|
||||
"litellm-core-utils",
|
||||
"litellm-framing",
|
||||
"litellm-providers",
|
||||
"litellm-llms",
|
||||
"litellm-types",
|
||||
"mime_guess",
|
||||
"moka",
|
||||
|
|
@ -2107,17 +2104,26 @@ dependencies = [
|
|||
]
|
||||
|
||||
[[package]]
|
||||
name = "litellm-providers"
|
||||
name = "litellm-llms"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aws-smithy-eventstream",
|
||||
"aws-smithy-types",
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
"futures-util",
|
||||
"litellm-auth",
|
||||
"litellm-auth-aws",
|
||||
"litellm-core-utils",
|
||||
"litellm-framing",
|
||||
"litellm-types",
|
||||
"rstest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.19",
|
||||
"time",
|
||||
"tokio",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ litellm-auth = { path = "crates/auth" }
|
|||
litellm-auth-aws = { path = "crates/auth-aws" }
|
||||
litellm-auth-azure = { path = "crates/auth-azure" }
|
||||
litellm-auth-gcp = { path = "crates/auth-gcp" }
|
||||
litellm-providers = { path = "crates/providers" }
|
||||
litellm-llms = { path = "crates/llms" }
|
||||
litellm-types = { path = "crates/types" }
|
||||
litellm-core-utils = { path = "crates/core-utils" }
|
||||
litellm-cache = { path = "crates/cache" }
|
||||
|
|
|
|||
|
|
@ -18,8 +18,7 @@ litellm-auth.workspace = true
|
|||
litellm-auth-aws.workspace = true
|
||||
litellm-auth-azure.workspace = true
|
||||
litellm-auth-gcp.workspace = true
|
||||
litellm-providers.workspace = true
|
||||
litellm-framing.workspace = true
|
||||
litellm-llms.workspace = true
|
||||
moka.workspace = true
|
||||
mime_guess = "2.0.5"
|
||||
rand.workspace = true
|
||||
|
|
@ -41,7 +40,5 @@ url.workspace = true
|
|||
veil.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
aws-smithy-eventstream = "=0.61.1"
|
||||
aws-smithy-types = "1.6.1"
|
||||
rstest.workspace = true
|
||||
rstest_reuse.workspace = true
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use litellm_providers::base_llm::chat::transformation::Error as LlmError;
|
||||
use litellm_llms::base_llm::chat::transformation::Error as LlmError;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ async fn signed_headers(
|
|||
use std::{collections::BTreeMap, time::SystemTime};
|
||||
|
||||
use litellm_auth_aws::{aws_auth_config, resolve_credentials, sign_bedrock_post};
|
||||
use litellm_providers::base_llm::audio_transcription::transformation::AudioTranscriptionAuth;
|
||||
use litellm_llms::base_llm::audio_transcription::transformation::AudioTranscriptionAuth;
|
||||
|
||||
let AudioTranscriptionAuth::AwsSigV4 { region, .. } = &request.auth else {
|
||||
return Ok(request.upstream_headers.clone());
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use litellm_core_utils::get_llm_provider_logic::{CustomLlmProvider, get_custom_llm_provider};
|
||||
use litellm_providers::{
|
||||
use litellm_llms::{
|
||||
base_llm::audio_transcription::transformation::{
|
||||
AudioTranscriptionAuth, BaseAudioTranscriptionConfig,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use litellm_providers::base_llm::audio_transcription::transformation::{
|
||||
use litellm_llms::base_llm::audio_transcription::transformation::{
|
||||
AudioTranscriptionAuth, BaseAudioTranscriptionConfig,
|
||||
};
|
||||
use serde_json::{Map, Value};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use litellm_providers::{
|
||||
use litellm_llms::{
|
||||
anthropic::chat::transformation::ANTHROPIC_CHAT_COMPLETIONS_CONFIG,
|
||||
base_llm::chat::transformation::BaseConfig,
|
||||
};
|
||||
|
|
@ -13,7 +13,7 @@ pub(super) fn chat_completions_provider_config(provider: &str) -> Option<&'stati
|
|||
match provider {
|
||||
"anthropic" => Some(&ANTHROPIC_CHAT_COMPLETIONS_CONFIG),
|
||||
"bedrock" => Some(
|
||||
&litellm_providers::bedrock::chat::converse_transformation::BEDROCK_CHAT_COMPLETIONS_CONFIG,
|
||||
&litellm_llms::bedrock::chat::converse_transformation::BEDROCK_CHAT_COMPLETIONS_CONFIG,
|
||||
),
|
||||
_ => None,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use litellm_providers::base_llm::chat::transformation::Error as LlmError;
|
||||
use litellm_llms::base_llm::chat::transformation::Error as LlmError;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
use litellm_providers::base_llm::chat::transformation::{
|
||||
ChatCompletionsAuth, ProviderChatResponseData,
|
||||
};
|
||||
use litellm_llms::base_llm::chat::transformation::{ChatCompletionsAuth, ProviderChatResponseData};
|
||||
use litellm_types::utils::ChatCompletionsResponse;
|
||||
use serde_json::Value;
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ mod client;
|
|||
mod common_utils;
|
||||
pub(crate) mod handler;
|
||||
mod prepare;
|
||||
pub mod streaming;
|
||||
use handler::execute_chat_completions_provider_call;
|
||||
use litellm_types::utils::ChatCompletionsResponse;
|
||||
use prepare::{parse_messages, resolve_provider_config, resolve_request};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use litellm_core_utils::get_llm_provider_logic::{CustomLlmProvider, get_custom_llm_provider};
|
||||
use litellm_providers::base_llm::chat::transformation::{BaseConfig, ChatCompletionsAuth};
|
||||
use litellm_llms::base_llm::chat::transformation::{BaseConfig, ChatCompletionsAuth};
|
||||
use litellm_types::llms::openai::ChatMessage;
|
||||
use serde_json::Value;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use litellm_providers::base_llm::chat::transformation::ChatCompletionsAuth;
|
||||
use litellm_llms::base_llm::chat::transformation::ChatCompletionsAuth;
|
||||
use serde_json::{Map, Value, json};
|
||||
|
||||
use super::{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use litellm_providers::base_llm::chat::transformation::{BaseConfig, ChatCompletionsAuth};
|
||||
use litellm_llms::base_llm::chat::transformation::{BaseConfig, ChatCompletionsAuth};
|
||||
use litellm_types::llms::openai::ChatMessage;
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
pub const OPENAI_DEFAULT_API_BASE: &str = "https://api.openai.com";
|
||||
pub const OPENAI_RESPONSES_DEFAULT_API_BASE: &str = "https://api.openai.com/v1";
|
||||
pub const OPENAI_RESPONSES_PATH: &str = "/responses";
|
||||
|
||||
/// Full-request timeout ceiling for Anthropic Messages provider calls, in
|
||||
/// seconds. Mirrors the Python Anthropic Messages default. The per-request
|
||||
|
|
@ -18,11 +16,6 @@ pub(crate) const UPSTREAM_ERROR_BODY_MAX_CHARS: usize = 256;
|
|||
/// does not carry an explicit provider prefix.
|
||||
pub const ANTHROPIC_MESSAGES_PROVIDER: &str = "anthropic";
|
||||
|
||||
/// Prefix identifying an Anthropic OAuth token. Mirrors Python's
|
||||
/// `ANTHROPIC_OAUTH_TOKEN_PREFIX`, which is what makes `validate_environment`
|
||||
/// authenticate with `authorization` and drop `x-api-key` entirely.
|
||||
pub(crate) const ANTHROPIC_OAUTH_TOKEN_PREFIX: &str = "sk-ant-oat";
|
||||
|
||||
/// Full-request timeout ceiling for chat completions provider calls, in
|
||||
/// seconds. Mirrors the Python chat completions default.
|
||||
pub(crate) const CHAT_COMPLETIONS_TIMEOUT_SECS: u64 = 600;
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
pub mod streaming;
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
pub mod batches;
|
||||
pub mod count_tokens;
|
||||
pub mod streaming;
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
pub mod chat;
|
||||
pub mod experimental_pass_through;
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
pub mod anthropic;
|
||||
pub mod azure_ai;
|
||||
pub mod base_llm;
|
||||
pub(crate) mod cohere;
|
||||
pub(crate) mod mistral;
|
||||
pub mod openai;
|
||||
pub(crate) mod reducto;
|
||||
pub(crate) mod vertex_ai;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use litellm_providers::{
|
||||
use litellm_llms::{
|
||||
anthropic::experimental_pass_through::messages::transformation::ANTHROPIC_MESSAGES_CONFIG,
|
||||
azure_ai::anthropic::messages_transformation::AZURE_ANTHROPIC_MESSAGES_CONFIG,
|
||||
base_llm::anthropic_messages::transformation::BaseAnthropicMessagesConfig,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use litellm_providers::base_llm::chat::transformation::Error as LlmError;
|
||||
use litellm_llms::base_llm::chat::transformation::Error as LlmError;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
|
|
@ -18,16 +18,6 @@ pub enum Error {
|
|||
Transport(#[from] crate::transport::Error),
|
||||
#[error(transparent)]
|
||||
Headers(#[from] crate::http_utils::HeaderError),
|
||||
#[error("stream framing failed: {0}")]
|
||||
StreamFraming(String),
|
||||
#[error("Anthropic SSE frame has no data")]
|
||||
MissingStreamData,
|
||||
#[error("Anthropic stream event is invalid: {0}")]
|
||||
InvalidStreamEvent(String),
|
||||
#[error("Bedrock event payload is invalid: {0}")]
|
||||
InvalidBedrockPayload(String),
|
||||
#[error("Bedrock event payload has invalid base64: {0}")]
|
||||
InvalidBedrockBase64(String),
|
||||
}
|
||||
|
||||
impl From<LlmError> for Error {
|
||||
|
|
@ -57,14 +47,6 @@ impl Error {
|
|||
}
|
||||
|
||||
pub fn is_response(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::InvalidResponse(_)
|
||||
| Self::StreamFraming(_)
|
||||
| Self::MissingStreamData
|
||||
| Self::InvalidStreamEvent(_)
|
||||
| Self::InvalidBedrockPayload(_)
|
||||
| Self::InvalidBedrockBase64(_)
|
||||
)
|
||||
matches!(self, Self::InvalidResponse(_))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use litellm_core_utils::get_llm_provider_logic::{CustomLlmProvider, get_custom_llm_provider};
|
||||
use litellm_providers::base_llm::anthropic_messages::transformation::{
|
||||
use litellm_llms::base_llm::anthropic_messages::transformation::{
|
||||
BaseAnthropicMessagesConfig, MessagesAuthStrategy,
|
||||
};
|
||||
use serde_json::{Map, Value};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use litellm_providers::base_llm::anthropic_messages::transformation::BaseAnthropicMessagesConfig;
|
||||
use litellm_llms::base_llm::anthropic_messages::transformation::BaseAnthropicMessagesConfig;
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
pub struct MessagesRequest<'a> {
|
||||
|
|
|
|||
|
|
@ -6,9 +6,7 @@ use std::{
|
|||
};
|
||||
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use litellm_types::responses::streaming_websocket::{
|
||||
ResponsesWsEvent, ResponsesWsEventType, ResponsesWsTransformResult,
|
||||
};
|
||||
use litellm_types::responses::streaming_websocket::ResponsesWsEventType;
|
||||
use rustls::{ClientConfig, RootCertStore};
|
||||
use tokio::{net::TcpStream, sync::Mutex};
|
||||
use tokio_tungstenite::{
|
||||
|
|
@ -23,119 +21,6 @@ use tokio_tungstenite::{
|
|||
};
|
||||
|
||||
use super::Error;
|
||||
use crate::constants::{OPENAI_RESPONSES_DEFAULT_API_BASE, OPENAI_RESPONSES_PATH};
|
||||
|
||||
pub trait ResponsesWebSocketProviderConfig: Sync {
|
||||
fn supports_native_websocket(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn model_in_websocket_url(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn complete_websocket_url(&self, api_base: Option<&str>, model: &str) -> String {
|
||||
complete_websocket_url(api_base, model, self.model_in_websocket_url())
|
||||
}
|
||||
|
||||
fn transform_ws_request(
|
||||
&self,
|
||||
event: &ResponsesWsEvent,
|
||||
model: &str,
|
||||
) -> Result<ResponsesWsTransformResult, Error>;
|
||||
|
||||
fn transform_ws_response(
|
||||
&self,
|
||||
event: &ResponsesWsEvent,
|
||||
model: &str,
|
||||
) -> Result<ResponsesWsTransformResult, Error>;
|
||||
}
|
||||
|
||||
pub fn complete_websocket_url(
|
||||
api_base: Option<&str>,
|
||||
model: &str,
|
||||
model_in_websocket_url: bool,
|
||||
) -> String {
|
||||
let base = api_base
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or(OPENAI_RESPONSES_DEFAULT_API_BASE);
|
||||
let (base_without_query, query) = base
|
||||
.split_once('?')
|
||||
.map_or((base, None), |(value, query)| (value, Some(query)));
|
||||
let response_url = format!(
|
||||
"{}{}",
|
||||
base_without_query.trim_end_matches('/'),
|
||||
OPENAI_RESPONSES_PATH
|
||||
);
|
||||
let scheme_flipped = if let Some(rest) = response_url.strip_prefix("https://") {
|
||||
format!("wss://{rest}")
|
||||
} else if let Some(rest) = response_url.strip_prefix("http://") {
|
||||
format!("ws://{rest}")
|
||||
} else {
|
||||
response_url
|
||||
};
|
||||
let url = query.map_or(scheme_flipped.clone(), |value| {
|
||||
format!("{scheme_flipped}?{value}")
|
||||
});
|
||||
if !model_in_websocket_url
|
||||
|| query.is_some_and(|value| {
|
||||
value
|
||||
.split('&')
|
||||
.any(|part| part.split('=').next() == Some("model"))
|
||||
})
|
||||
{
|
||||
return url;
|
||||
}
|
||||
format!(
|
||||
"{url}{}model={}",
|
||||
if query.is_some() { "&" } else { "?" },
|
||||
percent_encode(model)
|
||||
)
|
||||
}
|
||||
|
||||
fn percent_encode(value: &str) -> String {
|
||||
value
|
||||
.bytes()
|
||||
.map(|byte| {
|
||||
if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') {
|
||||
format!("{}", byte as char)
|
||||
} else {
|
||||
format!("%{byte:02X}")
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn enforce_model(event: &ResponsesWsEvent, model: &str) -> ResponsesWsEvent {
|
||||
if !event.is_response_create() {
|
||||
return event.clone();
|
||||
}
|
||||
let mut enforced = event.clone();
|
||||
let has_flat_model = enforced.data.contains_key("model");
|
||||
if let Some(response) = enforced
|
||||
.data
|
||||
.get_mut("response")
|
||||
.and_then(serde_json::Value::as_object_mut)
|
||||
{
|
||||
response.insert(
|
||||
"model".to_string(),
|
||||
serde_json::Value::String(model.to_string()),
|
||||
);
|
||||
if has_flat_model {
|
||||
enforced.data.insert(
|
||||
"model".to_string(),
|
||||
serde_json::Value::String(model.to_string()),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
enforced.data.insert(
|
||||
"model".to_string(),
|
||||
serde_json::Value::String(model.to_string()),
|
||||
);
|
||||
}
|
||||
enforced
|
||||
}
|
||||
|
||||
pub fn is_terminal_event(event_type: &ResponsesWsEventType) -> bool {
|
||||
matches!(
|
||||
|
|
@ -285,65 +170,3 @@ impl ResponsesWebSocketConnection {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn event(value: serde_json::Value) -> ResponsesWsEvent {
|
||||
serde_json::from_value(value).expect("valid event")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn url_construction_matches_python_defaults_and_query_behavior() {
|
||||
assert_eq!(
|
||||
complete_websocket_url(None, "gpt-5", true),
|
||||
"wss://api.openai.com/v1/responses?model=gpt-5"
|
||||
);
|
||||
assert_eq!(
|
||||
complete_websocket_url(Some("http://localhost:8080/"), "gpt 5", true),
|
||||
"ws://localhost:8080/responses?model=gpt%205"
|
||||
);
|
||||
assert_eq!(
|
||||
complete_websocket_url(Some("https://example.test/v1?foo=bar"), "gpt-5", true),
|
||||
"wss://example.test/v1/responses?foo=bar&model=gpt-5"
|
||||
);
|
||||
assert_eq!(
|
||||
complete_websocket_url(Some("https://example.test?model=existing"), "gpt-5", true),
|
||||
"wss://example.test/responses?model=existing"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enforce_model_overrides_flat_and_nested_values() {
|
||||
let flat = enforce_model(
|
||||
&event(serde_json::json!({"type":"response.create","model":"wrong"})),
|
||||
"gpt-5",
|
||||
);
|
||||
assert_eq!(flat.model(), Some("gpt-5"));
|
||||
let nested = enforce_model(
|
||||
&event(serde_json::json!({
|
||||
"type":"response.create",
|
||||
"model":"wrong",
|
||||
"response":{"model":"also-wrong"}
|
||||
})),
|
||||
"gpt-5",
|
||||
);
|
||||
assert_eq!(nested.model(), Some("gpt-5"));
|
||||
assert_eq!(
|
||||
nested
|
||||
.data
|
||||
.get("response")
|
||||
.and_then(|value| value.get("model")),
|
||||
Some(&serde_json::json!("gpt-5"))
|
||||
);
|
||||
let nested_without_flat = enforce_model(
|
||||
&event(serde_json::json!({
|
||||
"type":"response.create",
|
||||
"response":{"model":"also-wrong"}
|
||||
})),
|
||||
"gpt-5",
|
||||
);
|
||||
assert!(!nested_without_flat.data.contains_key("model"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "litellm-providers"
|
||||
name = "litellm-llms"
|
||||
version = "0.1.0"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
|
@ -10,9 +10,18 @@ litellm-types.workspace = true
|
|||
litellm-core-utils.workspace = true
|
||||
litellm-auth.workspace = true
|
||||
litellm-auth-aws.workspace = true
|
||||
litellm-framing.workspace = true
|
||||
base64.workspace = true
|
||||
bytes.workspace = true
|
||||
futures-util.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
thiserror.workspace = true
|
||||
time.workspace = true
|
||||
url.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
aws-smithy-eventstream = "=0.61.1"
|
||||
aws-smithy-types = "1.6.1"
|
||||
rstest.workspace = true
|
||||
tokio.workspace = true
|
||||
|
|
@ -1,11 +1,13 @@
|
|||
use litellm_providers::anthropic::experimental_pass_through::messages::transformation::resolve_anthropic_api_base;
|
||||
use litellm_types::llms::anthropic_messages::anthropic_response::AnthropicMessagesResponse;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use time::OffsetDateTime;
|
||||
use url::Url;
|
||||
|
||||
use crate::messages::Error;
|
||||
use crate::{
|
||||
anthropic::experimental_pass_through::messages::transformation::resolve_anthropic_api_base,
|
||||
base_llm::chat::transformation::Error,
|
||||
};
|
||||
|
||||
const BATCHES_PATH_SUFFIX: &str = "/v1/messages/batches";
|
||||
|
||||
|
|
@ -6,11 +6,13 @@ use litellm_types::{
|
|||
};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::super::experimental_pass_through::messages::streaming::{
|
||||
AnthropicContentBlock, AnthropicContentBlockDelta, AnthropicMessagesStreamEvent,
|
||||
AnthropicStreamUsage,
|
||||
use crate::{
|
||||
anthropic::experimental_pass_through::messages::streaming_iterator::{
|
||||
AnthropicContentBlock, AnthropicContentBlockDelta, AnthropicMessagesStreamEvent,
|
||||
AnthropicStreamUsage,
|
||||
},
|
||||
base_llm::{base_model_iterator::StreamTransformer, chat::transformation::Error},
|
||||
};
|
||||
use crate::chat_completions::{Error, streaming::StreamTransformer};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum AnthropicJsonChunkType {
|
||||
2
litellm-rust/crates/llms/src/anthropic/chat/mod.rs
Normal file
2
litellm-rust/crates/llms/src/anthropic/chat/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod handler;
|
||||
pub mod transformation;
|
||||
|
|
@ -2,7 +2,7 @@ use litellm_types::llms::anthropic_messages::anthropic_request::{AnthropicMessag
|
|||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{constants::ANTHROPIC_OAUTH_TOKEN_PREFIX, messages::Error};
|
||||
use crate::{anthropic::ANTHROPIC_OAUTH_TOKEN_PREFIX, base_llm::chat::transformation::Error};
|
||||
|
||||
const COUNT_TOKENS_ENDPOINT: &str = "https://api.anthropic.com/v1/messages/count_tokens";
|
||||
const TOKEN_COUNTING_BETA: &str = "token-counting-2024-11-01";
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
pub mod streaming_iterator;
|
||||
pub mod transformation;
|
||||
|
|
@ -9,7 +9,19 @@ use litellm_framing::{
|
|||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use crate::messages::Error;
|
||||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("stream framing failed: {0}")]
|
||||
StreamFraming(String),
|
||||
#[error("Anthropic SSE frame has no data")]
|
||||
MissingStreamData,
|
||||
#[error("Anthropic stream event is invalid: {0}")]
|
||||
InvalidStreamEvent(String),
|
||||
#[error("Bedrock event payload is invalid: {0}")]
|
||||
InvalidBedrockPayload(String),
|
||||
#[error("Bedrock event payload has invalid base64: {0}")]
|
||||
InvalidBedrockBase64(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
pub struct AnthropicStreamUsage {
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
pub mod batches;
|
||||
pub mod chat;
|
||||
pub mod count_tokens;
|
||||
pub mod experimental_pass_through;
|
||||
|
||||
pub const ANTHROPIC_OAUTH_TOKEN_PREFIX: &str = "sk-ant-oat";
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
pub mod anthropic_messages;
|
||||
pub mod audio_transcription;
|
||||
pub mod base_model_iterator;
|
||||
pub mod chat;
|
||||
pub mod responses;
|
||||
|
|
@ -0,0 +1,180 @@
|
|||
use litellm_types::responses::streaming_websocket::{ResponsesWsEvent, ResponsesWsTransformResult};
|
||||
|
||||
use crate::base_llm::chat::transformation::Error;
|
||||
|
||||
pub const OPENAI_RESPONSES_DEFAULT_API_BASE: &str = "https://api.openai.com/v1";
|
||||
pub const OPENAI_RESPONSES_PATH: &str = "/responses";
|
||||
|
||||
pub trait ResponsesWebSocketProviderConfig: Sync {
|
||||
fn supports_native_websocket(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn model_in_websocket_url(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn complete_websocket_url(&self, api_base: Option<&str>, model: &str) -> String {
|
||||
complete_websocket_url(api_base, model, self.model_in_websocket_url())
|
||||
}
|
||||
|
||||
fn transform_ws_request(
|
||||
&self,
|
||||
event: &ResponsesWsEvent,
|
||||
model: &str,
|
||||
) -> Result<ResponsesWsTransformResult, Error>;
|
||||
|
||||
fn transform_ws_response(
|
||||
&self,
|
||||
event: &ResponsesWsEvent,
|
||||
model: &str,
|
||||
) -> Result<ResponsesWsTransformResult, Error>;
|
||||
}
|
||||
|
||||
pub fn complete_websocket_url(
|
||||
api_base: Option<&str>,
|
||||
model: &str,
|
||||
model_in_websocket_url: bool,
|
||||
) -> String {
|
||||
let base = api_base
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or(OPENAI_RESPONSES_DEFAULT_API_BASE);
|
||||
let (base_without_query, query) = base
|
||||
.split_once('?')
|
||||
.map_or((base, None), |(value, query)| (value, Some(query)));
|
||||
let response_url = format!(
|
||||
"{}{}",
|
||||
base_without_query.trim_end_matches('/'),
|
||||
OPENAI_RESPONSES_PATH
|
||||
);
|
||||
let scheme_flipped = if let Some(rest) = response_url.strip_prefix("https://") {
|
||||
format!("wss://{rest}")
|
||||
} else if let Some(rest) = response_url.strip_prefix("http://") {
|
||||
format!("ws://{rest}")
|
||||
} else {
|
||||
response_url
|
||||
};
|
||||
let url = query.map_or(scheme_flipped.clone(), |value| {
|
||||
format!("{scheme_flipped}?{value}")
|
||||
});
|
||||
if !model_in_websocket_url
|
||||
|| query.is_some_and(|value| {
|
||||
value
|
||||
.split('&')
|
||||
.any(|part| part.split('=').next() == Some("model"))
|
||||
})
|
||||
{
|
||||
return url;
|
||||
}
|
||||
format!(
|
||||
"{url}{}model={}",
|
||||
if query.is_some() { "&" } else { "?" },
|
||||
percent_encode(model)
|
||||
)
|
||||
}
|
||||
|
||||
fn percent_encode(value: &str) -> String {
|
||||
value
|
||||
.bytes()
|
||||
.map(|byte| {
|
||||
if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') {
|
||||
format!("{}", byte as char)
|
||||
} else {
|
||||
format!("%{byte:02X}")
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn enforce_model(event: &ResponsesWsEvent, model: &str) -> ResponsesWsEvent {
|
||||
if !event.is_response_create() {
|
||||
return event.clone();
|
||||
}
|
||||
let mut enforced = event.clone();
|
||||
let has_flat_model = enforced.data.contains_key("model");
|
||||
if let Some(response) = enforced
|
||||
.data
|
||||
.get_mut("response")
|
||||
.and_then(serde_json::Value::as_object_mut)
|
||||
{
|
||||
response.insert(
|
||||
"model".to_string(),
|
||||
serde_json::Value::String(model.to_string()),
|
||||
);
|
||||
if has_flat_model {
|
||||
enforced.data.insert(
|
||||
"model".to_string(),
|
||||
serde_json::Value::String(model.to_string()),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
enforced.data.insert(
|
||||
"model".to_string(),
|
||||
serde_json::Value::String(model.to_string()),
|
||||
);
|
||||
}
|
||||
enforced
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn event(value: serde_json::Value) -> ResponsesWsEvent {
|
||||
serde_json::from_value(value).expect("valid event")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn url_construction_matches_python_defaults_and_query_behavior() {
|
||||
assert_eq!(
|
||||
complete_websocket_url(None, "gpt-5", true),
|
||||
"wss://api.openai.com/v1/responses?model=gpt-5"
|
||||
);
|
||||
assert_eq!(
|
||||
complete_websocket_url(Some("http://localhost:8080/"), "gpt 5", true),
|
||||
"ws://localhost:8080/responses?model=gpt%205"
|
||||
);
|
||||
assert_eq!(
|
||||
complete_websocket_url(Some("https://example.test/v1?foo=bar"), "gpt-5", true),
|
||||
"wss://example.test/v1/responses?foo=bar&model=gpt-5"
|
||||
);
|
||||
assert_eq!(
|
||||
complete_websocket_url(Some("https://example.test?model=existing"), "gpt-5", true),
|
||||
"wss://example.test/responses?model=existing"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enforce_model_overrides_flat_and_nested_values() {
|
||||
let flat = enforce_model(
|
||||
&event(serde_json::json!({"type":"response.create","model":"wrong"})),
|
||||
"gpt-5",
|
||||
);
|
||||
assert_eq!(flat.model(), Some("gpt-5"));
|
||||
let nested = enforce_model(
|
||||
&event(serde_json::json!({
|
||||
"type":"response.create",
|
||||
"model":"wrong",
|
||||
"response":{"model":"also-wrong"}
|
||||
})),
|
||||
"gpt-5",
|
||||
);
|
||||
assert_eq!(nested.model(), Some("gpt-5"));
|
||||
assert_eq!(
|
||||
nested
|
||||
.data
|
||||
.get("response")
|
||||
.and_then(|value| value.get("model")),
|
||||
Some(&serde_json::json!("gpt-5"))
|
||||
);
|
||||
let nested_without_flat = enforce_model(
|
||||
&event(serde_json::json!({
|
||||
"type":"response.create",
|
||||
"response":{"model":"also-wrong"}
|
||||
})),
|
||||
"gpt-5",
|
||||
);
|
||||
assert!(!nested_without_flat.data.contains_key("model"));
|
||||
}
|
||||
}
|
||||
|
|
@ -2,3 +2,4 @@ pub mod anthropic;
|
|||
pub mod azure_ai;
|
||||
pub mod base_llm;
|
||||
pub mod bedrock;
|
||||
pub mod openai;
|
||||
1
litellm-rust/crates/llms/src/openai/responses/mod.rs
Normal file
1
litellm-rust/crates/llms/src/openai/responses/mod.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod transformation;
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
use litellm_types::responses::streaming_websocket::{ResponsesWsEvent, ResponsesWsTransformResult};
|
||||
|
||||
use crate::responses::{
|
||||
Error,
|
||||
websocket::{ResponsesWebSocketProviderConfig, enforce_model},
|
||||
use crate::base_llm::{
|
||||
chat::transformation::Error,
|
||||
responses::transformation::{ResponsesWebSocketProviderConfig, enforce_model},
|
||||
};
|
||||
|
||||
pub struct OpenAiResponsesApiConfig;
|
||||
|
|
@ -1 +0,0 @@
|
|||
pub mod messages;
|
||||
Loading…
Add table
Reference in a new issue