Make updating settings override previous value

This commit is contained in:
Mubelotix
2025-07-28 12:37:44 +02:00
parent 1207785a07
commit 705b195941
4 changed files with 93 additions and 146 deletions

View File

@ -180,7 +180,7 @@ impl<'a> Index<'a, Owned> {
) -> (Value, StatusCode) { ) -> (Value, StatusCode) {
let url = let url =
format!("/indexes/{}/settings/chat", urlencode(self.uid.as_ref())); format!("/indexes/{}/settings/chat", urlencode(self.uid.as_ref()));
self.service.patch_encoded(url, settings, self.encoder).await self.service.put_encoded(url, settings, self.encoder).await
} }
pub async fn update_settings_displayed_attributes( pub async fn update_settings_displayed_attributes(

View File

@ -55,9 +55,7 @@ async fn set_reset_chat_issue_5772() {
"documentTemplate": "{% for field in fields %}{% if field.is_searchable and field.value != nil %}{{ field.name }}: {{ field.value }}\n{% endif %}{% endfor %}", "documentTemplate": "{% for field in fields %}{% if field.is_searchable and field.value != nil %}{{ field.name }}: {{ field.value }}\n{% endif %}{% endfor %}",
"documentTemplateMaxBytes": 400, "documentTemplateMaxBytes": 400,
"searchParameters": { "searchParameters": {
"limit": 16, "limit": 16
"sort": [],
"attributesToSearchOn": []
} }
} }
"#); "#);

View File

@ -1,44 +1,38 @@
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::num::NonZeroUsize;
use deserr::errors::JsonError; use deserr::errors::JsonError;
use deserr::Deserr; use deserr::Deserr;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use utoipa::ToSchema; use utoipa::ToSchema;
use crate::index::{self, ChatConfig, MatchingStrategy, RankingScoreThreshold, SearchParameters}; use crate::index::{ChatConfig, MatchingStrategy, RankingScoreThreshold, SearchParameters};
use crate::prompt::{default_max_bytes, PromptData}; use crate::prompt::PromptData;
use crate::update::Setting;
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Deserr, ToSchema)] #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Deserr, ToSchema)]
#[serde(deny_unknown_fields, rename_all = "camelCase")] #[serde(deny_unknown_fields, rename_all = "camelCase")]
#[deserr(error = JsonError, deny_unknown_fields, rename_all = camelCase)] #[deserr(error = JsonError, deny_unknown_fields, rename_all = camelCase)]
pub struct ChatSettings { pub struct ChatSettings {
#[serde(default, skip_serializing_if = "Setting::is_not_set")] pub description: String,
#[deserr(default)]
#[schema(value_type = Option<String>)]
pub description: Setting<String>,
/// A liquid template used to render documents to a text that can be embedded. /// A liquid template used to render documents to a text that can be embedded.
/// ///
/// Meillisearch interpolates the template for each document and sends the resulting text to the embedder. /// Meillisearch interpolates the template for each document and sends the resulting text to the embedder.
/// The embedder then generates document vectors based on this text. /// The embedder then generates document vectors based on this text.
#[serde(default, skip_serializing_if = "Setting::is_not_set")] pub document_template: String,
#[deserr(default)]
#[schema(value_type = Option<String>)]
pub document_template: Setting<String>,
/// Rendered texts are truncated to this size. Defaults to 400. /// Rendered texts are truncated to this size. Defaults to 400.
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[deserr(default)] #[deserr(default)]
#[schema(value_type = Option<usize>)] #[schema(value_type = Option<usize>)]
pub document_template_max_bytes: Setting<usize>, pub document_template_max_bytes: Option<NonZeroUsize>,
/// The search parameters to use for the LLM. /// The search parameters to use for the LLM.
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[deserr(default)] #[deserr(default)]
#[schema(value_type = Option<ChatSearchParams>)] #[schema(value_type = Option<ChatSearchParams>)]
pub search_parameters: Setting<ChatSearchParams>, pub search_parameters: Option<ChatSearchParams>,
} }
impl From<ChatConfig> for ChatSettings { impl From<ChatConfig> for ChatSettings {
@ -49,12 +43,10 @@ impl From<ChatConfig> for ChatSettings {
search_parameters, search_parameters,
} = config; } = config;
ChatSettings { ChatSettings {
description: Setting::Set(description), description,
document_template: Setting::Set(template), document_template: template,
document_template_max_bytes: Setting::Set( document_template_max_bytes: max_bytes,
max_bytes.unwrap_or(default_max_bytes()).get(), search_parameters: {
),
search_parameters: Setting::Set({
let SearchParameters { let SearchParameters {
hybrid, hybrid,
limit, limit,
@ -65,20 +57,68 @@ impl From<ChatConfig> for ChatSettings {
ranking_score_threshold, ranking_score_threshold,
} = search_parameters; } = search_parameters;
let hybrid = hybrid.map(|index::HybridQuery { semantic_ratio, embedder }| { if hybrid.is_none() &&
HybridQuery { semantic_ratio: SemanticRatio(semantic_ratio), embedder } limit.is_none() &&
}); sort.is_none() &&
distinct.is_none() &&
ChatSearchParams { matching_strategy.is_none() &&
hybrid: Setting::some_or_not_set(hybrid), attributes_to_search_on.is_none() &&
limit: Setting::some_or_not_set(limit), ranking_score_threshold.is_none()
sort: Setting::some_or_not_set(sort), {
distinct: Setting::some_or_not_set(distinct), None
matching_strategy: Setting::some_or_not_set(matching_strategy), } else {
attributes_to_search_on: Setting::some_or_not_set(attributes_to_search_on), Some(ChatSearchParams {
ranking_score_threshold: Setting::some_or_not_set(ranking_score_threshold), hybrid: hybrid.map(|h| HybridQuery {
} semantic_ratio: SemanticRatio(h.semantic_ratio),
embedder: h.embedder,
}), }),
limit,
sort,
distinct,
matching_strategy,
attributes_to_search_on,
ranking_score_threshold,
})
}
},
}
}
}
impl From<ChatSettings> for ChatConfig {
fn from(settings: ChatSettings) -> Self {
let ChatSettings {
description,
document_template,
document_template_max_bytes,
search_parameters,
} = settings;
let prompt = PromptData {
template: document_template,
max_bytes: document_template_max_bytes,
};
let search_parameters = match search_parameters {
Some(params) => SearchParameters {
hybrid: params.hybrid.map(|h| crate::index::HybridQuery {
semantic_ratio: h.semantic_ratio.0,
embedder: h.embedder,
}),
limit: params.limit,
sort: params.sort,
distinct: params.distinct,
matching_strategy: params.matching_strategy,
attributes_to_search_on: params.attributes_to_search_on,
ranking_score_threshold: params.ranking_score_threshold,
},
None => SearchParameters::default(),
};
ChatConfig {
description,
prompt,
search_parameters,
} }
} }
} }
@ -87,40 +127,40 @@ impl From<ChatConfig> for ChatSettings {
#[serde(deny_unknown_fields, rename_all = "camelCase")] #[serde(deny_unknown_fields, rename_all = "camelCase")]
#[deserr(error = JsonError, deny_unknown_fields, rename_all = camelCase)] #[deserr(error = JsonError, deny_unknown_fields, rename_all = camelCase)]
pub struct ChatSearchParams { pub struct ChatSearchParams {
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[deserr(default)] #[deserr(default)]
#[schema(value_type = Option<HybridQuery>)] #[schema(value_type = Option<HybridQuery>)]
pub hybrid: Setting<HybridQuery>, pub hybrid: Option<HybridQuery>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[deserr(default = Setting::Set(20))] #[deserr(default)]
#[schema(value_type = Option<usize>)] #[schema(value_type = Option<usize>)]
pub limit: Setting<usize>, pub limit: Option<usize>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[deserr(default)] #[deserr(default)]
#[schema(value_type = Option<Vec<String>>)] #[schema(value_type = Option<Vec<String>>)]
pub sort: Setting<Vec<String>>, pub sort: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[deserr(default)] #[deserr(default)]
#[schema(value_type = Option<String>)] #[schema(value_type = Option<String>)]
pub distinct: Setting<String>, pub distinct: Option<String>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[deserr(default)] #[deserr(default)]
#[schema(value_type = Option<MatchingStrategy>)] #[schema(value_type = Option<MatchingStrategy>)]
pub matching_strategy: Setting<MatchingStrategy>, pub matching_strategy: Option<MatchingStrategy>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[deserr(default)] #[deserr(default)]
#[schema(value_type = Option<Vec<String>>)] #[schema(value_type = Option<Vec<String>>)]
pub attributes_to_search_on: Setting<Vec<String>>, pub attributes_to_search_on: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")] #[serde(default, skip_serializing_if = "Option::is_none")]
#[deserr(default)] #[deserr(default)]
#[schema(value_type = Option<RankingScoreThreshold>)] #[schema(value_type = Option<RankingScoreThreshold>)]
pub ranking_score_threshold: Setting<RankingScoreThreshold>, pub ranking_score_threshold: Option<RankingScoreThreshold>,
} }
#[derive(Debug, Clone, Default, Deserr, ToSchema, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Default, Deserr, ToSchema, PartialEq, Serialize, Deserialize)]

View File

@ -1304,101 +1304,10 @@ impl<'a, 't, 'i> Settings<'a, 't, 'i> {
fn update_chat_config(&mut self) -> Result<bool> { fn update_chat_config(&mut self) -> Result<bool> {
match &mut self.chat { match &mut self.chat {
Setting::Set(ChatSettings { Setting::Set(settings) => {
description: new_description,
document_template: new_document_template,
document_template_max_bytes: new_document_template_max_bytes,
search_parameters: new_search_parameters,
}) => {
let ChatConfig { description, prompt, search_parameters } =
self.index.chat_config(self.wtxn)?;
let description = match new_description {
Setting::Set(new) => new.clone(),
Setting::Reset => Default::default(),
Setting::NotSet => description,
};
let prompt = PromptData {
template: match new_document_template {
Setting::Set(new) => new.clone(),
Setting::Reset => default_template_text().to_string(),
Setting::NotSet => prompt.template.clone(),
},
max_bytes: match new_document_template_max_bytes {
Setting::Set(m) => Some(
NonZeroUsize::new(*m)
.ok_or(InvalidChatSettingsDocumentTemplateMaxBytes)?,
),
Setting::Reset => Some(default_max_bytes()),
Setting::NotSet => prompt.max_bytes,
},
};
let search_parameters = match new_search_parameters {
Setting::Set(sp) => {
let ChatSearchParams {
hybrid,
limit,
sort,
distinct,
matching_strategy,
attributes_to_search_on,
ranking_score_threshold,
} = sp;
SearchParameters {
hybrid: match hybrid {
Setting::Set(hybrid) => Some(crate::index::HybridQuery {
semantic_ratio: *hybrid.semantic_ratio,
embedder: hybrid.embedder.clone(),
}),
Setting::Reset => None,
Setting::NotSet => search_parameters.hybrid.clone(),
},
limit: match limit {
Setting::Set(limit) => Some(*limit),
Setting::Reset => None,
Setting::NotSet => search_parameters.limit,
},
sort: match sort {
Setting::Set(sort) => Some(sort.clone()),
Setting::Reset => None,
Setting::NotSet => search_parameters.sort.clone(),
},
distinct: match distinct {
Setting::Set(distinct) => Some(distinct.clone()),
Setting::Reset => None,
Setting::NotSet => search_parameters.distinct.clone(),
},
matching_strategy: match matching_strategy {
Setting::Set(matching_strategy) => Some(*matching_strategy),
Setting::Reset => None,
Setting::NotSet => search_parameters.matching_strategy,
},
attributes_to_search_on: match attributes_to_search_on {
Setting::Set(attributes_to_search_on) => {
Some(attributes_to_search_on.clone())
}
Setting::Reset => None,
Setting::NotSet => {
search_parameters.attributes_to_search_on.clone()
}
},
ranking_score_threshold: match ranking_score_threshold {
Setting::Set(rst) => Some(*rst),
Setting::Reset => None,
Setting::NotSet => search_parameters.ranking_score_threshold,
},
}
}
Setting::Reset => Default::default(),
Setting::NotSet => search_parameters,
};
self.index.put_chat_config( self.index.put_chat_config(
self.wtxn, self.wtxn,
&ChatConfig { description, prompt, search_parameters }, &settings.clone().into(),
)?; )?;
Ok(true) Ok(true)