|
21 | 21 | import org.springframework.ai.ollama.api.OllamaApi.DeleteModelRequest; |
22 | 22 | import org.springframework.ai.ollama.api.OllamaApi.ListModelResponse; |
23 | 23 | import org.springframework.ai.ollama.api.OllamaApi.PullModelRequest; |
| 24 | +import org.springframework.util.Assert; |
24 | 25 | import org.springframework.util.CollectionUtils; |
25 | 26 | import reactor.util.retry.Retry; |
26 | 27 |
|
@@ -55,14 +56,30 @@ public OllamaModelManager(OllamaApi ollamaApi, ModelManagementOptions options) { |
55 | 56 | } |
56 | 57 |
|
57 | 58 | public boolean isModelAvailable(String modelName) { |
| 59 | + Assert.hasText(modelName, "modelName must not be empty"); |
58 | 60 | ListModelResponse listModelResponse = ollamaApi.listModels(); |
59 | 61 | if (!CollectionUtils.isEmpty(listModelResponse.models())) { |
60 | | - // Not an equality check to support the implicit ":latest" tag. |
61 | | - return listModelResponse.models().stream().anyMatch(m -> m.name().contains(modelName)); |
| 62 | + var normalizedModel = normalizeModelName(modelName); |
| 63 | + return listModelResponse.models().stream().anyMatch(m -> m.name().equals(normalizedModel)); |
62 | 64 | } |
63 | 65 | return false; |
64 | 66 | } |
65 | 67 |
|
| 68 | + /** |
| 69 | + * If the name starts with "hf.co" or "huggingface.co", leave it as is. If the name |
| 70 | + * follows the format "<string>:<string>", leave it as is. If the name follows the |
| 71 | + * format "<string>" and doesn't include any ":" sign, then add ":latest" as a suffix. |
| 72 | + */ |
| 73 | + private String normalizeModelName(String modelName) { |
| 74 | + if (modelName.startsWith("hf.co") || modelName.startsWith("huggingface.co")) { |
| 75 | + return modelName; |
| 76 | + } |
| 77 | + if (modelName.contains(":")) { |
| 78 | + return modelName; |
| 79 | + } |
| 80 | + return modelName + ":latest"; |
| 81 | + } |
| 82 | + |
66 | 83 | public void deleteModel(String modelName) { |
67 | 84 | logger.info("Start deletion of model: {}", modelName); |
68 | 85 | if (!isModelAvailable(modelName)) { |
|
0 commit comments