spring-projects / spring-projects/spring-ai

Add support for 'halfvec' for vector embeddings and distance functions in PgVectorStore

Open
#2,903 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Expected Behavior

When using the PgVector vector database (PgVectorStore) functionality I was hoping to use the halfvec type due to requirements needed from my local model.

Current Behavior

Currently only the vector type and distance functions supporting said type are supported

Context

Using the newer gemma models their embedded vector lengths are upward of 2000 (which is the limit of the vector column type which is used thoughout the org.springframework.ai.vectorstore.pgvector.PgVectorStore and related documentation.

I can work around this by explicitly creating the schema via:

CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS vector_store (
  id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
  content text,
  metadata json,
  embedding halfvec(2560)
);
CREATE INDEX ON vector_store USING HNSW (embedding halfvec_l2_ops);

But cannot use the database as the PgVectorStore.PgDistanceType enumeration doesn't provide the appropriate distance type functions for these specific column types.

Ive explicitly modified the code for my case to include the appropriate enum

public static enum PgDistanceType {
    HALFVEC_EUCLIDEAN_DISTANCE("<->", "halfvec_l2_ops", "SELECT *, embedding <-> ? AS distance FROM %s WHERE embedding <-> ? < ? %s ORDER BY distance LIMIT ? "),
...

Which allowed me to overcome the issue but perhaps should be added to the enum; perhaps along with other types that may need to be supported here too:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at PgVectorStore.PgDistanceType and compare its distance-function SQL with the halfvec schema and operators shown in the issue. Add the required halfvec distance support and verify that PgVectorStore can query the provided halfvec(2560) schema using the appropriate HNSW operator class.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, postgresql
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.