mirror of
				https://github.com/amithkoujalgi/ollama4j.git
				synced 2025-11-04 10:30:41 +01:00 
			
		
		
		
	Adds additional properties to chat and generate requests
This commit is contained in:
		@@ -1,26 +1,35 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models;
 | 
			
		||||
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonInclude;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonProperty;
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 | 
			
		||||
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.utils.BooleanToJsonFormatFlagSerializer;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.NonNull;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
@JsonInclude(JsonInclude.Include.NON_NULL)
 | 
			
		||||
public abstract class OllamaCommonRequestModel {
 | 
			
		||||
  
 | 
			
		||||
  @NonNull
 | 
			
		||||
  protected String model;  
 | 
			
		||||
  @JsonSerialize(using = BooleanToJsonFormatFlagSerializer.class)
 | 
			
		||||
  protected boolean returnFormatJson;
 | 
			
		||||
  @JsonProperty(value = "format")
 | 
			
		||||
  protected Boolean returnFormatJson;
 | 
			
		||||
  protected Map<String, Object> options;
 | 
			
		||||
  protected String template;
 | 
			
		||||
  protected boolean stream;
 | 
			
		||||
  @JsonProperty(value = "keep_alive")
 | 
			
		||||
  protected String keepAlive;
 | 
			
		||||
 | 
			
		||||
  
 | 
			
		||||
  public String toString() {
 | 
			
		||||
    try {
 | 
			
		||||
      return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
 | 
			
		||||
    } catch (JsonProcessingException e) {
 | 
			
		||||
      throw new RuntimeException(e);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,42 +1,39 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models.chat;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.models.OllamaCommonRequestModel;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody;
 | 
			
		||||
 | 
			
		||||
import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
import lombok.NonNull;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import lombok.Setter;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Defines a Request to use against the ollama /api/chat endpoint.
 | 
			
		||||
 *
 | 
			
		||||
 * @see <a
 | 
			
		||||
 *     href="https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion">Generate
 | 
			
		||||
 *     Chat Completion</a>
 | 
			
		||||
 * @see <a href=
 | 
			
		||||
 *      "https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion">Generate
 | 
			
		||||
 *      Chat Completion</a>
 | 
			
		||||
 */
 | 
			
		||||
@Data
 | 
			
		||||
@EqualsAndHashCode(callSuper = true)
 | 
			
		||||
@Getter
 | 
			
		||||
@Setter
 | 
			
		||||
public class OllamaChatRequestModel extends OllamaCommonRequestModel implements OllamaRequestBody {
 | 
			
		||||
 | 
			
		||||
  @NonNull private List<OllamaChatMessage> messages;
 | 
			
		||||
  private List<OllamaChatMessage> messages;
 | 
			
		||||
 | 
			
		||||
  public OllamaChatRequestModel(String model,List<OllamaChatMessage> messages){
 | 
			
		||||
    super(model);
 | 
			
		||||
  public OllamaChatRequestModel() {}
 | 
			
		||||
 | 
			
		||||
  public OllamaChatRequestModel(String model, List<OllamaChatMessage> messages) {
 | 
			
		||||
    this.model = model;
 | 
			
		||||
    this.messages = messages;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String toString() {
 | 
			
		||||
    try {
 | 
			
		||||
      return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
 | 
			
		||||
    } catch (JsonProcessingException e) {
 | 
			
		||||
      throw new RuntimeException(e);
 | 
			
		||||
  public boolean equals(Object o) {
 | 
			
		||||
    if (!(o instanceof OllamaChatRequestModel)) {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return this.toString().equals(o.toString());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +1,18 @@
 | 
			
		||||
package io.github.amithkoujalgi.ollama4j.core.models.generate;
 | 
			
		||||
 | 
			
		||||
import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.models.OllamaCommonRequestModel;
 | 
			
		||||
import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
import lombok.NonNull;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@EqualsAndHashCode(callSuper = true)
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import lombok.Setter;
 | 
			
		||||
 | 
			
		||||
@Getter
 | 
			
		||||
@Setter
 | 
			
		||||
public class OllamaGenerateRequestModel extends OllamaCommonRequestModel implements OllamaRequestBody{
 | 
			
		||||
 | 
			
		||||
  @NonNull
 | 
			
		||||
  private String prompt;
 | 
			
		||||
  private List<String> images;
 | 
			
		||||
 | 
			
		||||
@@ -24,23 +20,27 @@ public class OllamaGenerateRequestModel extends OllamaCommonRequestModel impleme
 | 
			
		||||
  private String context;
 | 
			
		||||
  private boolean raw;
 | 
			
		||||
 | 
			
		||||
  public OllamaGenerateRequestModel() {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public OllamaGenerateRequestModel(String model, String prompt) {
 | 
			
		||||
    super(model);
 | 
			
		||||
    this.model = model;
 | 
			
		||||
    this.prompt = prompt;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public OllamaGenerateRequestModel(String model, String prompt, List<String> images) {
 | 
			
		||||
    super(model);
 | 
			
		||||
    this.model = model;
 | 
			
		||||
    this.prompt = prompt;
 | 
			
		||||
    this.images = images;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String toString() {
 | 
			
		||||
    try {
 | 
			
		||||
      return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
 | 
			
		||||
    } catch (JsonProcessingException e) {
 | 
			
		||||
      throw new RuntimeException(e);
 | 
			
		||||
    @Override
 | 
			
		||||
  public boolean equals(Object o) {
 | 
			
		||||
    if (!(o instanceof OllamaGenerateRequestModel)) {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return this.toString().equals(o.toString());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user