From 74c180267ef1a32f9ca8943b762edfb78afab669 Mon Sep 17 00:00:00 2001 From: Louis Dureuil Date: Wed, 7 Feb 2024 11:03:00 +0100 Subject: [PATCH] pass dimensions only when defined --- milli/src/vector/openai.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/milli/src/vector/openai.rs b/milli/src/vector/openai.rs index 8712c7894..9deb2e2da 100644 --- a/milli/src/vector/openai.rs +++ b/milli/src/vector/openai.rs @@ -271,11 +271,7 @@ impl Embedder { let request = OpenAiRequest { model: self.options.embedding_model.name(), input: texts, - dimension: if self.options.embedding_model.supports_overriding_dimensions() { - self.options.dimensions.as_ref() - } else { - None - }, + dimensions: self.overriden_dimensions(), }; let response = client .post(OPENAI_EMBEDDINGS_URL) @@ -360,8 +356,11 @@ impl Embedder { tokens: &[usize], client: &reqwest::Client, ) -> Result { - let request = - OpenAiTokensRequest { model: self.options.embedding_model.name(), input: tokens }; + let request = OpenAiTokensRequest { + model: self.options.embedding_model.name(), + input: tokens, + dimensions: self.overriden_dimensions(), + }; let response = client .post(OPENAI_EMBEDDINGS_URL) .json(&request) @@ -414,6 +413,14 @@ impl Embedder { pub fn distribution(&self) -> Option { self.options.embedding_model.distribution() } + + fn overriden_dimensions(&self) -> Option { + if self.options.embedding_model.supports_overriding_dimensions() { + self.options.dimensions + } else { + None + } + } } // retrying in case of failure @@ -473,13 +480,16 @@ impl Retry { struct OpenAiRequest<'a, S: AsRef + serde::Serialize> { model: &'a str, input: &'a [S], - dimension: Option<&'a usize>, + #[serde(skip_serializing_if = "Option::is_none")] + dimensions: Option, } #[derive(Debug, Serialize)] struct OpenAiTokensRequest<'a> { model: &'a str, input: &'a [usize], + #[serde(skip_serializing_if = "Option::is_none")] + dimensions: Option, } #[derive(Debug, Deserialize)]