mod gguf;
mod metadata;
mod safetensors;
mod source;
mod tokenizer;
pub use gguf::GgufSource;
pub use metadata::{ModelMetadata, VisionConfig};
pub use safetensors::SafetensorsSource;
pub use source::{ModelSource, SamplerConfig, TensorDtype, TensorReader};
pub use tokenizer::TokenizerSpec;
use std::path::Path;
pub fn open_model_source(path: impl AsRef<Path>) -> Result<Box<dyn ModelSource>> {
let path = path.as_ref();
if path.is_file() && path.extension().is_some_and(|e| e == "gguf") {
return Ok(Box::new(GgufSource::load(path)?));
}
if path.is_dir() {
return Ok(Box::new(SafetensorsSource::load(path)?));
}
Err(FormatError::MissingFile(path.display().to_string()))
}
#[derive(Debug, thiserror::Error)]
pub enum FormatError {
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error("json error in {context}: {source}")]
Json {
context: String,
source: serde_json::Error,
},
#[error("safetensors error: {0}")]
Safetensors(String),
#[error("tensor not found: {0}")]
TensorNotFound(String),
#[error("unsupported dtype for tensor {tensor}: {dtype}")]
UnsupportedDtype {
tensor: String,
dtype: String,
},
#[error("missing file: {0}")]
MissingFile(String),
#[error("missing config field: {0}")]
MissingField(String),
}
pub type Result<T> = std::result::Result<T, FormatError>;