user1640826
user1640826

Reputation: 9

SpringAI: 4096 dimensions for .java files trying to load in PG Vector DB

I have pretty much default settings but i keep getting this on initializing the VectorStore

caused by: org.postgresql.util.PSQLException: ERROR: expected 1596 dimensions, not 4096

spring:
  ai:
    ollama:
      base-url: http://localhost:11434
      embedding:
        options:
          model: llama3
    vectorstore:
        pgvector:
          index-type: HNSW
          distance-type: COSINE_DISTANCE
          dimensions: 1596

My code to load is pretty simple if fails on the add. The vector_store table is created in the DB. Can't figure out why the .add is listing 4096 dimensions

 for (File file : javaFiles) {
                log.info("Loading document: {}", file.getName());

                Resource resource = new FileSystemResource(file);
                TextReader documentReader = new TextReader(resource);
                List<Document> documents = documentReader.get();

                TextSplitter textSplitter = new TokenTextSplitter();
                List<Document> splitDocuments = textSplitter.apply(documents);


                vectorStore.add(splitDocuments);
            }

I am running this logic and it fails giving the 4096 when 1596 is expected. using these dependencies

 <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
    </dependency>

Upvotes: -1

Views: 310

Answers (1)

Gilles
Gilles

Reputation: 311

Actually there is a limitation on PgVector which allows at most 2000 dimensions for HNSW indexes (see spring-ai documentation).

And since you're using llama3 which got 4096 "embedding dimensions" accordingly to what I can see from this reference then it could explains your error.

I guess you could either:

  • choose a different large-language-model which would have less "embedding dimensions" (like gemma:2b for example, and then setup the dimensions column to 384 instead of 1596)
  • or move from PgVector to a vectorial db allowing to store such dimensions (see the spring-ai documentation to search for another db implementations)
  • or wait for PgVector to provide an hypothetical solution in the future ?

Upvotes: 0

Related Questions