unai 0.0.1

Common AI abstractions
Documentation
use std::error::Error;

use async_trait::async_trait;

use super::completion::Completion;
use super::prompt::Message;
use super::prompt::Prompt;
use super::settings::Settings;

#[derive(Debug, Clone, Default)]
pub struct GenerateCompletionInput {
    /// The prompt to generate a completion for
    pub prompt: Prompt,

    /// The settings to use for the generation
    pub settings: Option<Settings>,

    /// JSON schema for structured generation
    pub schema: Option<serde_json::Value>,
}

impl From<String> for GenerateCompletionInput {
    fn from(message: String) -> Self {
        Self {
            prompt: Prompt::from(message),
            ..Default::default()
        }
    }
}

impl From<&str> for GenerateCompletionInput {
    fn from(message: &str) -> Self {
        Self::from(message.to_owned())
    }
}

impl From<Prompt> for GenerateCompletionInput {
    fn from(prompt: Prompt) -> Self {
        Self {
            prompt,
            ..Default::default()
        }
    }
}

impl From<Message> for GenerateCompletionInput {
    fn from(message: Message) -> Self {
        Self::from(Prompt::from(message))
    }
}

#[derive(Debug, thiserror::Error)]
pub enum GenerateCompletionError {
    #[error("an unknown error occurred: {0}")]
    Unknown(Box<dyn Error + Send + Sync>),
}

pub type GenerateCompletionResult = Result<Completion, GenerateCompletionError>;

#[async_trait]
pub trait LLM: Send + Sync {
    /// Generate a completion based on the request
    async fn generate_completion(&self, input: GenerateCompletionInput)
    -> GenerateCompletionResult;
}