Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectors only support f32 - should also support f64 #92

Open
jpmcb opened this issue Mar 23, 2024 · 3 comments
Open

Vectors only support f32 - should also support f64 #92

jpmcb opened this issue Mar 23, 2024 · 3 comments

Comments

@jpmcb
Copy link

jpmcb commented Mar 23, 2024

I noticed that the vectors only support f32 floats:

rust-client/src/qdrant.rs

Lines 2037 to 2042 in cd7ee0f

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Vector {
#[prost(float, repeated, tag = "1")]
pub data: ::prost::alloc::vec::Vec<f32>,
}

and I ran into an issue attempting to load PointStructs that are of Vec<f64> (using embeddings generated from an ollama rust client.

/// An embeddings generation response from Ollama.
#[derive(Debug, Deserialize, Clone)]
pub struct GenerateEmbeddingsResponse {
    #[serde(rename = "embedding")]
    #[allow(dead_code)]
    pub embeddings: Vec<f64>,
}

https://github.com/pepperoni21/ollama-rs/blob/f38634d33edd2ccdb440816aa866e5614a8a1983/src/generation/embeddings.rs#L54-L60

Trying to use this vec f64 results in:

error[E0277]: the trait bound `qdrant_client::qdrant::Vectors: From<Vec<f64>>` is not satisfied
    --> src/main.rs:99:42
     |
99   |     let point = vec![PointStruct::new(0, embedding_resp.embeddings, payload)];
     |                      ----------------    ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Vec<f64>>` is not implemented for `qdrant_client::qdrant::Vectors`
     |                      |
     |                      required by a bound introduced by this call
     |
     = help: the following other types implement trait `From<T>`:
               <qdrant_client::qdrant::Vectors as From<HashMap<std::string::String, qdrant_client::qdrant::Vector>>>
               <qdrant_client::qdrant::Vectors as From<HashMap<std::string::String, Vec<f32>>>>
               <qdrant_client::qdrant::Vectors as From<HashMap<std::string::String, Vec<(u32, f32)>>>>
               <qdrant_client::qdrant::Vectors as From<HashMap<std::string::String, &[(u32, f32)]>>>
               <qdrant_client::qdrant::Vectors as From<Vec<f32>>>
     = note: required for `Vec<f64>` to implement `Into<qdrant_client::qdrant::Vectors>`
note: required by a bound in `client::<impl qdrant_client::prelude::PointStruct>::new`
    --> /home/jmcb/.cargo/registry/src/index.crates.io-6f17d22bba15001f/qdrant-client-1.8.0/src/client.rs:1927:54
     |
1927 |     pub fn new(id: impl Into<PointId>, vectors: impl Into<Vectors>, payload: Payload) -> Self {
     |                                                      ^^^^^^^^^^^^^ required by this bound in `client::<impl PointStruct>::new`

For more information about this error, try `rustc --explain E0277`.
warning: `nvim-llama-qdrant-ctl` (bin "nvim-llama-qdrant-ctl") generated 1 warning
error: could not compile `nvim-llama-qdrant-ctl` (bin "nvim-llama-qdrant-ctl") due to 1 previous error; 1 warning emitted


Steps to reproduce

  1. Bootstrap qdrant client, collection, etc.
let qdrant_client = QdrantClient::from_url("http://localhost:6334").build()?;
let collection_name = "test";

qdrant_client
    .create_collection(&CreateCollection {
        collection_name: collection_name.into(),
        vectors_config: Some(VectorsConfig {
            config: Some(Config::Params(VectorParams {
                size:  384, // size of the all-minilm embeddings
                distance: Distance::Cosine.into(),
                ..Default::default()
            })),
        }),
        ..Default::default()
    })
    .await?;
  1. Bootstrap olllama client:
let ollama = Ollama::default();
let ollama = Ollama::new("http://localhost".to_string(), 11434);
  1. Generate an embedding using the ollama client - this is a Vec<f64>
let embedding_model = "all-minilm".to_string();
let embedding_prompt = "Woof woof, bark bark".to_string();
let embedding_resp = ollama
    .generate_embeddings(embedding_model, embedding_prompt, None)
    .await
    .unwrap();
  1. Attempt to use it in a PointStruct and notice error.
let payload: Payload = json!(
    {
        "test": "this is a test",
    }
)
    .try_into()
    .unwrap();

let point = vec![PointStruct::new(0, embedding_resp.embeddings, payload)];
client
    .upsert_points_blocking(collection_name, None, point, None)
    .await?;
@timvisee
Copy link
Member

You're right. Qdrant only supports f32 vectors at this point.

Search accuracy should be more than good enough with f32 floats, increasing with number of dimensions.

Do you have a specific use case in which you require f64 floats?

@jpmcb
Copy link
Author

jpmcb commented Mar 27, 2024

Do you have a specific use case in which you require f64 floats?

Not really I suppose.

I ended up just iterating the vec and converting them to f32 which seems fine.

But, I'd love if Ollama and Qdrant were easily plug and play since my main use case is building a local first RAG type application (and both Ollama / Qdrant running in Docker are perfect for this). Is the solution that the ollama-rs library needs to support f32 vecs? Or maybe there's a glue library that needs some thought?

Open to any / all suggestions!

@generall
Copy link
Member

I would rather request Ollama to provide f32, as it is what the industry is most commonly use nowadays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants