Skip to content

Commit

Permalink
[Beta] Add support for runs endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jan 8, 2024
1 parent 33b1354 commit 0c43e7f
Show file tree
Hide file tree
Showing 14 changed files with 875 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ Images images = imagesClient.createImage(createImageRequest);
| [Assistants](https://platform.openai.com/docs/api-reference/assistants) | ✔️ |
| [Threads](https://platform.openai.com/docs/api-reference/threads) | ️ ✔️ |
| [Messages](https://platform.openai.com/docs/api-reference/messages) | ✔️ |
| [Runs](https://platform.openai.com/docs/api-reference/runs) | |
| [Runs](https://platform.openai.com/docs/api-reference/runs) | ✔️ |


1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:${junitVersion}")
testImplementation("org.junit.jupiter:junit-jupiter-params:${junitVersion}")
testImplementation("org.assertj:assertj-core:3.25.1")
testImplementation("org.awaitility:awaitility:4.2.0")
}

publishing {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/github/stefanbratanov/chatjpt/ChatJPT.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public final class ChatJPT {
private final AssistantsClient assistantsClient;
private final ThreadsClient threadsClient;
private final MessagesClient messagesClient;
private final RunsClient runsClient;

private ChatJPT(
URI baseUrl, String apiKey, Optional<String> organization, HttpClient httpClient) {
Expand All @@ -54,6 +55,7 @@ private ChatJPT(
new AssistantsClient(baseUrl, apiKey, organization, httpClient, OBJECT_MAPPER);
threadsClient = new ThreadsClient(baseUrl, apiKey, organization, httpClient, OBJECT_MAPPER);
messagesClient = new MessagesClient(baseUrl, apiKey, organization, httpClient, OBJECT_MAPPER);
runsClient = new RunsClient(baseUrl, apiKey, organization, httpClient, OBJECT_MAPPER);
}

/**
Expand Down Expand Up @@ -144,6 +146,14 @@ public MessagesClient messagesClient() {
return messagesClient;
}

/**
* @return a client based on <a
* href="https://platform.openai.com/docs/api-reference/runs">Runs</a>
*/
public RunsClient runsClient() {
return runsClient;
}

/**
* @param apiKey the API key used for authentication
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/github/stefanbratanov/chatjpt/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ private Constants() {}
static final String CODE_INTERPRETER_TOOL_TYPE = "code_interpreter";
static final String RETRIEVAL_TOOL_TYPE = "retrieval";
static final String FUNCTION_TOOL_TYPE = "function";

static final String MESSAGE_CREATION_STEP_DETAILS_TYPE = "message_creation";
static final String TOOL_CALLS_STEP_DETAILS_TYPE = "tool_calls";

static final String CODE_INTERPRETER_TOOL_CALL_TYPE = "code_interpreter";
static final String RETRIEVAL_TOOL_CALL_TYPE = "retrieval";
static final String FUNCTION_TOOL_CALL_TYPE = "function";

static final String CODE_INTERPRETER_LOG_OUTPUT_TYPE = "logs";
static final String CODE_INTERPRETER_IMAGE_OUTPUT_TYPE = "image";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.github.stefanbratanov.chatjpt;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public record CreateRunRequest(
String assistantId,
Optional<String> model,
Optional<String> instructions,
Optional<List<Tool>> tools,
Optional<Map<String, String>> metadata) {

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

public static class Builder {

private String assistantId;

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

/**
* @param assistantId The ID of the assistant to use to execute this run.
*/
public Builder assistantId(String assistantId) {
this.assistantId = assistantId;
return this;
}

/**
* @param model The ID of the Model to be used to execute this run. If a value is provided here,
* it will override the model associated with the assistant. If not, the model associated
* with the assistant will be used.
*/
public Builder model(String model) {
this.model = Optional.of(model);
return this;
}

/**
* @param instructions Overrides the instructions of the assistant. This is useful for modifying
* the behavior on a per-run basis.
*/
public Builder instructions(String instructions) {
this.instructions = Optional.of(instructions);
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.
*/
public Builder tools(List<Tool> tools) {
this.tools = Optional.of(tools);
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 CreateRunRequest build() {
if (assistantId == null) {
throw new IllegalStateException("assistantId must be set");
}
return new CreateRunRequest(assistantId, model, instructions, tools, metadata);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package io.github.stefanbratanov.chatjpt;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public record CreateThreadAndRunRequest(
String assistantId,
Optional<Thread> 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();
}

public static class Builder {

private String assistantId;

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

/**
* @param assistantId The ID of the assistant to use to execute this run.
*/
public Builder assistantId(String assistantId) {
this.assistantId = assistantId;
return this;
}

/**
* @param thread Thread to be created as part of the request
*/
public Builder thread(Thread thread) {
this.thread = Optional.of(thread);
return this;
}

/**
* @param model The ID of the Model to be used to execute this run. If a value is provided here,
* it will override the model associated with the assistant. If not, the model associated
* with the assistant will be used.
*/
public Builder model(String model) {
this.model = Optional.of(model);
return this;
}

/**
* @param instructions Overrides the instructions of the assistant. This is useful for modifying
* the behavior on a per-run basis.
*/
public Builder instructions(String instructions) {
this.instructions = Optional.of(instructions);
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.
*/
public Builder tools(List<Tool> tools) {
this.tools = Optional.of(tools);
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 CreateThreadAndRunRequest build() {
if (assistantId == null) {
throw new IllegalStateException("assistantId must be set");
}
return new CreateThreadAndRunRequest(
assistantId, thread, model, instructions, tools, metadata);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.stefanbratanov.chatjpt;

import java.util.Map;
import java.util.Optional;

public record ModifyRunRequest(Optional<Map<String, String>> metadata) {

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

public static class Builder {

private Optional<Map<String, String>> metadata = Optional.empty();

/**
* @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 ModifyRunRequest build() {
return new ModifyRunRequest(metadata);
}
}
}
Loading

0 comments on commit 0c43e7f

Please sign in to comment.