From a079ef44e53acd9e8df51dbb31cf5aea4f9be5bd Mon Sep 17 00:00:00 2001 From: ageer <ageerle@163.com> Date: 星期二, 27 二月 2024 20:52:19 +0800 Subject: [PATCH] init v1.0.0 --- ruoyi-common/ruoyi-common-chat/src/main/java/com/xmzs/common/chat/openai/OpenAiStreamClient.java | 64 ++++++++++++++++++++++++++++++++ 1 files changed, 64 insertions(+), 0 deletions(-) diff --git a/ruoyi-common/ruoyi-common-chat/src/main/java/com/xmzs/common/chat/openai/OpenAiStreamClient.java b/ruoyi-common/ruoyi-common-chat/src/main/java/com/xmzs/common/chat/openai/OpenAiStreamClient.java index 3fc25f1..a979a0f 100644 --- a/ruoyi-common/ruoyi-common-chat/src/main/java/com/xmzs/common/chat/openai/OpenAiStreamClient.java +++ b/ruoyi-common/ruoyi-common-chat/src/main/java/com/xmzs/common/chat/openai/OpenAiStreamClient.java @@ -5,6 +5,7 @@ import cn.hutool.http.ContentType; import com.fasterxml.jackson.databind.ObjectMapper; import com.xmzs.common.chat.config.LocalCache; +import com.xmzs.common.chat.entity.Tts.TextToSpeech; import com.xmzs.common.chat.entity.billing.BillingUsage; import com.xmzs.common.chat.entity.billing.KeyInfo; import com.xmzs.common.chat.entity.billing.Subscription; @@ -37,10 +38,12 @@ import org.jetbrains.annotations.NotNull; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; +import retrofit2.Call; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.jackson.JacksonConverterFactory; +import java.io.IOException; import java.io.InputStream; import java.net.http.HttpClient; import java.net.http.HttpRequest; @@ -344,6 +347,67 @@ Transcriptions transcriptions = Transcriptions.builder().build(); return this.speechToTextTranscriptions(file, transcriptions); } + /** + * 鏂囨湰杞闊筹紙寮傛锛� + * + * @param textToSpeech 鍙傛暟 + * @param callback 杩斿洖鍊兼帴鏀� + * @since 1.1.2 + */ + public void textToSpeech(TextToSpeech textToSpeech, retrofit2.Callback callback) { + retrofit2.Call<ResponseBody> responseBody = this.openAiApi.textToSpeech(textToSpeech); + responseBody.enqueue(callback); + } + + /** + * 鏂囨湰杞闊筹紙鍚屾锛� + * + * @param textToSpeech 鍙傛暟 + * @since 1.1.3 + */ + public ResponseBody textToSpeech(TextToSpeech textToSpeech){ + Call<ResponseBody> responseBody = this.openAiApi.textToSpeech(textToSpeech); + try { + return responseBody.execute().body(); + } catch (IOException e) { + throw new BaseException("鏂囨湰杞闊筹紙鍚屾锛夊け璐�: "+e.getMessage()); + } + } + + /** + * 鏂囨湰杞闊筹紙鍏嬮殕锛� + * + * @param textToSpeech + * @return + */ + public ResponseBody textToSpeechClone(TextToSpeech textToSpeech) { + String baseUrl = "http://localhost:8081"; + String spk = "涓夋湀涓�"; + String text = textToSpeech.getInput(); + String lang = "zh"; + + // 鍒涘缓OkHttpClient瀹炰緥 + OkHttpClient client = new OkHttpClient(); + + // 鏋勫缓璇锋眰URL + HttpUrl.Builder urlBuilder = HttpUrl.parse(baseUrl).newBuilder(); + urlBuilder.addQueryParameter("spk", spk); + urlBuilder.addQueryParameter("text", text); + urlBuilder.addQueryParameter("lang", lang); + String url = urlBuilder.build().toString(); + + // 鍒涘缓璇锋眰瀵硅薄 + Request request = new Request.Builder() + .url(url) + .build(); + // 鍙戦�佽姹傚苟澶勭悊鍝嶅簲 + try { + return client.newCall(request).execute().body(); + } catch (IOException e) { + throw new BaseException("璇煶鍏嬮殕澶辫触锛亄}",e.getMessage()); + } + } + /** * 鏋勯�� -- Gitblit v1.9.3