Skip to content

Commit

Permalink
Add additional_instructions to CreateRunRequest + small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jan 19, 2024
1 parent ee7adfd commit 7c8a66d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ private Constants() {}

static final String TEXT_CONTENT_PART_TYPE = "text";
static final String IMAGE_CONTENT_PART_TYPE = "image_url";

static final String SUBMIT_TOOL_OUTPUTS_REQUIRED_ACTION_TYPE = "submit_tool_outputs";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public record CreateRunRequest(
String assistantId,
Optional<String> model,
Optional<String> instructions,
Optional<String> additionalInstructions,
Optional<List<Tool>> tools,
Optional<Map<String, String>> metadata) {

Expand All @@ -21,6 +22,7 @@ public static class Builder {

private Optional<String> model = Optional.empty();
private Optional<String> instructions = Optional.empty();
private Optional<String> additionalInstructions = Optional.empty();
private Optional<List<Tool>> tools = Optional.empty();
private Optional<Map<String, String>> metadata = Optional.empty();

Expand Down Expand Up @@ -51,6 +53,16 @@ public Builder instructions(String instructions) {
return this;
}

/**
* @param additionalInstructions Appends additional instructions at the end of the instructions
* for the run. This is useful for modifying the behavior on a per-run basis without
* overriding other instructions.
*/
public Builder additionalInstructions(String additionalInstructions) {
this.additionalInstructions = Optional.of(additionalInstructions);
return this;
}

/**
* @param tools Override the tools the assistant can use for this run. This is useful for
* modifying the behavior on a per-run basis.
Expand All @@ -74,7 +86,8 @@ public CreateRunRequest build() {
if (assistantId == null) {
throw new IllegalStateException("assistantId must be set");
}
return new CreateRunRequest(assistantId, model, instructions, tools, metadata);
return new CreateRunRequest(
assistantId, model, instructions, additionalInstructions, tools, metadata);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,12 @@

public record CreateThreadAndRunRequest(
String assistantId,
Optional<Thread> thread,
Optional<CreateThreadRequest> thread,
Optional<String> model,
Optional<String> instructions,
Optional<List<Tool>> tools,
Optional<Map<String, String>> metadata) {

public record Thread(
Optional<List<CreateThreadRequest.Message>> messages,
Optional<Map<String, String>> metadata) {

public static Builder newBuilder() {
return new Builder();
}

public static class Builder {

private Optional<List<CreateThreadRequest.Message>> messages = Optional.empty();
private Optional<Map<String, String>> metadata = Optional.empty();

/**
* @param messages A list of messages to start the thread with.
*/
public Builder messages(List<CreateThreadRequest.Message> messages) {
this.messages = Optional.of(messages);
return this;
}

/**
* @param metadata Set of 16 key-value pairs that can be attached to an object. This can be
* useful for storing additional information about the object in a structured format. Keys
* can be a maximum of 64 characters long and values can be a maxium of 512 characters
* long.
*/
public Builder metadata(Map<String, String> metadata) {
this.metadata = Optional.of(metadata);
return this;
}

public Thread build() {
return new Thread(messages, metadata);
}
}
}

public static Builder newBuilder() {
return new Builder();
}
Expand All @@ -58,7 +20,7 @@ public static class Builder {

private String assistantId;

private Optional<Thread> thread = Optional.empty();
private Optional<CreateThreadRequest> thread = Optional.empty();
private Optional<String> model = Optional.empty();
private Optional<String> instructions = Optional.empty();
private Optional<List<Tool>> tools = Optional.empty();
Expand All @@ -75,7 +37,7 @@ public Builder assistantId(String assistantId) {
/**
* @param thread Thread to be created as part of the request
*/
public Builder thread(Thread thread) {
public Builder thread(CreateThreadRequest thread) {
this.thread = Optional.of(thread);
return this;
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/io/github/stefanbratanov/jvm/openai/File.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
package io.github.stefanbratanov.jvm.openai;

public record File(String id, int bytes, long createdAt, String filename, String purpose) {}
public record File(
String id,
int bytes,
long createdAt,
String filename,
String purpose,
@Deprecated String status) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.stefanbratanov.jvm.openai;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -24,7 +25,8 @@ public final class FineTuningClient extends OpenAIClient {
}

/**
* Creates a job that fine-tunes a specified model from a given dataset.
* Creates a fine-tuning job which begins the process of creating a new model from a given
* dataset.
*
* <p>Response includes details of the enqueued job including job status and the name of the
* fine-tuned models once complete.
Expand Down Expand Up @@ -61,6 +63,7 @@ public PaginatedFineTuningJobs listFineTuningJobs(
}

public record PaginatedFineTuningJobs(List<FineTuningJob> data, boolean hasMore) {
@JsonIgnore
public String getLastJobId() {
return data.get(data.size() - 1).id();
}
Expand Down Expand Up @@ -93,6 +96,7 @@ public PaginatedFineTuningEvents listFineTuningJobEvents(
}

public record PaginatedFineTuningEvents(List<FineTuningJobEvent> data, boolean hasMore) {
@JsonIgnore
public String getLastEventId() {
return data.get(data.size() - 1).id();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public record ThreadRun(
/** Details on the action required to continue the run. */
public record RequiredAction(String type, SubmitToolOutputs submitToolOutputs) {
public record SubmitToolOutputs(List<ToolCall> toolCalls) {}

public static RequiredAction submitToolOutputsRequiredAction(
SubmitToolOutputs submitToolOutputs) {
return new RequiredAction(
Constants.SUBMIT_TOOL_OUTPUTS_REQUIRED_ACTION_TYPE, submitToolOutputs);
}
}

/** The last error associated with this run. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public record ToolChoice(String type, Function function) {
* @param name The name of the function to call.
*/
public record Function(String name) {}

public static ToolChoice functionToolChoice(Function function) {
return new ToolChoice(Constants.FUNCTION_TOOL_TYPE, function);
}
}

0 comments on commit 7c8a66d

Please sign in to comment.