Skip to content

Commit 92faa82

Browse files
committed
docs: fix ollama4j API examples and clean gradle snippets
1 parent c3fb6b6 commit 92faa82

6 files changed

Lines changed: 160 additions & 200 deletions

File tree

ai_docs/ollama/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@ dependencies {
9090
### 4. Basic Java Code
9191
```java
9292
import io.github.ollama4j.Ollama;
93+
import io.github.ollama4j.models.chat.OllamaChatMessageRole;
9394
import io.github.ollama4j.models.chat.OllamaChatRequest;
9495

9596
Ollama api = new Ollama("http://localhost:11434");
9697

9798
OllamaChatRequest request = OllamaChatRequest.builder()
98-
.model("llama3.1")
99-
.message("user", "Hello!")
99+
.withModel("llama3.1")
100+
.withMessage(OllamaChatMessageRole.USER, "Hello!")
100101
.build();
101102

102-
String response = api.chat(request).getMessage().getContent();
103+
String response = api.chat(request, token -> {}).getResponseModel().getMessage().getResponse();
103104
System.out.println(response);
104105
```
105106

@@ -148,7 +149,7 @@ For **meteor-mcp-addon**, follow this approach:
148149
### Phase 1: Add Dependency
149150
```kotlin
150151
// build.gradle.kts
151-
include(implementation("io.github.ollama4j:ollama4j:1.1.4")!!)
152+
implementation("io.github.ollama4j:ollama4j:1.1.4")
152153
```
153154

154155
### Phase 2: Create Config System

ai_docs/ollama/integration-recommendations.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ MCPServerConnection.callTool()
181181
```kotlin
182182
dependencies {
183183
// Existing dependencies
184-
include(implementation("com.google.genai:google-genai:1.23.0")!!)
184+
implementation("com.google.genai:google-genai:1.23.0")
185185

186186
// Add Ollama4j
187-
include(implementation("io.github.ollama4j:ollama4j:1.1.4")!!)
187+
implementation("io.github.ollama4j:ollama4j:1.1.4")
188188

189189
// ... rest of dependencies
190190
}
@@ -233,11 +233,11 @@ public class OllamaExecutor {
233233
// Simple prompt (no tools)
234234
public String execute(String model, String prompt) {
235235
OllamaChatRequest request = OllamaChatRequest.builder()
236-
.model(model)
237-
.message("user", prompt)
236+
.withModel(model)
237+
.withMessage(OllamaChatMessageRole.USER, prompt)
238238
.build();
239239

240-
return api.chat(request).getMessage().getContent();
240+
return api.chat(request, token -> {}).getResponseModel().getMessage().getResponse();
241241
}
242242

243243
// MCP-enhanced loop (with tool calling)
@@ -249,20 +249,21 @@ public class OllamaExecutor {
249249

250250
// Chat with tools enabled
251251
OllamaChatRequest request = OllamaChatRequest.builder()
252-
.model(model)
253-
.message("user", prompt)
254-
.withTools()
252+
.withModel(model)
253+
.withMessage(OllamaChatMessageRole.USER, prompt)
254+
.withUseTools(true)
255255
.build();
256256

257-
OllamaChatResult result = api.chat(request);
257+
OllamaChatResult result = api.chat(request, token -> {});
258258

259259
// Manual function calling loop
260-
while (result.getMessage().hasToolCalls()) {
260+
while (result.getResponseModel().getMessage().getToolCalls() != null
261+
&& !result.getResponseModel().getMessage().getToolCalls().isEmpty()) {
261262
// Execute tool calls
262-
List<ToolCall> toolCalls = result.getMessage().getToolCalls();
263+
List<OllamaChatToolCalls> toolCalls = result.getResponseModel().getMessage().getToolCalls();
263264
List<String> toolResults = new ArrayList<>();
264265

265-
for (ToolCall call : toolCalls) {
266+
for (OllamaChatToolCalls call : toolCalls) {
266267
String serverName = extractServerName(call.getFunction().getName());
267268
String toolName = extractToolName(call.getFunction().getName());
268269
Map<String, Object> args = call.getFunction().getArguments();
@@ -277,7 +278,7 @@ public class OllamaExecutor {
277278
// ... (similar to GeminiExecutor pattern)
278279
}
279280

280-
return result.getMessage().getContent();
281+
return result.getResponseModel().getMessage().getResponse();
281282
}
282283

283284
private void registerServerTools(MCPServerConnection server) {
@@ -691,7 +692,7 @@ public class OllamaClientManager {
691692
**Solution:** Add connection check in GUI, show clear error message
692693

693694
### Issue 2: Model Not Installed
694-
**Solution:** Detect ModelNotFoundException, suggest `ollama pull <model>`
695+
**Solution:** Detect the "model not found" error text from `OllamaException` and suggest `ollama pull <model>`
695696

696697
### Issue 3: Slow Response Times
697698
**Solution:** Use streaming, show "Thinking..." indicator

ai_docs/ollama/ollama-java-integration.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ Streaming responses are JSON objects separated by newlines (NDJSON format).
410410

411411
```java
412412
import io.github.ollama4j.Ollama;
413+
import io.github.ollama4j.models.chat.OllamaChatMessageRole;
413414
import io.github.ollama4j.models.chat.OllamaChatRequest;
414415
import io.github.ollama4j.models.chat.OllamaChatResult;
415416

@@ -418,12 +419,12 @@ public class OllamaChatExample {
418419
Ollama ollama = new Ollama("http://localhost:11434");
419420

420421
OllamaChatRequest request = OllamaChatRequest.builder()
421-
.model("llama3.1")
422-
.message("user", "What is the capital of France?")
422+
.withModel("llama3.1")
423+
.withMessage(OllamaChatMessageRole.USER, "What is the capital of France?")
423424
.build();
424425

425-
OllamaChatResult result = ollama.chat(request);
426-
System.out.println(result.getMessage().getContent());
426+
OllamaChatResult result = ollama.chat(request, token -> {});
427+
System.out.println(result.getResponseModel().getMessage().getResponse());
427428
}
428429
}
429430
```
@@ -459,12 +460,12 @@ api.registerTool(weatherSpec, weatherTool::getCurrentWeather);
459460

460461
// Use with chat
461462
OllamaChatRequest request = OllamaChatRequest.builder()
462-
.model("mistral")
463-
.message("user", "What's the weather in Tokyo?")
464-
.withTools() // Enable tool calling
463+
.withModel("mistral")
464+
.withMessage(OllamaChatMessageRole.USER, "What's the weather in Tokyo?")
465+
.withUseTools(true) // Enable tool calling
465466
.build();
466467

467-
OllamaChatResult result = api.chat(request);
468+
OllamaChatResult result = api.chat(request, token -> {});
468469
```
469470

