Skip to content

Commit

Permalink
Implement spec changes
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Sep 15, 2024
1 parent 627513d commit 250eaac
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public record ChatCompletion(
String serviceTier,
String systemFingerprint,
List<Choice> choices,
Usage usage) {
CompletionUsage usage) {

public record Choice(int index, Message message, Logprobs logprobs, String finishReason) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public record ChatCompletionChunk(
String model,
String serviceTier,
String systemFingerprint,
Usage usage) {
CompletionUsage usage) {

public record Choice(Delta delta, int index, Logprobs logprobs, String finishReason) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.stefanbratanov.jvm.openai;

/**
* Usage statistics for the completion request.
*
* @param completionTokensDetails Breakdown of tokens used in a completion.
*/
public record CompletionUsage(
int completionTokens,
int promptTokens,
int totalTokens,
CompletionTokensDetails completionTokensDetails) {

/**
* @param reasoningTokens Tokens generated by the model for reasoning.
*/
public record CompletionTokensDetails(Integer reasoningTokens) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public record CreateChatCompletionRequest(
Optional<Boolean> logprobs,
Optional<Integer> topLogprobs,
Optional<Integer> maxTokens,
Optional<Integer> maxCompletionTokens,
Optional<Integer> n,
Optional<Double> presencePenalty,
Optional<ResponseFormat> responseFormat,
Expand Down Expand Up @@ -57,6 +58,7 @@ public static class Builder {
private Optional<Boolean> logprobs = Optional.empty();
private Optional<Integer> topLogprobs = Optional.empty();
private Optional<Integer> maxTokens = Optional.empty();
private Optional<Integer> maxCompletionTokens = Optional.empty();
private Optional<Integer> n = Optional.empty();
private Optional<Double> presencePenalty = Optional.empty();
private Optional<ResponseFormat> responseFormat = Optional.empty();
Expand Down Expand Up @@ -148,12 +150,23 @@ public Builder topLogprobs(int topLogprobs) {
/**
* @param maxTokens The total length of input tokens and generated tokens is limited by the
* model's context length
* @deprecated This value is now deprecated in favor of `max_completion_tokens`
*/
@Deprecated
public Builder maxTokens(int maxTokens) {
this.maxTokens = Optional.of(maxTokens);
return this;
}

/**
* @param maxCompletionTokens An upper bound for the number of tokens that can be generated for
* a completion, including visible output tokens and reasoning tokens
*/
public Builder maxCompletionTokens(int maxCompletionTokens) {
this.maxCompletionTokens = Optional.of(maxCompletionTokens);
return this;
}

/**
* @param n How many chat completion choices to generate for each input message. Note that you
* will be charged based on the number of generated tokens across all of the choices. Keep n
Expand Down Expand Up @@ -200,8 +213,11 @@ public Builder seed(int seed) {
* @param serviceTier Specifies the latency tier to use for processing the request. This
* parameter is relevant for customers subscribed to the scale tier service:
* <ul>
* <li>If set to 'auto', the system will utilize scale tier credits until they are
* exhausted.
* <li>If set to 'auto', and the Project is Scale tier enabled, the system will utilize
* scale tier credits until they are exhausted.
* <Li>If set to 'auto', and the Project is not Scale tier enabled, the request will be
* processed using the default service tier with a lower uptime SLA and no latency
* guarantee.
* <li>If set to 'default', the request will be processed using the default service tier
* with a lower uptime SLA and no latency guarantee.
* </ul>
Expand Down Expand Up @@ -330,6 +346,7 @@ public CreateChatCompletionRequest build() {
logprobs,
topLogprobs,
maxTokens,
maxCompletionTokens,
n,
presencePenalty,
responseFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Builder hyperparameters(Hyperparameters hyperparameters) {
}

/**
* @param suffix A string of up to 18 characters that will be added to your fine-tuned model
* @param suffix A string of up to 64 characters that will be added to your fine-tuned model
* name
*/
public Builder suffix(String suffix) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.github.stefanbratanov.jvm.openai;

public record CreateProjectRequest(String name) {
import java.util.Optional;

public record CreateProjectRequest(
String name, Optional<String> appUseCase, Optional<String> businessWebsite) {

public static Builder newBuilder() {
return new Builder();
Expand All @@ -10,6 +13,9 @@ public static class Builder {

private String name;

private Optional<String> appUseCase = Optional.empty();
private Optional<String> businessWebsite = Optional.empty();

/**
* @param name The friendly name of the project, this name appears in reports.
*/
Expand All @@ -18,8 +24,25 @@ public Builder name(String name) {
return this;
}

/**
* @param appUseCase A description of your business, project, or use case.
*/
public Builder appUseCase(String appUseCase) {
this.appUseCase = Optional.of(appUseCase);
return this;
}

/**
* @param businessWebsite Your business URL, or if you don't have one yet, a URL to your
* LinkedIn or other social media.
*/
public Builder businessWebsite(String businessWebsite) {
this.businessWebsite = Optional.of(businessWebsite);
return this;
}

public CreateProjectRequest build() {
return new CreateProjectRequest(name);
return new CreateProjectRequest(name, appUseCase, businessWebsite);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.github.stefanbratanov.jvm.openai;

public record ModifyProjectRequest(String name) {
import java.util.Optional;

public record ModifyProjectRequest(
String name, Optional<String> appUseCase, Optional<String> businessWebsite) {

public static Builder newBuilder() {
return new Builder();
Expand All @@ -10,6 +13,9 @@ public static class Builder {

private String name;

private Optional<String> appUseCase = Optional.empty();
private Optional<String> businessWebsite = Optional.empty();

/**
* @param name The updated name of the project, this name appears in reports.
*/
Expand All @@ -18,8 +24,25 @@ public Builder name(String name) {
return this;
}

/**
* @param appUseCase A description of your business, project, or use case.
*/
public Builder appUseCase(String appUseCase) {
this.appUseCase = Optional.of(appUseCase);
return this;
}

/**
* @param businessWebsite Your business URL, or if you don't have one yet, a URL to your
* LinkedIn or other social media.
*/
public Builder businessWebsite(String businessWebsite) {
this.businessWebsite = Optional.of(businessWebsite);
return this;
}

public ModifyProjectRequest build() {
return new ModifyProjectRequest(name);
return new ModifyProjectRequest(name, appUseCase, businessWebsite);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package io.github.stefanbratanov.jvm.openai;

/** Represents an individual project. */
public record Project(String id, String name, long createdAt, Long archivedAt, String status) {}
public record Project(
String id,
String name,
long createdAt,
Long archivedAt,
String status,
String appUseCase,
String businessWebsite) {}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ public static RequiredAction submitToolOutputsRequiredAction(

/** Details on why the run is incomplete. Will be `null` if the run is not incomplete. */
public record IncompleteDetails(String reason) {}

/**
* Usage statistics related to the run. This value will be `null` if the run is not in a terminal
* state (i.e. `in_progress`, `queued`, etc.).
*/
public record Usage(int completionTokens, int promptTokens, int totalTokens) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ public String type() {
}
}
}

/**
* Usage statistics related to the run step. This value will be `null` while the run step's status
* is `in_progress`.
*/
public record Usage(int completionTokens, int promptTokens, int totalTokens) {}
}
10 changes: 8 additions & 2 deletions src/main/java/io/github/stefanbratanov/jvm/openai/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ record FileSearchTool(Optional<FileSearch> fileSearch) implements Tool {
public record FileSearch(
Optional<Integer> maxNumResults, Optional<RankingOptions> rankingOptions) {

/**
* @param ranker The ranker to use for the file search. If not specified will use the `auto`
* ranker.
* @param scoreThreshold The score threshold for the file search. All values must be a
* floating point number between 0 and 1.
*/
public record RankingOptions(String ranker, Double scoreThreshold) {}
}

Expand Down Expand Up @@ -70,8 +76,8 @@ static FileSearchTool fileSearchTool(int maxNumResults) {

/**
* @param maxNumResults The maximum number of results the file search tool should output.
* @param rankingOptions The score threshold for the file search. All values must be a floating
* point number between 0 and 1.
* @param rankingOptions The ranking options for the file search. If not specified, the file
* search tool will use the `auto` ranker and a score_threshold of 0.
*/
static FileSearchTool fileSearchTool(int maxNumResults, RankingOptions rankingOptions) {
return new FileSearchTool(
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/io/github/stefanbratanov/jvm/openai/Usage.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.github.stefanbratanov.jvm.openai.AuditLogEvent.UserDeletedEvent;
import io.github.stefanbratanov.jvm.openai.AuditLogEvent.UserUpdatedEvent;
import io.github.stefanbratanov.jvm.openai.AuditLogsClient.PaginatedAuditLogs;
import io.github.stefanbratanov.jvm.openai.CompletionUsage.CompletionTokensDetails;
import io.github.stefanbratanov.jvm.openai.CreateChatCompletionRequest.StreamOptions;
import io.github.stefanbratanov.jvm.openai.FineTuningJobIntegration.Wandb;
import io.github.stefanbratanov.jvm.openai.ProjectApiKey.Owner;
Expand Down Expand Up @@ -70,7 +71,7 @@ public CreateChatCompletionRequest randomCreateChatCompletionRequest() {
.logitBias(randomLogitBias(randomInt(0, 6)))
.logprobs(randomBoolean())
.topLogprobs(randomInt(0, 20))
.maxTokens(randomInt(0, 10_000))
.maxCompletionTokens(randomInt(0, 10_000))
.n(randomInt(1, 128))
.presencePenalty(randomDouble(-2.0, 2.0))
.responseFormat(
Expand Down Expand Up @@ -102,7 +103,7 @@ public ChatCompletion randomChatCompletion() {
oneOf("scale", "default"),
randomString(10),
listOf(randomInt(1, 3), this::randomChatCompletionChoice),
randomUsage());
randomCompletionUsage());
}

public SpeechRequest randomSpeechRequest() {
Expand Down Expand Up @@ -146,7 +147,7 @@ public CreateFineTuningJobRequest randomCreateFineTuningJobRequest() {
// satisfy exclusiveMinimum of 0
Optional.of(oneOf("auto", randomDouble(0 + EPSILON, 10_000))),
Optional.of(oneOf("auto", randomInt(1, 50)))))
.suffix(randomString(1, 40))
.suffix(randomString(1, 64))
.validationFile(randomString(10))
.integrations(listOf(randomInt(1, 5), this::randomIntegration))
.seed(randomInt(0, 2147483646))
Expand Down Expand Up @@ -616,7 +617,7 @@ public ThreadRun randomThreadRun() {
randomString(10, 200),
listOf(randomInt(1, 20), this::randomTool),
randomMetadata(),
randomUsage(),
new ThreadRun.Usage(randomInt(0, 100), randomInt(0, 100), randomInt(0, 100)),
randomDouble(0, 2),
randomDouble(0, 1),
randomInt(256, 10_000),
Expand Down Expand Up @@ -651,7 +652,7 @@ public ThreadRunStep randomThreadRunStep() {
randomLong(7, 888),
randomLong(9, 345),
randomMetadata(),
randomUsage());
new ThreadRunStep.Usage(randomInt(0, 100), randomInt(0, 100), randomInt(0, 100)));
}

public ThreadRunStepDelta randomThreadRunStepDelta() {
Expand Down Expand Up @@ -776,7 +777,11 @@ public User randomUser() {
}

public CreateProjectRequest randomCreateProjectRequest() {
return CreateProjectRequest.newBuilder().name(randomString(7)).build();
return CreateProjectRequest.newBuilder()
.name(randomString(7))
.appUseCase(randomString(12))
.businessWebsite("example.com")
.build();
}

public Project randomProject() {
Expand All @@ -785,7 +790,9 @@ public Project randomProject() {
randomString(7),
randomLong(10_000, 1_000_000),
randomLong(11_111, 1_111_111),
oneOf("active", "archived"));
oneOf("active", "archived"),
randomString(12),
"example.com");
}

public CreateProjectUserRequest randomCreateProjectUserRequest() {
Expand Down Expand Up @@ -1050,8 +1057,12 @@ private ToolCall randomToolCall() {
List.of(new Content("text", randomString(10))))))));
}

private Usage randomUsage() {
return new Usage(randomInt(0, 100), randomInt(0, 100), randomInt(0, 100));
private CompletionUsage randomCompletionUsage() {
return new CompletionUsage(
randomInt(0, 100),
randomInt(0, 100),
randomInt(0, 100),
new CompletionTokensDetails(randomInt(0, 100)));
}

private Annotation randomThreadMessageAnnotation() {
Expand Down

0 comments on commit 250eaac

Please sign in to comment.