spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

ollama embedding cannot set dimensions

Open mlmw92 opened this issue 7 months ago • 7 comments
trafficstars

Please do a quick search on GitHub issues first, there might be already a duplicate issue for the one you are about to create. If the bug is trivial, just go ahead and create the issue. Otherwise, please take a few moments and fill in the following sections:

Bug description

I vectorized the text using ElasticsearchVectorStore and ollama, and found that the embedding request dimensions were not set, which I had set myself but did not take effect

Environment

Spring AI version 1.0.0-SNAPSHOT Spring Boot version 3.4.1

Steps to reproduce

Expected behavior A clear and concise description of what you expected to happen.

Minimal Complete Reproducible example Please provide a failing test or a minimal complete verifiable example that reproduces the issue. Bug reports that are reproducible will take priority in resolution over reports that are not reproducible.

mlmw92 avatar Apr 12 '25 09:04 mlmw92

java.lang.IllegalStateException: [1:9796] failed to parse: The [dense_vector] field [embedding] in doc [document with id 'dfa35621-c788-4c54-adee-85296574386c'] has a different number of dimensions [768] than defined in the mapping [1536] at org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore.doAdd(ElasticsearchVectorStore.java:204) at org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore.lambda$add$1(AbstractObservationVectorStore.java:85) at io.micrometer.observation.Observation.observe(Observation.java:498) at org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore.add(AbstractObservationVectorStore.java:85)

mlmw92 avatar Apr 12 '25 10:04 mlmw92

Could you please share your configuration and related code?

dev-jonghoonpark avatar Apr 15 '25 18:04 dev-jonghoonpark

this is vectorStore and embedding configuration

spring:
  ai:
    vectorstore:
      elasticsearch:
        initialize-schema: true
        index-name: vs-smart-asst
        dimensions: 1536
        similarity: cosine
    ollama:
      base-url: http://127.0.0.1:11434
      embedding:
        options:
          model: 'nomic-embed-text:Q8_0'

mlmw92 avatar Apr 16 '25 09:04 mlmw92

@Bean
    @ConditionalOnMissingBean
    ElasticsearchVectorStore vectorStore(ElasticsearchVectorStoreProperties properties, RestClient restClient,
                                         OllamaEmbeddingModel embeddingModel, ObjectProvider<ObservationRegistry> observationRegistry,
                                         ObjectProvider<VectorStoreObservationConvention> customObservationConvention,
                                         BatchingStrategy batchingStrategy) {
        ElasticsearchVectorStoreOptions elasticsearchVectorStoreOptions = new ElasticsearchVectorStoreOptions();

        if (StringUtils.hasText(properties.getIndexName())) {
            elasticsearchVectorStoreOptions.setIndexName(properties.getIndexName());
        }
        if (properties.getDimensions() != null) {
            elasticsearchVectorStoreOptions.setDimensions(properties.getDimensions());
        }
        if (properties.getSimilarity() != null) {
            elasticsearchVectorStoreOptions.setSimilarity(properties.getSimilarity());
        }

        return ElasticsearchVectorStore.builder(restClient, embeddingModel)
                .options(elasticsearchVectorStoreOptions)
                .initializeSchema(properties.isInitializeSchema())
                .observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
                .customObservationConvention(customObservationConvention.getIfAvailable(() -> null))
                .batchingStrategy(batchingStrategy)
                .build();
    }

mlmw92 avatar Apr 16 '25 10:04 mlmw92

Sorry for the late reply.

It seems that there is no way to set the dimension of the embedding model in ollama. https://github.com/ollama/ollama/issues/651

And the nomic-embed-text embedding model you used (probably a custom one) seems to be 768 Dimension or less. https://docs.nomic.ai/atlas/embeddings-and-retrieval/text-embedding

So it looks like you are getting the error because of the mismatched dimensions.

This part should be implemented in ollama first, after which we can add functionality in spring-ai.

dev-jonghoonpark avatar Apr 21 '25 00:04 dev-jonghoonpark

Thank you for your reply. It was possible in the previous version, but I forgot which version it was. After upgrading to the latest snapshot version, I discovered this issue. Now I have set the dimension to 768 and it can be used normally. Thank you.

mlmw92 avatar Apr 22 '25 01:04 mlmw92