add config to own_assist_common

This commit is contained in:
2026-07-28 17:48:31 +02:00
parent b776cafab7
commit c5b594e1da
5 changed files with 108 additions and 57 deletions
+5 -3
View File
@@ -1,12 +1,14 @@
[package]
name = "own_assist_common"
version = "0.3.0"
version = "0.4.0"
edition = "2024"
license = "GPL-3.0"
[dependencies]
thiserror = "2.0.18"
toml = "1.1.2"
serde = "1.0.228"
serde = { version = "1.0.228", features = ["derive"] }
log = "0.4.29"
tracing-subscriber = { version = "0.3.23", features = ["env-filter"]}
tracing-subscriber = { version = "0.3.23", features = ["env-filter"]}
url = { version = "2.5.8", features = ["serde"] }
ollama-rs = { version = "0.3.6", features = ["headers"]}
+93
View File
@@ -0,0 +1,93 @@
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, 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, 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 ollama_default_url = Url::parse("http://127.0.0.1:11434").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, 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, Deserialize)]
pub struct MCPClientConfig {
pub name: Option<String>,
pub url: Url,
pub authorization: Option<String>, // should probably implement oauth some time // actually, fuck oauth
}
+1
View File
@@ -1,3 +1,4 @@
pub mod config;
pub mod config_loader;
mod exit_error;
mod tracing_init;