forked from Mirror/ollama4j
		
	Introduced the Spotless Maven plugin for code formatting in pom.xml. Updated copyright headers to include 'and contributors' and changed the year to 2025 in all main source files. Minor formatting and import order improvements applied throughout the codebase.
		
			
				
	
	
		
			59 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
/*
 | 
						|
 * Ollama4j - Java library for interacting with Ollama server.
 | 
						|
 * Copyright (c) 2025 Amith Koujalgi and contributors.
 | 
						|
 *
 | 
						|
 * Licensed under the MIT License (the "License");
 | 
						|
 * you may not use this file except in compliance with the License.
 | 
						|
 *
 | 
						|
*/
 | 
						|
package io.github.ollama4j.models.chat;
 | 
						|
 | 
						|
import io.github.ollama4j.models.generate.OllamaStreamHandler;
 | 
						|
import io.github.ollama4j.models.generate.OllamaTokenHandler;
 | 
						|
import lombok.RequiredArgsConstructor;
 | 
						|
 | 
						|
@RequiredArgsConstructor
 | 
						|
public class OllamaChatStreamObserver implements OllamaTokenHandler {
 | 
						|
    private final OllamaStreamHandler thinkingStreamHandler;
 | 
						|
    private final OllamaStreamHandler responseStreamHandler;
 | 
						|
 | 
						|
    private String message = "";
 | 
						|
 | 
						|
    @Override
 | 
						|
    public void accept(OllamaChatResponseModel token) {
 | 
						|
        if (responseStreamHandler == null || token == null || token.getMessage() == null) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        String thinking = token.getMessage().getThinking();
 | 
						|
        String content = token.getMessage().getContent();
 | 
						|
 | 
						|
        boolean hasThinking = thinking != null && !thinking.isEmpty();
 | 
						|
        boolean hasContent = !content.isEmpty();
 | 
						|
 | 
						|
        //        if (hasThinking && !hasContent) {
 | 
						|
        ////            message += thinking;
 | 
						|
        //            message = thinking;
 | 
						|
        //        } else {
 | 
						|
        ////            message += content;
 | 
						|
        //            message = content;
 | 
						|
        //        }
 | 
						|
        //
 | 
						|
        //        responseStreamHandler.accept(message);
 | 
						|
 | 
						|
        if (!hasContent && hasThinking && thinkingStreamHandler != null) {
 | 
						|
            // message = message + thinking;
 | 
						|
 | 
						|
            // use only new tokens received, instead of appending the tokens to the previous
 | 
						|
            // ones and sending the full string again
 | 
						|
            thinkingStreamHandler.accept(thinking);
 | 
						|
        } else if (hasContent && responseStreamHandler != null) {
 | 
						|
            // message = message + response;
 | 
						|
 | 
						|
            // use only new tokens received, instead of appending the tokens to the previous
 | 
						|
            // ones and sending the full string again
 | 
						|
            responseStreamHandler.accept(content);
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |