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

Comment From: dev-jonghoonpark

fixed and merged in #2256 Thank you for merging!

@ilayaperumalg