From 88d03c56ab479a1ad0710a368f6735e171017f92 Mon Sep 17 00:00:00 2001 From: Louis Dureuil Date: Wed, 7 Feb 2024 11:48:47 +0100 Subject: [PATCH] Don't accept dimensions of 0 (ever) or dimensions greater than the default dimensions of the model --- meilisearch-types/src/error.rs | 2 ++ milli/src/error.rs | 9 +++++++++ milli/src/update/settings.rs | 17 +++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index 796eb5713..1b54e77c0 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -348,6 +348,8 @@ impl ErrorCode for milli::Error { | UserError::MissingFieldForSource { .. } | UserError::InvalidOpenAiModel { .. } | UserError::InvalidOpenAiModelDimensions { .. } + | UserError::InvalidOpenAiModelDimensionsMax { .. } + | UserError::InvalidSettingsDimensions { .. } | UserError::InvalidPrompt(_) => Code::InvalidSettingsEmbedders, UserError::TooManyEmbedders(_) => Code::InvalidSettingsEmbedders, UserError::InvalidPromptForEmbeddings(..) => Code::InvalidSettingsEmbedders, diff --git a/milli/src/error.rs b/milli/src/error.rs index 9cb984db1..1147085dd 100644 --- a/milli/src/error.rs +++ b/milli/src/error.rs @@ -234,6 +234,15 @@ only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and undersco dimensions: usize, expected_dimensions: usize, }, + #[error("`.embedders.{embedder_name}.dimensions`: Model `{model}` does not support overriding its dimensions to a value higher than {max_dimensions}. Found {dimensions}")] + InvalidOpenAiModelDimensionsMax { + embedder_name: String, + model: &'static str, + dimensions: usize, + max_dimensions: usize, + }, + #[error("`.embedders.{embedder_name}.dimensions`: `dimensions` cannot be zero")] + InvalidSettingsDimensions { embedder_name: String }, } impl From for Error { diff --git a/milli/src/update/settings.rs b/milli/src/update/settings.rs index b8289626b..a3ba42119 100644 --- a/milli/src/update/settings.rs +++ b/milli/src/update/settings.rs @@ -1122,6 +1122,14 @@ pub fn validate_embedding_settings( let Setting::Set(settings) = settings else { return Ok(settings) }; let EmbeddingSettings { source, model, revision, api_key, dimensions, document_template } = settings; + + if let Some(0) = dimensions.set() { + return Err(crate::error::UserError::InvalidSettingsDimensions { + embedder_name: name.to_owned(), + } + .into()); + } + let Some(inferred_source) = source.set() else { return Ok(Setting::Set(EmbeddingSettings { source, @@ -1153,6 +1161,15 @@ pub fn validate_embedding_settings( } .into()); } + if dimensions > model.default_dimensions() { + return Err(crate::error::UserError::InvalidOpenAiModelDimensionsMax { + embedder_name: name.to_owned(), + model: model.name(), + dimensions, + max_dimensions: model.default_dimensions(), + } + .into()); + } } } }