diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessage.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessage.java new file mode 100644 index 0000000..cd0becd --- /dev/null +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessage.java @@ -0,0 +1,40 @@ +package io.github.amithkoujalgi.ollama4j.core.models.chat; + +import com.fasterxml.jackson.core.JsonProcessingException; +import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper; + +import java.io.File; +import java.util.List; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; + +/** + * Defines a single Message to be used inside a chat request against the ollama /api/chat endpoint. + * + * @see https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion + */ +@Data +@AllArgsConstructor +@RequiredArgsConstructor +public class OllamaChatMessage { + + @NonNull + private OllamaChatMessageRole role; + + @NonNull + private String content; + + private List images; + + @Override + public String toString() { + try { + return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessageRole.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessageRole.java new file mode 100644 index 0000000..ec7f9be --- /dev/null +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessageRole.java @@ -0,0 +1,16 @@ +package io.github.amithkoujalgi.ollama4j.core.models.chat; + +/** + * Defines the possible Chat Message roles. + */ +public enum OllamaChatMessageRole { + SYSTEM("system"), + USER("user"), + ASSISTANT("assisstant"); + + private String roleName; + + private OllamaChatMessageRole(String roleName){ + this.roleName = roleName; + } +} diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestBuilder.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestBuilder.java new file mode 100644 index 0000000..bcd710c --- /dev/null +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestBuilder.java @@ -0,0 +1,64 @@ +package io.github.amithkoujalgi.ollama4j.core.models.chat; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import io.github.amithkoujalgi.ollama4j.core.utils.Options; + +/** + * Helper class for creating {@link OllamaChatRequestModel} objects using the builder-pattern. + */ +public class OllamaChatRequestBuilder { + + private OllamaChatRequestBuilder(String model, List messages){ + request = new OllamaChatRequestModel(model, messages); + } + + private OllamaChatRequestModel request; + + public static OllamaChatRequestBuilder getInstance(String model){ + return new OllamaChatRequestBuilder(model, new ArrayList<>()); + } + + public OllamaChatRequestModel build(){ + return request; + } + + public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, File... images){ + List messages = this.request.getMessages(); + messages.add(new OllamaChatMessage(role,content,List.of(images))); + return this; + } + + public OllamaChatRequestBuilder withMessages(List messages){ + this.request.setMessages(messages); + return this; + } + + public OllamaChatRequestBuilder withOptions(Options options){ + this.request.setOptions(options); + return this; + } + + public OllamaChatRequestBuilder withFormat(String format){ + this.request.setFormat(format); + return this; + } + + public OllamaChatRequestBuilder withTemplate(String template){ + this.request.setTemplate(template); + return this; + } + + public OllamaChatRequestBuilder withStreaming(){ + this.request.setStream(true); + return this; + } + + public OllamaChatRequestBuilder withKeepAlive(String keepAlive){ + this.request.setKeepAlive(keepAlive); + return this; + } + +} diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestModel.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestModel.java new file mode 100644 index 0000000..6ada64d --- /dev/null +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestModel.java @@ -0,0 +1,61 @@ +package io.github.amithkoujalgi.ollama4j.core.models.chat; + +import java.net.http.HttpRequest; +import java.net.http.HttpRequest.BodyPublisher; +import java.util.List; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody; +import io.github.amithkoujalgi.ollama4j.core.utils.Options; +import io.github.amithkoujalgi.ollama4j.core.utils.Utils; + +import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; + +/** + * Defines a Request to use against the ollama /api/chat endpoint. + * + * @see https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion + */ +@Data +@AllArgsConstructor +@RequiredArgsConstructor +public class OllamaChatRequestModel implements OllamaRequestBody{ + + @NonNull + private String model; + + @NonNull + private List messages; + + private String format; + private Options options; + private String template; + private boolean stream; + private String keepAlive; + + @Override + public String toString() { + try { + return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + @Override + public BodyPublisher getBodyPublisher() { + try { + return HttpRequest.BodyPublishers.ofString( + Utils.getObjectMapper().writeValueAsString(this)); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException("Request not Body convertible.",e); + } + } + +} \ No newline at end of file diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/OllamaRequestBody.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/OllamaRequestBody.java new file mode 100644 index 0000000..1c4d533 --- /dev/null +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/OllamaRequestBody.java @@ -0,0 +1,26 @@ +package io.github.amithkoujalgi.ollama4j.core.utils; + +import java.net.http.HttpRequest.BodyPublisher; +import java.net.http.HttpRequest.BodyPublishers; + +import com.fasterxml.jackson.core.JsonProcessingException; + +/** + * Interface to represent a OllamaRequest as HTTP-Request Body via {@link BodyPublishers}. + */ +public interface OllamaRequestBody { + + /** + * Transforms the OllamaRequest Object to a JSON Object via Jackson. + * + * @return JSON representation of a OllamaRequest + */ + default BodyPublisher getBodyPublisher(){ + try { + return BodyPublishers.ofString( + Utils.getObjectMapper().writeValueAsString(this)); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException("Request not Body convertible.",e); + } + } +}