33
44import com .unfbx .chatgpt .FirstKeyStrategy ;
55import 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 ;
69import com .unfbx .chatgpt .entity .assistant .*;
710import com .unfbx .chatgpt .entity .assistant .message .MessageFileResponse ;
811import com .unfbx .chatgpt .entity .assistant .message .MessageResponse ;
1922import com .unfbx .chatgpt .entity .assistant .thread .ThreadResponse ;
2023import com .unfbx .chatgpt .interceptor .OpenAILogger ;
2124import com .unfbx .chatgpt .interceptor .OpenAiResponseInterceptor ;
25+ import lombok .SneakyThrows ;
2226import lombok .extern .slf4j .Slf4j ;
2327import okhttp3 .OkHttpClient ;
28+ import okhttp3 .ResponseBody ;
2429import okhttp3 .logging .HttpLoggingInterceptor ;
2530import org .junit .Before ;
2631import org .junit .Test ;
2732
33+ import java .io .*;
2834import java .net .InetSocketAddress ;
2935import java .net .Proxy ;
3036import 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