spring-projects / spring-projects/spring-ai

OllamaEmbeddingModel - Duration validation not aligned with the Ollama Docs

Open
#2,197 1 comment 1 reaction 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

Bug description
In the documentation of Ollama ( https://github.com/ollama/ollama/blob/main/docs/faq.md#how-do-i-keep-a-model-loaded-in-memory-or-make-it-unload-immediately ) its clearly stated, that if i want a model to stay in memory, a negative value should be presented. Here the citation:
any negative number which will keep the model loaded in memory (e.g. -1 or "-1m")

Therefore i would expect, that setting the property spring.ai.ollama.embedding.options.keep-alive=-1 would keep it in memory.
But in reality, the org.springframework.ai.ollama.OllamaEmbeddingModel.DurationParser uses the following regex: (\d+)(ms|s|m|h). The value of -1 violates it in two ways:

  • a negative value is generally not allowed
  • a value without a duration is not allowed

Environment
spring-ai-ollama-1.0.0-SNAPSHOT

Steps to reproduce
Set the property to spring.ai.ollama.embedding.options.keep-alive=-1 and make a embedding request.

Expected behavior
The model is kept in memory and no exception is raised.

Minimal Complete Reproducible example
The following test class is written in Groovy using the Spock framework.
Based on the documentation of the Ollama as state above, i would expect that all of these tests would pass, but only the first one passes.
org.spockframework:spock-core:2.4-M1-groovy-4.0

import org.springframework.ai.ollama.OllamaEmbeddingModel
import spock.lang.Specification

class OllamaEmbeddingModelDurationParserSpec extends Specification {
    OllamaEmbeddingModel.DurationParser parser = new OllamaEmbeddingModel.DurationParser()

    def "the parser parses a positive number with duration"() {
        when:
        def result = parser.parse("1m")
        then:
        noExceptionThrown()
        result != null
    }

    def "the parser parses a negative number with duration"() {
        when:
        def result = parser.parse("-1m")
        then:
        noExceptionThrown()
        result != null
    }

    def "the parser parses a positive number without duration"() {
        when:
        def result = parser.parse("1")
        then:
        noExceptionThrown()
        result != null
    }

    def "the parser parses a negative number without duration"() {
        when:
        def result = parser.parse("-1")
        then:
        noExceptionThrown()
        result != null
    }
}

Output from the test (Exceptions only):


Expected no exception to be thrown, but got 'java.lang.IllegalArgumentException'

	at spock.lang.Specification.noExceptionThrown(Specification.java:118)
	at com.pineapple.netnix.OllamaEmbeddingModelDurationParserSpec.the parser parses a negative number with duration(Test.groovy:21)
Caused by: java.lang.IllegalArgumentException: Invalid duration format: -1m
	at org.springframework.ai.ollama.OllamaEmbeddingModel$DurationParser.parse(OllamaEmbeddingModel.java:214)
	at com.pineapple.netnix.OllamaEmbeddingModelDurationParserSpec.the parser parses a negative number with duration(Test.groovy:19)


Expected no exception to be thrown, but got 'java.lang.IllegalArgumentException'

	at spock.lang.Specification.noExceptionThrown(Specification.java:118)
	at com.pineapple.netnix.OllamaEmbeddingModelDurationParserSpec.the parser parses a positive number without duration(Test.groovy:29)
Caused by: java.lang.IllegalArgumentException: Invalid duration format: 1
	at org.springframework.ai.ollama.OllamaEmbeddingModel$DurationParser.parse(OllamaEmbeddingModel.java:214)
	at com.pineapple.netnix.OllamaEmbeddingModelDurationParserSpec.the parser parses a positive number without duration(Test.groovy:27)


Expected no exception to be thrown, but got 'java.lang.IllegalArgumentException'

	at spock.lang.Specification.noExceptionThrown(Specification.java:118)
	at com.pineapple.netnix.OllamaEmbeddingModelDurationParserSpec.the parser parses a negative number without duration(Test.groovy:37)
Caused by: java.lang.IllegalArgumentException: Invalid duration format: -1
	at org.springframework.ai.ollama.OllamaEmbeddingModel$DurationParser.parse(OllamaEmbeddingModel.java:214)
	at com.pineapple.netnix.OllamaEmbeddingModelDurationParserSpec.the parser parses a negative number without duration(Test.groovy:35)


Potential side discussion
I know this is not directly related to the bug, but maybe the name of the property might be misleading. Without reading the documentation, i would expect that spring.ai.ollama.embedding.options.keep-alive takes a boolean, defining if it should keep it in memory, or not. Of course it makes more sense to define a duration here, but in that case i would suggest a rename of the property to keep-alive-duration

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 with org.springframework.ai.ollama.OllamaEmbeddingModel.DurationParser, identified at OllamaEmbeddingModel.java:214, and run the supplied Spock specification against the four duration forms. Done means negative and unitless values parse without IllegalArgumentException and the -1 embedding request keeps the model loaded.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, ollama
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.