use std::path::{Path, PathBuf};
use super::error::AiError;
pub const DEFAULT_MODEL: &str = "phi-3.5";
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum ModelId {
#[default]
Phi35,
Phi3,
Llama32_1b,
Llama32_3b,
Llama31_8b,
Qwen25_15b,
Qwen25_3b,
Qwen25_7b,
TinyLlama,
Gguf(PathBuf),
}
impl ModelId {
pub fn parse(raw: &str) -> Result<Self, AiError> {
let t = raw.trim();
if t.is_empty() {
return Err(AiError::usage(
"pass --model NAME or a .gguf path".to_string(),
));
}
let path = Path::new(t);
if t.ends_with(".gguf") || path.exists() {
return Ok(Self::Gguf(path.to_path_buf()));
}
Ok(match t {
"phi-3.5" | "phi3.5" | "default" => Self::Phi35,
"phi-3" | "phi3" => Self::Phi3,
"llama3.2-1b" | "llama-3.2-1b" => Self::Llama32_1b,
"llama3.2-3b" | "llama-3.2-3b" => Self::Llama32_3b,
"llama3.1-8b" | "llama-3.1-8b" => Self::Llama31_8b,
"qwen2.5-1.5b" | "qwen-2.5-1.5b" => Self::Qwen25_15b,
"qwen2.5-3b" | "qwen-2.5-3b" => Self::Qwen25_3b,
"qwen2.5-7b" | "qwen-2.5-7b" => Self::Qwen25_7b,
"tinyllama" | "tiny-llama" => Self::TinyLlama,
other => {
return Err(AiError::usage(format!(
"unknown model {other:?}. presets: {}; or pass a .gguf path",
Self::presets().join(", ")
)));
}
})
}
pub fn presets() -> &'static [&'static str] {
&[
"phi-3.5",
"phi-3",
"llama3.2-1b",
"llama3.2-3b",
"llama3.1-8b",
"qwen2.5-1.5b",
"qwen2.5-3b",
"qwen2.5-7b",
"tinyllama",
]
}
pub fn as_str(&self) -> String {
match self {
Self::Phi35 => "phi-3.5".into(),
Self::Phi3 => "phi-3".into(),
Self::Llama32_1b => "llama3.2-1b".into(),
Self::Llama32_3b => "llama3.2-3b".into(),
Self::Llama31_8b => "llama3.1-8b".into(),
Self::Qwen25_15b => "qwen2.5-1.5b".into(),
Self::Qwen25_3b => "qwen2.5-3b".into(),
Self::Qwen25_7b => "qwen2.5-7b".into(),
Self::TinyLlama => "tinyllama".into(),
Self::Gguf(p) => p.display().to_string(),
}
}
}
#[cfg(feature = "kalosm")]
pub async fn load(id: &ModelId, verbose: bool) -> Result<KalosmCompleter, AiError> {
use kalosm::language::{FileSource, Llama, LlamaSource};
use kalosm_common::Cache;
let source = match id {
ModelId::Phi35 => LlamaSource::phi_3_5_mini_4k_instruct(),
ModelId::Phi3 => LlamaSource::phi_3_mini_4k_instruct(),
ModelId::Llama32_1b => LlamaSource::llama_3_2_1b_chat(),
ModelId::Llama32_3b => LlamaSource::llama_3_2_3b_chat(),
ModelId::Llama31_8b => LlamaSource::llama_3_1_8b_chat(),
ModelId::Qwen25_15b => LlamaSource::qwen_2_5_1_5b_instruct(),
ModelId::Qwen25_3b => LlamaSource::qwen_2_5_3b_instruct(),
ModelId::Qwen25_7b => LlamaSource::qwen_2_5_7b_instruct(),
ModelId::TinyLlama => LlamaSource::tiny_llama_1_1b_chat(),
ModelId::Gguf(path) => LlamaSource::new(FileSource::local(path.clone())),
};
let source = source.with_cache(Cache::new(super::cache::cache_dir()?));
if verbose {
eprintln!(
"loading model {} cache={}",
id.as_str(),
super::cache::cache_dir()?.display()
);
}
let llama = Llama::builder()
.with_source(source)
.build()
.await
.map_err(|e| AiError::Model(format!("failed to load {}: {e}", id.as_str())))?;
Ok(KalosmCompleter { llama })
}
#[cfg(feature = "kalosm")]
pub struct KalosmCompleter {
llama: kalosm::language::Llama,
}
#[cfg(feature = "kalosm")]
impl super::plan::Completer for KalosmCompleter {
async fn complete(
&self,
view: &super::view::DocumentView,
instruction: &str,
) -> Result<super::plan::Plan, AiError> {
use super::plan::{system_prompt, user_prompt, Plan};
use kalosm::language::{ChatModelExt, Parse};
use std::sync::Arc;
let task = self
.llama
.task(system_prompt(view.format))
.with_constraints(Arc::new(Plan::new_parser()));
let user = user_prompt(view, instruction);
task(&user).await.map_err(|e| AiError::Model(e.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::ModelId;
#[test]
fn default_is_phi35() {
assert_eq!(ModelId::parse("phi-3.5").unwrap(), ModelId::Phi35);
assert_eq!(ModelId::parse("default").unwrap(), ModelId::Phi35);
}
#[test]
fn gguf_path_wins() {
match ModelId::parse("./models/foo.gguf").unwrap() {
ModelId::Gguf(p) => assert!(p.ends_with("foo.gguf")),
other => panic!("{other:?}"),
}
}
#[test]
fn unknown_is_usage() {
let err = ModelId::parse("gpt-4").unwrap_err();
assert!(err.is_usage());
}
}