Skip to content

Commit 5aca21a

Browse files
Grt1228guorutao
authored andcommitted
feature 1.1.3 assistant api——run开发
1 parent 6c9c9e2 commit 5aca21a

7 files changed

Lines changed: 495 additions & 39 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ WebSocket参考:[OpenAIWebSocketEventSourceListener](https://github.com/Grt122
7979
- 支持全部OpenAI的Api
8080

8181
# 📑 更新日志
82+
- [x] 1.1.3 支持Assistant、Run、Thread、Message Api
8283
- [x] 1.1.2-beta0 支持附加图片的ChatCompletion、指定返回数据格式、Tool Call、Dall-e-3生成图片、FineTuneJob、文本转语音TTS。官方文档示例:[chatgpt-java.unfbx.com](https://chatgpt-java.unfbx.com/docs/category/-%E6%A0%B8%E5%BF%83%E5%8A%9F%E8%83%BD) 。测试案例[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/v1_1_2/OpenAiClientTest.java)
8384
- [x] 1.1.1-beta0 function call改成GPT插件模式调用更加简单的调用方式.参考实现[PluginTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/PluginTest.java)
8485
- [x] 1.0.15 moderations接口更新,代码优化,序列化修复。

README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ WebSocket Reference:[OpenAIWebSocketEventSourceListener](https://github.com/Gr
6565
- Supports all OpenAI APIs.
6666

6767
# 📑 Update Log
68+
- [x] 1.1.3 Upgrade to support Assistant、Run、Thread、Message Api
69+
- [x] 1.1.2-beta0 Upgrade to support chat completion with picture GPT-4V、return JSON model、Tool Call、Dall-e-3、Fine Tune Job、TTS.
6870
- [x] 1.1.1-beta0 function call update to GPT plugin model, so easy use the plugin api. eg: [PluginTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/PluginTest.java)
6971
- [x] 1.0.15 moderations api update,code fix,bug fix
7072
- [x] 1.0.14 Upgrade to support the latest version gpt-3.5—0614、gpt-4.0—0614 models, support function-calling full test e.g.:[OpenAiClientFunctionTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientFunctionTest.java)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.unfbx</groupId>
77
<artifactId>chatgpt-java</artifactId>
8-
<version>1.1.2-beta0</version>
8+
<version>1.1.3</version>
99
<name>chatgpt-java</name>
1010
<description>OpenAI Java SDK, OpenAI Api for Java. ChatGPT Java SDK .</description>
1111
<url>https://chatgpt-java.unfbx.com</url>

src/main/java/com/unfbx/chatgpt/OpenAiApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ Single<AssistantListResponse<MessageResponse>> messages(@Path("thread_id") Strin
668668
*/
669669
@GET("v1/threads/{thread_id}/messages/{message_id}/files")
670670
@Headers("OpenAI-Beta: assistants=v1")
671-
Single<AssistantListResponse<MessageResponse>> messageFiles(@Path("thread_id") String threadId, @Path("message_id") String messageId,
671+
Single<AssistantListResponse<MessageFileResponse>> messageFiles(@Path("thread_id") String threadId, @Path("message_id") String messageId,
672672
@Query("limit") Integer limit, @Query("order") String order, @Query("before") String before, @Query("after") String after);
673673

674674

src/main/java/com/unfbx/chatgpt/OpenAiClient.java

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import com.unfbx.chatgpt.constant.OpenAIConst;
88
import com.unfbx.chatgpt.entity.Tts.TextToSpeech;
99
import com.unfbx.chatgpt.entity.assistant.*;
10+
import com.unfbx.chatgpt.entity.assistant.message.MessageFileResponse;
11+
import com.unfbx.chatgpt.entity.assistant.message.MessageResponse;
12+
import com.unfbx.chatgpt.entity.assistant.message.ModifyMessage;
13+
import com.unfbx.chatgpt.entity.assistant.run.*;
14+
import com.unfbx.chatgpt.entity.assistant.thread.ThreadMessage;
1015
import com.unfbx.chatgpt.entity.billing.BillingUsage;
1116
import com.unfbx.chatgpt.entity.billing.CreditGrantsResponse;
1217
import com.unfbx.chatgpt.entity.billing.Subscription;
@@ -32,6 +37,7 @@
3237
import com.unfbx.chatgpt.entity.fineTune.job.FineTuneJobListResponse;
3338
import com.unfbx.chatgpt.entity.fineTune.job.FineTuneJobResponse;
3439
import com.unfbx.chatgpt.entity.images.*;
40+
import com.unfbx.chatgpt.entity.images.Image;
3541
import com.unfbx.chatgpt.entity.models.Model;
3642
import com.unfbx.chatgpt.entity.models.ModelResponse;
3743
import com.unfbx.chatgpt.entity.moderations.Moderation;
@@ -61,6 +67,8 @@
6167
import retrofit2.Retrofit;
6268
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
6369
import retrofit2.converter.jackson.JacksonConverterFactory;
70+
import retrofit2.http.*;
71+
import retrofit2.http.Headers;
6472

6573
import java.time.LocalDate;
6674
import java.util.*;
@@ -112,6 +120,12 @@ public class OpenAiClient {
112120
@Getter
113121
private OpenAiAuthInterceptor authInterceptor;
114122

123+
/**
124+
* 默认的分页参数
125+
*/
126+
@Getter
127+
private PageRequest pageRequest = PageRequest.builder().build();
128+
115129
/**
116130
* 构造器
117131
*
@@ -1111,6 +1125,7 @@ public DeleteResponse deleteAssistantFile(String assistantId, String fileId) {
11111125
* @return 助手文件列表
11121126
*/
11131127
public AssistantListResponse<AssistantFileResponse> assistantFiles(String assistantId, PageRequest pageRequest) {
1128+
pageRequest = Optional.ofNullable(pageRequest).orElse(this.getPageRequest());
11141129
Single<AssistantListResponse<AssistantFileResponse>> deleteResponse = this.openAiApi.assistantFiles(assistantId, pageRequest.getLimit(), pageRequest.getOrder(), pageRequest.getBefore(), pageRequest.getAfter());
11151130
return deleteResponse.blockingGet();
11161131
}
@@ -1160,6 +1175,223 @@ public DeleteResponse deleteThread(String threadId) {
11601175
return this.openAiApi.deleteThread(threadId).blockingGet();
11611176
}
11621177

1178+
/**
1179+
* 为线程创建消息
1180+
*
1181+
* @param threadId 线程id
1182+
* @param message message参数
1183+
* @return
1184+
* @since 1.1.3
1185+
*/
1186+
public MessageResponse message(String threadId, ThreadMessage message) {
1187+
return this.openAiApi.message(threadId, message).blockingGet();
1188+
}
1189+
1190+
/**
1191+
* 检索某一个线程对应的消息详细信息
1192+
*
1193+
* @param threadId 线程id
1194+
* @param messageId 消息id
1195+
* @return
1196+
* @since 1.1.3
1197+
*/
1198+
public MessageResponse retrieveMessage(String threadId, String messageId) {
1199+
return this.openAiApi.retrieveMessage(threadId, messageId).blockingGet();
1200+
}
1201+
1202+
/**
1203+
* 修改某一个线程对应的消息
1204+
*
1205+
* @param threadId 线程id
1206+
* @param messageId 消息id
1207+
* @param message 消息体
1208+
* @return
1209+
* @since 1.1.3
1210+
*/
1211+
public MessageResponse modifyMessage(String threadId, String messageId, ModifyMessage message) {
1212+
return this.openAiApi.modifyMessage(threadId, messageId, message).blockingGet();
1213+
}
1214+
1215+
/**
1216+
* 获取某一个线程的消息集合
1217+
*
1218+
* @param threadId 线程id
1219+
* @param pageRequest 分页信息
1220+
* @return
1221+
* @since 1.1.3
1222+
*/
1223+
public AssistantListResponse<MessageResponse> messages(String threadId, PageRequest pageRequest) {
1224+
pageRequest = Optional.ofNullable(pageRequest).orElse(this.getPageRequest());
1225+
return this.openAiApi.messages(threadId,
1226+
pageRequest.getLimit(),
1227+
pageRequest.getOrder(),
1228+
pageRequest.getBefore(),
1229+
pageRequest.getAfter()).blockingGet();
1230+
}
1231+
1232+
/**
1233+
* 检索某一个线程对应某一个消息的一个文件信息
1234+
*
1235+
* @param threadId 线程id
1236+
* @param messageId 消息id
1237+
* @param fileId 文件id
1238+
* @return
1239+
* @since 1.1.3
1240+
*/
1241+
public MessageFileResponse retrieveMessageFile(String threadId, String messageId, String fileId) {
1242+
return this.openAiApi.retrieveMessageFile(threadId, messageId, fileId).blockingGet();
1243+
}
1244+
1245+
/**
1246+
* messageFiles集合
1247+
*
1248+
* @param threadId 线程id
1249+
* @param messageId 消息id
1250+
* @param pageRequest 分页信息
1251+
* @return
1252+
* @since 1.1.3
1253+
*/
1254+
public AssistantListResponse<MessageFileResponse> messageFiles(String threadId, String messageId, PageRequest pageRequest) {
1255+
pageRequest = Optional.ofNullable(pageRequest).orElse(this.getPageRequest());
1256+
return this.openAiApi.messageFiles(threadId, messageId,
1257+
pageRequest.getLimit(),
1258+
pageRequest.getOrder(),
1259+
pageRequest.getBefore(),
1260+
pageRequest.getAfter()).blockingGet();
1261+
}
1262+
1263+
1264+
/**
1265+
* 创建Run
1266+
*
1267+
* @param threadId 线程id
1268+
* @param run run
1269+
* @return
1270+
* @since 1.1.3
1271+
*/
1272+
public RunResponse run(String threadId, Run run) {
1273+
return this.openAiApi.run(threadId, run).blockingGet();
1274+
}
1275+
1276+
1277+
/**
1278+
* 检索run详细信息
1279+
*
1280+
* @param threadId 线程id
1281+
* @param runId run_id
1282+
* @return
1283+
* @since 1.1.3
1284+
*/
1285+
public RunResponse retrieveRun(String threadId, String runId) {
1286+
return this.openAiApi.retrieveRun(threadId, runId).blockingGet();
1287+
}
1288+
1289+
/**
1290+
* 修改某一个run
1291+
*
1292+
* @param threadId 线程id
1293+
* @param runId run_id
1294+
* @param run 消息体
1295+
* @return
1296+
* @since 1.1.3
1297+
*/
1298+
public RunResponse modifyRun(String threadId, String runId, ModifyRun run) {
1299+
return this.openAiApi.modifyRun(threadId, runId, run).blockingGet();
1300+
}
1301+
1302+
1303+
/**
1304+
* 获取某一个线程的run集合
1305+
*
1306+
* @param threadId 线程id
1307+
* @param pageRequest 分页信息
1308+
* @return
1309+
* @since 1.1.3
1310+
*/
1311+
public AssistantListResponse<RunResponse> runs(String threadId, PageRequest pageRequest) {
1312+
pageRequest = Optional.ofNullable(pageRequest).orElse(this.getPageRequest());
1313+
return this.openAiApi.runs(threadId,
1314+
pageRequest.getLimit(),
1315+
pageRequest.getOrder(),
1316+
pageRequest.getBefore(),
1317+
pageRequest.getAfter()).blockingGet();
1318+
}
1319+
1320+
1321+
/**
1322+
* 获取某一个线程的run集合
1323+
*
1324+
* @param threadId 线程id
1325+
* @param runId run id
1326+
* @param toolOutputs 为其提交输出的工具列表。
1327+
* @return
1328+
* @since 1.1.3
1329+
*/
1330+
public RunResponse submitToolOutputs(String threadId, String runId, ToolOutputBody toolOutputs) {
1331+
return this.openAiApi.submitToolOutputs(threadId, runId, toolOutputs).blockingGet();
1332+
}
1333+
1334+
1335+
/**
1336+
* 取消正在进行中的run
1337+
*
1338+
* @param threadId 线程id
1339+
* @param runId run id
1340+
* @return
1341+
* @since 1.1.3
1342+
*/
1343+
public RunResponse cancelRun(String threadId, String runId) {
1344+
return this.openAiApi.cancelRun(threadId, runId).blockingGet();
1345+
}
1346+
1347+
1348+
/**
1349+
* 创建一个线程并在一个请求中运行它
1350+
*
1351+
* @param threadRun 对象
1352+
* @return
1353+
* @since 1.1.3
1354+
*/
1355+
public RunResponse threadRun(ThreadRun threadRun) {
1356+
return this.openAiApi.threadRun(threadRun).blockingGet();
1357+
}
1358+
1359+
/**
1360+
* 检索run step详细信息
1361+
*
1362+
* @param threadId 线程id
1363+
* @param runId run_id
1364+
* @param stepId step_id
1365+
* @return
1366+
* @since 1.1.3
1367+
*/
1368+
public RunStepResponse retrieveRunStep(String threadId, String runId, String stepId) {
1369+
if (StrUtil.isBlank(stepId)) {
1370+
log.error("step id不能为空");
1371+
throw new BaseException(CommonError.PARAM_ERROR);
1372+
}
1373+
return this.openAiApi.retrieveRunStep(threadId, runId, stepId).blockingGet();
1374+
}
1375+
1376+
1377+
/**
1378+
* 获取某一个线程的run step集合
1379+
*
1380+
* @param threadId 线程id
1381+
* @param runId run_id
1382+
* @param pageRequest 分页信息
1383+
* @return
1384+
* @since 1.1.3
1385+
*/
1386+
public AssistantListResponse<RunStepResponse> runSteps(String threadId, String runId, PageRequest pageRequest) {
1387+
pageRequest = Optional.ofNullable(pageRequest).orElse(this.getPageRequest());
1388+
return this.openAiApi.runSteps(threadId, runId,
1389+
pageRequest.getLimit(),
1390+
pageRequest.getOrder(),
1391+
pageRequest.getBefore(),
1392+
pageRequest.getAfter()).blockingGet();
1393+
}
1394+
11631395

11641396
public static final class Builder {
11651397
/**

src/main/java/com/unfbx/chatgpt/entity/assistant/run/RunStepResponse.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5-
import lombok.AllArgsConstructor;
6-
import lombok.Builder;
7-
import lombok.Getter;
8-
import lombok.NoArgsConstructor;
5+
import lombok.*;
96
import lombok.extern.slf4j.Slf4j;
107

118
import java.io.Serializable;
@@ -18,7 +15,7 @@
1815
* @since 1.1.3
1916
* 2023-11-20
2017
*/
21-
@Getter
18+
@Data
2219
@Slf4j
2320
@Builder
2421
@JsonInclude(JsonInclude.Include.NON_NULL)

0 commit comments

Comments
 (0)