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

Commit 741cfe2

Browse files
authored
Add MessageContent types and simplify list querying (#405)
Changed assistants to use the new shared ListSearchParams.
1 parent 4ff2758 commit 741cfe2

File tree

14 files changed

+220
-68
lines changed

14 files changed

+220
-68
lines changed

api/src/main/java/com/theokanning/openai/assistants/ListAssistant.java

-16
This file was deleted.

api/src/main/java/com/theokanning/openai/assistants/ListAssistantQueryRequest.java

-39
This file was deleted.

api/src/main/java/com/theokanning/openai/messages/Message.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class Message {
5050
/**
5151
* The content of the message in an array of text and/or images.
5252
*/
53-
List<Object> content;
53+
List<MessageContent> content;
5454

5555
/**
5656
* If applicable, the ID of the assistant that authored this message.

api/src/main/java/com/theokanning/openai/messages/MessageContent.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.theokanning.openai.messages;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.theokanning.openai.messages.content.ImageFile;
5+
import com.theokanning.openai.messages.content.Text;
36
import lombok.Data;
47

58

@@ -15,5 +18,14 @@ public class MessageContent {
1518
*/
1619
String type;
1720

18-
// todo handle different content types
21+
/**
22+
* Text content of the message. Only present if type == text
23+
*/
24+
Text text;
25+
26+
/**
27+
* The image content of a message. Only present if type == image_file
28+
*/
29+
@JsonProperty("image_file")
30+
ImageFile imageFile;
1931
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.theokanning.openai.messages.content;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
/**
9+
* An annotation for a text Message
10+
* <p>
11+
* https://platform.openai.com/docs/api-reference/messages/object
12+
*/
13+
@Data
14+
@NoArgsConstructor
15+
@AllArgsConstructor
16+
public class Annotation {
17+
/**
18+
* The type of annotation, either file_citation or file_path
19+
*/
20+
String type;
21+
22+
/**
23+
* The text in the message content that needs to be replaced
24+
*/
25+
String text;
26+
27+
/**
28+
* File citation details, only present when type == file_citation
29+
*/
30+
@JsonProperty("file_citation")
31+
FileCitation fileCitation;
32+
33+
/**
34+
* File path details, only present when type == file_path
35+
*/
36+
@JsonProperty("file_path")
37+
FilePath filePath;
38+
39+
@JsonProperty("start_index")
40+
int startIndex;
41+
42+
@JsonProperty("end_index")
43+
int endIndex;
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.theokanning.openai.messages.content;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
/**
9+
* A citation within the message that points to a specific quote from a specific File associated with the
10+
* assistant or the message. Generated when the assistant uses the "retrieval" tool to search files.
11+
* <p>
12+
* https://platform.openai.com/docs/api-reference/messages/object
13+
*/
14+
@Data
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class FileCitation {
18+
19+
/**
20+
* The ID of the specific File the citation is from.
21+
*/
22+
@JsonProperty("file_id")
23+
String fileId;
24+
25+
/**
26+
* The specific quote in the file.
27+
*/
28+
String quote;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.theokanning.openai.messages.content;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
/**
9+
* A URL for the file that's generated when the assistant used the code_interpreter tool to generate a file.
10+
* <p>
11+
* https://platform.openai.com/docs/api-reference/messages/object
12+
*/
13+
@Data
14+
@NoArgsConstructor
15+
@AllArgsConstructor
16+
public class FilePath {
17+
18+
/**
19+
* The ID of the file that was generated
20+
*/
21+
@JsonProperty("file_id")
22+
String fileId;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.theokanning.openai.messages.content;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
/**
9+
* References an image File int eh content of a message.
10+
* <p>
11+
* /https://platform.openai.com/docs/api-reference/messages/object
12+
*/
13+
@Data
14+
@NoArgsConstructor
15+
@AllArgsConstructor
16+
public class ImageFile {
17+
18+
/**
19+
* The File ID of the image in the message content.
20+
*/
21+
@JsonProperty("file_id")
22+
String fileId;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.theokanning.openai.messages.content;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.util.List;
8+
9+
/**
10+
* The text content that is part of a message
11+
* <p>
12+
* https://platform.openai.com/docs/api-reference/messages/object
13+
*/
14+
@Data
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class Text {
18+
19+
/**
20+
* The data that makes up the text.
21+
*/
22+
String value;
23+
24+
/**
25+
* Text annotations that show additional details
26+
*/
27+
List<Annotation> annotations;
28+
}

api/src/test/java/com/theokanning/openai/JsonTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.theokanning.openai.finetune.FineTuneEvent;
1919
import com.theokanning.openai.finetune.FineTuneResult;
2020
import com.theokanning.openai.image.ImageResult;
21+
import com.theokanning.openai.messages.Message;
2122
import com.theokanning.openai.model.Model;
2223
import com.theokanning.openai.moderation.ModerationRequest;
2324
import com.theokanning.openai.moderation.ModerationResult;
@@ -50,6 +51,7 @@ public class JsonTest {
5051
ImageResult.class,
5152
TranscriptionResult.class,
5253
TranslationResult.class,
54+
Message.class,
5355
Model.class,
5456
ModerationRequest.class,
5557
ModerationResult.class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"id": "msg_abc123",
3+
"object": "thread.message",
4+
"created_at": 1698983503,
5+
"thread_id": "thread_abc123",
6+
"role": "assistant",
7+
"content": [
8+
{
9+
"type": "text",
10+
"text": {
11+
"value": "Hi! How can I help you today?",
12+
"annotations": [
13+
{
14+
"type": "file_citation",
15+
"text": "file citation text",
16+
"file_citation": {
17+
"file_id": "file citation id",
18+
"quote": "Enough, Reggie"
19+
},
20+
"start_index": 0,
21+
"end_index": 1
22+
},
23+
{
24+
"type": "file_path",
25+
"text": "file path text",
26+
"file_path": {
27+
"file_id": "file id"
28+
},
29+
"start_index": 1,
30+
"end_index": 2
31+
}
32+
]
33+
}
34+
},
35+
{
36+
"type": "image_file",
37+
"image_file": {
38+
"file_id": "image file id"
39+
}
40+
}
41+
],
42+
"file_ids": [],
43+
"assistant_id": "asst_abc123",
44+
"run_id": "run_abc123",
45+
"metadata": {}
46+
}

client/src/main/java/com/theokanning/openai/client/OpenAiApi.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public interface OpenAiApi {
213213

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

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

230230
@Headers({"OpenAI-Beta: assistants=v1"})
231231
@GET("/v1/assistants/{assistant_id}/files")
232-
Single<ListAssistant<Assistant>> listAssistantFiles(@Path("assistant_id") String assistantId, @QueryMap Map<String, Object> filterRequest);
232+
Single<OpenAiResponse<Assistant>> listAssistantFiles(@Path("assistant_id") String assistantId, @QueryMap Map<String, Object> filterRequest);
233233

234234
@Headers({"OpenAI-Beta: assistants=v1"})
235235
@POST("/v1/threads")

service/src/main/java/com/theokanning/openai/service/OpenAiService.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ public DeleteResult deleteAssistant(String assistantId) {
379379
return execute(api.deleteAssistant(assistantId));
380380
}
381381

382-
public ListAssistant<Assistant> listAssistants(ListAssistantQueryRequest filterRequest) {
383-
Map<String, Object> queryParameters = mapper.convertValue(filterRequest, new TypeReference<Map<String, Object>>() {
382+
public OpenAiResponse<Assistant> listAssistants(ListSearchParameters params) {
383+
Map<String, Object> queryParameters = mapper.convertValue(params, new TypeReference<Map<String, Object>>() {
384384
});
385385
return execute(api.listAssistants(queryParameters));
386386
}
@@ -397,8 +397,8 @@ public DeleteResult deleteAssistantFile(String assistantId, String fileId) {
397397
return execute(api.deleteAssistantFile(assistantId, fileId));
398398
}
399399

400-
public ListAssistant<Assistant> listAssistantFiles(String assistantId, ListAssistantQueryRequest filterRequest) {
401-
Map<String, Object> queryParameters = mapper.convertValue(filterRequest, new TypeReference<Map<String, Object>>() {
400+
public OpenAiResponse<Assistant> listAssistantFiles(String assistantId, ListSearchParameters params) {
401+
Map<String, Object> queryParameters = mapper.convertValue(params, new TypeReference<Map<String, Object>>() {
402402
});
403403
return execute(api.listAssistantFiles(assistantId, queryParameters));
404404
}

service/src/test/java/com/theokanning/openai/service/AssistantTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.theokanning.openai.service;
22

33
import com.theokanning.openai.DeleteResult;
4+
import com.theokanning.openai.ListSearchParameters;
5+
import com.theokanning.openai.OpenAiResponse;
46
import com.theokanning.openai.assistants.*;
57
import com.theokanning.openai.file.File;
68
import com.theokanning.openai.utils.TikTokensUtil;
@@ -21,8 +23,6 @@ public class AssistantTest {
2123

2224
static OpenAiService service = new OpenAiService(token);
2325

24-
25-
2626
@Test
2727
void retrieveAssistant() {
2828
Assistant createAssistantResponse = createAndValidateAssistant();
@@ -58,7 +58,7 @@ void deleteAssistant() {
5858

5959
@Test
6060
void listAssistants() {
61-
ListAssistant<Assistant> assistants = service.listAssistants(ListAssistantQueryRequest.builder().build());
61+
OpenAiResponse<Assistant> assistants = service.listAssistants(ListSearchParameters.builder().build());
6262

6363
assertNotNull(assistants);
6464
assertFalse(assistants.getData().isEmpty());
@@ -101,10 +101,10 @@ void listAssistantFiles() {
101101
@AfterAll
102102
static void clean() {
103103
//Clean up all data created during this test
104-
ListAssistantQueryRequest queryFilter = ListAssistantQueryRequest.builder()
104+
ListSearchParameters queryFilter = ListSearchParameters.builder()
105105
.limit(100)
106106
.build();
107-
ListAssistant<Assistant> assistantListAssistant = service.listAssistants(queryFilter);
107+
OpenAiResponse<Assistant> assistantListAssistant = service.listAssistants(queryFilter);
108108
assistantListAssistant.getData().forEach(assistant ->{
109109
service.deleteAssistant(assistant.getId());
110110
});

0 commit comments

Comments
 (0)