model-gateway-rs 0.2.0

A Rust library for model gateway services, providing traits and SDKs for various AI models.
Documentation
use serde::{Deserialize, Serialize};

use crate::model::llm::{ChatMessage, LlmOutput};

#[derive(Debug, Clone, Serialize)]
pub struct OllamaChatOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub num_predict: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
}

#[derive(Debug, Clone, Serialize)]
pub struct OllamaChatRequest {
    pub model: String,
    pub messages: Vec<ChatMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<OllamaChatOptions>,
}

#[derive(Debug, Deserialize)]
pub struct OllamaChatResponse {
    pub model: String,
    pub created_at: String,
    pub message: ChatMessage,
    pub done_reason: Option<String>,
    pub done: bool,
    pub total_duration: Option<u64>,
    pub load_duration: Option<u64>,
    pub prompt_eval_count: Option<u32>,
    pub prompt_eval_duration: Option<u64>,
    pub eval_count: Option<u32>,
    pub eval_duration: Option<u64>,
}

impl OllamaChatResponse {
    pub fn first_message(&self) -> String {
        self.message.content.clone()
    }
}

impl From<OllamaChatResponse> for LlmOutput {
    fn from(response: OllamaChatResponse) -> Self {
        let usage = response.prompt_eval_count.zip(response.eval_count).map(|(p, c)| p + c);
        LlmOutput {
            message: Some(response.message),
            usage,
        }
    }
}