use std::sync::Arc;
use af_llm::{CompletionRequest, CompletionResponse, LlmClient, LlmError};
use async_trait::async_trait;
use tokio::sync::mpsc::UnboundedSender;
#[async_trait]
pub trait ChatModel: Send + Sync {
async fn complete_streaming(
&self,
request: &CompletionRequest,
delta_tx: UnboundedSender<(String, bool)>,
) -> Result<CompletionResponse, LlmError>;
}
#[async_trait]
impl ChatModel for LlmClient {
async fn complete_streaming(
&self,
request: &CompletionRequest,
delta_tx: UnboundedSender<(String, bool)>,
) -> Result<CompletionResponse, LlmError> {
self.complete_stream_single_attempt(request, |content, has_tools| {
let _ = delta_tx.send((content.to_string(), has_tools));
})
.await
}
}
#[async_trait]
impl ChatModel for Arc<dyn ChatModel> {
async fn complete_streaming(
&self,
request: &CompletionRequest,
delta_tx: UnboundedSender<(String, bool)>,
) -> Result<CompletionResponse, LlmError> {
(**self).complete_streaming(request, delta_tx).await
}
}