litellm_rs/core/completion/default_router/
mod.rs1use super::{CompletionOptions, CompletionResponse, CompletionStream, Message, Router};
4
5use crate::core::router::{RuntimeBinding, default_runtime};
6use crate::utils::error::gateway_error::{GatewayError, Result};
7use async_trait::async_trait;
8
9mod router_impl;
10
11pub struct DefaultRouter {
16 runtime_binding: Option<RuntimeBinding>,
17}
18
19impl DefaultRouter {
20 pub async fn new() -> Result<Self> {
22 Ok(Self {
23 runtime_binding: None,
24 })
25 }
26
27 pub fn from_runtime(runtime: RuntimeBinding) -> Self {
29 Self {
30 runtime_binding: Some(runtime),
31 }
32 }
33}
34
35pub struct ErrorRouter {
37 error: String,
38}
39
40impl ErrorRouter {
41 pub fn new(error: impl Into<String>) -> Self {
43 Self {
44 error: error.into(),
45 }
46 }
47}
48
49#[async_trait]
50impl Router for ErrorRouter {
51 async fn complete(
52 &self,
53 _model: &str,
54 _messages: Vec<Message>,
55 _options: CompletionOptions,
56 ) -> Result<CompletionResponse> {
57 Err(GatewayError::internal(format!(
58 "Router initialization failed: {}",
59 self.error
60 )))
61 }
62
63 async fn complete_stream(
64 &self,
65 _model: &str,
66 _messages: Vec<Message>,
67 _options: CompletionOptions,
68 ) -> Result<CompletionStream> {
69 Err(GatewayError::internal(format!(
70 "Router initialization failed: {}",
71 self.error
72 )))
73 }
74}
75
76pub async fn completion(
78 model: &str,
79 messages: Vec<Message>,
80 options: Option<CompletionOptions>,
81) -> Result<CompletionResponse> {
82 let handle = default_runtime().map_err(GatewayError::from)?;
83 router_impl::complete_with_runtime_handle(&handle, model, messages, options.unwrap_or_default())
84 .await
85}
86
87pub async fn acompletion(
89 model: &str,
90 messages: Vec<Message>,
91 options: Option<CompletionOptions>,
92) -> Result<CompletionResponse> {
93 completion(model, messages, options).await
94}
95
96pub async fn completion_stream(
98 model: &str,
99 messages: Vec<Message>,
100 options: Option<CompletionOptions>,
101) -> Result<CompletionStream> {
102 let handle = default_runtime().map_err(GatewayError::from)?;
103 router_impl::complete_stream_with_runtime_handle(
104 &handle,
105 model,
106 messages,
107 options.unwrap_or_default(),
108 )
109 .await
110}