
com.cohere.api.requests.GenerateRequest Maven / Gradle / Ivy
Show all versions of cohere-java Show documentation
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.cohere.api.requests;
import com.cohere.api.core.ObjectMappers;
import com.cohere.api.types.GenerateRequestReturnLikelihoods;
import com.cohere.api.types.GenerateRequestTruncate;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonDeserialize(builder = GenerateRequest.Builder.class)
public final class GenerateRequest {
private final String prompt;
private final Optional model;
private final Optional numGenerations;
private final Optional maxTokens;
private final Optional truncate;
private final Optional temperature;
private final Optional seed;
private final Optional preset;
private final Optional> endSequences;
private final Optional> stopSequences;
private final Optional k;
private final Optional p;
private final Optional frequencyPenalty;
private final Optional presencePenalty;
private final Optional returnLikelihoods;
private final Optional rawPrompting;
private final Map additionalProperties;
private GenerateRequest(
String prompt,
Optional model,
Optional numGenerations,
Optional maxTokens,
Optional truncate,
Optional temperature,
Optional seed,
Optional preset,
Optional> endSequences,
Optional> stopSequences,
Optional k,
Optional p,
Optional frequencyPenalty,
Optional presencePenalty,
Optional returnLikelihoods,
Optional rawPrompting,
Map additionalProperties) {
this.prompt = prompt;
this.model = model;
this.numGenerations = numGenerations;
this.maxTokens = maxTokens;
this.truncate = truncate;
this.temperature = temperature;
this.seed = seed;
this.preset = preset;
this.endSequences = endSequences;
this.stopSequences = stopSequences;
this.k = k;
this.p = p;
this.frequencyPenalty = frequencyPenalty;
this.presencePenalty = presencePenalty;
this.returnLikelihoods = returnLikelihoods;
this.rawPrompting = rawPrompting;
this.additionalProperties = additionalProperties;
}
/**
* @return The input text that serves as the starting point for generating the response.
* Note: The prompt will be pre-processed and modified before reaching the model.
*/
@JsonProperty("prompt")
public String getPrompt() {
return prompt;
}
/**
* @return The identifier of the model to generate with. Currently available models are command
(default), command-nightly
(experimental), command-light
, and command-light-nightly
(experimental).
* Smaller, "light" models are faster, while larger models will perform better. Custom models can also be supplied with their full ID.
*/
@JsonProperty("model")
public Optional getModel() {
return model;
}
/**
* @return The maximum number of generations that will be returned. Defaults to 1
, min value of 1
, max value of 5
.
*/
@JsonProperty("num_generations")
public Optional getNumGenerations() {
return numGenerations;
}
@JsonProperty("stream")
public Boolean getStream() {
return false;
}
/**
* @return The maximum number of tokens the model will generate as part of the response. Note: Setting a low value may result in incomplete generations.
* This parameter is off by default, and if it's not specified, the model will continue generating until it emits an EOS completion token. See BPE Tokens for more details.
* Can only be set to 0
if return_likelihoods
is set to ALL
to get the likelihood of the prompt.
*/
@JsonProperty("max_tokens")
public Optional getMaxTokens() {
return maxTokens;
}
/**
* @return One of NONE|START|END
to specify how the API will handle inputs longer than the maximum token length.
* Passing START
will discard the start of the input. END
will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model.
* If NONE
is selected, when the input exceeds the maximum input token length an error will be returned.
*/
@JsonProperty("truncate")
public Optional getTruncate() {
return truncate;
}
/**
* @return A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations. See Temperature for more details.
* Defaults to 0.75
, min value of 0.0
, max value of 5.0
.
*/
@JsonProperty("temperature")
public Optional getTemperature() {
return temperature;
}
/**
* @return If specified, the backend will make a best effort to sample tokens deterministically, such that repeated requests with the same seed and parameters should return the same result. However, determinsim cannot be totally guaranteed.
*/
@JsonProperty("seed")
public Optional getSeed() {
return seed;
}
/**
* @return Identifier of a custom preset. A preset is a combination of parameters, such as prompt, temperature etc. You can create presets in the playground.
* When a preset is specified, the prompt
parameter becomes optional, and any included parameters will override the preset's parameters.
*/
@JsonProperty("preset")
public Optional getPreset() {
return preset;
}
/**
* @return The generated text will be cut at the beginning of the earliest occurrence of an end sequence. The sequence will be excluded from the text.
*/
@JsonProperty("end_sequences")
public Optional> getEndSequences() {
return endSequences;
}
/**
* @return The generated text will be cut at the end of the earliest occurrence of a stop sequence. The sequence will be included the text.
*/
@JsonProperty("stop_sequences")
public Optional> getStopSequences() {
return stopSequences;
}
/**
* @return Ensures only the top k
most likely tokens are considered for generation at each step.
* Defaults to 0
, min value of 0
, max value of 500
.
*/
@JsonProperty("k")
public Optional getK() {
return k;
}
/**
* @return Ensures that only the most likely tokens, with total probability mass of p
, are considered for generation at each step. If both k
and p
are enabled, p
acts after k
.
* Defaults to 0.75
. min value of 0.01
, max value of 0.99
.
*/
@JsonProperty("p")
public Optional getP() {
return p;
}
/**
* @return Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
* Using frequency_penalty
in combination with presence_penalty
is not supported on newer models.
*/
@JsonProperty("frequency_penalty")
public Optional getFrequencyPenalty() {
return frequencyPenalty;
}
/**
* @return Defaults to 0.0
, min value of 0.0
, max value of 1.0
.
* Can be used to reduce repetitiveness of generated tokens. Similar to frequency_penalty
, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
* Using frequency_penalty
in combination with presence_penalty
is not supported on newer models.
*/
@JsonProperty("presence_penalty")
public Optional getPresencePenalty() {
return presencePenalty;
}
/**
* @return One of GENERATION|ALL|NONE
to specify how and if the token likelihoods are returned with the response. Defaults to NONE
.
* If GENERATION
is selected, the token likelihoods will only be provided for generated text.
* If ALL
is selected, the token likelihoods will be provided both for the prompt and the generated text.
*/
@JsonProperty("return_likelihoods")
public Optional getReturnLikelihoods() {
return returnLikelihoods;
}
/**
* @return When enabled, the user's prompt will be sent to the model without any pre-processing.
*/
@JsonProperty("raw_prompting")
public Optional getRawPrompting() {
return rawPrompting;
}
@java.lang.Override
public boolean equals(Object other) {
if (this == other) return true;
return other instanceof GenerateRequest && equalTo((GenerateRequest) other);
}
@JsonAnyGetter
public Map getAdditionalProperties() {
return this.additionalProperties;
}
private boolean equalTo(GenerateRequest other) {
return prompt.equals(other.prompt)
&& model.equals(other.model)
&& numGenerations.equals(other.numGenerations)
&& maxTokens.equals(other.maxTokens)
&& truncate.equals(other.truncate)
&& temperature.equals(other.temperature)
&& seed.equals(other.seed)
&& preset.equals(other.preset)
&& endSequences.equals(other.endSequences)
&& stopSequences.equals(other.stopSequences)
&& k.equals(other.k)
&& p.equals(other.p)
&& frequencyPenalty.equals(other.frequencyPenalty)
&& presencePenalty.equals(other.presencePenalty)
&& returnLikelihoods.equals(other.returnLikelihoods)
&& rawPrompting.equals(other.rawPrompting);
}
@java.lang.Override
public int hashCode() {
return Objects.hash(
this.prompt,
this.model,
this.numGenerations,
this.maxTokens,
this.truncate,
this.temperature,
this.seed,
this.preset,
this.endSequences,
this.stopSequences,
this.k,
this.p,
this.frequencyPenalty,
this.presencePenalty,
this.returnLikelihoods,
this.rawPrompting);
}
@java.lang.Override
public String toString() {
return ObjectMappers.stringify(this);
}
public static PromptStage builder() {
return new Builder();
}
public interface PromptStage {
_FinalStage prompt(String prompt);
Builder from(GenerateRequest other);
}
public interface _FinalStage {
GenerateRequest build();
_FinalStage model(Optional model);
_FinalStage model(String model);
_FinalStage numGenerations(Optional numGenerations);
_FinalStage numGenerations(Integer numGenerations);
_FinalStage maxTokens(Optional maxTokens);
_FinalStage maxTokens(Integer maxTokens);
_FinalStage truncate(Optional truncate);
_FinalStage truncate(GenerateRequestTruncate truncate);
_FinalStage temperature(Optional temperature);
_FinalStage temperature(Double temperature);
_FinalStage seed(Optional seed);
_FinalStage seed(Double seed);
_FinalStage preset(Optional preset);
_FinalStage preset(String preset);
_FinalStage endSequences(Optional> endSequences);
_FinalStage endSequences(List endSequences);
_FinalStage stopSequences(Optional> stopSequences);
_FinalStage stopSequences(List stopSequences);
_FinalStage k(Optional k);
_FinalStage k(Integer k);
_FinalStage p(Optional p);
_FinalStage p(Double p);
_FinalStage frequencyPenalty(Optional frequencyPenalty);
_FinalStage frequencyPenalty(Double frequencyPenalty);
_FinalStage presencePenalty(Optional presencePenalty);
_FinalStage presencePenalty(Double presencePenalty);
_FinalStage returnLikelihoods(Optional returnLikelihoods);
_FinalStage returnLikelihoods(GenerateRequestReturnLikelihoods returnLikelihoods);
_FinalStage rawPrompting(Optional rawPrompting);
_FinalStage rawPrompting(Boolean rawPrompting);
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static final class Builder implements PromptStage, _FinalStage {
private String prompt;
private Optional rawPrompting = Optional.empty();
private Optional returnLikelihoods = Optional.empty();
private Optional presencePenalty = Optional.empty();
private Optional frequencyPenalty = Optional.empty();
private Optional p = Optional.empty();
private Optional k = Optional.empty();
private Optional> stopSequences = Optional.empty();
private Optional> endSequences = Optional.empty();
private Optional preset = Optional.empty();
private Optional seed = Optional.empty();
private Optional temperature = Optional.empty();
private Optional truncate = Optional.empty();
private Optional maxTokens = Optional.empty();
private Optional numGenerations = Optional.empty();
private Optional model = Optional.empty();
@JsonAnySetter
private Map additionalProperties = new HashMap<>();
private Builder() {}
@java.lang.Override
public Builder from(GenerateRequest other) {
prompt(other.getPrompt());
model(other.getModel());
numGenerations(other.getNumGenerations());
maxTokens(other.getMaxTokens());
truncate(other.getTruncate());
temperature(other.getTemperature());
seed(other.getSeed());
preset(other.getPreset());
endSequences(other.getEndSequences());
stopSequences(other.getStopSequences());
k(other.getK());
p(other.getP());
frequencyPenalty(other.getFrequencyPenalty());
presencePenalty(other.getPresencePenalty());
returnLikelihoods(other.getReturnLikelihoods());
rawPrompting(other.getRawPrompting());
return this;
}
/**
* The input text that serves as the starting point for generating the response.
* Note: The prompt will be pre-processed and modified before reaching the model.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
@JsonSetter("prompt")
public _FinalStage prompt(String prompt) {
this.prompt = prompt;
return this;
}
/**
* When enabled, the user's prompt will be sent to the model without any pre-processing.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage rawPrompting(Boolean rawPrompting) {
this.rawPrompting = Optional.of(rawPrompting);
return this;
}
@java.lang.Override
@JsonSetter(value = "raw_prompting", nulls = Nulls.SKIP)
public _FinalStage rawPrompting(Optional rawPrompting) {
this.rawPrompting = rawPrompting;
return this;
}
/**
* One of GENERATION|ALL|NONE
to specify how and if the token likelihoods are returned with the response. Defaults to NONE
.
* If GENERATION
is selected, the token likelihoods will only be provided for generated text.
* If ALL
is selected, the token likelihoods will be provided both for the prompt and the generated text.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage returnLikelihoods(GenerateRequestReturnLikelihoods returnLikelihoods) {
this.returnLikelihoods = Optional.of(returnLikelihoods);
return this;
}
@java.lang.Override
@JsonSetter(value = "return_likelihoods", nulls = Nulls.SKIP)
public _FinalStage returnLikelihoods(Optional returnLikelihoods) {
this.returnLikelihoods = returnLikelihoods;
return this;
}
/**
* Defaults to 0.0
, min value of 0.0
, max value of 1.0
.
* Can be used to reduce repetitiveness of generated tokens. Similar to frequency_penalty
, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
* Using frequency_penalty
in combination with presence_penalty
is not supported on newer models.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage presencePenalty(Double presencePenalty) {
this.presencePenalty = Optional.of(presencePenalty);
return this;
}
@java.lang.Override
@JsonSetter(value = "presence_penalty", nulls = Nulls.SKIP)
public _FinalStage presencePenalty(Optional presencePenalty) {
this.presencePenalty = presencePenalty;
return this;
}
/**
* Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
* Using frequency_penalty
in combination with presence_penalty
is not supported on newer models.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage frequencyPenalty(Double frequencyPenalty) {
this.frequencyPenalty = Optional.of(frequencyPenalty);
return this;
}
@java.lang.Override
@JsonSetter(value = "frequency_penalty", nulls = Nulls.SKIP)
public _FinalStage frequencyPenalty(Optional frequencyPenalty) {
this.frequencyPenalty = frequencyPenalty;
return this;
}
/**
* Ensures that only the most likely tokens, with total probability mass of p
, are considered for generation at each step. If both k
and p
are enabled, p
acts after k
.
* Defaults to 0.75
. min value of 0.01
, max value of 0.99
.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage p(Double p) {
this.p = Optional.of(p);
return this;
}
@java.lang.Override
@JsonSetter(value = "p", nulls = Nulls.SKIP)
public _FinalStage p(Optional p) {
this.p = p;
return this;
}
/**
* Ensures only the top k
most likely tokens are considered for generation at each step.
* Defaults to 0
, min value of 0
, max value of 500
.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage k(Integer k) {
this.k = Optional.of(k);
return this;
}
@java.lang.Override
@JsonSetter(value = "k", nulls = Nulls.SKIP)
public _FinalStage k(Optional k) {
this.k = k;
return this;
}
/**
* The generated text will be cut at the end of the earliest occurrence of a stop sequence. The sequence will be included the text.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage stopSequences(List stopSequences) {
this.stopSequences = Optional.of(stopSequences);
return this;
}
@java.lang.Override
@JsonSetter(value = "stop_sequences", nulls = Nulls.SKIP)
public _FinalStage stopSequences(Optional> stopSequences) {
this.stopSequences = stopSequences;
return this;
}
/**
* The generated text will be cut at the beginning of the earliest occurrence of an end sequence. The sequence will be excluded from the text.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage endSequences(List endSequences) {
this.endSequences = Optional.of(endSequences);
return this;
}
@java.lang.Override
@JsonSetter(value = "end_sequences", nulls = Nulls.SKIP)
public _FinalStage endSequences(Optional> endSequences) {
this.endSequences = endSequences;
return this;
}
/**
* Identifier of a custom preset. A preset is a combination of parameters, such as prompt, temperature etc. You can create presets in the playground.
* When a preset is specified, the prompt
parameter becomes optional, and any included parameters will override the preset's parameters.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage preset(String preset) {
this.preset = Optional.of(preset);
return this;
}
@java.lang.Override
@JsonSetter(value = "preset", nulls = Nulls.SKIP)
public _FinalStage preset(Optional preset) {
this.preset = preset;
return this;
}
/**
* If specified, the backend will make a best effort to sample tokens deterministically, such that repeated requests with the same seed and parameters should return the same result. However, determinsim cannot be totally guaranteed.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage seed(Double seed) {
this.seed = Optional.of(seed);
return this;
}
@java.lang.Override
@JsonSetter(value = "seed", nulls = Nulls.SKIP)
public _FinalStage seed(Optional seed) {
this.seed = seed;
return this;
}
/**
* A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations. See Temperature for more details.
* Defaults to 0.75
, min value of 0.0
, max value of 5.0
.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage temperature(Double temperature) {
this.temperature = Optional.of(temperature);
return this;
}
@java.lang.Override
@JsonSetter(value = "temperature", nulls = Nulls.SKIP)
public _FinalStage temperature(Optional temperature) {
this.temperature = temperature;
return this;
}
/**
* One of NONE|START|END
to specify how the API will handle inputs longer than the maximum token length.
* Passing START
will discard the start of the input. END
will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model.
* If NONE
is selected, when the input exceeds the maximum input token length an error will be returned.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage truncate(GenerateRequestTruncate truncate) {
this.truncate = Optional.of(truncate);
return this;
}
@java.lang.Override
@JsonSetter(value = "truncate", nulls = Nulls.SKIP)
public _FinalStage truncate(Optional truncate) {
this.truncate = truncate;
return this;
}
/**
* The maximum number of tokens the model will generate as part of the response. Note: Setting a low value may result in incomplete generations.
* This parameter is off by default, and if it's not specified, the model will continue generating until it emits an EOS completion token. See BPE Tokens for more details.
* Can only be set to 0
if return_likelihoods
is set to ALL
to get the likelihood of the prompt.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage maxTokens(Integer maxTokens) {
this.maxTokens = Optional.of(maxTokens);
return this;
}
@java.lang.Override
@JsonSetter(value = "max_tokens", nulls = Nulls.SKIP)
public _FinalStage maxTokens(Optional maxTokens) {
this.maxTokens = maxTokens;
return this;
}
/**
* The maximum number of generations that will be returned. Defaults to 1
, min value of 1
, max value of 5
.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage numGenerations(Integer numGenerations) {
this.numGenerations = Optional.of(numGenerations);
return this;
}
@java.lang.Override
@JsonSetter(value = "num_generations", nulls = Nulls.SKIP)
public _FinalStage numGenerations(Optional numGenerations) {
this.numGenerations = numGenerations;
return this;
}
/**
* The identifier of the model to generate with. Currently available models are command
(default), command-nightly
(experimental), command-light
, and command-light-nightly
(experimental).
* Smaller, "light" models are faster, while larger models will perform better. Custom models can also be supplied with their full ID.
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage model(String model) {
this.model = Optional.of(model);
return this;
}
@java.lang.Override
@JsonSetter(value = "model", nulls = Nulls.SKIP)
public _FinalStage model(Optional model) {
this.model = model;
return this;
}
@java.lang.Override
public GenerateRequest build() {
return new GenerateRequest(
prompt,
model,
numGenerations,
maxTokens,
truncate,
temperature,
seed,
preset,
endSequences,
stopSequences,
k,
p,
frequencyPenalty,
presencePenalty,
returnLikelihoods,
rawPrompting,
additionalProperties);
}
}
}