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

Commit

Permalink
Add MessageContent types and simplify list querying (#405)
Browse files Browse the repository at this point in the history
Changed assistants to use the new shared ListSearchParams.
  • Loading branch information
TheoKanning authored Nov 16, 2023
1 parent 4ff2758 commit 741cfe2
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 68 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Message {
/**
* The content of the message in an array of text and/or images.
*/
List<Object> content;
List<MessageContent> content;

/**
* If applicable, the ID of the assistant that authored this message.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.theokanning.openai.messages;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.theokanning.openai.messages.content.ImageFile;
import com.theokanning.openai.messages.content.Text;
import lombok.Data;


Expand All @@ -15,5 +18,14 @@ public class MessageContent {
*/
String type;

// todo handle different content types
/**
* Text content of the message. Only present if type == text
*/
Text text;

/**
* The image content of a message. Only present if type == image_file
*/
@JsonProperty("image_file")
ImageFile imageFile;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.theokanning.openai.messages.content;

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

/**
* An annotation for a text Message
* <p>
* https://platform.openai.com/docs/api-reference/messages/object
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Annotation {
/**
* The type of annotation, either file_citation or file_path
*/
String type;

/**
* The text in the message content that needs to be replaced
*/
String text;

/**
* File citation details, only present when type == file_citation
*/
@JsonProperty("file_citation")
FileCitation fileCitation;

/**
* File path details, only present when type == file_path
*/
@JsonProperty("file_path")
FilePath filePath;

@JsonProperty("start_index")
int startIndex;

@JsonProperty("end_index")
int endIndex;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.theokanning.openai.messages.content;

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

/**
* A citation within the message that points to a specific quote from a specific File associated with the
* assistant or the message. Generated when the assistant uses the "retrieval" tool to search files.
* <p>
* https://platform.openai.com/docs/api-reference/messages/object
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FileCitation {

/**
* The ID of the specific File the citation is from.
*/
@JsonProperty("file_id")
String fileId;

/**
* The specific quote in the file.
*/
String quote;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.theokanning.openai.messages.content;

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

/**
* A URL for the file that's generated when the assistant used the code_interpreter tool to generate a file.
* <p>
* https://platform.openai.com/docs/api-reference/messages/object
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FilePath {

/**
* The ID of the file that was generated
*/
@JsonProperty("file_id")
String fileId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.theokanning.openai.messages.content;

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

/**
* References an image File int eh content of a message.
* <p>
* /https://platform.openai.com/docs/api-reference/messages/object
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ImageFile {

/**
* The File ID of the image in the message content.
*/
@JsonProperty("file_id")
String fileId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.theokanning.openai.messages.content;

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

import java.util.List;

/**
* The text content that is part of a message
* <p>
* https://platform.openai.com/docs/api-reference/messages/object
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Text {

/**
* The data that makes up the text.
*/
String value;

/**
* Text annotations that show additional details
*/
List<Annotation> annotations;
}
2 changes: 2 additions & 0 deletions api/src/test/java/com/theokanning/openai/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.theokanning.openai.finetune.FineTuneEvent;
import com.theokanning.openai.finetune.FineTuneResult;
import com.theokanning.openai.image.ImageResult;
import com.theokanning.openai.messages.Message;
import com.theokanning.openai.model.Model;
import com.theokanning.openai.moderation.ModerationRequest;
import com.theokanning.openai.moderation.ModerationResult;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class JsonTest {
ImageResult.class,
TranscriptionResult.class,
TranslationResult.class,
Message.class,
Model.class,
ModerationRequest.class,
ModerationResult.class
Expand Down
46 changes: 46 additions & 0 deletions api/src/test/resources/fixtures/Message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"id": "msg_abc123",
"object": "thread.message",
"created_at": 1698983503,
"thread_id": "thread_abc123",
"role": "assistant",
"content": [
{
"type": "text",
"text": {
"value": "Hi! How can I help you today?",
"annotations": [
{
"type": "file_citation",
"text": "file citation text",
"file_citation": {
"file_id": "file citation id",
"quote": "Enough, Reggie"
},
"start_index": 0,
"end_index": 1
},
{
"type": "file_path",
"text": "file path text",
"file_path": {
"file_id": "file id"
},
"start_index": 1,
"end_index": 2
}
]
}
},
{
"type": "image_file",
"image_file": {
"file_id": "image file id"
}
}
],
"file_ids": [],
"assistant_id": "asst_abc123",
"run_id": "run_abc123",
"metadata": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public interface OpenAiApi {

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

@Headers({"OpenAI-Beta: assistants=v1"})
@POST("/v1/assistants/{assistant_id}/files")
Expand All @@ -229,7 +229,7 @@ public interface OpenAiApi {

@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);
Single<OpenAiResponse<Assistant>> listAssistantFiles(@Path("assistant_id") String assistantId, @QueryMap Map<String, Object> filterRequest);

@Headers({"OpenAI-Beta: assistants=v1"})
@POST("/v1/threads")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ 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>>() {
public OpenAiResponse<Assistant> listAssistants(ListSearchParameters params) {
Map<String, Object> queryParameters = mapper.convertValue(params, new TypeReference<Map<String, Object>>() {
});
return execute(api.listAssistants(queryParameters));
}
Expand All @@ -397,8 +397,8 @@ 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>>() {
public OpenAiResponse<Assistant> listAssistantFiles(String assistantId, ListSearchParameters params) {
Map<String, Object> queryParameters = mapper.convertValue(params, new TypeReference<Map<String, Object>>() {
});
return execute(api.listAssistantFiles(assistantId, queryParameters));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.theokanning.openai.service;

import com.theokanning.openai.DeleteResult;
import com.theokanning.openai.ListSearchParameters;
import com.theokanning.openai.OpenAiResponse;
import com.theokanning.openai.assistants.*;
import com.theokanning.openai.file.File;
import com.theokanning.openai.utils.TikTokensUtil;
Expand All @@ -21,8 +23,6 @@ public class AssistantTest {

static OpenAiService service = new OpenAiService(token);



@Test
void retrieveAssistant() {
Assistant createAssistantResponse = createAndValidateAssistant();
Expand Down Expand Up @@ -58,7 +58,7 @@ void deleteAssistant() {

@Test
void listAssistants() {
ListAssistant<Assistant> assistants = service.listAssistants(ListAssistantQueryRequest.builder().build());
OpenAiResponse<Assistant> assistants = service.listAssistants(ListSearchParameters.builder().build());

assertNotNull(assistants);
assertFalse(assistants.getData().isEmpty());
Expand Down Expand Up @@ -101,10 +101,10 @@ void listAssistantFiles() {
@AfterAll
static void clean() {
//Clean up all data created during this test
ListAssistantQueryRequest queryFilter = ListAssistantQueryRequest.builder()
ListSearchParameters queryFilter = ListSearchParameters.builder()
.limit(100)
.build();
ListAssistant<Assistant> assistantListAssistant = service.listAssistants(queryFilter);
OpenAiResponse<Assistant> assistantListAssistant = service.listAssistants(queryFilter);
assistantListAssistant.getData().forEach(assistant ->{
service.deleteAssistant(assistant.getId());
});
Expand Down

0 comments on commit 741cfe2

Please sign in to comment.