Skip to content

Commit 640ad2d

Browse files
author
guorutao
committed
feature 1.1.2 新的api开发:tts
1 parent 25e2dbf commit 640ad2d

14 files changed

+469
-100
lines changed

fine_tune_test_file.json1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "chagpt-java好用么?"}, {"role": "assistant", "content": "还行吧。"}]}
2+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "chagpt-java最新版本是什么?"}, {"role": "assistant", "content": "1.1.2版本"}]}
3+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "chagpt-java支持流式输出么?"}, {"role": "assistant", "content": "最新版支持流式输出"}]}
4+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "chagpt-java的作者是谁?"}, {"role": "assistant", "content": "作者是Unfbx,个人网站:https://www.unfbx.com"}]}
5+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,微信公众号是什么?"}, {"role": "assistant", "content": "程序员的黑洞。"}]}
6+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,个人博客地址是什么?"}, {"role": "assistant", "content": "https://www.unfbx.com"}]}
7+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,chagpt-java官方地址是什么?"}, {"role": "assistant", "content": "https://chatgpt-java.unfbx.com"}]}
8+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,chagpt-java的github地址是什么?"}, {"role": "assistant", "content": "https://github.com/Grt1228/chatgpt-java"}]}
9+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "Unfbx是chagpt-java的作者,chagpt-java的简单介绍。"}, {"role": "assistant", "content": "OpenAI官方Api的Java SDK,可以快速接入项目使用。目前支持OpenAI官方全部接口,**同时支持Tokens计算。参考文档:[Tokens_README.md](https://github.com/Grt1228/chatgpt-java/blob/main/Tokens_README.md)**"}]}
10+
{"messages": [{"role": "system", "content": "OnBot是一个聊天机器人。"}, {"role": "user", "content": "OpenAi的官方Java Sdk 是什么?"}, {"role": "assistant", "content": "chagpt-java,github地址:https://github.com/Grt1228/chatgpt-java"}]}

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.unfbx.chatgpt.entity.billing.BillingUsage;
55
import com.unfbx.chatgpt.entity.billing.CreditGrantsResponse;
66
import com.unfbx.chatgpt.entity.billing.Subscription;
7-
import com.unfbx.chatgpt.entity.chat.BaseChatCompletion;
87
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
98
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
109
import com.unfbx.chatgpt.entity.chat.ChatCompletionWithPicture;
@@ -217,7 +216,7 @@ Single<UploadFileResponse> uploadFile(@Part MultipartBody.Part file,
217216
* 微调作业集合
218217
*
219218
* @return Single OpenAiResponse FineTuneResponse
220-
* @see #fineTuneJobs()
219+
* @see #fineTuneJobs(String, Integer)
221220
*/
222221
@Deprecated
223222
@GET("v1/fine-tunes")
@@ -248,7 +247,7 @@ Single<UploadFileResponse> uploadFile(@Part MultipartBody.Part file,
248247
* 微调作业事件列表
249248
*
250249
* @return Single OpenAiResponse Event
251-
* @see #fineTuneJobEvents(String fineTuneJobId)
250+
* @see #fineTuneJobEvents(String, String, Integer)
252251
*/
253252
@Deprecated
254253
@GET("v1/fine-tunes/{fine_tune_id}/events")
@@ -362,8 +361,10 @@ Single<WhisperResponse> speechToTextTranslations(@Part MultipartBody.Part file,
362361
*
363362
* @param textToSpeech
364363
* @return
364+
* @since 1.1.2
365365
*/
366366
@POST("v1/audio/speech")
367+
@Streaming
367368
Call<ResponseBody> textToSpeech(@Body TextToSpeech textToSpeech);
368369

369370

@@ -380,11 +381,13 @@ Single<WhisperResponse> speechToTextTranslations(@Part MultipartBody.Part file,
380381
/**
381382
* 微调job集合
382383
*
384+
* @param after 上一个分页请求中最后一个job id
385+
* @param limit 每次查询数量
383386
* @return FineTuneJobListResponse #FineTuneResponse
384387
* @since 1.1.2
385388
*/
386389
@GET("v1/fine_tuning/jobs")
387-
Single<FineTuneJobListResponse<FineTuneJobResponse>> fineTuneJobs();
390+
Single<FineTuneJobListResponse<FineTuneJobResponse>> fineTuneJobs(@Query("after") String after, @Query("limit") Integer limit);
388391

389392

390393
/**
@@ -400,7 +403,7 @@ Single<WhisperResponse> speechToTextTranslations(@Part MultipartBody.Part file,
400403
/**
401404
* 取消微调job
402405
*
403-
* @param fineTuneJobId
406+
* @param fineTuneJobId JobId
404407
* @return FineTuneJobResponse
405408
* @since 1.1.2
406409
*/
@@ -410,11 +413,13 @@ Single<WhisperResponse> speechToTextTranslations(@Part MultipartBody.Part file,
410413
/**
411414
* 微调job事件列表
412415
*
413-
* @param fineTuneJobId
416+
* @param fineTuneJobId JobId
417+
* @param after 上一个分页请求中最后一个id,默认值:null
418+
* @param limit 每次查询数量 默认值:20
414419
* @return FineTuneJobListResponse #FineTuneResponse
415420
* @since 1.1.2
416421
*/
417422
@GET("v1/fine_tuning/jobs/{fine_tuning_job_id}/events")
418-
Single<FineTuneJobListResponse<FineTuneJobEvent>> fineTuneJobEvents(@Path("fine_tuning_job_id") String fineTuneJobId);
423+
Single<FineTuneJobListResponse<FineTuneJobEvent>> fineTuneJobEvents(@Path("fine_tuning_job_id") String fineTuneJobId, @Query("after") String after, @Query("limit") Integer limit);
419424

420425
}

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import retrofit2.converter.jackson.JacksonConverterFactory;
6161

6262
import java.io.IOException;
63+
import java.io.InputStream;
6364
import java.time.LocalDate;
6465
import java.util.*;
6566
import java.util.concurrent.TimeUnit;
@@ -586,7 +587,7 @@ public FineTuneResponse fineTune(String trainingFileId) {
586587
* 微调模型列表
587588
*
588589
* @return FineTuneResponse list
589-
* @see #fineTuneJobs()
590+
* @see #fineTuneJobs(String, Integer)
590591
*/
591592
@Deprecated
592593
public List<FineTuneResponse> fineTunes() {
@@ -625,7 +626,7 @@ public FineTuneResponse cancelFineTune(String fineTuneId) {
625626
*
626627
* @param fineTuneId 微调作业id
627628
* @return Event List
628-
* @see #fineTuneJobEvents(String fineTuneJobId)
629+
* @see #fineTuneJobEvents(String, String, Integer)
629630
*/
630631
@Deprecated
631632
public List<Event> fineTuneEvents(String fineTuneId) {
@@ -933,18 +934,23 @@ public FineTuneJobResponse fineTuneJob(FineTuneJob fineTuneJob) {
933934
* @since 1.1.2
934935
*/
935936
public FineTuneJobResponse fineTuneJob(String trainingFileId) {
936-
FineTuneJob fineTuneJob = FineTuneJob.builder().trainingFile(trainingFileId).build();
937+
FineTuneJob fineTuneJob = FineTuneJob.builder()
938+
.model(FineTuneJob.Model.GPT_3_5_TURBO_1106.getName())
939+
.trainingFile(trainingFileId)
940+
.build();
937941
return this.fineTuneJob(fineTuneJob);
938942
}
939943

940944
/**
941-
* 微调job列表
945+
* 微调job集合
942946
*
943-
* @return FineTuneJobListResponse #FineTuneJobResponse
947+
* @param after 上一个分页请求中最后一个job id,默认值:null
948+
* @param limit 每次查询数量 默认值:20
949+
* @return FineTuneJobListResponse #FineTuneResponse
944950
* @since 1.1.2
945951
*/
946-
public FineTuneJobListResponse<FineTuneJobResponse> fineTuneJobs() {
947-
Single<FineTuneJobListResponse<FineTuneJobResponse>> fineTuneJobs = this.openAiApi.fineTuneJobs();
952+
public FineTuneJobListResponse<FineTuneJobResponse> fineTuneJobs(String after, Integer limit) {
953+
Single<FineTuneJobListResponse<FineTuneJobResponse>> fineTuneJobs = this.openAiApi.fineTuneJobs(after, limit);
948954
return fineTuneJobs.blockingGet();
949955
}
950956

@@ -976,29 +982,20 @@ public FineTuneJobResponse cancelFineTuneJob(String fineTuneJobId) {
976982
* 微调作业事件列表
977983
*
978984
* @param fineTuneJobId 微调job id
985+
* @param after 上一个分页请求中最后一个id,默认值:null
986+
* @param limit 每次查询数量 默认值:20
979987
* @return Event List
980988
* @since 1.1.2
981989
*/
982-
public FineTuneJobListResponse<FineTuneJobEvent> fineTuneJobEvents(String fineTuneJobId) {
983-
Single<FineTuneJobListResponse<FineTuneJobEvent>> events = this.openAiApi.fineTuneJobEvents(fineTuneJobId);
990+
public FineTuneJobListResponse<FineTuneJobEvent> fineTuneJobEvents(String fineTuneJobId, String after, Integer limit) {
991+
Single<FineTuneJobListResponse<FineTuneJobEvent>> events = this.openAiApi.fineTuneJobEvents(fineTuneJobId, after, limit);
984992
return events.blockingGet();
985993
}
986994

987995

988-
public void textToSpeech(TextToSpeech textToSpeech) {
996+
public void textToSpeech(TextToSpeech textToSpeech, Callback callback) {
989997
Call<ResponseBody> responseBody = this.openAiApi.textToSpeech(textToSpeech);
990-
responseBody.enqueue(new Callback<ResponseBody>() {
991-
@SneakyThrows
992-
@Override
993-
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
994-
System.out.println("--------------------->" + response.body());
995-
}
996-
997-
@Override
998-
public void onFailure(Call<ResponseBody> call, Throwable t) {
999-
1000-
}
1001-
});
998+
responseBody.enqueue(callback);
1002999
}
10031000

10041001
public static final class Builder {

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,6 @@ private OkHttpClient okHttpClient() {
156156
.build();
157157
}
158158

159-
160-
public void textToSpeed(TextToSpeech textToSpeech, EventSourceListener eventSourceListener) {
161-
try {
162-
EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);
163-
ObjectMapper mapper = new ObjectMapper();
164-
String requestBody = mapper.writeValueAsString(textToSpeech);
165-
Request request = new Request.Builder()
166-
.url(this.apiHost + "v1/audio/speech")
167-
.post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody))
168-
.build();
169-
//创建事件
170-
EventSource eventSource = factory.newEventSource(request, eventSourceListener);
171-
} catch (JsonProcessingException e) {
172-
log.error("请求参数解析异常:{}", e);
173-
e.printStackTrace();
174-
} catch (Exception e) {
175-
log.error("请求参数解析异常:{}", e);
176-
e.printStackTrace();
177-
}
178-
}
179-
180159
/**
181160
* 问答接口 stream 形式
182161
*

src/main/java/com/unfbx/chatgpt/entity/Tts/TtsVoice.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
@AllArgsConstructor
1212
public enum TtsVoice {
1313

14-
ALLOY("mp3"),
15-
ECHO("opus"),
16-
FABLE("aac"),
17-
ONYX("flac"),
18-
NOVA("flac"),
19-
SHIMMER("flac"),
14+
ALLOY("alloy"),
15+
ECHO("echo"),
16+
FABLE("fable"),
17+
ONYX("onyx"),
18+
NOVA("nova"),
19+
SHIMMER("shimmer"),
2020
;
2121

2222
private final String name;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.unfbx.chatgpt.entity.fineTune.job;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import lombok.Data;
5+
6+
/**
7+
* 描述:
8+
*
9+
* @author https://www.unfbx.com
10+
* @since 1.1.2
11+
* 2023-11-12
12+
*/
13+
@Data
14+
@JsonIgnoreProperties(ignoreUnknown = true)
15+
public class FineTuneError {
16+
private String message;
17+
private String param;
18+
private String code;
19+
}

src/main/java/com/unfbx/chatgpt/entity/fineTune/job/FineTuneJobResponse.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5-
import com.unfbx.chatgpt.entity.common.OpenAiResponse;
65
import lombok.*;
76

87
import java.io.Serializable;
98
import java.util.List;
109

10+
/**
11+
* 描述:
12+
*
13+
* @author https://www.unfbx.com
14+
* @since 1.1.2
15+
* 2023-11-12
16+
*/
1117
@Data
1218
@JsonIgnoreProperties(ignoreUnknown = true)
1319
public class FineTuneJobResponse implements Serializable {
@@ -18,7 +24,7 @@ public class FineTuneJobResponse implements Serializable {
1824
private Long createdAt;
1925

2026
@JsonProperty("error")
21-
private OpenAiResponse.Error error;
27+
private FineTuneError error;
2228

2329
@JsonProperty("fine_tuned_model")
2430
private String fineTunedModel;
@@ -42,7 +48,7 @@ public class FineTuneJobResponse implements Serializable {
4248
private List<String> resultFiles;
4349

4450
@JsonProperty("status")
45-
private Long status;
51+
private String status;
4652

4753
@JsonProperty("trained_tokens")
4854
private Integer trainedTokens;
@@ -52,4 +58,7 @@ public class FineTuneJobResponse implements Serializable {
5258

5359
@JsonProperty("validation_file")
5460
private String validationFile;
61+
62+
@JsonProperty("data")
63+
private String data;
5564
}

src/main/java/com/unfbx/chatgpt/entity/images/Image.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public class Image implements Serializable {
3030
*
3131
* @see Model
3232
*/
33-
private String model;
33+
@Builder.Default
34+
private String model = Model.DALL_E_3.getName();
3435

3536
/**
3637
* 此参数仅仅dall-e-3,默认值:standard

src/test/java/com/unfbx/chatgpt/OpenAiClientFunctionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void before() {
5858
.authInterceptor(new DynamicKeyOpenAiAuthInterceptor())
5959
.okHttpClient(okHttpClient)
6060
//自己做了代理就传代理地址,没有可不不传,(关注公众号回复:openai ,获取免费的测试代理地址)
61-
.apiHost("https://dgr.life/")
61+
.apiHost("https://*************/")
6262
.build();
6363

6464
openAiStreamClient = OpenAiStreamClient.builder()
@@ -69,7 +69,7 @@ public void before() {
6969
.authInterceptor(new DynamicKeyOpenAiAuthInterceptor())
7070
.okHttpClient(okHttpClient)
7171
//自己做了代理就传代理地址,没有可不不传,(关注公众号回复:openai ,获取免费的测试代理地址)
72-
.apiHost("https://dgr.life/")
72+
.apiHost("https://*************/")
7373
.build();
7474
}
7575

src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,16 @@ public void before() {
8080
.build();
8181
v2 = OpenAiClient.builder()
8282
//支持多key传入,请求时候随机选择
83-
.apiKey(Arrays.asList("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"))
83+
.apiKey(Arrays.asList("************************"))
8484
//自定义key的获取策略:默认KeyRandomStrategy
8585
//.keyStrategy(new KeyRandomStrategy())
8686
.keyStrategy(new FirstKeyStrategy())
8787
.okHttpClient(okHttpClient)
8888
//自己做了代理就传代理地址,没有可不不传,(关注公众号回复:openai ,获取免费的测试代理地址)
89-
.apiHost("https://dgr.life/")
89+
.apiHost("https://*********/")
9090
.build();
9191
}
9292

93-
@Test
94-
public void textToSpeed() throws IOException {
95-
TextToSpeech textToSpeech = TextToSpeech.builder()
96-
.model("tts-1")
97-
.input("In the vast, white expanse of the winter landscape, a drama unfolds that is as timeless as it is raw. Here, in the cradle of nature's harshest trials, a pack of grey wolves has singled out a bison from the herd—a desperate struggle for life and sustenance is about to begin.\n" +
98-
"\n" +
99-
"In a carefully orchestrated assault, the pack encircles their quarry, each wolf keenly aware of its role. Muscles tense and breaths visible in the frigid air, they inch closer, probing for a weakness. The bison, a formidable giant, stands its ground, backed by the survival instincts honed over millennia. Its hulking form casts a solitary shadow against the snow's blinding canvas.\n" +
100-
"\n" +
101-
"The dance of predator and prey plays out as a symphony of survival—each movement, each feint, holds the weight of life itself. The wolves take turns attacking, conserving strength while wearing down their target. The herd, once the bison's allies, scatter into the distance, a stark reminder that in these wild territories, the law of survival supersedes the bonds of kinship.\n" +
102-
"\n" +
103-
"A burst of activity—the wolves close in. The bison, though mighty, is tiring, its breaths labored, its movements sluggish. The wolves sense the turning tide. With relentless determination, they press their advantage, a testament to the brutal beauty of the natural order.\n" +
104-
"\n" +
105-
"As the struggle reaches its inevitable conclusion, we are reminded of the delicate balance that governs these wild spaces. Life, death, struggle, and survival—the cycle continues, each chapter written in the snow, for as long as the wolf roams and the bison roves these frozen plains.")
106-
.voice("alloy")
107-
.responseFormat("mp3")
108-
.build();
109-
v2.textToSpeech(textToSpeech);
110-
CountDownLatch countDownLatch = new CountDownLatch(1);
111-
try {
112-
countDownLatch.await();
113-
} catch (InterruptedException e) {
114-
e.printStackTrace();
115-
}
116-
}
11793
@Test
11894
public void subscription() {
11995
Subscription subscription = v2.subscription();

0 commit comments

Comments
 (0)