Improve code documentation in OllamaChatEndpointCaller by enhancing comments for clarity and ensuring proper null checks for message handling in streamed responses.

This commit is contained in:
amithkoujalgi
2025-11-02 21:18:57 +05:30
parent c628d1fa20
commit 6623c94e92

View File

@@ -44,12 +44,15 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
/** /**
* Parses streamed Response line from ollama chat. Using {@link * Parses streamed Response line from ollama chat. Using {@link
* com.fasterxml.jackson.databind.ObjectMapper#readValue(String, TypeReference)} should throw * com.fasterxml.jackson.databind.ObjectMapper#readValue(String, TypeReference)}
* should throw
* {@link IllegalArgumentException} in case of null line or {@link * {@link IllegalArgumentException} in case of null line or {@link
* com.fasterxml.jackson.core.JsonParseException} in case the JSON Object cannot be parsed to a * com.fasterxml.jackson.core.JsonParseException} in case the JSON Object cannot
* {@link OllamaChatResponseModel}. Thus, the ResponseModel should never be null. * be parsed to a
* {@link OllamaChatResponseModel}. Thus, the ResponseModel should never be
* null.
* *
* @param line streamed line of ollama stream response * @param line streamed line of ollama stream response
* @param responseBuffer Stringbuffer to add latest response message part to * @param responseBuffer Stringbuffer to add latest response message part to
* @return TRUE, if ollama-Response has 'done' state * @return TRUE, if ollama-Response has 'done' state
*/ */
@@ -59,9 +62,11 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
try { try {
OllamaChatResponseModel ollamaResponseModel = OllamaChatResponseModel ollamaResponseModel =
Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class); Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class);
// It seems that under heavy load Ollama responds with an empty chat message part in the // It seems that under heavy load Ollama responds with an empty chat message
// part in the
// streamed response. // streamed response.
// Thus, we null check the message and hope that the next streamed response has some // Thus, we null check the message and hope that the next streamed response has
// some
// message content again. // message content again.
OllamaChatMessage message = ollamaResponseModel.getMessage(); OllamaChatMessage message = ollamaResponseModel.getMessage();
if (message != null) { if (message != null) {
@@ -118,7 +123,9 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
parseResponseAndAddToBuffer(line, responseBuffer, thinkingBuffer); parseResponseAndAddToBuffer(line, responseBuffer, thinkingBuffer);
ollamaChatResponseModel = ollamaChatResponseModel =
Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class); Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class);
if (body.stream && ollamaChatResponseModel.getMessage().getToolCalls() != null) { if (body.stream
&& ollamaChatResponseModel.getMessage() != null
&& ollamaChatResponseModel.getMessage().getToolCalls() != null) {
wantedToolsForStream = ollamaChatResponseModel.getMessage().getToolCalls(); wantedToolsForStream = ollamaChatResponseModel.getMessage().getToolCalls();
} }
if (finished && body.stream) { if (finished && body.stream) {
@@ -153,7 +160,8 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
} }
/** /**
* Handles error status codes and appends error messages to the response buffer. Returns true if * Handles error status codes and appends error messages to the response buffer.
* Returns true if
* an error was handled, false otherwise. * an error was handled, false otherwise.
*/ */
private boolean handleErrorStatus(int statusCode, String line, StringBuilder responseBuffer) private boolean handleErrorStatus(int statusCode, String line, StringBuilder responseBuffer)