Skip to content

Commit ebfdf74

Browse files
author
guorutao
committed
Merge branch '1.1.3' into develop
2 parents fe97780 + 9192f23 commit ebfdf74

File tree

3 files changed

+84
-13
lines changed

3 files changed

+84
-13
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@
6767
import retrofit2.Retrofit;
6868
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
6969
import retrofit2.converter.jackson.JacksonConverterFactory;
70-
import retrofit2.http.*;
71-
import retrofit2.http.Headers;
7270

71+
import java.io.IOException;
7372
import java.time.LocalDate;
7473
import java.util.*;
7574
import java.util.concurrent.TimeUnit;
@@ -1008,7 +1007,7 @@ public FineTuneJobListResponse<FineTuneJobEvent> fineTuneJobEvents(String fineTu
10081007
}
10091008

10101009
/**
1011-
* 文本转语音
1010+
* 文本转语音(异步)
10121011
*
10131012
* @param textToSpeech 参数
10141013
* @param callback 返回值接收
@@ -1019,6 +1018,17 @@ public void textToSpeech(TextToSpeech textToSpeech, Callback callback) {
10191018
responseBody.enqueue(callback);
10201019
}
10211020

1021+
/**
1022+
* 文本转语音(同步)
1023+
*
1024+
* @param textToSpeech 参数
1025+
* @since 1.1.3
1026+
*/
1027+
public ResponseBody textToSpeech(TextToSpeech textToSpeech) throws IOException {
1028+
Call<ResponseBody> responseBody = this.openAiApi.textToSpeech(textToSpeech);
1029+
return responseBody.execute().body();
1030+
}
1031+
10221032
/**
10231033
* 创建助手
10241034
*

src/main/java/com/unfbx/chatgpt/entity/billing/Subscription.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.unfbx.chatgpt.entity.billing;
22

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

@@ -10,7 +11,7 @@
1011
* @since 2023-04-08
1112
*/
1213
@Data
13-
//@JsonIgnoreProperties(ignoreUnknown = true)
14+
@JsonIgnoreProperties(ignoreUnknown = true)
1415
public class Subscription {
1516
@JsonProperty("object")
1617
private String object;

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

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
import com.unfbx.chatgpt.FirstKeyStrategy;
55
import com.unfbx.chatgpt.OpenAiClient;
6+
import com.unfbx.chatgpt.entity.Tts.TextToSpeech;
7+
import com.unfbx.chatgpt.entity.Tts.TtsFormat;
8+
import com.unfbx.chatgpt.entity.Tts.TtsVoice;
69
import com.unfbx.chatgpt.entity.assistant.*;
710
import com.unfbx.chatgpt.entity.assistant.message.MessageFileResponse;
811
import com.unfbx.chatgpt.entity.assistant.message.MessageResponse;
@@ -19,12 +22,15 @@
1922
import com.unfbx.chatgpt.entity.assistant.thread.ThreadResponse;
2023
import com.unfbx.chatgpt.interceptor.OpenAILogger;
2124
import com.unfbx.chatgpt.interceptor.OpenAiResponseInterceptor;
25+
import lombok.SneakyThrows;
2226
import lombok.extern.slf4j.Slf4j;
2327
import okhttp3.OkHttpClient;
28+
import okhttp3.ResponseBody;
2429
import okhttp3.logging.HttpLoggingInterceptor;
2530
import org.junit.Before;
2631
import org.junit.Test;
2732

33+
import java.io.*;
2834
import java.net.InetSocketAddress;
2935
import java.net.Proxy;
3036
import java.util.*;
@@ -66,7 +72,7 @@ public void before() {
6672
.keyStrategy(new FirstKeyStrategy())
6773
.okHttpClient(okHttpClient)
6874
//自己做了代理就传代理地址,没有可不不传,(关注公众号回复:openai ,获取免费的测试代理地址)
69-
.apiHost("https://dgr.life/")
75+
.apiHost("https://******************/")
7076
.build();
7177
}
7278

@@ -78,8 +84,8 @@ public void assistant() {
7884
Tool tool = Tool.builder().type(Tool.Type.CODE_INTERPRETER.getName()).build();
7985
Assistant assistant = Assistant.builder()
8086
.model(BaseChatCompletion.Model.GPT_3_5_TURBO_16K_0613.getName())
81-
.name("UnfbxBotV3")
82-
.description("UnfbxBotV3是一个自定义数学bot。")
87+
.name("UnfbxBot")
88+
.description("UnfbxBot是一个自定义数学bot。")
8389
.instructions("你是一个数学导师。当我问你问题时,编写并运行Java代码来回答问题。")
8490
.tools(Collections.singletonList(tool))
8591
// .fileIds()
@@ -458,22 +464,24 @@ public void cancelRun() {
458464

459465
/**
460466
* 开始一个thread run
467+
* asst_bE2sCiib7eL300O9MWJanUd4
461468
*/
462469
@Test
463470
public void threadRun() {
464-
ThreadRun threadRun = ThreadRun.builder().assistantId("asst_V4Grgp2BQWAmH03Bs56E1H7c").build();
471+
ThreadRun threadRun = ThreadRun.builder().assistantId("asst_bE2sCiib7eL300O9MWJanUd4").build();
465472
RunResponse response = client.threadRun(threadRun);
466473
System.out.println(response.toString());
474+
//RunResponse(id=run_NOznCqW1NQatPLbJfKvOkemE, object=thread.run, createdAt=1700560119, threadId=thread_lVyAEZuzqxeSDHJWBVLbjYeE, assistantId=asst_bE2sCiib7eL300O9MWJanUd4, status=queued, requiredAction=null, lastError=null, expiresAt=1700560719, startedAt=null, cancelledAt=null, failedAt=null, completedAt=null, model=gpt-3.5-turbo-16k-0613, instructions=你是一个数学导师。当我问你问题时,编写并运行Java代码来回答问题。, tools=[Tool(type=code_interpreter, function=null)], fileIds=[], metadata={})
467475
//RunResponse(id=run_dmR2AtFe9nyGal6uSSxG6GI5, object=thread.run, createdAt=1700492750, threadId=thread_E1mqqdApmpNOCBfoLnqH7KMp, assistantId=asst_V4Grgp2BQWAmH03Bs56E1H7c, status=queued, requiredAction=null, lastError=null, expiresAt=1700493350, startedAt=null, cancelledAt=null, failedAt=null, completedAt=null, model=gpt-3.5-turbo-16k-0613, instructions=你是一个数学导师。当我问你问题时,编写并运行Java代码来回答问题。, tools=[Tool(type=code_interpreter, function=null)], fileIds=[file-tJoDYPF2MMlIOlvwfGbIV94D], metadata={})
468476
}
469477

470478
@Test
471479
public void retrieveRunStep() {
472-
RunStepResponse response = client.retrieveRunStep("thread_E1mqqdApmpNOCBfoLnqH7KMp",
473-
"run_dmR2AtFe9nyGal6uSSxG6GI5",
474-
"");
480+
RunStepResponse response = client.retrieveRunStep("thread_lVyAEZuzqxeSDHJWBVLbjYeE",
481+
"run_NOznCqW1NQatPLbJfKvOkemE",
482+
"step_3YUJoS3ioYX5Ziz4AooGLKqd");
475483
System.out.println(response.toString());
476-
//RunResponse(id=run_dmR2AtFe9nyGal6uSSxG6GI5, object=thread.run, createdAt=1700492750, threadId=thread_E1mqqdApmpNOCBfoLnqH7KMp, assistantId=asst_V4Grgp2BQWAmH03Bs56E1H7c, status=queued, requiredAction=null, lastError=null, expiresAt=1700493350, startedAt=null, cancelledAt=null, failedAt=null, completedAt=null, model=gpt-3.5-turbo-16k-0613, instructions=你是一个数学导师。当我问你问题时,编写并运行Java代码来回答问题。, tools=[Tool(type=code_interpreter, function=null)], fileIds=[file-tJoDYPF2MMlIOlvwfGbIV94D], metadata={})
484+
//RunStepResponse(id=step_3YUJoS3ioYX5Ziz4AooGLKqd, object=thread.run.step, createdAt=1700560129, assistantId=asst_bE2sCiib7eL300O9MWJanUd4, threadId=thread_lVyAEZuzqxeSDHJWBVLbjYeE, runId=run_NOznCqW1NQatPLbJfKvOkemE, type=message_creation, status=completed, stepDetails=StepDetail(type=message_creation, messageCreation=com.unfbx.chatgpt.entity.assistant.run.MessageCreation@bcec031, toolCalls=null), lastError=null, expiresAt=null, cancelledAt=null, failedAt=null, completedAt=1700560136, metadata=null)
477485
}
478486

479487
/**
@@ -482,9 +490,61 @@ public void retrieveRunStep() {
482490
@Test
483491
public void runSteps() {
484492
// PageRequest pageRequest = PageRequest.builder().build();
485-
AssistantListResponse<RunStepResponse> response = client.runSteps("thread_E1mqqdApmpNOCBfoLnqH7KMp", "run_dmR2AtFe9nyGal6uSSxG6GI5", null);
493+
AssistantListResponse<RunStepResponse> response = client.runSteps("thread_lVyAEZuzqxeSDHJWBVLbjYeE", "run_NOznCqW1NQatPLbJfKvOkemE", null);
486494
response.getData().forEach(e -> System.out.println(e.toString()));
487495
}
488496

489497

498+
/**
499+
* tts使用示例
500+
*/
501+
@SneakyThrows
502+
@Test
503+
public void textToSpeed() {
504+
TextToSpeech textToSpeech = TextToSpeech.builder()
505+
.model(TextToSpeech.Model.TTS_1_HD.getName())
506+
.input("OpenAI官方Api的Java SDK,可以快速接入项目使用。目前支持OpenAI官方全部接口,同时支持Tokens计算。官方github地址:https://github.com/Grt1228/chatgpt-java。欢迎star。")
507+
.voice(TtsVoice.NOVA.getName())
508+
.responseFormat(TtsFormat.MP3.getName())
509+
.build();
510+
java.io.File file = new java.io.File("C:\\Users\\grt\\Desktop\\test.mp3");
511+
ResponseBody responseBody = client.textToSpeech(textToSpeech);
512+
InputStream inputStream = responseBody.byteStream();
513+
//创建文件
514+
if (!file.exists()) {
515+
if (!file.getParentFile().exists())
516+
file.getParentFile().mkdir();
517+
try {
518+
file.createNewFile();
519+
} catch (IOException e) {
520+
e.printStackTrace();
521+
log.error("createNewFile IOException");
522+
}
523+
}
524+
525+
OutputStream os = null;
526+
try {
527+
os = new BufferedOutputStream(new FileOutputStream(file));
528+
byte data[] = new byte[8192];
529+
int len;
530+
while ((len = inputStream.read(data, 0, 8192)) != -1) {
531+
os.write(data, 0, len);
532+
}
533+
} catch (IOException e) {
534+
e.printStackTrace();
535+
} finally {
536+
try {
537+
inputStream.close();
538+
} catch (IOException e) {
539+
e.printStackTrace();
540+
}
541+
try {
542+
if (os != null) {
543+
os.close();
544+
}
545+
} catch (IOException e) {
546+
e.printStackTrace();
547+
}
548+
}
549+
}
490550
}

0 commit comments

Comments
 (0)