Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaGenerateEndpointCaller.java
Go to the documentation of this file.
1package io.github.ollama4j.models.request;
2
3import com.fasterxml.jackson.core.JsonProcessingException;
4import io.github.ollama4j.exceptions.OllamaBaseException;
5import io.github.ollama4j.models.response.OllamaResult;
6import io.github.ollama4j.models.generate.OllamaGenerateResponseModel;
7import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
8import io.github.ollama4j.models.generate.OllamaStreamHandler;
9import io.github.ollama4j.utils.OllamaRequestBody;
10import io.github.ollama4j.utils.Utils;
11import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
14import java.io.IOException;
15
17
18 private static final Logger LOG = LoggerFactory.getLogger(OllamaGenerateEndpointCaller.class);
19
20 private OllamaGenerateStreamObserver streamObserver;
21
22 public OllamaGenerateEndpointCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
23 super(host, basicAuth, requestTimeoutSeconds, verbose);
24 }
25
26 @Override
27 protected String getEndpointSuffix() {
28 return "/api/generate";
29 }
30
31 @Override
32 protected boolean parseResponseAndAddToBuffer(String line, StringBuilder responseBuffer) {
33 try {
34 OllamaGenerateResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line, OllamaGenerateResponseModel.class);
35 responseBuffer.append(ollamaResponseModel.getResponse());
36 if (streamObserver != null) {
37 streamObserver.notify(ollamaResponseModel);
38 }
39 return ollamaResponseModel.isDone();
40 } catch (JsonProcessingException e) {
41 LOG.error("Error parsing the Ollama chat response!", e);
42 return true;
43 }
44 }
45
47 throws OllamaBaseException, IOException, InterruptedException {
48 streamObserver = new OllamaGenerateStreamObserver(streamHandler);
49 return super.callSync(body);
50 }
51}
void notify(OllamaGenerateResponseModel currentResponsePart)
OllamaResult call(OllamaRequestBody body, OllamaStreamHandler streamHandler)
boolean parseResponseAndAddToBuffer(String line, StringBuilder responseBuffer)
OllamaGenerateEndpointCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose)
static ObjectMapper getObjectMapper()
Definition Utils.java:17