From bcfaf735dac45f28b2659993572de080b90ea39a Mon Sep 17 00:00:00 2001 From: bidek <1751659+bidek@users.noreply.github.com> Date: Mon, 23 Sep 2024 10:29:55 +0200 Subject: [PATCH] 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 - [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 - [ ] 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) --- .../model/ollama/OllamaMessagesUtils.java | 2 +- .../model/ollama/OllamaToolChatModelIT.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaMessagesUtils.java b/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaMessagesUtils.java index 3ba5e1ea2..abb3670c1 100644 --- a/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaMessagesUtils.java +++ b/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaMessagesUtils.java @@ -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()) diff --git a/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/OllamaToolChatModelIT.java b/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/OllamaToolChatModelIT.java index 5f4c944bb..735bc97d1 100644 --- a/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/OllamaToolChatModelIT.java +++ b/langchain4j-ollama/src/test/java/dev/langchain4j/model/ollama/OllamaToolChatModelIT.java @@ -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 toolSpecifications = singletonList(toolWithoutParameter); + + // when + List chatMessages = singletonList( + userMessage("What is the current time?") + ); + + // then + assertDoesNotThrow(() -> { + ollamaChatModel.generate(chatMessages, toolSpecifications); + }); + + } + } \ No newline at end of file