mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 20:07:10 +02:00
Renames request caller to endpoint caller
This commit is contained in:
parent
14982011d9
commit
a635dd9be2
@ -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.
|
* 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);
|
private static final Logger LOG = LoggerFactory.getLogger(OllamaAPI.class);
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ public abstract class OllamaServerCaller {
|
|||||||
private long requestTimeoutSeconds;
|
private long requestTimeoutSeconds;
|
||||||
private boolean verbose;
|
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.host = host;
|
||||||
this.basicAuth = basicAuth;
|
this.basicAuth = basicAuth;
|
||||||
this.requestTimeoutSeconds = requestTimeoutSeconds;
|
this.requestTimeoutSeconds = requestTimeoutSeconds;
|
||||||
@ -45,6 +45,9 @@ public abstract class OllamaServerCaller {
|
|||||||
|
|
||||||
protected abstract String getEndpointSuffix();
|
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.
|
* Calls the api server on the given host and endpoint suffix asynchronously, aka waiting for the response.
|
||||||
*
|
*
|
||||||
@ -89,10 +92,9 @@ public abstract class OllamaServerCaller {
|
|||||||
.readValue("{\"error\":\"Unauthorized\"}", OllamaErrorResponseModel.class);
|
.readValue("{\"error\":\"Unauthorized\"}", OllamaErrorResponseModel.class);
|
||||||
responseBuffer.append(ollamaResponseModel.getError());
|
responseBuffer.append(ollamaResponseModel.getError());
|
||||||
} else {
|
} else {
|
||||||
OllamaResponseModel ollamaResponseModel =
|
boolean finished = parseResponseAndAddToBuffer(line,responseBuffer);
|
||||||
Utils.getObjectMapper().readValue(line, OllamaResponseModel.class);
|
if (finished) {
|
||||||
if (!ollamaResponseModel.isDone()) {
|
break;
|
||||||
responseBuffer.append(ollamaResponseModel.getResponse());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user