forked from Mirror/ollama4j
		
	extends ollamaChatResult to have full access to OllamaChatResult
This commit is contained in:
		@@ -602,7 +602,7 @@ public class OllamaAPI {
 | 
			
		||||
        OllamaResult result = generate(model, prompt, raw, options, null);
 | 
			
		||||
        toolResult.setModelResult(result);
 | 
			
		||||
 | 
			
		||||
        String toolsResponse = result.getContent();
 | 
			
		||||
        String toolsResponse = result.getResponse();
 | 
			
		||||
        if (toolsResponse.contains("[TOOL_CALLS]")) {
 | 
			
		||||
            toolsResponse = toolsResponse.replace("[TOOL_CALLS]", "");
 | 
			
		||||
        }
 | 
			
		||||
@@ -767,7 +767,7 @@ public class OllamaAPI {
 | 
			
		||||
     */
 | 
			
		||||
    public OllamaChatResult chat(OllamaChatRequest request, OllamaStreamHandler streamHandler) throws OllamaBaseException, IOException, InterruptedException {
 | 
			
		||||
        OllamaChatEndpointCaller requestCaller = new OllamaChatEndpointCaller(host, basicAuth, requestTimeoutSeconds, verbose);
 | 
			
		||||
        OllamaResult result;
 | 
			
		||||
        OllamaChatResult result;
 | 
			
		||||
 | 
			
		||||
        // add all registered tools to Request
 | 
			
		||||
        request.setTools(toolRegistry.getRegisteredSpecs().stream().map(Tools.ToolSpecification::getToolPrompt).collect(Collectors.toList()));
 | 
			
		||||
@@ -779,7 +779,7 @@ public class OllamaAPI {
 | 
			
		||||
            result = requestCaller.callSync(request);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return new OllamaChatResult(result.getContent(), result.getResponseTime(), result.getHttpStatusCode(), request.getMessages());
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void registerTool(Tools.ToolSpecification toolSpecification) {
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ import java.io.IOException;
 | 
			
		||||
import java.net.URISyntaxException;
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
@@ -38,6 +39,10 @@ public class OllamaChatRequestBuilder {
 | 
			
		||||
        request = new OllamaChatRequest(request.getModel(), new ArrayList<>());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content){
 | 
			
		||||
        return withMessage(role,content, Collections.emptyList());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List<OllamaChatToolCalls> toolCalls,List<File> images) {
 | 
			
		||||
        List<OllamaChatMessage> messages = this.request.getMessages();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,28 +2,40 @@ package io.github.ollama4j.models.chat;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import io.github.ollama4j.models.response.OllamaResult;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
 | 
			
		||||
import static io.github.ollama4j.utils.Utils.getObjectMapper;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Specific chat-API result that contains the chat history sent to the model and appends the answer as {@link OllamaChatResult} given by the
 | 
			
		||||
 * {@link OllamaChatMessageRole#ASSISTANT} role.
 | 
			
		||||
 */
 | 
			
		||||
public class OllamaChatResult extends OllamaResult {
 | 
			
		||||
@Getter
 | 
			
		||||
public class OllamaChatResult {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private List<OllamaChatMessage> chatHistory;
 | 
			
		||||
 | 
			
		||||
    public OllamaChatResult(String response, long responseTime, int httpStatusCode, List<OllamaChatMessage> chatHistory) {
 | 
			
		||||
        super(response, responseTime, httpStatusCode);
 | 
			
		||||
    private OllamaChatResponseModel response;
 | 
			
		||||
 | 
			
		||||
    public OllamaChatResult(OllamaChatResponseModel response, List<OllamaChatMessage> chatHistory) {
 | 
			
		||||
        this.chatHistory = chatHistory;
 | 
			
		||||
        this.response = response;
 | 
			
		||||
        appendAnswerToChatHistory(response);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public List<OllamaChatMessage> getChatHistory() {
 | 
			
		||||
        return chatHistory;
 | 
			
		||||
    private void appendAnswerToChatHistory(OllamaChatResponseModel response) {
 | 
			
		||||
        this.chatHistory.add(response.getMessage());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void appendAnswerToChatHistory(String answer) {
 | 
			
		||||
        OllamaChatMessage assistantMessage = new OllamaChatMessage(OllamaChatMessageRole.ASSISTANT, answer);
 | 
			
		||||
        this.chatHistory.add(assistantMessage);
 | 
			
		||||
    @Override
 | 
			
		||||
    public String toString() {
 | 
			
		||||
        try {
 | 
			
		||||
            return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
 | 
			
		||||
        } catch (JsonProcessingException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,9 @@ import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import com.fasterxml.jackson.core.type.TypeReference;
 | 
			
		||||
import io.github.ollama4j.exceptions.OllamaBaseException;
 | 
			
		||||
import io.github.ollama4j.models.chat.OllamaChatMessage;
 | 
			
		||||
import io.github.ollama4j.models.chat.OllamaChatRequest;
 | 
			
		||||
import io.github.ollama4j.models.chat.OllamaChatResult;
 | 
			
		||||
import io.github.ollama4j.models.response.OllamaErrorResponse;
 | 
			
		||||
import io.github.ollama4j.models.response.OllamaResult;
 | 
			
		||||
import io.github.ollama4j.models.chat.OllamaChatResponseModel;
 | 
			
		||||
import io.github.ollama4j.models.chat.OllamaChatStreamObserver;
 | 
			
		||||
@@ -13,7 +16,15 @@ import io.github.ollama4j.utils.Utils;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
import java.io.BufferedReader;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.io.InputStreamReader;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.net.http.HttpClient;
 | 
			
		||||
import java.net.http.HttpRequest;
 | 
			
		||||
import java.net.http.HttpResponse;
 | 
			
		||||
import java.nio.charset.StandardCharsets;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Specialization class for requests
 | 
			
		||||
@@ -64,9 +75,68 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public OllamaResult call(OllamaRequestBody body, OllamaStreamHandler streamHandler)
 | 
			
		||||
    public OllamaChatResult call(OllamaChatRequest body, OllamaStreamHandler streamHandler)
 | 
			
		||||
            throws OllamaBaseException, IOException, InterruptedException {
 | 
			
		||||
        streamObserver = new OllamaChatStreamObserver(streamHandler);
 | 
			
		||||
        return super.callSync(body);
 | 
			
		||||
        return callSync(body);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public OllamaChatResult callSync(OllamaChatRequest body) throws OllamaBaseException, IOException, InterruptedException {
 | 
			
		||||
        // Create Request
 | 
			
		||||
        HttpClient httpClient = HttpClient.newHttpClient();
 | 
			
		||||
        URI uri = URI.create(getHost() + getEndpointSuffix());
 | 
			
		||||
        HttpRequest.Builder requestBuilder =
 | 
			
		||||
                getRequestBuilderDefault(uri)
 | 
			
		||||
                        .POST(
 | 
			
		||||
                                body.getBodyPublisher());
 | 
			
		||||
        HttpRequest request = requestBuilder.build();
 | 
			
		||||
        if (isVerbose()) LOG.info("Asking model: " + body.toString());
 | 
			
		||||
        HttpResponse<InputStream> response =
 | 
			
		||||
                httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
 | 
			
		||||
 | 
			
		||||
        int statusCode = response.statusCode();
 | 
			
		||||
        InputStream responseBodyStream = response.body();
 | 
			
		||||
        StringBuilder responseBuffer = new StringBuilder();
 | 
			
		||||
        OllamaChatResponseModel ollamaChatResponseModel = null;
 | 
			
		||||
        try (BufferedReader reader =
 | 
			
		||||
                     new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
 | 
			
		||||
 | 
			
		||||
            String line;
 | 
			
		||||
            while ((line = reader.readLine()) != null) {
 | 
			
		||||
                if (statusCode == 404) {
 | 
			
		||||
                    LOG.warn("Status code: 404 (Not Found)");
 | 
			
		||||
                    OllamaErrorResponse ollamaResponseModel =
 | 
			
		||||
                            Utils.getObjectMapper().readValue(line, OllamaErrorResponse.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
                } else if (statusCode == 401) {
 | 
			
		||||
                    LOG.warn("Status code: 401 (Unauthorized)");
 | 
			
		||||
                    OllamaErrorResponse ollamaResponseModel =
 | 
			
		||||
                            Utils.getObjectMapper()
 | 
			
		||||
                                    .readValue("{\"error\":\"Unauthorized\"}", OllamaErrorResponse.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
                } else if (statusCode == 400) {
 | 
			
		||||
                    LOG.warn("Status code: 400 (Bad Request)");
 | 
			
		||||
                    OllamaErrorResponse ollamaResponseModel = Utils.getObjectMapper().readValue(line,
 | 
			
		||||
                            OllamaErrorResponse.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
                } else {
 | 
			
		||||
                    boolean finished = parseResponseAndAddToBuffer(line, responseBuffer);
 | 
			
		||||
                        ollamaChatResponseModel = Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class);
 | 
			
		||||
                    if (finished && body.stream) {
 | 
			
		||||
                        ollamaChatResponseModel.getMessage().setContent(responseBuffer.toString());
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (statusCode != 200) {
 | 
			
		||||
            LOG.error("Status code " + statusCode);
 | 
			
		||||
            throw new OllamaBaseException(responseBuffer.toString());
 | 
			
		||||
        } else {
 | 
			
		||||
            OllamaChatResult ollamaResult =
 | 
			
		||||
                    new OllamaChatResult(ollamaChatResponseModel,body.getMessages());
 | 
			
		||||
            if (isVerbose()) LOG.info("Model response: " + ollamaResult);
 | 
			
		||||
            return ollamaResult;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ import io.github.ollama4j.models.response.OllamaErrorResponse;
 | 
			
		||||
import io.github.ollama4j.models.response.OllamaResult;
 | 
			
		||||
import io.github.ollama4j.utils.OllamaRequestBody;
 | 
			
		||||
import io.github.ollama4j.utils.Utils;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
@@ -24,14 +25,15 @@ import java.util.Base64;
 | 
			
		||||
/**
 | 
			
		||||
 * Abstract helperclass to call the ollama api server.
 | 
			
		||||
 */
 | 
			
		||||
@Getter
 | 
			
		||||
public abstract class OllamaEndpointCaller {
 | 
			
		||||
 | 
			
		||||
    private static final Logger LOG = LoggerFactory.getLogger(OllamaAPI.class);
 | 
			
		||||
 | 
			
		||||
    private String host;
 | 
			
		||||
    private BasicAuth basicAuth;
 | 
			
		||||
    private long requestTimeoutSeconds;
 | 
			
		||||
    private boolean verbose;
 | 
			
		||||
    private final String host;
 | 
			
		||||
    private final BasicAuth basicAuth;
 | 
			
		||||
    private final long requestTimeoutSeconds;
 | 
			
		||||
    private final boolean verbose;
 | 
			
		||||
 | 
			
		||||
    public OllamaEndpointCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
 | 
			
		||||
        this.host = host;
 | 
			
		||||
@@ -45,80 +47,13 @@ public abstract class OllamaEndpointCaller {
 | 
			
		||||
    protected abstract boolean parseResponseAndAddToBuffer(String line, StringBuilder responseBuffer);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Calls the api server on the given host and endpoint suffix asynchronously, aka waiting for the response.
 | 
			
		||||
     *
 | 
			
		||||
     * @param body POST body payload
 | 
			
		||||
     * @return result answer given by the assistant
 | 
			
		||||
     * @throws OllamaBaseException  any response code than 200 has been returned
 | 
			
		||||
     * @throws IOException          in case the responseStream can not be read
 | 
			
		||||
     * @throws InterruptedException in case the server is not reachable or network issues happen
 | 
			
		||||
     */
 | 
			
		||||
    public OllamaResult callSync(OllamaRequestBody body) throws OllamaBaseException, IOException, InterruptedException {
 | 
			
		||||
        // Create Request
 | 
			
		||||
        long startTime = System.currentTimeMillis();
 | 
			
		||||
        HttpClient httpClient = HttpClient.newHttpClient();
 | 
			
		||||
        URI uri = URI.create(this.host + getEndpointSuffix());
 | 
			
		||||
        HttpRequest.Builder requestBuilder =
 | 
			
		||||
                getRequestBuilderDefault(uri)
 | 
			
		||||
                        .POST(
 | 
			
		||||
                                body.getBodyPublisher());
 | 
			
		||||
        HttpRequest request = requestBuilder.build();
 | 
			
		||||
        if (this.verbose) LOG.info("Asking model: " + body.toString());
 | 
			
		||||
        HttpResponse<InputStream> response =
 | 
			
		||||
                httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
 | 
			
		||||
 | 
			
		||||
        int statusCode = response.statusCode();
 | 
			
		||||
        InputStream responseBodyStream = response.body();
 | 
			
		||||
        StringBuilder responseBuffer = new StringBuilder();
 | 
			
		||||
        try (BufferedReader reader =
 | 
			
		||||
                     new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
 | 
			
		||||
            String line;
 | 
			
		||||
            while ((line = reader.readLine()) != null) {
 | 
			
		||||
                if (statusCode == 404) {
 | 
			
		||||
                    LOG.warn("Status code: 404 (Not Found)");
 | 
			
		||||
                    OllamaErrorResponse ollamaResponseModel =
 | 
			
		||||
                            Utils.getObjectMapper().readValue(line, OllamaErrorResponse.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
                } else if (statusCode == 401) {
 | 
			
		||||
                    LOG.warn("Status code: 401 (Unauthorized)");
 | 
			
		||||
                    OllamaErrorResponse ollamaResponseModel =
 | 
			
		||||
                            Utils.getObjectMapper()
 | 
			
		||||
                                    .readValue("{\"error\":\"Unauthorized\"}", OllamaErrorResponse.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
                } else if (statusCode == 400) {
 | 
			
		||||
                    LOG.warn("Status code: 400 (Bad Request)");
 | 
			
		||||
                    OllamaErrorResponse ollamaResponseModel = Utils.getObjectMapper().readValue(line,
 | 
			
		||||
                            OllamaErrorResponse.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
                } else {
 | 
			
		||||
                    boolean finished = parseResponseAndAddToBuffer(line, responseBuffer);
 | 
			
		||||
                    if (finished) {
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (statusCode != 200) {
 | 
			
		||||
            LOG.error("Status code " + statusCode);
 | 
			
		||||
            throw new OllamaBaseException(responseBuffer.toString());
 | 
			
		||||
        } else {
 | 
			
		||||
            long endTime = System.currentTimeMillis();
 | 
			
		||||
            OllamaResult ollamaResult =
 | 
			
		||||
                    new OllamaResult(responseBuffer.toString().trim(), endTime - startTime, statusCode);
 | 
			
		||||
            if (verbose) LOG.info("Model response: " + ollamaResult);
 | 
			
		||||
            return ollamaResult;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get default request builder.
 | 
			
		||||
     *
 | 
			
		||||
     * @param uri URI to get a HttpRequest.Builder
 | 
			
		||||
     * @return HttpRequest.Builder
 | 
			
		||||
     */
 | 
			
		||||
    private HttpRequest.Builder getRequestBuilderDefault(URI uri) {
 | 
			
		||||
    protected HttpRequest.Builder getRequestBuilderDefault(URI uri) {
 | 
			
		||||
        HttpRequest.Builder requestBuilder =
 | 
			
		||||
                HttpRequest.newBuilder(uri)
 | 
			
		||||
                        .header("Content-Type", "application/json")
 | 
			
		||||
@@ -134,7 +69,7 @@ public abstract class OllamaEndpointCaller {
 | 
			
		||||
     *
 | 
			
		||||
     * @return basic authentication header value (encoded credentials)
 | 
			
		||||
     */
 | 
			
		||||
    private String getBasicAuthHeaderValue() {
 | 
			
		||||
    protected String getBasicAuthHeaderValue() {
 | 
			
		||||
        String credentialsToEncode = this.basicAuth.getUsername() + ":" + this.basicAuth.getPassword();
 | 
			
		||||
        return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
 | 
			
		||||
    }
 | 
			
		||||
@@ -144,7 +79,7 @@ public abstract class OllamaEndpointCaller {
 | 
			
		||||
     *
 | 
			
		||||
     * @return true when Basic Auth credentials set
 | 
			
		||||
     */
 | 
			
		||||
    private boolean isBasicAuthCredentialsSet() {
 | 
			
		||||
    protected boolean isBasicAuthCredentialsSet() {
 | 
			
		||||
        return this.basicAuth != null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package io.github.ollama4j.models.request;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import io.github.ollama4j.exceptions.OllamaBaseException;
 | 
			
		||||
import io.github.ollama4j.models.response.OllamaErrorResponse;
 | 
			
		||||
import io.github.ollama4j.models.response.OllamaResult;
 | 
			
		||||
import io.github.ollama4j.models.generate.OllamaGenerateResponseModel;
 | 
			
		||||
import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
 | 
			
		||||
@@ -11,7 +12,15 @@ import io.github.ollama4j.utils.Utils;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
import java.io.BufferedReader;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.io.InputStreamReader;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.net.http.HttpClient;
 | 
			
		||||
import java.net.http.HttpRequest;
 | 
			
		||||
import java.net.http.HttpResponse;
 | 
			
		||||
import java.nio.charset.StandardCharsets;
 | 
			
		||||
 | 
			
		||||
public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller {
 | 
			
		||||
 | 
			
		||||
@@ -46,6 +55,73 @@ public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller {
 | 
			
		||||
    public OllamaResult call(OllamaRequestBody body, OllamaStreamHandler streamHandler)
 | 
			
		||||
            throws OllamaBaseException, IOException, InterruptedException {
 | 
			
		||||
        streamObserver = new OllamaGenerateStreamObserver(streamHandler);
 | 
			
		||||
        return super.callSync(body);
 | 
			
		||||
        return callSync(body);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Calls the api server on the given host and endpoint suffix asynchronously, aka waiting for the response.
 | 
			
		||||
     *
 | 
			
		||||
     * @param body POST body payload
 | 
			
		||||
     * @return result answer given by the assistant
 | 
			
		||||
     * @throws OllamaBaseException  any response code than 200 has been returned
 | 
			
		||||
     * @throws IOException          in case the responseStream can not be read
 | 
			
		||||
     * @throws InterruptedException in case the server is not reachable or network issues happen
 | 
			
		||||
     */
 | 
			
		||||
    public OllamaResult callSync(OllamaRequestBody body) throws OllamaBaseException, IOException, InterruptedException {
 | 
			
		||||
        // Create Request
 | 
			
		||||
        long startTime = System.currentTimeMillis();
 | 
			
		||||
        HttpClient httpClient = HttpClient.newHttpClient();
 | 
			
		||||
        URI uri = URI.create(getHost() + getEndpointSuffix());
 | 
			
		||||
        HttpRequest.Builder requestBuilder =
 | 
			
		||||
                getRequestBuilderDefault(uri)
 | 
			
		||||
                        .POST(
 | 
			
		||||
                                body.getBodyPublisher());
 | 
			
		||||
        HttpRequest request = requestBuilder.build();
 | 
			
		||||
        if (isVerbose()) LOG.info("Asking model: " + body.toString());
 | 
			
		||||
        HttpResponse<InputStream> response =
 | 
			
		||||
                httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
 | 
			
		||||
 | 
			
		||||
        int statusCode = response.statusCode();
 | 
			
		||||
        InputStream responseBodyStream = response.body();
 | 
			
		||||
        StringBuilder responseBuffer = new StringBuilder();
 | 
			
		||||
        try (BufferedReader reader =
 | 
			
		||||
                     new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
 | 
			
		||||
            String line;
 | 
			
		||||
            while ((line = reader.readLine()) != null) {
 | 
			
		||||
                if (statusCode == 404) {
 | 
			
		||||
                    LOG.warn("Status code: 404 (Not Found)");
 | 
			
		||||
                    OllamaErrorResponse ollamaResponseModel =
 | 
			
		||||
                            Utils.getObjectMapper().readValue(line, OllamaErrorResponse.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
                } else if (statusCode == 401) {
 | 
			
		||||
                    LOG.warn("Status code: 401 (Unauthorized)");
 | 
			
		||||
                    OllamaErrorResponse ollamaResponseModel =
 | 
			
		||||
                            Utils.getObjectMapper()
 | 
			
		||||
                                    .readValue("{\"error\":\"Unauthorized\"}", OllamaErrorResponse.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
                } else if (statusCode == 400) {
 | 
			
		||||
                    LOG.warn("Status code: 400 (Bad Request)");
 | 
			
		||||
                    OllamaErrorResponse ollamaResponseModel = Utils.getObjectMapper().readValue(line,
 | 
			
		||||
                            OllamaErrorResponse.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
                } else {
 | 
			
		||||
                    boolean finished = parseResponseAndAddToBuffer(line, responseBuffer);
 | 
			
		||||
                    if (finished) {
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (statusCode != 200) {
 | 
			
		||||
            LOG.error("Status code " + statusCode);
 | 
			
		||||
            throw new OllamaBaseException(responseBuffer.toString());
 | 
			
		||||
        } else {
 | 
			
		||||
            long endTime = System.currentTimeMillis();
 | 
			
		||||
            OllamaResult ollamaResult =
 | 
			
		||||
                    new OllamaResult(responseBuffer.toString().trim(), endTime - startTime, statusCode);
 | 
			
		||||
            if (isVerbose()) LOG.info("Model response: " + ollamaResult);
 | 
			
		||||
            return ollamaResult;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ public class OllamaResult {
 | 
			
		||||
   *
 | 
			
		||||
   * @return String completion/response text
 | 
			
		||||
   */
 | 
			
		||||
  private final String content;
 | 
			
		||||
  private final String response;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * -- GETTER --
 | 
			
		||||
@@ -35,8 +35,8 @@ public class OllamaResult {
 | 
			
		||||
   */
 | 
			
		||||
  private long responseTime = 0;
 | 
			
		||||
 | 
			
		||||
  public OllamaResult(String content, long responseTime, int httpStatusCode) {
 | 
			
		||||
    this.content = content;
 | 
			
		||||
  public OllamaResult(String response, long responseTime, int httpStatusCode) {
 | 
			
		||||
    this.response = response;
 | 
			
		||||
    this.responseTime = responseTime;
 | 
			
		||||
    this.httpStatusCode = httpStatusCode;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user