2023-12-13 15:38:44 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-03-27 11:50:22 +01:00
|
|
|
use deserr::{DeserializeError, Deserr};
|
2024-03-25 10:05:38 +01:00
|
|
|
use ordered_float::OrderedFloat;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2023-11-15 15:46:37 +01:00
|
|
|
use self::error::{EmbedError, NewEmbedderError};
|
2023-12-13 15:38:44 +01:00
|
|
|
use crate::prompt::{Prompt, PromptData};
|
2024-04-24 16:40:12 +02:00
|
|
|
use crate::ThreadPoolNoAbort;
|
2023-11-15 15:46:37 +01:00
|
|
|
|
|
|
|
pub mod error;
|
|
|
|
pub mod hf;
|
2023-12-12 21:19:48 +01:00
|
|
|
pub mod manual;
|
2023-11-15 15:46:37 +01:00
|
|
|
pub mod openai;
|
2024-05-14 11:22:16 +02:00
|
|
|
pub mod parsed_vectors;
|
2023-11-15 15:46:37 +01:00
|
|
|
pub mod settings;
|
|
|
|
|
2024-03-20 10:08:28 +01:00
|
|
|
pub mod ollama;
|
2024-03-14 14:44:43 +01:00
|
|
|
pub mod rest;
|
2024-03-20 10:08:28 +01:00
|
|
|
|
2023-11-15 15:46:37 +01:00
|
|
|
pub use self::error::Error;
|
|
|
|
|
|
|
|
pub type Embedding = Vec<f32>;
|
|
|
|
|
2024-03-19 15:41:37 +01:00
|
|
|
pub const REQUEST_PARALLELISM: usize = 40;
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// One or multiple embeddings stored consecutively in a flat vector.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub struct Embeddings<F> {
|
|
|
|
data: Vec<F>,
|
|
|
|
dimension: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> Embeddings<F> {
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Declares an empty vector of embeddings of the specified dimensions.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn new(dimension: usize) -> Self {
|
|
|
|
Self { data: Default::default(), dimension }
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Declares a vector of embeddings containing a single element.
|
|
|
|
///
|
|
|
|
/// The dimension is inferred from the length of the passed embedding.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn from_single_embedding(embedding: Vec<F>) -> Self {
|
|
|
|
Self { dimension: embedding.len(), data: embedding }
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Declares a vector of embeddings from its components.
|
|
|
|
///
|
|
|
|
/// `data.len()` must be a multiple of `dimension`, otherwise an error is returned.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn from_inner(data: Vec<F>, dimension: usize) -> Result<Self, Vec<F>> {
|
|
|
|
let mut this = Self::new(dimension);
|
|
|
|
this.append(data)?;
|
|
|
|
Ok(this)
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Returns the number of embeddings in this vector of embeddings.
|
2023-12-12 23:39:01 +01:00
|
|
|
pub fn embedding_count(&self) -> usize {
|
|
|
|
self.data.len() / self.dimension
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Dimension of a single embedding.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn dimension(&self) -> usize {
|
|
|
|
self.dimension
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Deconstructs self into the inner flat vector.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn into_inner(self) -> Vec<F> {
|
|
|
|
self.data
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// A reference to the inner flat vector.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn as_inner(&self) -> &[F] {
|
|
|
|
&self.data
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Iterates over the embeddings contained in the flat vector.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &'_ [F]> + '_ {
|
|
|
|
self.data.as_slice().chunks_exact(self.dimension)
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Push an embedding at the end of the embeddings.
|
|
|
|
///
|
|
|
|
/// If `embedding.len() != self.dimension`, then the push operation fails.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn push(&mut self, mut embedding: Vec<F>) -> Result<(), Vec<F>> {
|
|
|
|
if embedding.len() != self.dimension {
|
|
|
|
return Err(embedding);
|
|
|
|
}
|
|
|
|
self.data.append(&mut embedding);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Append a flat vector of embeddings a the end of the embeddings.
|
|
|
|
///
|
|
|
|
/// If `embeddings.len() % self.dimension != 0`, then the append operation fails.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn append(&mut self, mut embeddings: Vec<F>) -> Result<(), Vec<F>> {
|
|
|
|
if embeddings.len() % self.dimension != 0 {
|
|
|
|
return Err(embeddings);
|
|
|
|
}
|
|
|
|
self.data.append(&mut embeddings);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// An embedder can be used to transform text into embeddings.
|
2023-11-15 15:46:37 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Embedder {
|
2024-03-12 15:00:26 +01:00
|
|
|
/// An embedder based on running local models, fetched from the Hugging Face Hub.
|
2023-11-15 15:46:37 +01:00
|
|
|
HuggingFace(hf::Embedder),
|
2024-03-12 15:00:26 +01:00
|
|
|
/// An embedder based on making embedding queries against the OpenAI API.
|
2024-03-19 15:41:37 +01:00
|
|
|
OpenAi(openai::Embedder),
|
2024-03-12 15:00:26 +01:00
|
|
|
/// An embedder based on the user providing the embeddings in the documents and queries.
|
2023-12-12 21:19:48 +01:00
|
|
|
UserProvided(manual::Embedder),
|
2024-03-25 10:05:38 +01:00
|
|
|
/// An embedder based on making embedding queries against an <https://ollama.com> embedding server.
|
2024-03-20 10:08:28 +01:00
|
|
|
Ollama(ollama::Embedder),
|
2024-03-25 10:05:38 +01:00
|
|
|
/// An embedder based on making embedding queries against a generic JSON/REST embedding server.
|
|
|
|
Rest(rest::Embedder),
|
2023-11-15 15:46:37 +01:00
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Configuration for an embedder.
|
2023-11-15 15:46:37 +01:00
|
|
|
#[derive(Debug, Clone, Default, serde::Deserialize, serde::Serialize)]
|
|
|
|
pub struct EmbeddingConfig {
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Options of the embedder, specific to each kind of embedder
|
2023-11-15 15:46:37 +01:00
|
|
|
pub embedder_options: EmbedderOptions,
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Document template
|
2023-11-15 15:46:37 +01:00
|
|
|
pub prompt: PromptData,
|
|
|
|
// TODO: add metrics and anything needed
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Map of embedder configurations.
|
|
|
|
///
|
|
|
|
/// Each configuration is mapped to a name.
|
2023-12-13 15:38:44 +01:00
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct EmbeddingConfigs(HashMap<String, (Arc<Embedder>, Arc<Prompt>)>);
|
|
|
|
|
|
|
|
impl EmbeddingConfigs {
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Create the map from its internal component.s
|
2023-12-13 15:38:44 +01:00
|
|
|
pub fn new(data: HashMap<String, (Arc<Embedder>, Arc<Prompt>)>) -> Self {
|
|
|
|
Self(data)
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Get an embedder configuration and template from its name.
|
2023-12-13 15:38:44 +01:00
|
|
|
pub fn get(&self, name: &str) -> Option<(Arc<Embedder>, Arc<Prompt>)> {
|
|
|
|
self.0.get(name).cloned()
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Get the default embedder configuration, if any.
|
2023-12-13 15:38:44 +01:00
|
|
|
pub fn get_default(&self) -> Option<(Arc<Embedder>, Arc<Prompt>)> {
|
2024-03-28 11:49:00 +01:00
|
|
|
self.get(self.get_default_embedder_name())
|
2023-12-13 15:38:44 +01:00
|
|
|
}
|
|
|
|
|
2024-05-14 11:42:26 +02:00
|
|
|
pub fn inner_as_ref(&self) -> &HashMap<String, (Arc<Embedder>, Arc<Prompt>)> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
|
2024-06-12 14:02:12 +02:00
|
|
|
pub fn into_inner(self) -> HashMap<String, (Arc<Embedder>, Arc<Prompt>)> {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Get the name of the default embedder configuration.
|
|
|
|
///
|
|
|
|
/// The default embedder is determined as follows:
|
|
|
|
///
|
|
|
|
/// - If there is only one embedder, it is always the default.
|
|
|
|
/// - If there are multiple embedders and one of them is called `default`, then that one is the default embedder.
|
|
|
|
/// - In all other cases, there is no default embedder.
|
2024-03-28 11:49:00 +01:00
|
|
|
pub fn get_default_embedder_name(&self) -> &str {
|
2023-12-13 15:38:44 +01:00
|
|
|
let mut it = self.0.keys();
|
|
|
|
let first_name = it.next();
|
|
|
|
let second_name = it.next();
|
|
|
|
match (first_name, second_name) {
|
2024-03-28 11:49:00 +01:00
|
|
|
(None, _) => "default",
|
|
|
|
(Some(first), None) => first,
|
|
|
|
(Some(_), Some(_)) => "default",
|
2023-12-13 15:38:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoIterator for EmbeddingConfigs {
|
|
|
|
type Item = (String, (Arc<Embedder>, Arc<Prompt>));
|
|
|
|
|
|
|
|
type IntoIter = std::collections::hash_map::IntoIter<String, (Arc<Embedder>, Arc<Prompt>)>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Options of an embedder, specific to each kind of embedder.
|
2023-11-15 15:46:37 +01:00
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
|
|
|
|
pub enum EmbedderOptions {
|
|
|
|
HuggingFace(hf::EmbedderOptions),
|
|
|
|
OpenAi(openai::EmbedderOptions),
|
2024-03-20 10:08:28 +01:00
|
|
|
Ollama(ollama::EmbedderOptions),
|
2023-12-12 21:19:48 +01:00
|
|
|
UserProvided(manual::EmbedderOptions),
|
2024-03-25 10:05:38 +01:00
|
|
|
Rest(rest::EmbedderOptions),
|
2023-11-15 15:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for EmbedderOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::HuggingFace(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EmbedderOptions {
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Default options for the Hugging Face embedder
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn huggingface() -> Self {
|
|
|
|
Self::HuggingFace(hf::EmbedderOptions::new())
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Default options for the OpenAI embedder
|
2023-12-12 21:19:48 +01:00
|
|
|
pub fn openai(api_key: Option<String>) -> Self {
|
2023-11-15 15:46:37 +01:00
|
|
|
Self::OpenAi(openai::EmbedderOptions::with_default_model(api_key))
|
|
|
|
}
|
2024-03-20 10:08:28 +01:00
|
|
|
|
2024-03-25 11:50:00 +01:00
|
|
|
pub fn ollama(api_key: Option<String>, url: Option<String>) -> Self {
|
|
|
|
Self::Ollama(ollama::EmbedderOptions::with_default_model(api_key, url))
|
2024-03-20 10:08:28 +01:00
|
|
|
}
|
2023-11-15 15:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Embedder {
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Spawns a new embedder built from its options.
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn new(options: EmbedderOptions) -> std::result::Result<Self, NewEmbedderError> {
|
|
|
|
Ok(match options {
|
|
|
|
EmbedderOptions::HuggingFace(options) => Self::HuggingFace(hf::Embedder::new(options)?),
|
2024-03-19 15:41:37 +01:00
|
|
|
EmbedderOptions::OpenAi(options) => Self::OpenAi(openai::Embedder::new(options)?),
|
2024-03-20 10:08:28 +01:00
|
|
|
EmbedderOptions::Ollama(options) => Self::Ollama(ollama::Embedder::new(options)?),
|
2023-12-12 21:19:48 +01:00
|
|
|
EmbedderOptions::UserProvided(options) => {
|
|
|
|
Self::UserProvided(manual::Embedder::new(options))
|
|
|
|
}
|
2024-03-25 10:05:38 +01:00
|
|
|
EmbedderOptions::Rest(options) => Self::Rest(rest::Embedder::new(options)?),
|
2023-11-15 15:46:37 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Embed one or multiple texts.
|
|
|
|
///
|
|
|
|
/// Each text can be embedded as one or multiple embeddings.
|
2024-03-19 15:41:37 +01:00
|
|
|
pub fn embed(
|
2023-11-15 15:46:37 +01:00
|
|
|
&self,
|
|
|
|
texts: Vec<String>,
|
|
|
|
) -> std::result::Result<Vec<Embeddings<f32>>, EmbedError> {
|
|
|
|
match self {
|
2023-12-12 21:19:48 +01:00
|
|
|
Embedder::HuggingFace(embedder) => embedder.embed(texts),
|
2024-03-14 11:14:31 +01:00
|
|
|
Embedder::OpenAi(embedder) => embedder.embed(texts),
|
2024-03-19 15:41:37 +01:00
|
|
|
Embedder::Ollama(embedder) => embedder.embed(texts),
|
2023-12-12 21:19:48 +01:00
|
|
|
Embedder::UserProvided(embedder) => embedder.embed(texts),
|
2024-03-25 10:05:38 +01:00
|
|
|
Embedder::Rest(embedder) => embedder.embed(texts),
|
2023-11-15 15:46:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 11:49:23 +01:00
|
|
|
pub fn embed_one(&self, text: String) -> std::result::Result<Embedding, EmbedError> {
|
|
|
|
let mut embeddings = self.embed(vec![text])?;
|
|
|
|
let embeddings = embeddings.pop().ok_or_else(EmbedError::missing_embedding)?;
|
|
|
|
Ok(if embeddings.iter().nth(1).is_some() {
|
|
|
|
tracing::warn!("Ignoring embeddings past the first one in long search query");
|
|
|
|
embeddings.iter().next().unwrap().to_vec()
|
|
|
|
} else {
|
|
|
|
embeddings.into_inner()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Embed multiple chunks of texts.
|
|
|
|
///
|
|
|
|
/// Each chunk is composed of one or multiple texts.
|
2024-01-29 11:23:18 +01:00
|
|
|
pub fn embed_chunks(
|
2023-11-15 15:46:37 +01:00
|
|
|
&self,
|
|
|
|
text_chunks: Vec<Vec<String>>,
|
2024-04-24 16:40:12 +02:00
|
|
|
threads: &ThreadPoolNoAbort,
|
2023-11-15 15:46:37 +01:00
|
|
|
) -> std::result::Result<Vec<Vec<Embeddings<f32>>>, EmbedError> {
|
|
|
|
match self {
|
2023-12-12 21:19:48 +01:00
|
|
|
Embedder::HuggingFace(embedder) => embedder.embed_chunks(text_chunks),
|
2024-03-19 15:41:37 +01:00
|
|
|
Embedder::OpenAi(embedder) => embedder.embed_chunks(text_chunks, threads),
|
|
|
|
Embedder::Ollama(embedder) => embedder.embed_chunks(text_chunks, threads),
|
2023-12-12 21:19:48 +01:00
|
|
|
Embedder::UserProvided(embedder) => embedder.embed_chunks(text_chunks),
|
2024-03-25 10:05:38 +01:00
|
|
|
Embedder::Rest(embedder) => embedder.embed_chunks(text_chunks, threads),
|
2023-11-15 15:46:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Indicates the preferred number of chunks to pass to [`Self::embed_chunks`]
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn chunk_count_hint(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
Embedder::HuggingFace(embedder) => embedder.chunk_count_hint(),
|
|
|
|
Embedder::OpenAi(embedder) => embedder.chunk_count_hint(),
|
2024-03-20 10:08:28 +01:00
|
|
|
Embedder::Ollama(embedder) => embedder.chunk_count_hint(),
|
2023-12-12 21:19:48 +01:00
|
|
|
Embedder::UserProvided(_) => 1,
|
2024-03-25 10:05:38 +01:00
|
|
|
Embedder::Rest(embedder) => embedder.chunk_count_hint(),
|
2023-11-15 15:46:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Indicates the preferred number of texts in a single chunk passed to [`Self::embed`]
|
2023-11-15 15:46:37 +01:00
|
|
|
pub fn prompt_count_in_chunk_hint(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
Embedder::HuggingFace(embedder) => embedder.prompt_count_in_chunk_hint(),
|
|
|
|
Embedder::OpenAi(embedder) => embedder.prompt_count_in_chunk_hint(),
|
2024-03-20 10:08:28 +01:00
|
|
|
Embedder::Ollama(embedder) => embedder.prompt_count_in_chunk_hint(),
|
2023-12-12 21:19:48 +01:00
|
|
|
Embedder::UserProvided(_) => 1,
|
2024-03-25 10:05:38 +01:00
|
|
|
Embedder::Rest(embedder) => embedder.prompt_count_in_chunk_hint(),
|
2023-12-12 21:19:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Indicates the dimensions of a single embedding produced by the embedder.
|
2023-12-12 21:19:48 +01:00
|
|
|
pub fn dimensions(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
Embedder::HuggingFace(embedder) => embedder.dimensions(),
|
|
|
|
Embedder::OpenAi(embedder) => embedder.dimensions(),
|
2024-03-20 10:08:28 +01:00
|
|
|
Embedder::Ollama(embedder) => embedder.dimensions(),
|
2023-12-12 21:19:48 +01:00
|
|
|
Embedder::UserProvided(embedder) => embedder.dimensions(),
|
2024-03-25 10:05:38 +01:00
|
|
|
Embedder::Rest(embedder) => embedder.dimensions(),
|
2023-11-15 15:46:37 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-14 16:01:35 +01:00
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// An optional distribution used to apply an affine transformation to the similarity score of a document.
|
2023-12-14 16:01:35 +01:00
|
|
|
pub fn distribution(&self) -> Option<DistributionShift> {
|
|
|
|
match self {
|
|
|
|
Embedder::HuggingFace(embedder) => embedder.distribution(),
|
|
|
|
Embedder::OpenAi(embedder) => embedder.distribution(),
|
2024-03-20 10:08:28 +01:00
|
|
|
Embedder::Ollama(embedder) => embedder.distribution(),
|
2024-03-27 11:50:22 +01:00
|
|
|
Embedder::UserProvided(embedder) => embedder.distribution(),
|
2024-03-25 10:05:38 +01:00
|
|
|
Embedder::Rest(embedder) => embedder.distribution(),
|
2023-12-14 16:01:35 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-15 15:46:37 +01:00
|
|
|
}
|
2023-12-12 10:05:06 +01:00
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Describes the mean and sigma of distribution of embedding similarity in the embedding space.
|
|
|
|
///
|
|
|
|
/// The intended use is to make the similarity score more comparable to the regular ranking score.
|
|
|
|
/// This allows to correct effects where results are too "packed" around a certain value.
|
2024-03-25 10:05:38 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
|
|
|
#[serde(from = "DistributionShiftSerializable")]
|
|
|
|
#[serde(into = "DistributionShiftSerializable")]
|
2023-12-12 10:05:06 +01:00
|
|
|
pub struct DistributionShift {
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Value where the results are "packed".
|
|
|
|
///
|
|
|
|
/// Similarity scores are translated so that they are packed around 0.5 instead
|
2024-03-25 10:05:38 +01:00
|
|
|
pub current_mean: OrderedFloat<f32>,
|
2024-03-12 15:00:26 +01:00
|
|
|
|
|
|
|
/// standard deviation of a similarity score.
|
|
|
|
///
|
|
|
|
/// Set below 0.4 to make the results less packed around the mean, and above 0.4 to make them more packed.
|
2024-03-25 10:05:38 +01:00
|
|
|
pub current_sigma: OrderedFloat<f32>,
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:50:33 +01:00
|
|
|
impl<E> Deserr<E> for DistributionShift
|
|
|
|
where
|
|
|
|
E: DeserializeError,
|
|
|
|
{
|
|
|
|
fn deserialize_from_value<V: deserr::IntoValue>(
|
|
|
|
value: deserr::Value<V>,
|
2024-07-09 17:25:39 +02:00
|
|
|
location: deserr::ValuePointerRef<'_>,
|
2024-03-27 11:50:33 +01:00
|
|
|
) -> Result<Self, E> {
|
|
|
|
let value = DistributionShiftSerializable::deserialize_from_value(value, location)?;
|
|
|
|
if value.mean < 0. || value.mean > 1. {
|
|
|
|
return Err(deserr::take_cf_content(E::error::<std::convert::Infallible>(
|
|
|
|
None,
|
|
|
|
deserr::ErrorKind::Unexpected {
|
|
|
|
msg: format!(
|
|
|
|
"the distribution mean must be in the range [0, 1], got {}",
|
|
|
|
value.mean
|
|
|
|
),
|
|
|
|
},
|
|
|
|
location,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
if value.sigma <= 0. || value.sigma > 1. {
|
|
|
|
return Err(deserr::take_cf_content(E::error::<std::convert::Infallible>(
|
|
|
|
None,
|
|
|
|
deserr::ErrorKind::Unexpected {
|
|
|
|
msg: format!(
|
|
|
|
"the distribution sigma must be in the range ]0, 1], got {}",
|
|
|
|
value.sigma
|
|
|
|
),
|
|
|
|
},
|
|
|
|
location,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Deserr)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
#[deserr(deny_unknown_fields)]
|
2024-03-25 10:05:38 +01:00
|
|
|
struct DistributionShiftSerializable {
|
2024-03-27 11:50:33 +01:00
|
|
|
mean: f32,
|
|
|
|
sigma: f32,
|
2024-03-25 10:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DistributionShift> for DistributionShiftSerializable {
|
|
|
|
fn from(
|
|
|
|
DistributionShift {
|
|
|
|
current_mean: OrderedFloat(current_mean),
|
|
|
|
current_sigma: OrderedFloat(current_sigma),
|
|
|
|
}: DistributionShift,
|
|
|
|
) -> Self {
|
2024-03-27 11:50:33 +01:00
|
|
|
Self { mean: current_mean, sigma: current_sigma }
|
2024-03-25 10:05:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DistributionShiftSerializable> for DistributionShift {
|
2024-03-27 11:50:33 +01:00
|
|
|
fn from(DistributionShiftSerializable { mean, sigma }: DistributionShiftSerializable) -> Self {
|
|
|
|
Self { current_mean: OrderedFloat(mean), current_sigma: OrderedFloat(sigma) }
|
2024-03-25 10:05:38 +01:00
|
|
|
}
|
2023-12-12 10:05:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DistributionShift {
|
|
|
|
/// `None` if sigma <= 0.
|
|
|
|
pub fn new(mean: f32, sigma: f32) -> Option<Self> {
|
|
|
|
if sigma <= 0.0 {
|
|
|
|
None
|
|
|
|
} else {
|
2024-03-25 10:05:38 +01:00
|
|
|
Some(Self { current_mean: OrderedFloat(mean), current_sigma: OrderedFloat(sigma) })
|
2023-12-12 10:05:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shift(&self, score: f32) -> f32 {
|
2024-03-25 10:05:38 +01:00
|
|
|
let current_mean = self.current_mean.0;
|
|
|
|
let current_sigma = self.current_sigma.0;
|
2023-12-12 10:05:06 +01:00
|
|
|
// <https://math.stackexchange.com/a/2894689>
|
|
|
|
// We're somewhat abusively mapping the distribution of distances to a gaussian.
|
|
|
|
// The parameters we're given is the mean and sigma of the native result distribution.
|
|
|
|
// We're using them to retarget the distribution to a gaussian centered on 0.5 with a sigma of 0.4.
|
|
|
|
|
|
|
|
let target_mean = 0.5;
|
|
|
|
let target_sigma = 0.4;
|
|
|
|
|
|
|
|
// a^2 sig1^2 = sig2^2 => a^2 = sig2^2 / sig1^2 => a = sig2 / sig1, assuming a, sig1, and sig2 positive.
|
2024-03-25 10:05:38 +01:00
|
|
|
let factor = target_sigma / current_sigma;
|
2023-12-12 10:05:06 +01:00
|
|
|
// a*mu1 + b = mu2 => b = mu2 - a*mu1
|
2024-03-25 10:05:38 +01:00
|
|
|
let offset = target_mean - (factor * current_mean);
|
2023-12-12 10:05:06 +01:00
|
|
|
|
|
|
|
let mut score = factor * score + offset;
|
|
|
|
|
|
|
|
// clamp the final score in the ]0, 1] interval.
|
|
|
|
if score <= 0.0 {
|
|
|
|
score = f32::EPSILON;
|
|
|
|
}
|
|
|
|
if score > 1.0 {
|
|
|
|
score = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
score
|
|
|
|
}
|
|
|
|
}
|
2024-02-26 10:41:47 +01:00
|
|
|
|
2024-03-12 15:00:26 +01:00
|
|
|
/// Whether CUDA is supported in this version of Meilisearch.
|
2024-02-26 10:41:47 +01:00
|
|
|
pub const fn is_cuda_enabled() -> bool {
|
|
|
|
cfg!(feature = "cuda")
|
|
|
|
}
|
2024-05-28 14:22:19 +02:00
|
|
|
|
|
|
|
pub fn arroy_db_range_for_embedder(embedder_id: u8) -> impl Iterator<Item = u16> {
|
|
|
|
let embedder_id = (embedder_id as u16) << 8;
|
|
|
|
|
|
|
|
(0..=u8::MAX).map(move |k| embedder_id | (k as u16))
|
|
|
|
}
|