Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Support Assistants #395

Merged
merged 12 commits into from
Nov 12, 2023
24 changes: 24 additions & 0 deletions api/src/main/java/com/theokanning/openai/assistants/Assistant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.theokanning.openai.assistants;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class Assistant extends AssistantBase {

/**
* The identifier, which can be referenced in API endpoints.
*/
String id;

/**
* The object type which is always 'assistant'
*/
String object;

/**
* The Unix timestamp(in seconds) for when the assistant was created
*/
@JsonProperty("created_at")
Integer createdAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.theokanning.openai.assistants;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

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

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class AssistantBase {

/**
* ID of the model to use
*/
@NonNull
String model;

/**
* The name of the assistant. The maximum length is 256
*/
String name;

/**
* The description of the assistant.
*/
String description;

/**
* The system instructions that the assistant uses.
*/
String instructions;

/**
* A list of tools enabled on the assistant.
*/
List<Tool> tools;

/**
* A list of file IDs attached to this assistant.
*/
@JsonProperty("file_ids")
List<String> fields;

/**
* Set of 16 key-value pairs that can be attached to an object.
*/
Map<String, String> metadata;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.theokanning.openai.assistants;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class AssistantFile {

/**
* The identifier of the Assistant File
*/
String id;

/**
* The object type, which is always assistant.file.
*/
String object;

/**
* The Unix timestamp (in seconds) for when the assistant file was created.
*/
@JsonProperty("created_at")
String createdAt;

/**
* The assistant ID that the file is attached to
*/
@JsonProperty("assistant_id")
String assistantId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.theokanning.openai.assistants;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class AssistantFileRequest {

@JsonProperty("file_id")
String fileId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.theokanning.openai.assistants;


public class AssistantRequest extends AssistantBase {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.theokanning.openai.assistants;

import com.fasterxml.jackson.annotation.JsonProperty;

public enum AssistantSortOrder {

@JsonProperty("asc")
ASC,

@JsonProperty("desc")
DESC
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.theokanning.openai.assistants;

import com.fasterxml.jackson.annotation.JsonProperty;

public enum AssistantToolsEnum {

@JsonProperty("code_interpreter")
CODE_INTERPRETER,

@JsonProperty("function")
FUNCTION,

@JsonProperty("retrieval")
RETRIEVAL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.theokanning.openai.assistants;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.theokanning.openai.OpenAiResponse;

public class ListAssistant<T extends AssistantBase> extends OpenAiResponse<T> {

@JsonProperty("first_id")
String firstId;

@JsonProperty("last_id")
String lastId;

@JsonProperty("has_more")
boolean hasMore;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.theokanning.openai.assistants;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class ListAssistantQueryRequest {
/**
* A limit on the number of objects to be returned.
* Limit can range between 1 and 100, and the default is 20
*/

Integer limit;

/**
* Sort order by the 'created_at' timestamp of the objects.
* 'asc' for ascending order and 'desc' for descending order.
*/
AssistantSortOrder order;

/**
* A cursor for use in pagination. after is an object ID that defines your place in the list.
* For instance, if you make a list request and receive 100 objects, ending with obj_foo,
* your subsequent call can include after=obj_foo in order to fetch the next page of the list
*/
String after;

/**
* A cursor for use in pagination. before is an object ID that defines your place in the list.
* For instance, if you make a list request and receive 100 objects, ending with obj_foo,
* your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
*/
String before;
}
12 changes: 12 additions & 0 deletions api/src/main/java/com/theokanning/openai/assistants/Tool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.theokanning.openai.assistants;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Tool {
AssistantToolsEnum type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class TikTokensUtil {
modelMap.put(ModelEnum.GPT_4_32K.getName(), registry.getEncodingForModel(ModelType.GPT_4));
modelMap.put(ModelEnum.GPT_4_32K_0314.getName(), registry.getEncodingForModel(ModelType.GPT_4));
modelMap.put(ModelEnum.GPT_4_0314.getName(), registry.getEncodingForModel(ModelType.GPT_4));
modelMap.put(ModelEnum.GPT_4_1106_preview.getName(), registry.getEncodingForModel(ModelType.GPT_4));
}

/**
Expand Down Expand Up @@ -261,7 +262,11 @@ public enum ModelEnum {
* Temporary model, not recommended for use.
*/
GPT_4_32K_0314("gpt-4-32k-0314"),
;

/**
* Temporary model, not recommended for use.
*/
GPT_4_1106_preview("gpt-4-1106-preview");
private String name;
}

Expand Down
43 changes: 43 additions & 0 deletions client/src/main/java/com/theokanning/openai/client/OpenAiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import com.theokanning.openai.DeleteResult;
import com.theokanning.openai.OpenAiResponse;
import com.theokanning.openai.assistants.AssistantBase;
import com.theokanning.openai.assistants.Assistant;
import com.theokanning.openai.assistants.AssistantFile;
import com.theokanning.openai.assistants.AssistantFileRequest;
import com.theokanning.openai.assistants.ListAssistant;
import com.theokanning.openai.assistants.ListAssistantQueryRequest;
import com.theokanning.openai.audio.CreateSpeechRequest;
import com.theokanning.openai.audio.TranscriptionResult;
import com.theokanning.openai.audio.TranslationResult;
Expand Down Expand Up @@ -36,6 +42,7 @@
import retrofit2.http.*;

import java.time.LocalDate;
import java.util.Map;

public interface OpenAiApi {

Expand Down Expand Up @@ -185,4 +192,40 @@ public interface OpenAiApi {
@GET("v1/dashboard/billing/usage")
Single<BillingUsage> billingUsage(@Query("start_date") LocalDate starDate, @Query("end_date") LocalDate endDate);


@Headers({"OpenAI-Beta: assistants=v1"})
@POST("/v1/assistants")
Single<Assistant> createAssistant(@Body AssistantBase request);

@Headers({"OpenAI-Beta: assistants=v1"})
@GET("/v1/assistants/{assistant_id}")
Single<Assistant> retrieveAssistant(@Path("assistant_id") String assistantId);

@Headers({"OpenAI-Beta: assistants=v1"})
@POST("/v1/assistants/{assistant_id}")
Single<Assistant> modifyAssistant(@Path("assistant_id") String assistantId, @Body AssistantBase request);

@Headers({"OpenAI-Beta: assistants=v1"})
@DELETE("/v1/assistants/{assistant_id}")
Single<DeleteResult> deleteAssistant(@Path("assistant_id") String assistantId);

@Headers({"OpenAI-Beta: assistants=v1"})
@GET("/v1/assistants")
Single<ListAssistant<Assistant>> listAssistants(@QueryMap Map<String, Object> filterRequest);

@Headers({"OpenAI-Beta: assistants=v1"})
@POST("/v1/assistants/{assistant_id}/files")
Single<AssistantFile> createAssistantFile(@Path("assistant_id") String assistantId, @Body AssistantFileRequest fileRequest);

@Headers({"OpenAI-Beta: assistants=v1"})
@GET("/v1/assistants/{assistant_id}/files/{file_id}")
Single<AssistantFile> retrieveAssistantFile(@Path("assistant_id") String assistantId, @Path("file_id") String fileId);

@Headers({"OpenAI-Beta: assistants=v1"})
@DELETE("/v1/assistants/{assistant_id}/files/{file_id}")
Single<DeleteResult> deleteAssistantFile(@Path("assistant_id") String assistantId, @Path("file_id") String fileId);

@Headers({"OpenAI-Beta: assistants=v1"})
@GET("/v1/assistants/{assistant_id}/files")
Single<ListAssistant<Assistant>> listAssistantFiles(@Path("assistant_id") String assistantId, @QueryMap Map<String, Object> filterRequest);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.theokanning.openai.service;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.node.TextNode;
import com.theokanning.openai.DeleteResult;
import com.theokanning.openai.OpenAiError;
import com.theokanning.openai.OpenAiHttpException;
import com.theokanning.openai.assistants.Assistant;
import com.theokanning.openai.assistants.AssistantBase;
import com.theokanning.openai.assistants.AssistantFile;
import com.theokanning.openai.assistants.AssistantFileRequest;
import com.theokanning.openai.assistants.ListAssistant;
import com.theokanning.openai.assistants.ListAssistantQueryRequest;
import com.theokanning.openai.audio.CreateSpeechRequest;
import com.theokanning.openai.audio.CreateTranscriptionRequest;
import com.theokanning.openai.audio.CreateTranslationRequest;
Expand Down Expand Up @@ -53,6 +60,7 @@
import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -348,6 +356,44 @@ public ModerationResult createModeration(ModerationRequest request) {
return execute(api.createModeration(request));
}

public Assistant createAssistant(AssistantBase request) {
return execute(api.createAssistant(request));
}

public Assistant retrieveAssistant(String assistantId) {
return execute(api.retrieveAssistant(assistantId));
}

public Assistant modifyAssistant(String assistantId, AssistantBase request) {
return execute(api.modifyAssistant(assistantId, request));
}

public DeleteResult deleteAssistant(String assistantId) {
return execute(api.deleteAssistant(assistantId));
}

public ListAssistant<Assistant> listAssistants(ListAssistantQueryRequest filterRequest) {
Map<String, Object> queryParameters = mapper.convertValue(filterRequest, new TypeReference<Map<String, Object>>() {});
return execute(api.listAssistants(queryParameters));
}

public AssistantFile createAssistantFile(String assistantId, AssistantFileRequest fileRequest) {
return execute(api.createAssistantFile(assistantId, fileRequest));
}

public AssistantFile retrieveAssistantFile(String assistantId, String fileId) {
return execute(api.retrieveAssistantFile(assistantId, fileId));
}

public DeleteResult deleteAssistantFile(String assistantId, String fileId) {
return execute(api.deleteAssistantFile(assistantId, fileId));
}

public ListAssistant<Assistant> listAssistantFiles(String assistantId, ListAssistantQueryRequest filterRequest) {
Map<String, Object> queryParameters = mapper.convertValue(filterRequest, new TypeReference<Map<String, Object>>() {});
return execute(api.listAssistantFiles(assistantId, queryParameters));
}

public ResponseBody createSpeech(CreateSpeechRequest request) {
return execute(api.createSpeech(request));
}
Expand Down
Loading