Fix NullPointerException with Ollama + tool calls

Fixed #1459
This commit is contained in:
Mudabir Hussain
2024-10-06 15:34:33 +04:00
committed by Mark Pollack
parent 6e62ff7d83
commit 2090826656
3 changed files with 25 additions and 7 deletions

View File

@@ -211,13 +211,18 @@ public class OllamaChatModel extends AbstractToolCallSupport implements ChatMode
Flux<ChatResponse> chatResponse = ollamaResponse.map(chunk -> {
String content = (chunk.message() != null) ? chunk.message().content() : "";
List<AssistantMessage.ToolCall> toolCalls = chunk.message().toolCalls() == null ? List.of()
: chunk.message()
.toolCalls()
.stream()
.map(toolCall -> new AssistantMessage.ToolCall("", "function", toolCall.function().name(),
ModelOptionsUtils.toJsonString(toolCall.function().arguments())))
.toList();
List<AssistantMessage.ToolCall> toolCalls = List.of();
// Added null checks to prevent NPE when accessing tool calls
if (chunk.message() != null && chunk.message().toolCalls() != null) {
toolCalls = chunk.message()
.toolCalls()
.stream()
.map(toolCall -> new AssistantMessage.ToolCall("", "function", toolCall.function().name(),
ModelOptionsUtils.toJsonString(toolCall.function().arguments())))
.toList();
}
var assistantMessage = new AssistantMessage(content, Map.of(), toolCalls);

View File

@@ -40,6 +40,7 @@ import java.io.IOException;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
@SpringBootTest
@Testcontainers
@@ -67,6 +68,18 @@ class OllamaChatModelMultimodalIT extends BaseOllamaIT {
@Autowired
private OllamaChatModel chatModel;
@Test
void unsupportedMediaType() throws IOException {
var imageData = new ClassPathResource("/norway.webp");
var userMessage = new UserMessage("Explain what do you see on this picture?",
List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageData)));
assertThrows(RuntimeException.class, () -> chatModel.call(new Prompt(List.of(userMessage))));
}
@Test
void multiModalityTest() throws IOException {

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB