Skip to content

Commit

Permalink
Merge branch '1.1.3' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
guorutao committed Nov 21, 2023
2 parents fe97780 + 9192f23 commit ebfdf74
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
16 changes: 13 additions & 3 deletions src/main/java/com/unfbx/chatgpt/OpenAiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.http.*;
import retrofit2.http.Headers;

import java.io.IOException;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -1008,7 +1007,7 @@ public FineTuneJobListResponse<FineTuneJobEvent> fineTuneJobEvents(String fineTu
}

/**
* 文本转语音
* 文本转语音(异步)
*
* @param textToSpeech 参数
* @param callback 返回值接收
Expand All @@ -1019,6 +1018,17 @@ public void textToSpeech(TextToSpeech textToSpeech, Callback callback) {
responseBody.enqueue(callback);
}

/**
* 文本转语音(同步)
*
* @param textToSpeech 参数
* @since 1.1.3
*/
public ResponseBody textToSpeech(TextToSpeech textToSpeech) throws IOException {
Call<ResponseBody> responseBody = this.openAiApi.textToSpeech(textToSpeech);
return responseBody.execute().body();
}

/**
* 创建助手
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unfbx.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

Expand All @@ -10,7 +11,7 @@
* @since 2023-04-08
*/
@Data
//@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Subscription {
@JsonProperty("object")
private String object;
Expand Down
78 changes: 69 additions & 9 deletions src/test/java/com/unfbx/chatgpt/v1_1_3/OpenAiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import com.unfbx.chatgpt.FirstKeyStrategy;
import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.entity.Tts.TextToSpeech;
import com.unfbx.chatgpt.entity.Tts.TtsFormat;
import com.unfbx.chatgpt.entity.Tts.TtsVoice;
import com.unfbx.chatgpt.entity.assistant.*;
import com.unfbx.chatgpt.entity.assistant.message.MessageFileResponse;
import com.unfbx.chatgpt.entity.assistant.message.MessageResponse;
Expand All @@ -19,12 +22,15 @@
import com.unfbx.chatgpt.entity.assistant.thread.ThreadResponse;
import com.unfbx.chatgpt.interceptor.OpenAILogger;
import com.unfbx.chatgpt.interceptor.OpenAiResponseInterceptor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import org.junit.Before;
import org.junit.Test;

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

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

/**
* 开始一个thread run
* asst_bE2sCiib7eL300O9MWJanUd4
*/
@Test
public void threadRun() {
ThreadRun threadRun = ThreadRun.builder().assistantId("asst_V4Grgp2BQWAmH03Bs56E1H7c").build();
ThreadRun threadRun = ThreadRun.builder().assistantId("asst_bE2sCiib7eL300O9MWJanUd4").build();
RunResponse response = client.threadRun(threadRun);
System.out.println(response.toString());
//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={})
//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={})
}

@Test
public void retrieveRunStep() {
RunStepResponse response = client.retrieveRunStep("thread_E1mqqdApmpNOCBfoLnqH7KMp",
"run_dmR2AtFe9nyGal6uSSxG6GI5",
"");
RunStepResponse response = client.retrieveRunStep("thread_lVyAEZuzqxeSDHJWBVLbjYeE",
"run_NOznCqW1NQatPLbJfKvOkemE",
"step_3YUJoS3ioYX5Ziz4AooGLKqd");
System.out.println(response.toString());
//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={})
//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)
}

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


/**
* tts使用示例
*/
@SneakyThrows
@Test
public void textToSpeed() {
TextToSpeech textToSpeech = TextToSpeech.builder()
.model(TextToSpeech.Model.TTS_1_HD.getName())
.input("OpenAI官方Api的Java SDK,可以快速接入项目使用。目前支持OpenAI官方全部接口,同时支持Tokens计算。官方github地址:https://github.com/Grt1228/chatgpt-java。欢迎star。")
.voice(TtsVoice.NOVA.getName())
.responseFormat(TtsFormat.MP3.getName())
.build();
java.io.File file = new java.io.File("C:\\Users\\grt\\Desktop\\test.mp3");
ResponseBody responseBody = client.textToSpeech(textToSpeech);
InputStream inputStream = responseBody.byteStream();
//创建文件
if (!file.exists()) {
if (!file.getParentFile().exists())
file.getParentFile().mkdir();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
log.error("createNewFile IOException");
}
}

OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(file));
byte data[] = new byte[8192];
int len;
while ((len = inputStream.read(data, 0, 8192)) != -1) {
os.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

0 comments on commit ebfdf74

Please sign in to comment.