mirror of
				https://github.com/amithkoujalgi/ollama4j.git
				synced 2025-11-04 10:30:41 +01:00 
			
		
		
		
	updated readme
This commit is contained in:
		@@ -2,7 +2,6 @@ package io.github.amithkoujalgi.ollama4j.core.models;
 | 
			
		||||
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
 | 
			
		||||
 | 
			
		||||
import java.io.BufferedReader;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
@@ -17,79 +16,129 @@ import java.util.Queue;
 | 
			
		||||
 | 
			
		||||
@SuppressWarnings("unused")
 | 
			
		||||
public class OllamaAsyncResultCallback extends Thread {
 | 
			
		||||
    private final HttpClient client;
 | 
			
		||||
    private final URI uri;
 | 
			
		||||
    private final OllamaRequestModel ollamaRequestModel;
 | 
			
		||||
    private final Queue<String> queue = new LinkedList<>();
 | 
			
		||||
    private String result;
 | 
			
		||||
    private boolean isDone;
 | 
			
		||||
    private long responseTime = 0;
 | 
			
		||||
  private final HttpClient client;
 | 
			
		||||
  private final URI uri;
 | 
			
		||||
  private final OllamaRequestModel ollamaRequestModel;
 | 
			
		||||
  private final Queue<String> queue = new LinkedList<>();
 | 
			
		||||
  private String result;
 | 
			
		||||
  private boolean isDone;
 | 
			
		||||
  private boolean succeeded;
 | 
			
		||||
 | 
			
		||||
    public OllamaAsyncResultCallback(HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) {
 | 
			
		||||
        this.client = client;
 | 
			
		||||
        this.ollamaRequestModel = ollamaRequestModel;
 | 
			
		||||
        this.uri = uri;
 | 
			
		||||
        this.isDone = false;
 | 
			
		||||
        this.result = "";
 | 
			
		||||
        this.queue.add("");
 | 
			
		||||
    }
 | 
			
		||||
  private int httpStatusCode;
 | 
			
		||||
  private long responseTime = 0;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void run() {
 | 
			
		||||
        try {
 | 
			
		||||
            long startTime = System.currentTimeMillis();
 | 
			
		||||
            HttpRequest request = HttpRequest.newBuilder(uri).POST(HttpRequest.BodyPublishers.ofString(Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))).header("Content-Type", "application/json").build();
 | 
			
		||||
            HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
 | 
			
		||||
            int statusCode = response.statusCode();
 | 
			
		||||
  public OllamaAsyncResultCallback(
 | 
			
		||||
      HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) {
 | 
			
		||||
    this.client = client;
 | 
			
		||||
    this.ollamaRequestModel = ollamaRequestModel;
 | 
			
		||||
    this.uri = uri;
 | 
			
		||||
    this.isDone = false;
 | 
			
		||||
    this.result = "";
 | 
			
		||||
    this.queue.add("");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
            InputStream responseBodyStream = response.body();
 | 
			
		||||
            String responseString = "";
 | 
			
		||||
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
 | 
			
		||||
                String line;
 | 
			
		||||
                StringBuilder responseBuffer = new StringBuilder();
 | 
			
		||||
                while ((line = reader.readLine()) != null) {
 | 
			
		||||
                    OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line, OllamaResponseModel.class);
 | 
			
		||||
                    queue.add(ollamaResponseModel.getResponse());
 | 
			
		||||
                    if (!ollamaResponseModel.getDone()) {
 | 
			
		||||
                        responseBuffer.append(ollamaResponseModel.getResponse());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                reader.close();
 | 
			
		||||
                this.isDone = true;
 | 
			
		||||
                this.result = responseBuffer.toString();
 | 
			
		||||
                long endTime = System.currentTimeMillis();
 | 
			
		||||
                responseTime = endTime - startTime;
 | 
			
		||||
  @Override
 | 
			
		||||
  public void run() {
 | 
			
		||||
    try {
 | 
			
		||||
      long startTime = System.currentTimeMillis();
 | 
			
		||||
      HttpRequest request =
 | 
			
		||||
          HttpRequest.newBuilder(uri)
 | 
			
		||||
              .POST(
 | 
			
		||||
                  HttpRequest.BodyPublishers.ofString(
 | 
			
		||||
                      Utils.getObjectMapper().writeValueAsString(ollamaRequestModel)))
 | 
			
		||||
              .header("Content-Type", "application/json")
 | 
			
		||||
              .build();
 | 
			
		||||
      HttpResponse<InputStream> response =
 | 
			
		||||
          client.send(request, HttpResponse.BodyHandlers.ofInputStream());
 | 
			
		||||
      int statusCode = response.statusCode();
 | 
			
		||||
      this.httpStatusCode = statusCode;
 | 
			
		||||
 | 
			
		||||
      InputStream responseBodyStream = response.body();
 | 
			
		||||
      try (BufferedReader reader =
 | 
			
		||||
          new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
 | 
			
		||||
        String line;
 | 
			
		||||
        StringBuilder responseBuffer = new StringBuilder();
 | 
			
		||||
        while ((line = reader.readLine()) != null) {
 | 
			
		||||
          if (statusCode == 404) {
 | 
			
		||||
            OllamaErrorResponseModel ollamaResponseModel =
 | 
			
		||||
                Utils.getObjectMapper().readValue(line, OllamaErrorResponseModel.class);
 | 
			
		||||
            queue.add(ollamaResponseModel.getError());
 | 
			
		||||
            responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
          } else {
 | 
			
		||||
            OllamaResponseModel ollamaResponseModel =
 | 
			
		||||
                Utils.getObjectMapper().readValue(line, OllamaResponseModel.class);
 | 
			
		||||
            queue.add(ollamaResponseModel.getResponse());
 | 
			
		||||
            if (!ollamaResponseModel.getDone()) {
 | 
			
		||||
              responseBuffer.append(ollamaResponseModel.getResponse());
 | 
			
		||||
            }
 | 
			
		||||
            if (statusCode != 200) {
 | 
			
		||||
                throw new OllamaBaseException(statusCode + " - " + responseString);
 | 
			
		||||
            }
 | 
			
		||||
        } catch (IOException | InterruptedException | OllamaBaseException e) {
 | 
			
		||||
            this.isDone = true;
 | 
			
		||||
            this.result = "FAILED! " + e.getMessage();
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
        reader.close();
 | 
			
		||||
 | 
			
		||||
    public boolean isComplete() {
 | 
			
		||||
        return isDone;
 | 
			
		||||
        this.isDone = true;
 | 
			
		||||
        this.succeeded = true;
 | 
			
		||||
        this.result = responseBuffer.toString();
 | 
			
		||||
        long endTime = System.currentTimeMillis();
 | 
			
		||||
        responseTime = endTime - startTime;
 | 
			
		||||
      }
 | 
			
		||||
      if (statusCode != 200) {
 | 
			
		||||
        throw new OllamaBaseException(this.result);
 | 
			
		||||
      }
 | 
			
		||||
    } catch (IOException | InterruptedException | OllamaBaseException e) {
 | 
			
		||||
      this.isDone = true;
 | 
			
		||||
      this.succeeded = false;
 | 
			
		||||
      this.result = "[FAILED] " + e.getMessage();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the final response when the execution completes. Does not return intermediate results.
 | 
			
		||||
     * @return response text
 | 
			
		||||
     */
 | 
			
		||||
    public String getResponse() {
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
  /**
 | 
			
		||||
   * Returns the status of the thread. This does not indicate that the request was successful or a
 | 
			
		||||
   * failure, rather it is just a status flag to indicate if the thread is active or ended.
 | 
			
		||||
   *
 | 
			
		||||
   * @return boolean - status
 | 
			
		||||
   */
 | 
			
		||||
  public boolean isComplete() {
 | 
			
		||||
    return isDone;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
    public Queue<String> getStream() {
 | 
			
		||||
        return queue;
 | 
			
		||||
    }
 | 
			
		||||
  /**
 | 
			
		||||
   * Returns the HTTP response status code for the request that was made to Ollama server.
 | 
			
		||||
   *
 | 
			
		||||
   * @return int - the status code for the request
 | 
			
		||||
   */
 | 
			
		||||
  public int getHttpStatusCode() {
 | 
			
		||||
    return httpStatusCode;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the response time in seconds.
 | 
			
		||||
     * @return response time in seconds
 | 
			
		||||
     */
 | 
			
		||||
    public long getResponseTime() {
 | 
			
		||||
        return responseTime;
 | 
			
		||||
    }
 | 
			
		||||
  /**
 | 
			
		||||
   * Returns the status of the request. Indicates if the request was successful or a failure. If the
 | 
			
		||||
   * request was a failure, the `getResponse()` method will return the error message.
 | 
			
		||||
   *
 | 
			
		||||
   * @return boolean - status
 | 
			
		||||
   */
 | 
			
		||||
  public boolean isSucceeded() {
 | 
			
		||||
    return succeeded;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Returns the final response when the execution completes. Does not return intermediate results.
 | 
			
		||||
   *
 | 
			
		||||
   * @return String - response text
 | 
			
		||||
   */
 | 
			
		||||
  public String getResponse() {
 | 
			
		||||
    return result;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public Queue<String> getStream() {
 | 
			
		||||
    return queue;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Returns the response time in milliseconds.
 | 
			
		||||
   *
 | 
			
		||||
   * @return long - response time in milliseconds.
 | 
			
		||||
   */
 | 
			
		||||
  public long getResponseTime() {
 | 
			
		||||
    return responseTime;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,18 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonProperty;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@JsonIgnoreProperties(ignoreUnknown = true)
 | 
			
		||||
public class OllamaErrorResponseModel {
 | 
			
		||||
  private String error;
 | 
			
		||||
 | 
			
		||||
  public String getError() {
 | 
			
		||||
    return error;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setError(String error) {
 | 
			
		||||
    this.error = error;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,20 +1,45 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models;
 | 
			
		||||
 | 
			
		||||
import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
 | 
			
		||||
/** The type Ollama result. */
 | 
			
		||||
@SuppressWarnings("unused")
 | 
			
		||||
public class OllamaResult {
 | 
			
		||||
    private String response;
 | 
			
		||||
    private long responseTime = 0;
 | 
			
		||||
  private final String response;
 | 
			
		||||
 | 
			
		||||
    public OllamaResult(String response, long responseTime) {
 | 
			
		||||
        this.response = response;
 | 
			
		||||
        this.responseTime = responseTime;
 | 
			
		||||
    }
 | 
			
		||||
  private long responseTime = 0;
 | 
			
		||||
 | 
			
		||||
    public String getResponse() {
 | 
			
		||||
        return response;
 | 
			
		||||
    }
 | 
			
		||||
  public OllamaResult(String response, long responseTime) {
 | 
			
		||||
    this.response = response;
 | 
			
		||||
    this.responseTime = responseTime;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
    public long getResponseTime() {
 | 
			
		||||
        return responseTime;
 | 
			
		||||
  /**
 | 
			
		||||
   * Get the response text
 | 
			
		||||
   *
 | 
			
		||||
   * @return String - response text
 | 
			
		||||
   */
 | 
			
		||||
  public String getResponse() {
 | 
			
		||||
    return response;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Get the response time in milliseconds.
 | 
			
		||||
   *
 | 
			
		||||
   * @return long - response time in milliseconds
 | 
			
		||||
   */
 | 
			
		||||
  public long getResponseTime() {
 | 
			
		||||
    return responseTime;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String toString() {
 | 
			
		||||
    try {
 | 
			
		||||
      return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
 | 
			
		||||
    } catch (JsonProcessingException e) {
 | 
			
		||||
      throw new RuntimeException(e);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user