-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Beta] Add support for threads endpoints
- Loading branch information
1 parent
147097e
commit 6cef5a7
Showing
15 changed files
with
346 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/io/github/stefanbratanov/chatjpt/CreateThreadRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.github.stefanbratanov.chatjpt; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public record CreateThreadRequest( | ||
Optional<List<ThreadMessage>> messages, Optional<Map<String, String>> metadata) { | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private final List<ThreadMessage> messages = new LinkedList<>(); | ||
|
||
private Optional<Map<String, String>> metadata = Optional.empty(); | ||
|
||
/** | ||
* @param message message to append to the list of messages to start the thread with. | ||
*/ | ||
public Builder message(ThreadMessage message) { | ||
messages.add(message); | ||
return this; | ||
} | ||
|
||
/** | ||
* @param messages messages to append to the list of messages to start the thread with. | ||
*/ | ||
public Builder messages(List<ThreadMessage> messages) { | ||
this.messages.addAll(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 CreateThreadRequest build() { | ||
return new CreateThreadRequest( | ||
messages.isEmpty() ? Optional.empty() : Optional.of(messages), metadata); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.stefanbratanov.chatjpt; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public sealed interface Message permits ChatMessage, ThreadMessage { | ||
@JsonProperty(access = JsonProperty.Access.READ_ONLY) | ||
String role(); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/io/github/stefanbratanov/chatjpt/ModifyThreadRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ModifyThreadRequest(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 ModifyThreadRequest build() { | ||
return new ModifyThreadRequest(metadata); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/io/github/stefanbratanov/chatjpt/OpenAIAssistantsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.github.stefanbratanov.chatjpt; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Subclasses should be based on the <a | ||
* href="https://platform.openai.com/docs/assistants/overview">Assistants API</a> | ||
*/ | ||
class OpenAIAssistantsClient extends OpenAIClient { | ||
|
||
OpenAIAssistantsClient( | ||
String apiKey, | ||
Optional<String> organization, | ||
HttpClient httpClient, | ||
ObjectMapper objectMapper) { | ||
super(apiKey, organization, httpClient, objectMapper); | ||
} | ||
|
||
@Override | ||
HttpRequest.Builder newHttpRequestBuilder(String... headers) { | ||
return super.newHttpRequestBuilder(headers).header("OpenAI-Beta", "assistants=v1"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.stefanbratanov.chatjpt; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* A Thread represents a conversation. It is recommended creating one Thread per user as soon as the | ||
* user initiates the conversation. | ||
*/ | ||
public record Thread(String id, long createdAt, Map<String, String> metadata) {} |
60 changes: 60 additions & 0 deletions
60
src/main/java/io/github/stefanbratanov/chatjpt/ThreadMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.github.stefanbratanov.chatjpt; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public record ThreadMessage( | ||
String content, Optional<List<String>> fileIds, Optional<Map<String, String>> metadata) | ||
implements Message { | ||
@Override | ||
public String role() { | ||
return Constants.USER_MESSAGE_ROLE; | ||
} | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private String content; | ||
private Optional<List<String>> fileIds = Optional.empty(); | ||
private Optional<Map<String, String>> metadata = Optional.empty(); | ||
|
||
/** | ||
* @param content The content of the message. | ||
*/ | ||
public Builder content(String content) { | ||
this.content = content; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param fileIds A list of File IDs that the message should use. There can be a maximum of 10 | ||
* files attached to a message. Useful for tools like retrieval and code_interpreter that | ||
* can access and use files. | ||
*/ | ||
public Builder fileIds(List<String> fileIds) { | ||
this.fileIds = Optional.of(fileIds); | ||
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 ThreadMessage build() { | ||
if (content == null) { | ||
throw new IllegalStateException("content must be set"); | ||
} | ||
return new ThreadMessage(content, fileIds, metadata); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/io/github/stefanbratanov/chatjpt/ThreadsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.github.stefanbratanov.chatjpt; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.Optional; | ||
|
||
/** Based on <a href="https://platform.openai.com/docs/api-reference/threads">Threads</a> */ | ||
public class ThreadsClient extends OpenAIAssistantsClient { | ||
|
||
private final URI baseUrl; | ||
|
||
ThreadsClient( | ||
URI baseUrl, | ||
String apiKey, | ||
Optional<String> organization, | ||
HttpClient httpClient, | ||
ObjectMapper objectMapper) { | ||
super(apiKey, organization, httpClient, objectMapper); | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
/** | ||
* Create a thread. | ||
* | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Thread createThread(CreateThreadRequest request) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.THREADS.getPath())) | ||
.POST(createBodyPublisher(request)) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Thread.class); | ||
} | ||
|
||
/** | ||
* Retrieves a thread. | ||
* | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Thread retrieveThread(String threadId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.THREADS.getPath() + "/" + threadId)) | ||
.GET() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Thread.class); | ||
} | ||
|
||
/** | ||
* Modifies a thread. | ||
* | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Thread modifyThread(String threadId, ModifyThreadRequest request) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.THREADS.getPath() + "/" + threadId)) | ||
.POST(createBodyPublisher(request)) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Thread.class); | ||
} | ||
|
||
/** | ||
* Delete a thread. | ||
* | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public DeletionStatus deleteThread(String threadId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.THREADS.getPath() + "/" + threadId)) | ||
.DELETE() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), DeletionStatus.class); | ||
} | ||
} |
Oops, something went wrong.