-
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.
- Loading branch information
1 parent
c902893
commit 3c246fc
Showing
13 changed files
with
256 additions
and
13 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
25 changes: 25 additions & 0 deletions
25
src/main/java/io/github/stefanbratanov/jvm/openai/CreateProjectRequest.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,25 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
public record CreateProjectRequest(String name) { | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private String name; | ||
|
||
/** | ||
* @param name The friendly name of the project, this name appears in reports. | ||
*/ | ||
public Builder name(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
public CreateProjectRequest build() { | ||
return new CreateProjectRequest(name); | ||
} | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/io/github/stefanbratanov/jvm/openai/ModifyProjectRequest.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,25 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
public record ModifyProjectRequest(String name) { | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private String name; | ||
|
||
/** | ||
* @param name The updated name of the project, this name appears in reports. | ||
*/ | ||
public Builder name(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
public ModifyProjectRequest build() { | ||
return new ModifyProjectRequest(name); | ||
} | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
src/main/java/io/github/stefanbratanov/jvm/openai/Project.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,4 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
/** Represents an individual project. */ | ||
public record Project(String id, String name, long createdAt, Long archivedAt, String status) {} |
128 changes: 128 additions & 0 deletions
128
src/main/java/io/github/stefanbratanov/jvm/openai/ProjectsClient.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,128 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpRequest.BodyPublishers; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Manage the projects within an organization includes creation, updating, and archiving or | ||
* projects. The Default project cannot be modified or archived. | ||
* | ||
* <p>Based on <a href="https://platform.openai.com/docs/api-reference/projects">Projects</a> | ||
*/ | ||
public final class ProjectsClient extends OpenAIClient { | ||
|
||
private final URI baseUrl; | ||
|
||
ProjectsClient( | ||
URI baseUrl, | ||
String[] authenticationHeaders, | ||
HttpClient httpClient, | ||
Optional<Duration> requestTimeout) { | ||
super(authenticationHeaders, httpClient, requestTimeout); | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
/** | ||
* Returns a list of projects. | ||
* | ||
* @param after A cursor for use in pagination. after is an object ID that defines your place in | ||
* the list. | ||
* @param limit A limit on the number of objects to be returned. | ||
* @param includeArchived If true returns all projects including those that have been archived. | ||
* Archived projects are not included by default. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public PaginatedProjects listProjects( | ||
Optional<String> after, Optional<Integer> limit, Optional<Boolean> includeArchived) { | ||
String queryParameters = | ||
createQueryParameters( | ||
Map.of( | ||
Constants.LIMIT_QUERY_PARAMETER, | ||
limit, | ||
Constants.AFTER_QUERY_PARAMETER, | ||
after, | ||
"include_archived", | ||
includeArchived)); | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.PROJECTS.getPath() + queryParameters)) | ||
.GET() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), PaginatedProjects.class); | ||
} | ||
|
||
public record PaginatedProjects( | ||
List<Project> data, String firstId, String lastId, boolean hasMore) {} | ||
|
||
/** | ||
* Create a new project in the organization. Projects can be created and archived, but cannot be | ||
* deleted. | ||
* | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Project createProject(CreateProjectRequest request) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder(Constants.CONTENT_TYPE_HEADER, Constants.JSON_MEDIA_TYPE) | ||
.uri(baseUrl.resolve(Endpoint.PROJECTS.getPath())) | ||
.POST(createBodyPublisher(request)) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Project.class); | ||
} | ||
|
||
/** | ||
* Retrieves a project. | ||
* | ||
* @param projectId The ID of the project. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Project retrieveProject(String projectId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.PROJECTS.getPath() + "/" + projectId)) | ||
.GET() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Project.class); | ||
} | ||
|
||
/** | ||
* Modifies a project in the organization. | ||
* | ||
* @param projectId The ID of the project. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Project modifyProject(String projectId, ModifyProjectRequest request) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder(Constants.CONTENT_TYPE_HEADER, Constants.JSON_MEDIA_TYPE) | ||
.uri(baseUrl.resolve(Endpoint.PROJECTS.getPath() + "/" + projectId)) | ||
.POST(createBodyPublisher(request)) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Project.class); | ||
} | ||
|
||
/** | ||
* Archives a project in the organization. Archived projects cannot be used or updated. | ||
* | ||
* @param projectId The ID of the project. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Project archiveProject(String projectId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.PROJECTS.getPath() + "/" + projectId + "/archive")) | ||
.POST(BodyPublishers.noBody()) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Project.class); | ||
} | ||
} |
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
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