use crate::{
AudioSpeechRequest, ChatCompletionChunk, ChatCompletionRequest, ChatCompletionResponse,
EmbeddingRequest, EmbeddingResponse, Error, ImageRequest, MultipartField,
};
use bytes::Bytes;
use futures_core::Stream;
use std::{future::Future, pin::Pin};
pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + Send + 'a>>;
pub trait Provider: Send + Sync {
fn chat_completion(
&self,
request: &ChatCompletionRequest,
) -> impl Future<Output = Result<ChatCompletionResponse, Error>> + Send;
fn chat_completion_stream(
&self,
request: &ChatCompletionRequest,
) -> impl Future<Output = Result<BoxStream<'static, Result<ChatCompletionChunk, Error>>, Error>> + Send;
fn embedding(
&self,
_request: &EmbeddingRequest,
) -> impl Future<Output = Result<EmbeddingResponse, Error>> + Send {
async { Err(Error::not_implemented("embedding")) }
}
fn image_generation(
&self,
_request: &ImageRequest,
) -> impl Future<Output = Result<(Bytes, String), Error>> + Send {
async { Err(Error::not_implemented("image_generation")) }
}
fn audio_speech(
&self,
_request: &AudioSpeechRequest,
) -> impl Future<Output = Result<(Bytes, String), Error>> + Send {
async { Err(Error::not_implemented("audio_speech")) }
}
fn audio_transcription(
&self,
_model: &str,
_fields: &[MultipartField],
) -> impl Future<Output = Result<(Bytes, String), Error>> + Send {
async { Err(Error::not_implemented("audio_transcription")) }
}
fn is_openai_compat(&self) -> bool {
false
}
fn is_anthropic_compat(&self) -> bool {
false
}
fn chat_completion_raw(
&self,
_model: &str,
raw_body: Bytes,
) -> impl Future<Output = Result<Bytes, Error>> + Send {
async move {
let request: ChatCompletionRequest = crate::json::from_slice(&raw_body)
.map_err(|e| Error::Internal(e.to_string()))?;
let resp = self.chat_completion(&request).await?;
Ok(Bytes::from(
crate::json::to_vec(&resp).map_err(|e| Error::Internal(e.to_string()))?,
))
}
}
fn anthropic_messages_raw(
&self,
_raw_body: Bytes,
) -> impl Future<Output = Result<Bytes, Error>> + Send {
async { Err(Error::not_implemented("anthropic_messages_raw")) }
}
}