mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 11:57:12 +02:00
Optimizes ChatStreamObserver to use only the last message instead of parsing all messages again
This commit is contained in:
parent
0f414f71a3
commit
cf4e7a96e8
@ -2,10 +2,8 @@ package io.github.amithkoujalgi.ollama4j.core.models.chat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.github.amithkoujalgi.ollama4j.core.OllamaStreamHandler;
|
||||
import lombok.NonNull;
|
||||
|
||||
public class OllamaChatStreamObserver {
|
||||
|
||||
@ -13,7 +11,7 @@ public class OllamaChatStreamObserver {
|
||||
|
||||
private List<OllamaChatResponseModel> responseParts = new ArrayList<>();
|
||||
|
||||
private String message;
|
||||
private String message = "";
|
||||
|
||||
public OllamaChatStreamObserver(OllamaStreamHandler streamHandler) {
|
||||
this.streamHandler = streamHandler;
|
||||
@ -25,8 +23,7 @@ public class OllamaChatStreamObserver {
|
||||
}
|
||||
|
||||
protected void handleCurrentResponsePart(OllamaChatResponseModel currentResponsePart){
|
||||
List<@NonNull String> allResponsePartsByNow = responseParts.stream().map(r -> r.getMessage().getContent()).collect(Collectors.toList());
|
||||
message = String.join("", allResponsePartsByNow);
|
||||
message = message + currentResponsePart.getMessage().getContent();
|
||||
streamHandler.accept(message);
|
||||
}
|
||||
|
||||
|
@ -183,7 +183,7 @@ class TestRealAPIs {
|
||||
|
||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel,(s) -> {
|
||||
LOG.info(s);
|
||||
String substring = s.substring(sb.toString().length(), s.length()-1);
|
||||
String substring = s.substring(sb.toString().length(), s.length());
|
||||
LOG.info(substring);
|
||||
sb.append(substring);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user