Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaChatStreamObserver.java
Go to the documentation of this file.
1/*
2 * Ollama4j - Java library for interacting with Ollama server.
3 * Copyright (c) 2025 Amith Koujalgi and contributors.
4 *
5 * Licensed under the MIT License (the "License");
6 * you may not use this file except in compliance with the License.
7 *
8*/
9package io.github.ollama4j.models.chat;
10
11import io.github.ollama4j.models.generate.OllamaGenerateTokenHandler;
12import lombok.AllArgsConstructor;
13import lombok.NoArgsConstructor;
14import lombok.Setter;
15
16@Setter
17@NoArgsConstructor
18@AllArgsConstructor
20 private OllamaGenerateTokenHandler thinkingStreamHandler;
21 private OllamaGenerateTokenHandler responseStreamHandler;
22
23 @Override
24 public void accept(OllamaChatResponseModel token) {
25 if (responseStreamHandler == null || token == null || token.getMessage() == null) {
26 return;
27 }
28
29 String thinking = token.getMessage().getThinking();
30 String response = token.getMessage().getResponse();
31
32 boolean hasThinking = thinking != null && !thinking.isEmpty();
33 boolean hasResponse = response != null && !response.isEmpty();
34
35 if (!hasResponse && hasThinking && thinkingStreamHandler != null) {
36 thinkingStreamHandler.accept(thinking);
37 } else if (hasResponse) {
38 responseStreamHandler.accept(response);
39 }
40 }
41}