Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds additional parameters for completions API #87

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public Uni<CompletionResults> complete(CompletionQuery query) throws ConnectorEx
results.setCreated(response.getCreated());
results.setChoices(response.getChoices());
results.setUsage(response.getUsage());
results.setObject(response.getObject());
results.setSystemFingerprint(response.getSystemFingerprint());
return results;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

import io.quarkus.runtime.annotations.RegisterForReflection;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import de.l3s.interweb.core.completion.CompletionQuery;
import de.l3s.interweb.core.completion.ResponseFormat;

@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class CompletionBody {

private List<CompletionMessage> messages;
Expand All @@ -27,13 +30,35 @@ public final class CompletionBody {
@JsonProperty("max_tokens")
private Integer maxTokens;

/**
* How many completions to generate for each prompt. Minimum of 1 (default) and maximum of 128 allowed.
* Note: Because this parameter generates many completions, it can quickly consume your token quota.
*/
private Integer n;

/**
* If specified, our system will make the best effort to sample deterministically,
* such that repeated requests with the same seed and parameters should return the same result.
* Determinism isn't guaranteed, and you should refer to the system_fingerprint response parameter to monitor changes in the backend.
*/
private Integer seed;

@JsonProperty("response_format")
private ResponseFormat responseFormat;

private String[] stop;

public CompletionBody(CompletionQuery query) {
this.messages = query.getMessages().stream().map(CompletionMessage::new).toList();
this.temperature = query.getTemperature();
this.topP = query.getTopP();
this.frequencyPenalty = query.getPresencePenalty();
this.presencePenalty = query.getPresencePenalty();
this.maxTokens = query.getMaxTokens();
this.n = query.getN();
this.seed = query.getSeed();
this.responseFormat = query.getResponseFormat();
this.stop = query.getStop();
}

public List<CompletionMessage> getMessages() {
Expand All @@ -59,4 +84,20 @@ public Double getPresencePenalty() {
public Integer getMaxTokens() {
return maxTokens;
}

public Integer getN() {
return n;
}

public Integer getSeed() {
return seed;
}

public ResponseFormat getResponseFormat() {
return responseFormat;
}

public String[] getStop() {
return stop;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import io.quarkus.runtime.annotations.RegisterForReflection;

import com.fasterxml.jackson.annotation.JsonProperty;

import de.l3s.interweb.core.completion.Choice;
import de.l3s.interweb.core.completion.Usage;

Expand All @@ -14,6 +16,8 @@ public class CompletionResponse {
private String object;
private String model;
private Usage usage;
@JsonProperty("system_fingerprint")
private String systemFingerprint;
private Instant created;
private List<Choice> choices;

Expand Down Expand Up @@ -41,14 +45,6 @@ public void setModel(String model) {
this.model = model;
}

public List<Choice> getChoices() {
return choices;
}

public void setChoices(List<Choice> choices) {
this.choices = choices;
}

public Usage getUsage() {
return usage;
}
Expand All @@ -57,11 +53,27 @@ public void setUsage(Usage usage) {
this.usage = usage;
}

public String getSystemFingerprint() {
return systemFingerprint;
}

public void setSystemFingerprint(String systemFingerprint) {
this.systemFingerprint = systemFingerprint;
}

public Instant getCreated() {
return created;
}

public void setCreated(Instant created) {
this.created = created;
}

public List<Choice> getChoices() {
return choices;
}

public void setChoices(List<Choice> choices) {
this.choices = choices;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import io.quarkus.runtime.annotations.RegisterForReflection;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

@RegisterForReflection
Expand Down Expand Up @@ -98,6 +99,30 @@ public class CompletionQuery {
@JsonProperty(value = "generate_title")
private boolean generateTitle;

/**
* How many completions to generate for each prompt. Minimum of 1 (default) and maximum of 128 allowed.
* Note: Because this parameter generates many completions, it can quickly consume your token quota.
*/
private Integer n;

/**
* If specified, our system will make the best effort to sample deterministically,
* such that repeated requests with the same seed and parameters should return the same result.
* Determinism isn't guaranteed, and you should refer to the system_fingerprint response parameter to monitor changes in the backend.
*/
private Integer seed;

/**
* An object specifying the format that the model must output. Used to enable JSON mode.
*/
private ResponseFormat responseFormat;

/**
* Up to 4 sequences where the API will stop generating further tokens.
*/
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
private String[] stop;

public String getModel() {
return model;
}
Expand Down Expand Up @@ -185,4 +210,36 @@ public boolean isGenerateTitle() {
public void setGenerateTitle(boolean generateTitle) {
this.generateTitle = generateTitle;
}

public void setN(Integer n) {
this.n = n;
}

public Integer getN() {
return n;
}

public void setSeed(Integer seed) {
this.seed = seed;
}

public Integer getSeed() {
return seed;
}

public void setResponseFormat(ResponseFormat responseFormat) {
this.responseFormat = responseFormat;
}

public ResponseFormat getResponseFormat() {
return responseFormat;
}

public void setStop(String[] stop) {
this.stop = stop;
}

public String[] getStop() {
return stop;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@

@RegisterForReflection
@JsonIgnoreProperties("results")
@JsonPropertyOrder({"id", "title", "model", "choices", "usage", "cost", "elapsed_time", "created"})
@JsonPropertyOrder({"id", "object", "title", "model", "choices", "usage", "cost", "elapsed_time", "system_fingerprint", "created"})
public class CompletionResults extends Results<Choice> {
@JsonProperty(value = "id")
private UUID chatId;
@JsonProperty(value = "title")
private String chatTitle;
private String object;
private String model;
private Usage usage;
private UsageCost cost;
@JsonProperty(value = "system_fingerprint")
private String systemFingerprint;
private Instant created;

public UUID getChatId() {
Expand All @@ -42,6 +45,14 @@ public void setChatTitle(String chatTitle) {
this.chatTitle = chatTitle;
}

public String getObject() {
return this.object;
}

public void setObject(String object) {
this.object = object;
}

public String getModel() {
return model;
}
Expand Down Expand Up @@ -81,6 +92,14 @@ public UsageCost getCost() {
return cost;
}

public String getSystemFingerprint() {
return systemFingerprint;
}

public void setSystemFingerprint(String systemFingerprint) {
this.systemFingerprint = systemFingerprint;
}

public Instant getCreated() {
return created;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.l3s.interweb.core.completion;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class ResponseFormat {

public enum ResponseType {
json_object,
text
}

private ResponseType type;

public ResponseFormat() {
}

public ResponseFormat(ResponseType type) {
this.type = type;
}

public ResponseType getType() {
return type;
}

public void setType(ResponseType type) {
this.type = type;
}
}
Loading