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
Prev Previous commit
Next Next commit
RemyOhajinwa#390 Create Assistant File
  • Loading branch information
codechunker committed Nov 11, 2023
commit 1be8bfe74c69b1fa8dc91fda58d71099c23f1ce6
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
Expand Up @@ -4,6 +4,8 @@
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.DeleteAssistantResult;
import com.theokanning.openai.assistants.ListAssistant;
import com.theokanning.openai.assistants.ListAssistantQueryRequest;
Expand Down Expand Up @@ -208,4 +210,7 @@ public interface OpenAiApi {
@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
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.CreateTranscriptionRequest;
Expand Down Expand Up @@ -167,6 +169,7 @@ public File uploadFile(String purpose, String filepath) {
RequestBody purposeBody = RequestBody.create(okhttp3.MultipartBody.FORM, purpose);
RequestBody fileBody = RequestBody.create(MediaType.parse("text"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", filepath, fileBody);
System.out.println(purposeBody.contentType());

return execute(api.uploadFile(purposeBody, body));
}
Expand Down Expand Up @@ -372,6 +375,10 @@ public ListAssistant<Assistant> listAssistants(ListAssistantQueryRequest filterR
return execute(api.listAssistants(queryParameters));
}

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


/**
* Calls the Open AI api, returns the response, and parses error messages if the request fails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.theokanning.openai.DeleteResult;
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.AssistantRequest;
import com.theokanning.openai.assistants.AssistantSortOrder;
import com.theokanning.openai.assistants.AssistantToolsEnum;
import com.theokanning.openai.assistants.ListAssistant;
import com.theokanning.openai.assistants.ListAssistantQueryRequest;
import com.theokanning.openai.assistants.Tool;
import com.theokanning.openai.file.File;
import com.theokanning.openai.utils.TikTokensUtil;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -32,7 +35,7 @@ public class AssistantTest {
static void initAssistants() {
//Done so that listAssistant tests won't fail on initial setup of project
for (int i = 0; i < 3; i++) {
createAndValidateAssistant();
// createAndValidateAssistant();
}
}

Expand Down Expand Up @@ -128,6 +131,20 @@ void listAssistants_returnsDescSortedAssistants() {
assertTrue(firstTwoDescending && lastTwoDescending);
}

@Test
void createAssistantFile() {
String filePath = "src/test/resources/assistant-file-data.json";
File uploadedFile = service.uploadFile("fine-tune", filePath);//uploading with 'purpose=assistants' seem not to be working

Assistant assistant = createAndValidateAssistant();

AssistantFile assistantFile = service.createAssistantFile(assistant.getId(), new AssistantFileRequest(uploadedFile.getId()));

assertNotNull(assistantFile);
assertEquals(uploadedFile.getId(), assistantFile.getId());
assertEquals(assistant.getId(), assistantFile.getAssistantId());
}

private static Assistant createAndValidateAssistant() {
AssistantBase assistantRequest = assistantStub();
Assistant createAssistantResponse = service.createAssistant(assistantRequest);
Expand Down
1 change: 1 addition & 0 deletions service/src/test/resources/assistant-file-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"prompt": "prompt", "completion": "text"}