mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
Every other crate names its error Error. The format crates already re-exported it under that name, so only the inference crate and a few importers change
115 lines
3.6 KiB
Rust
115 lines
3.6 KiB
Rust
use litellm_host::observation::ObservationSender;
|
|
pub mod route;
|
|
pub mod types;
|
|
pub use litellm_inference::Error;
|
|
mod common_utils;
|
|
pub mod constants;
|
|
pub(crate) mod handler;
|
|
use litellm_llms_types::formats::chat_completions::ChatCompletionsResponse;
|
|
|
|
use litellm_auth::AuthServices;
|
|
use litellm_inference::provider::resolve_llm_provider;
|
|
use litellm_secrets::source::SecretSource;
|
|
use std::sync::Arc;
|
|
use types::{ChatCompletionsCall, ChatCompletionsRequest};
|
|
|
|
#[derive(Clone)]
|
|
pub struct ChatCompletionsRoute {
|
|
http: litellm_http::Client,
|
|
auth: Arc<AuthServices>,
|
|
secrets: Arc<dyn SecretSource>,
|
|
cache: Option<litellm_cache_response::ScopedCache>,
|
|
}
|
|
|
|
impl ChatCompletionsRoute {
|
|
pub fn new(
|
|
http: litellm_http::Client,
|
|
auth: Arc<AuthServices>,
|
|
secrets: Arc<dyn SecretSource>,
|
|
) -> Self {
|
|
Self {
|
|
http,
|
|
auth,
|
|
secrets,
|
|
cache: None,
|
|
}
|
|
}
|
|
|
|
pub fn with_cache(self, cache: litellm_cache_response::ScopedCache) -> Self {
|
|
Self {
|
|
cache: Some(cache),
|
|
..self
|
|
}
|
|
}
|
|
|
|
pub async fn execute(
|
|
&self,
|
|
request: ChatCompletionsRequest<'_>,
|
|
interceptors: &impl litellm_host::interceptors::Interceptors<Error>,
|
|
options: impl Into<litellm_inference::CallOptions>,
|
|
) -> Result<ChatCompletionsResponse, Error> {
|
|
let litellm_inference::CallOptions {
|
|
cache: cache_options,
|
|
observers,
|
|
} = options.into();
|
|
litellm_host::lifecycle::observe_unary(
|
|
observers.clone(),
|
|
self.run(
|
|
request.into(),
|
|
cache_options,
|
|
interceptors,
|
|
observers.as_ref(),
|
|
),
|
|
)
|
|
.await
|
|
}
|
|
|
|
#[tracing::instrument(name = "litellm.route", skip_all, fields(
|
|
route = "chat_completions",
|
|
model = %call.model,
|
|
provider,
|
|
resolved_model,
|
|
stream = false,
|
|
outcome
|
|
))]
|
|
async fn run(
|
|
&self,
|
|
call: ChatCompletionsCall,
|
|
cache_options: Option<litellm_cache_response::CachePolicy>,
|
|
interceptors: &impl litellm_host::interceptors::Interceptors<Error>,
|
|
observers: Option<&ObservationSender>,
|
|
) -> Result<ChatCompletionsResponse, Error> {
|
|
litellm_inference::diagnostic::unary(async {
|
|
let model = call.model.clone();
|
|
let custom_llm_provider = call.custom_llm_provider.clone();
|
|
let provider =
|
|
resolve_llm_provider(&model, custom_llm_provider.as_deref(), "chat completions")?;
|
|
let config = common_utils::chat_completions_provider(provider.provider)
|
|
.ok_or_else(|| Error::InvalidProvider(<&str>::from(provider.provider).to_string()))?
|
|
.config();
|
|
let call = common_utils::resolve_request(call, config)?;
|
|
litellm_inference::diagnostic::provider(
|
|
provider.model,
|
|
<&'static str>::from(provider.provider),
|
|
);
|
|
let secrets = self.secrets.resolve(&config.secret_names()).await?;
|
|
let execute: futures_util::future::BoxFuture<
|
|
'_,
|
|
Result<ChatCompletionsResponse, Error>,
|
|
> = Box::pin(handler::execute(
|
|
&self.http,
|
|
&self.auth,
|
|
config,
|
|
provider,
|
|
call,
|
|
secrets,
|
|
self.cache.clone(),
|
|
cache_options,
|
|
interceptors,
|
|
observers,
|
|
));
|
|
execute.await
|
|
})
|
|
.await
|
|
}
|
|
}
|