use std::sync::Arc;
use async_trait::async_trait;
use super::completion::Completion;
use super::llm::{GenerateCompletionError, GenerateCompletionInput, GenerateCompletionResult, LLM};
use super::settings::Settings;
pub type MockLLMResult = Result<Completion, String>;
pub struct MockLLM {
result: MockLLMResult,
}
impl MockLLM {
pub fn new(result: MockLLMResult) -> Self {
Self { result }
}
}
#[async_trait]
impl LLM for MockLLM {
async fn generate_completion(
&self,
_input: GenerateCompletionInput,
) -> GenerateCompletionResult {
match &self.result {
Ok(completion) => Ok(completion.clone()),
Err(err_msg) => Err(GenerateCompletionError::Unknown(err_msg.clone().into())),
}
}
}
pub struct WithDefaultSettings {
llm: Box<dyn LLM>,
default_settings: Settings,
}
impl WithDefaultSettings {
pub fn new(llm: Box<dyn LLM>, default_settings: Settings) -> Self {
Self {
llm,
default_settings,
}
}
}
#[async_trait]
impl LLM for WithDefaultSettings {
async fn generate_completion(
&self,
mut input: GenerateCompletionInput,
) -> GenerateCompletionResult {
let input_settings = input.settings.unwrap_or_default();
input.settings = Some(input_settings.merge(self.default_settings.clone()));
self.llm.generate_completion(input).await
}
}