Fix for issue #1804 - When tool is a method without parameters, the handling of parameters in ToolSpecification will result in an error. (#1812)

## Issue
Fixes #1804

## Change
Ollama: handling case when tool don`t have parameters.

## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes
- [x] I have added unit and integration tests for my change
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)
This commit is contained in:
bidek 2024-09-23 10:29:55 +02:00 committed by GitHub
parent c872a4d46c
commit bcfaf735da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -42,7 +42,7 @@ class OllamaMessagesUtils {
.function(Function.builder()
.name(toolSpecification.name())
.description(toolSpecification.description())
.parameters(Parameters.builder()
.parameters(toolSpecification.parameters() == null ? null : Parameters.builder()
.properties(toolSpecification.parameters().properties())
.required(toolSpecification.parameters().required())
.build())

View File

@ -20,6 +20,7 @@ import static dev.langchain4j.model.ollama.OllamaImage.TOOL_MODEL;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
class OllamaToolChatModelIT extends AbstractOllamaToolsLanguageModelInfrastructure {
@ -31,6 +32,11 @@ class OllamaToolChatModelIT extends AbstractOllamaToolsLanguageModelInfrastructu
.addParameter("location", STRING, description("The location to get the weather for, e.g. San Francisco, CA"))
.build();
ToolSpecification toolWithoutParameter = ToolSpecification.builder()
.name("get_current_time")
.description("Get the current time")
.build();
ChatLanguageModel ollamaChatModel = OllamaChatModel.builder()
.baseUrl(ollama.getEndpoint())
.modelName(TOOL_MODEL)
@ -90,4 +96,22 @@ class OllamaToolChatModelIT extends AbstractOllamaToolsLanguageModelInfrastructu
assertThat(aiMessage.toolExecutionRequests()).isNull();
}
@Test
void should_handle_tool_without_parameter() {
// given
List<ToolSpecification> toolSpecifications = singletonList(toolWithoutParameter);
// when
List<ChatMessage> chatMessages = singletonList(
userMessage("What is the current time?")
);
// then
assertDoesNotThrow(() -> {
ollamaChatModel.generate(chatMessages, toolSpecifications);
});
}
}