Skip to content

Commit 322d0fb

Browse files
committed
Refactor Ollama implementation and improve documentation
- Enhance OllamaModelPuller with configurable retry timeout - Update test cases to use LLAMA3_1 instead of LLAMA3_2 - Improve Ollama documentation with clearer prerequisites and model pulling instructions - Update Spring AI introduction page with new logo and diagram - Remove unnecessary main method from OllamaModelPuller
1 parent 272e541 commit 322d0fb

File tree

12 files changed

+104
-52
lines changed

12 files changed

+104
-52
lines changed

models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/api/OllamaModelPuller.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ public class OllamaModelPuller {
3535

3636
private OllamaApi ollamaApi;
3737

38+
private final long pullRetryTimeoutMs;
39+
3840
public OllamaModelPuller(OllamaApi ollamaApi) {
41+
this(ollamaApi, 5000);
42+
}
43+
44+
public OllamaModelPuller(OllamaApi ollamaApi, long retryTimeoutMs) {
3945
this.ollamaApi = ollamaApi;
46+
this.pullRetryTimeoutMs = retryTimeoutMs;
4047
}
4148

4249
public boolean isModelAvailable(String modelName) {
@@ -56,35 +63,24 @@ public boolean deleteModel(String modelName) {
5663
return this.ollamaApi.deleteModel(new DeleteModelRequest(modelName)).getStatusCode().equals(HttpStatus.OK);
5764
}
5865

59-
public String pullModel(String modelName, boolean reTry) {
66+
public String pullModel(String modelName, boolean enablePullRetry) {
6067
String status = "";
6168
do {
6269
logger.info("Start Pulling model: {}", modelName);
6370
var progress = this.ollamaApi.pullModel(new PullModelRequest(modelName));
6471
status = progress.status();
6572
logger.info("Pulling model: {} - Status: {}", modelName, status);
73+
6674
try {
67-
Thread.sleep(5000);
75+
Thread.sleep(this.pullRetryTimeoutMs);
6876
}
6977
catch (InterruptedException e) {
7078
e.printStackTrace();
7179
}
7280
}
73-
while (reTry && !status.equals("success"));
74-
return status;
75-
}
81+
while (enablePullRetry && !status.equals("success"));
7682

77-
public static void main(String[] args) {
78-
79-
var utils = new OllamaModelPuller(new OllamaApi());
80-
81-
System.out.println(utils.isModelAvailable("orca-mini:latest"));
82-
83-
String model = "hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q8_0";
84-
85-
if (!utils.isModelAvailable(model)) {
86-
utils.pullModel(model, true);
87-
}
83+
return status;
8884
}
8985

9086
}

models/spring-ai-ollama/src/test/java/org/springframework/ai/ollama/BaseOllamaIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class BaseOllamaIT {
1010
private static final Logger logger = LoggerFactory.getLogger(BaseOllamaIT.class);
1111

1212
// Toggle for running tests locally on native Ollama for a faster feedback loop.
13-
private static final boolean useTestcontainers = false;
13+
private static final boolean useTestcontainers = true;
1414

1515
public static final OllamaContainer ollamaContainer;
1616

models/spring-ai-ollama/src/test/java/org/springframework/ai/ollama/OllamaChatModelFunctionCallingIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class OllamaChatModelFunctionCallingIT extends BaseOllamaIT {
5252

5353
private static final Logger logger = LoggerFactory.getLogger(OllamaChatModelFunctionCallingIT.class);
5454

55-
private static final String MODEL = OllamaModel.LLAMA3_2.getName();
55+
private static final String MODEL = OllamaModel.LLAMA3_1.getName();
5656

5757
@Autowired
5858
ChatModel chatModel;

models/spring-ai-ollama/src/test/java/org/springframework/ai/ollama/api/tool/OllamaApiToolFunctionCallIT.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
@DisabledIf("isDisabled")
4747
public class OllamaApiToolFunctionCallIT extends BaseOllamaIT {
4848

49-
private static final String MODEL = OllamaModel.LLAMA3_2.getName();
49+
private static final String MODEL = OllamaModel.LLAMA3_1.getName();
5050

5151
private static final Logger logger = LoggerFactory.getLogger(OllamaApiToolFunctionCallIT.class);
5252

@@ -64,13 +64,11 @@ public static void beforeAll() throws IOException, InterruptedException {
6464
public void toolFunctionCall() {
6565
// Step 1: send the conversation and available functions to the model
6666
var message = Message.builder(Role.USER)
67-
// .withContent("What's the weather like in San Francisco, Tokyo, and Paris?
68-
// Perform multiple function calls for each location.")
69-
.withContent("What's the weather like in San Francisco, Tokyo, and Paris?")
67+
.withContent("What's the weather like in San Francisco, Tokyo, and Paris? Return temperature in Celsius.")
7068
.build();
7169

7270
var functionTool = new OllamaApi.ChatRequest.Tool(new OllamaApi.ChatRequest.Tool.Function("getCurrentWeather",
73-
"Get the weather in location. Return temperature in Celsius.", ModelOptionsUtils.jsonToMap("""
71+
"Get the weather in location like city names.", ModelOptionsUtils.jsonToMap("""
7472
{
7573
"type": "object",
7674
"properties": {

spring-ai-docs/src/main/antora/modules/ROOT/images/spring-ai-integration-diagram.svg renamed to spring-ai-docs/src/main/antora/modules/ROOT/images/spring-ai-integration-diagram2.svg

Lines changed: 11 additions & 11 deletions
Loading
Lines changed: 51 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)