Compare commits

...

11 Commits

Author SHA1 Message Date
Amith Koujalgi
a70925d842 Merge pull request #235 from ollama4j/dependabot/maven/org.junit.jupiter-junit-jupiter-api-6.0.1
Some checks failed
CodeQL / Analyze (java) (push) Failing after 8s
CodeQL / Analyze (javascript) (push) Failing after 6s
Bump org.junit.jupiter:junit-jupiter-api from 6.0.0 to 6.0.1
2025-11-03 14:42:28 +05:30
dependabot[bot]
256ea0b2a4 Bump org.junit.jupiter:junit-jupiter-api from 6.0.0 to 6.0.1
Bumps [org.junit.jupiter:junit-jupiter-api](https://github.com/junit-team/junit-framework) from 6.0.0 to 6.0.1.
- [Release notes](https://github.com/junit-team/junit-framework/releases)
- [Commits](https://github.com/junit-team/junit-framework/compare/r6.0.0...r6.0.1)

---
updated-dependencies:
- dependency-name: org.junit.jupiter:junit-jupiter-api
  dependency-version: 6.0.1
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-11-03 00:48:56 +00:00
Amith Koujalgi
13ccba40f7 Merge pull request #228 from ollama4j/dependabot/npm_and_yarn/docs/docsearch/js-4.2.0
Some checks failed
Mark stale issues / stale (push) Failing after 8s
Mark stale issues and PRs / stale (push) Failing after 33s
CodeQL / Analyze (java) (push) Failing after 9s
CodeQL / Analyze (javascript) (push) Failing after 7s
Bump @docsearch/js from 4.1.0 to 4.2.0 in /docs
2025-11-02 21:39:34 +05:30
Amith Koujalgi
30e5598468 Merge pull request #229 from ollama4j/dependabot/maven/org.testcontainers-ollama-1.21.3
Bump org.testcontainers:ollama from 1.20.2 to 1.21.3
2025-11-02 21:39:17 +05:30
Amith Koujalgi
24a37ed858 Merge pull request #230 from ollama4j/snyk-upgrade-3dd2ca8c00ec72375652b43f81db0c03
[Snyk] Upgrade @docsearch/js from 4.1.0 to 4.2.0
2025-11-02 21:38:53 +05:30
Amith Koujalgi
fc63e1f786 Merge pull request #226 from ollama4j/fix-docs-build
Fix: setting tools in `generate()` method and issue #227
2025-11-02 21:37:55 +05:30
amithkoujalgi
6623c94e92 Improve code documentation in OllamaChatEndpointCaller by enhancing comments for clarity and ensuring proper null checks for message handling in streamed responses. 2025-11-02 21:18:57 +05:30
dependabot[bot]
995fbb14ba Bump org.testcontainers:ollama from 1.20.2 to 1.21.3
Bumps [org.testcontainers:ollama](https://github.com/testcontainers/testcontainers-java) from 1.20.2 to 1.21.3.
- [Release notes](https://github.com/testcontainers/testcontainers-java/releases)
- [Changelog](https://github.com/testcontainers/testcontainers-java/blob/main/CHANGELOG.md)
- [Commits](https://github.com/testcontainers/testcontainers-java/compare/1.20.2...1.21.3)

---
updated-dependencies:
- dependency-name: org.testcontainers:ollama
  dependency-version: 1.21.3
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-27 00:45:18 +00:00
dependabot[bot]
4a1161b88a Bump @docsearch/js from 4.1.0 to 4.2.0 in /docs
Bumps [@docsearch/js](https://github.com/algolia/docsearch/tree/HEAD/packages/docsearch-js) from 4.1.0 to 4.2.0.
- [Release notes](https://github.com/algolia/docsearch/releases)
- [Changelog](https://github.com/algolia/docsearch/blob/main/CHANGELOG.md)
- [Commits](https://github.com/algolia/docsearch/commits/v4.2.0/packages/docsearch-js)

---
updated-dependencies:
- dependency-name: "@docsearch/js"
  dependency-version: 4.2.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-27 00:33:19 +00:00
amithkoujalgi
c628d1fa20 Enhance documentation on tool creation by outlining two methods: manual registration of static/regular methods and annotation-based tool discovery. Include a link for further reading on annotation-based registration. 2025-10-24 10:39:39 +05:30
amithkoujalgi
614f7422b6 Refactor tool handling in Ollama class to merge request-specific and globally registered tools into a single list, ensuring original requests remain unmodified. Also, remove unnecessary newline in documentation for model pulling command. 2025-10-23 22:20:24 +05:30
5 changed files with 44 additions and 13 deletions

View File

@@ -209,7 +209,6 @@ To download/pull the model into your Ollama server, run the following command in
```shell ```shell
ollama pull mistral ollama pull mistral
``` ```
You can list the models available on your model server by running the following command in your terminal. You can list the models available on your model server by running the following command in your terminal.

View File

@@ -31,7 +31,19 @@ You could do that with ease with the `function calling` capabilities of the mode
### Create Tools/Functions ### Create Tools/Functions
We can create static functions as our tools. There are two ways to create and register your tools:
1. **Define static or regular methods and register them explicitly as tools.**
You can create standalone functions (static or instance methods) and manually associate them with your tool specifications.
2. **Use annotation-based tool discovery for automatic registration.**
By annotating your tool methods, you can leverage `registerAnnotatedTools()` to automatically scan your classpath, find all annotated tool functions, and register them without extra boilerplate.
Learn more about annotation-based tool registration [here](/apis-generate/chat-with-tools#annotation-based-tool-registration).
Choose the approach that best fits your project—manual for precise control, or annotation-based for easier scaling.
Let's start by exploring the first approach: manually defining and registering your tools/functions.
This function takes the arguments `location` and `fuelType` and performs an operation with these arguments and returns This function takes the arguments `location` and `fuelType` and performs an operation with these arguments and returns
fuel price value. fuel price value.

View File

@@ -284,7 +284,7 @@
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId> <artifactId>junit-jupiter-api</artifactId>
<version>6.0.0</version> <version>6.0.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
@@ -302,7 +302,7 @@
<dependency> <dependency>
<groupId>org.testcontainers</groupId> <groupId>org.testcontainers</groupId>
<artifactId>ollama</artifactId> <artifactId>ollama</artifactId>
<version>1.20.2</version> <version>1.21.3</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@@ -804,9 +804,21 @@ public class Ollama {
ocm.setResponse(request.getPrompt()); ocm.setResponse(request.getPrompt());
chatRequest.setMessages(msgs); chatRequest.setMessages(msgs);
msgs.add(ocm); msgs.add(ocm);
// Merge request's tools and globally registered tools into a new list to avoid mutating the
// original request
List<Tools.Tool> allTools = new ArrayList<>();
if (request.getTools() != null) {
allTools.addAll(request.getTools());
}
List<Tools.Tool> registeredTools = this.getRegisteredTools();
if (registeredTools != null) {
allTools.addAll(registeredTools);
}
OllamaChatTokenHandler hdlr = null; OllamaChatTokenHandler hdlr = null;
chatRequest.setUseTools(true); chatRequest.setUseTools(true);
chatRequest.setTools(request.getTools()); chatRequest.setTools(allTools);
if (streamObserver != null) { if (streamObserver != null) {
chatRequest.setStream(true); chatRequest.setStream(true);
if (streamObserver.getResponseStreamHandler() != null) { if (streamObserver.getResponseStreamHandler() != null) {

View File

@@ -44,12 +44,15 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
/** /**
* Parses streamed Response line from ollama chat. Using {@link * Parses streamed Response line from ollama chat. Using {@link
* com.fasterxml.jackson.databind.ObjectMapper#readValue(String, TypeReference)} should throw * com.fasterxml.jackson.databind.ObjectMapper#readValue(String, TypeReference)}
* should throw
* {@link IllegalArgumentException} in case of null line or {@link * {@link IllegalArgumentException} in case of null line or {@link
* com.fasterxml.jackson.core.JsonParseException} in case the JSON Object cannot be parsed to a * com.fasterxml.jackson.core.JsonParseException} in case the JSON Object cannot
* {@link OllamaChatResponseModel}. Thus, the ResponseModel should never be null. * be parsed to a
* {@link OllamaChatResponseModel}. Thus, the ResponseModel should never be
* null.
* *
* @param line streamed line of ollama stream response * @param line streamed line of ollama stream response
* @param responseBuffer Stringbuffer to add latest response message part to * @param responseBuffer Stringbuffer to add latest response message part to
* @return TRUE, if ollama-Response has 'done' state * @return TRUE, if ollama-Response has 'done' state
*/ */
@@ -59,9 +62,11 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
try { try {
OllamaChatResponseModel ollamaResponseModel = OllamaChatResponseModel ollamaResponseModel =
Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class); Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class);
// It seems that under heavy load Ollama responds with an empty chat message part in the // It seems that under heavy load Ollama responds with an empty chat message
// part in the
// streamed response. // streamed response.
// Thus, we null check the message and hope that the next streamed response has some // Thus, we null check the message and hope that the next streamed response has
// some
// message content again. // message content again.
OllamaChatMessage message = ollamaResponseModel.getMessage(); OllamaChatMessage message = ollamaResponseModel.getMessage();
if (message != null) { if (message != null) {
@@ -118,7 +123,9 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
parseResponseAndAddToBuffer(line, responseBuffer, thinkingBuffer); parseResponseAndAddToBuffer(line, responseBuffer, thinkingBuffer);
ollamaChatResponseModel = ollamaChatResponseModel =
Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class); Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class);
if (body.stream && ollamaChatResponseModel.getMessage().getToolCalls() != null) { if (body.stream
&& ollamaChatResponseModel.getMessage() != null
&& ollamaChatResponseModel.getMessage().getToolCalls() != null) {
wantedToolsForStream = ollamaChatResponseModel.getMessage().getToolCalls(); wantedToolsForStream = ollamaChatResponseModel.getMessage().getToolCalls();
} }
if (finished && body.stream) { if (finished && body.stream) {
@@ -153,7 +160,8 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
} }
/** /**
* Handles error status codes and appends error messages to the response buffer. Returns true if * Handles error status codes and appends error messages to the response buffer.
* Returns true if
* an error was handled, false otherwise. * an error was handled, false otherwise.
*/ */
private boolean handleErrorStatus(int statusCode, String line, StringBuilder responseBuffer) private boolean handleErrorStatus(int statusCode, String line, StringBuilder responseBuffer)