90 lines
2.9 KiB
Rust
90 lines
2.9 KiB
Rust
use ollama_rs::error::OllamaError;
|
|
use ollama_rs::models::ModelOptions;
|
|
use ollama_rs::models::create::{CreateModelRequest, CreateModelStatus};
|
|
use serde::Deserialize;
|
|
use url::Url;
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct AudioClientConfig {
|
|
pub url: Url,
|
|
pub authorization: Option<String>,
|
|
}
|
|
|
|
fn ollama_default_url() -> Url {
|
|
Url::parse("http://127.0.0.1:11434").unwrap()
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct OllamaConfig {
|
|
#[serde(default = "ollama_default_url")]
|
|
pub url: Url,
|
|
#[serde(rename = "model")]
|
|
pub model_config: OllamaModelConfig,
|
|
pub authorization: Option<String>,
|
|
}
|
|
|
|
impl OllamaConfig {
|
|
pub fn ollama(&self) -> ollama_rs::Ollama {
|
|
let mut ollama_headers = ollama_rs::headers::HeaderMap::new();
|
|
|
|
if let Some(authorization_header) = &self.authorization {
|
|
ollama_headers.append(
|
|
"Authorization",
|
|
ollama_rs::headers::HeaderValue::from_str(authorization_header.as_str()).unwrap(),
|
|
);
|
|
}
|
|
|
|
let mut ollama = ollama_rs::Ollama::from_url(self.url.clone());
|
|
ollama.set_headers(Some(ollama_headers));
|
|
|
|
ollama
|
|
}
|
|
|
|
pub async fn create_model(
|
|
&self,
|
|
download_needed_message: Option<String>,
|
|
) -> Result<CreateModelStatus, OllamaError> {
|
|
let ollama = self.ollama();
|
|
let model_config = &self.model_config;
|
|
|
|
let model_options = ModelOptions::default()
|
|
.num_ctx(model_config.context_size.unwrap_or(2048)) // 2048 is the ollama default.
|
|
.temperature(model_config.temperature.unwrap_or(0.8)); // 0.8 is the ollama default
|
|
|
|
// print message to warn user of long waiting times
|
|
if let Ok(models) = ollama.list_local_models().await
|
|
&& !models.iter().any(|m| m.name == model_config.from_model)
|
|
&& let Some(download_needed_message) = download_needed_message
|
|
{
|
|
log::info!("{}", download_needed_message);
|
|
println!("{}", download_needed_message);
|
|
}
|
|
|
|
let mut create_model_request = CreateModelRequest::new(model_config.name.clone())
|
|
.from_model(model_config.from_model.clone())
|
|
.parameters(model_options);
|
|
|
|
if let Some(system_prompt) = &model_config.system_prompt {
|
|
create_model_request = create_model_request.system(system_prompt.clone());
|
|
}
|
|
|
|
ollama.create_model(create_model_request).await
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct OllamaModelConfig {
|
|
#[serde(rename = "from-model")]
|
|
pub from_model: String,
|
|
pub name: String,
|
|
pub system_prompt: Option<String>,
|
|
pub context_size: Option<u64>,
|
|
pub temperature: Option<f32>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct MCPClientConfig {
|
|
pub name: Option<String>,
|
|
pub url: Url,
|
|
pub authorization: Option<String>, // should probably implement oauth some time // actually, fuck oauth
|
|
} |