Skip to content

ollama - Align keep-alive handling across OllamaEmbeddingModel and OllamaChatModel #4511

@dev-jonghoonpark

Description

@dev-jonghoonpark

Issue
Currently, the keep-alive option is handled inconsistently between models.

  • In OllamaEmbeddingModel, a duration parser is used to process the keep-alive option.
  • In OllamaChatModel, the raw user input is passed directly without parsing.

public static class DurationParser {
private static final Pattern PATTERN = Pattern.compile("(-?\\d+)(ms|s|m|h)");
public static Duration parse(String input) {
if (!StringUtils.hasText(input)) {
return null;
}
Matcher matcher = PATTERN.matcher(input);
if (matcher.matches()) {
long value = Long.parseLong(matcher.group(1));
String unit = matcher.group(2);
return switch (unit) {
case "ms" -> Duration.ofMillis(value);
case "s" -> Duration.ofSeconds(value);
case "m" -> Duration.ofMinutes(value);
case "h" -> Duration.ofHours(value);
default -> throw new IllegalArgumentException("Unsupported time unit: " + unit);
};
}
else {
throw new IllegalArgumentException("Invalid duration format: " + input);
}
}
}

Proposed Solution
For better consistency, we should either:

  1. Remove the duration parser from OllamaEmbeddingModel, or
  2. Apply the duration parser to OllamaChatModel as well.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions