mirror of
				https://github.com/amithkoujalgi/ollama4j.git
				synced 2025-11-04 10:30:41 +01:00 
			
		
		
		
	Renames request caller to endpoint caller
This commit is contained in:
		@@ -0,0 +1,44 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models.request;
 | 
			
		||||
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatResponseModel;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Specialization class for requests
 | 
			
		||||
 */
 | 
			
		||||
public class OllamaChatEndpointCaller extends OllamaEndpointCaller{
 | 
			
		||||
 | 
			
		||||
    private static final Logger LOG = LoggerFactory.getLogger(OllamaChatEndpointCaller.class);
 | 
			
		||||
 | 
			
		||||
    public OllamaChatEndpointCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
 | 
			
		||||
        super(host, basicAuth, requestTimeoutSeconds, verbose);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected String getEndpointSuffix() {
 | 
			
		||||
        return "/api/chat";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected boolean parseResponseAndAddToBuffer(String line, StringBuilder responseBuffer) {
 | 
			
		||||
                try {
 | 
			
		||||
                    OllamaChatResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line, OllamaChatResponseModel.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getMessage().getContent());
 | 
			
		||||
                    return ollamaResponseModel.isDone();
 | 
			
		||||
                } catch (JsonProcessingException e) {
 | 
			
		||||
                    LOG.error("Error parsing the Ollama chat response!",e);
 | 
			
		||||
                    return true;
 | 
			
		||||
                }         
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,16 +0,0 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models.request;
 | 
			
		||||
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
 | 
			
		||||
 | 
			
		||||
public class OllamaChatRequestCaller extends OllamaServerCaller{
 | 
			
		||||
 | 
			
		||||
    public OllamaChatRequestCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
 | 
			
		||||
        super(host, basicAuth, requestTimeoutSeconds, verbose);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected String getEndpointSuffix() {
 | 
			
		||||
        return "/api/generate";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -27,7 +27,7 @@ import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
 | 
			
		||||
/**
 | 
			
		||||
 * Abstract helperclass to call the ollama api server.
 | 
			
		||||
 */
 | 
			
		||||
public abstract class OllamaServerCaller {
 | 
			
		||||
public abstract class OllamaEndpointCaller {
 | 
			
		||||
    
 | 
			
		||||
    private static final Logger LOG = LoggerFactory.getLogger(OllamaAPI.class);
 | 
			
		||||
 | 
			
		||||
@@ -36,7 +36,7 @@ public abstract class OllamaServerCaller {
 | 
			
		||||
    private long requestTimeoutSeconds;
 | 
			
		||||
    private boolean verbose;
 | 
			
		||||
 | 
			
		||||
    public OllamaServerCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
 | 
			
		||||
    public OllamaEndpointCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
 | 
			
		||||
        this.host = host;
 | 
			
		||||
        this.basicAuth = basicAuth;
 | 
			
		||||
        this.requestTimeoutSeconds = requestTimeoutSeconds;
 | 
			
		||||
@@ -44,6 +44,9 @@ public abstract class OllamaServerCaller {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected abstract String getEndpointSuffix();
 | 
			
		||||
 | 
			
		||||
    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.
 | 
			
		||||
@@ -89,11 +92,10 @@ public abstract class OllamaServerCaller {
 | 
			
		||||
                  .readValue("{\"error\":\"Unauthorized\"}", OllamaErrorResponseModel.class);
 | 
			
		||||
          responseBuffer.append(ollamaResponseModel.getError());
 | 
			
		||||
        } else {
 | 
			
		||||
          OllamaResponseModel ollamaResponseModel =
 | 
			
		||||
              Utils.getObjectMapper().readValue(line, OllamaResponseModel.class);
 | 
			
		||||
          if (!ollamaResponseModel.isDone()) {
 | 
			
		||||
            responseBuffer.append(ollamaResponseModel.getResponse());
 | 
			
		||||
          }
 | 
			
		||||
          boolean finished = parseResponseAndAddToBuffer(line,responseBuffer);
 | 
			
		||||
            if (finished) {
 | 
			
		||||
              break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
@@ -0,0 +1,40 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models.request;
 | 
			
		||||
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.models.OllamaResponseModel;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
 | 
			
		||||
 | 
			
		||||
public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller{
 | 
			
		||||
 | 
			
		||||
    private static final Logger LOG = LoggerFactory.getLogger(OllamaGenerateEndpointCaller.class);
 | 
			
		||||
 | 
			
		||||
    public OllamaGenerateEndpointCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
 | 
			
		||||
        super(host, basicAuth, requestTimeoutSeconds, verbose);   
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected String getEndpointSuffix() {
 | 
			
		||||
        return "/api/generate";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected boolean parseResponseAndAddToBuffer(String line, StringBuilder responseBuffer) {
 | 
			
		||||
                try {
 | 
			
		||||
                    OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line, OllamaResponseModel.class);
 | 
			
		||||
                    responseBuffer.append(ollamaResponseModel.getResponse());
 | 
			
		||||
                    return ollamaResponseModel.isDone();
 | 
			
		||||
                } catch (JsonProcessingException e) {
 | 
			
		||||
                    LOG.error("Error parsing the Ollama chat response!",e);
 | 
			
		||||
                    return true;
 | 
			
		||||
                }         
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
@@ -1,18 +0,0 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models.request;
 | 
			
		||||
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
 | 
			
		||||
 | 
			
		||||
public class OllamaGenerateRequestCaller extends OllamaServerCaller{
 | 
			
		||||
 | 
			
		||||
    public OllamaGenerateRequestCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
 | 
			
		||||
        super(host, basicAuth, requestTimeoutSeconds, verbose);   
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected String getEndpointSuffix() {
 | 
			
		||||
        return "/api/generate";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user