Skip to content

Commit 037606e

Browse files
committed
Add Assistant Messages API
Also added just enough Threads API to create messages.
1 parent f223175 commit 037606e

13 files changed

Lines changed: 616 additions & 7 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.theokanning.openai;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
/**
10+
* Common options when getting a list of objects
11+
*/
12+
@Builder
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
@Data
16+
public class ListSearchParameters {
17+
/**
18+
* A limit on the number of objects to be returned.
19+
* Limit can range between 1 and 100, and the default is 20
20+
*/
21+
22+
Integer limit;
23+
24+
/**
25+
* Sort order by the 'created_at' timestamp of the objects.
26+
* 'asc' for ascending order and 'desc' for descending order.
27+
*/
28+
Order order;
29+
30+
/**
31+
* A cursor for use in pagination. after is an object ID that defines your place in the list.
32+
* For instance, if you make a list request and receive 100 objects, ending with obj_foo,
33+
* your subsequent call can include after=obj_foo in order to fetch the next page of the list
34+
*/
35+
String after;
36+
37+
/**
38+
* A cursor for use in pagination. before is an object ID that defines your place in the list.
39+
* For instance, if you make a list request and receive 100 objects, ending with obj_foo,
40+
* your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
41+
*/
42+
String before;
43+
44+
public enum Order {
45+
@JsonProperty("asc")
46+
ASCENDING,
47+
48+
@JsonProperty("desc")
49+
DESCENDING
50+
}
51+
}

api/src/main/java/com/theokanning/openai/OpenAiResponse.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.theokanning.openai;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import lombok.Data;
45

56
import java.util.List;
@@ -18,4 +19,22 @@ public class OpenAiResponse<T> {
1819
* The type of object returned, should be "list"
1920
*/
2021
public String object;
22+
23+
/**
24+
* The first id included
25+
*/
26+
@JsonProperty("first_id")
27+
public String firstId;
28+
29+
/**
30+
* The last id included
31+
*/
32+
@JsonProperty("last_id")
33+
public String lastId;
34+
35+
/**
36+
* True if there are objects after lastId
37+
*/
38+
@JsonProperty("hasMore")
39+
public boolean hasMore;
2140
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.theokanning.openai.messages;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
13+
/**
14+
* Represents a Message within a thread.
15+
* <p>
16+
* https://platform.openai.com/docs/api-reference/messages/object
17+
*/
18+
@Builder
19+
@NoArgsConstructor
20+
@AllArgsConstructor
21+
@Data
22+
public class Message {
23+
/**
24+
* The identifier, which can be referenced in API endpoints.
25+
*/
26+
String id;
27+
28+
/**
29+
* The object type, which is always thread.message.
30+
*/
31+
String object;
32+
33+
/**
34+
* The Unix timestamp (in seconds) for when the message was created.
35+
*/
36+
@JsonProperty("created_at")
37+
int createdAt;
38+
39+
/**
40+
* The thread ID that this message belongs to.
41+
*/
42+
@JsonProperty("thread_id")
43+
String threadId;
44+
45+
/**
46+
* The entity that produced the message. One of user or assistant.
47+
*/
48+
String role;
49+
50+
/**
51+
* The content of the message in an array of text and/or images.
52+
*/
53+
List<Object> content;
54+
55+
/**
56+
* If applicable, the ID of the assistant that authored this message.
57+
*/
58+
@JsonProperty("assistant_id")
59+
String assistantId;
60+
61+
/**
62+
* If applicable, the ID of the run associated with the authoring of this message.
63+
*/
64+
@JsonProperty("run_id")
65+
String runId;
66+
67+
/**
68+
* A list of file IDs that the assistant should use.
69+
* Useful for tools like retrieval and code_interpreter that can access files.
70+
* A maximum of 10 files can be attached to a message.
71+
*/
72+
@JsonProperty("file_ids")
73+
List<String> fileIds;
74+
75+
/**
76+
* Set of 16 key-value pairs that can be attached to an object.
77+
* This can be useful for storing additional information about the object in a structured format.
78+
* Keys can be a maximum of 64 characters long, and values can be a maximum of 512 characters long.
79+
*/
80+
Map<String, String> metadata;
81+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.theokanning.openai.messages;
2+
3+
import lombok.Data;
4+
5+
6+
/**
7+
* Represents the content of a message
8+
* <p>
9+
* https://platform.openai.com/docs/api-reference/messages/object
10+
*/
11+
@Data
12+
public class MessageContent {
13+
/**
14+
* The content type, either "text" or "image_file"
15+
*/
16+
String type;
17+
18+
// todo handle different content types
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.theokanning.openai.messages;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
/**
9+
* A list of files attached to a Message
10+
* <p>
11+
* https://platform.openai.com/docs/api-reference/messages/file-object
12+
*/
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
@Data
16+
public class MessageFile {
17+
/**
18+
* The identifier, which can be referenced in API endpoints.
19+
*/
20+
String id;
21+
22+
/**
23+
* The object type, which is always thread.message.file.
24+
*/
25+
String object;
26+
27+
/**
28+
* The Unix timestamp (in seconds) for when the message file was created.
29+
*/
30+
@JsonProperty("created_at")
31+
int createdAt;
32+
33+
/**
34+
* The ID of the message that the File is attached to.
35+
*/
36+
@JsonProperty("message_id")
37+
String messageId;
38+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.theokanning.openai.messages;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.*;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
/**
10+
* Creates a Message
11+
* <p>
12+
* https://platform.openai.com/docs/api-reference/messages/createMessage
13+
*/
14+
@Builder
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
@Data
18+
public class MessageRequest {
19+
/**
20+
* The role of the entity that is creating the message.
21+
* Currently only "user" is supported.
22+
*/
23+
@NonNull
24+
@Builder.Default
25+
String role = "user";
26+
27+
/**
28+
* The content of the message.
29+
*/
30+
@NonNull
31+
String content;
32+
33+
/**
34+
* A list of File IDs that the message should use.
35+
* Defaults to an empty list.
36+
* There can be a maximum of 10 files attached to a message.
37+
* Useful for tools like retrieval and code_interpreter that can access and use files.
38+
*/
39+
@JsonProperty("file_ids")
40+
List<String> fileIds;
41+
42+
/**
43+
* Set of 16 key-value pairs that can be attached to an object.
44+
* This can be useful for storing additional information about the object in a structured format.
45+
* Keys can be a maximum of 64 characters long, and values can be a maximum of 512 characters long.
46+
*/
47+
Map<String, String> metadata;
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.theokanning.openai.messages;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.Map;
9+
10+
/**
11+
* Modifies a Message
12+
* <p>
13+
* https://platform.openai.com/docs/api-reference/messages/modifyMessage
14+
*/
15+
@Builder
16+
@NoArgsConstructor
17+
@AllArgsConstructor
18+
@Data
19+
public class ModifyMessageRequest {
20+
21+
/**
22+
* Set of 16 key-value pairs that can be attached to an object.
23+
* This can be useful for storing additional information about the object in a structured format.
24+
* Keys can be a maximum of 64 characters long, and values can be a maximum of 512 characters long.
25+
*/
26+
Map<String, String> metadata;
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.theokanning.openai.threads;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.Map;
9+
10+
/**
11+
* Represents a Thread with an assistant
12+
* <p>
13+
* https://platform.openai.com/docs/api-reference/threads/object
14+
*/
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
@Data
18+
public class Thread {
19+
/**
20+
* The identifier, which can be referenced in API endpoints.
21+
*/
22+
String id;
23+
24+
/**
25+
* The object type, which is always thread.
26+
*/
27+
String object;
28+
29+
/**
30+
* The Unix timestamp (in seconds) for when the thread was created.
31+
*/
32+
@JsonProperty("created_at")
33+
int createdAt;
34+
35+
/**
36+
* Set of 16 key-value pairs that can be attached to an object.
37+
* This can be useful for storing additional information about the object in a structured format.
38+
* Keys can be a maximum of 64 characters long, and values can be a maximum of 512 characters long.
39+
*/
40+
Map<String, String> metadata;
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.theokanning.openai.threads;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.theokanning.openai.messages.MessageRequest;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
/**
14+
* Creates a thread
15+
* <p>
16+
* https://platform.openai.com/docs/api-reference/threads/createThread
17+
*/
18+
@Builder
19+
@NoArgsConstructor
20+
@AllArgsConstructor
21+
@Data
22+
public class ThreadRequest {
23+
/**
24+
* A list of messages to start the thread with. Optional.
25+
*/
26+
@JsonProperty("messages")
27+
List<MessageRequest> messages;
28+
29+
/**
30+
* Set of 16 key-value pairs that can be attached to an object.
31+
* This can be useful for storing additional information about the object in a structured format.
32+
* Keys can be a maximum of 64 characters long, and values can be a maximum of 512 characters long.
33+
*/
34+
@JsonProperty("metadata")
35+
Map<String, String> metadata;
36+
}

0 commit comments

Comments
 (0)