470471
### Example 3: Annotation-Based Tool Registration

ai_docs/ollama/ollama4j-1.1.4-api-reference.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ OllamaChatRequest request = OllamaChatRequest.builder()
3939
.withMessage(OllamaChatMessageRole.USER, "Hello!")
4040
.withOptions(options)
4141
.withKeepAlive("5m")
42-
.build(); // NO build() method needed - returns OllamaChatRequest directly
42+
.build(); // Optional convenience call; returns the same OllamaChatRequest instance
4343
```
4444

4545
**Key Methods**:
@@ -390,7 +390,7 @@ public class OllamaExample {
390390
1. **Wrong main class**: Use `Ollama`
391391
2. **Wrong builder method**: Use `.withModel()`, NOT `.model()`
392392
3. **Wrong response accessor**: Use `result.getResponseModel().getMessage().getResponse()`, NOT `result.getMessage()`
393-
4. **Calling build()**: Builder methods return `OllamaChatRequest` directly, no `.build()` needed
393+
4. **Calling build()**: `.build()` exists, but it is optional and returns the same request instance
394394
5. **Wrong message field**: `OllamaChatMessage` has `getResponse()`, NOT `getContent()` or `getMessage()`
395395
6. **Tool classes**: Import from `io.github.ollama4j.tools.Tools.*`, NOT separate top-level classes
396396

@@ -410,7 +410,7 @@ String response = result.getMessage(); // Wrong accessor
410410
Ollama ollama = new Ollama("http://localhost:11434");
411411
OllamaChatRequest request = OllamaChatRequest.builder()
412412
.withModel("llama3.2") // Correct method
413-
// Returns OllamaChatRequest directly, no .build()
413+
// build() is optional and returns the same instance
414414
String response = result.getResponseModel().getMessage().getResponse();
415415
```
416416

0 commit comments

Comments
 (0)