use std::pin::Pin;
use async_trait::async_trait;
use futures::Stream;
use crate::schemas::{Message, StreamData};
use super::{options::CallOptions, GenerateResult, LLMError};
#[async_trait]
pub trait LLM: Sync + Send {
async fn generate(&self, messages: &[Message]) -> Result<GenerateResult, LLMError>;
async fn invoke(&self, prompt: &str) -> Result<String, LLMError> {
self.generate(&[Message::new_human_message(prompt)])
.await
.map(|res| res.generation)
}
async fn stream(
&self,
_messages: &[Message],
) -> Result<Pin<Box<dyn Stream<Item = Result<StreamData, LLMError>> + Send>>, LLMError>;
fn add_options(&mut self, _options: CallOptions) {
}
fn messages_to_string(&self, messages: &[Message]) -> String {
messages
.iter()
.map(|m| format!("{:?}: {}", m.message_type, m.content))
.collect::<Vec<String>>()
.join("\n")
}
}