tts_asr_with_video

This commit is contained in:
gcw_4spBpAfv
2026-02-28 10:14:03 +08:00
parent 6aa84d6b77
commit d63d4b03cf
13 changed files with 1823 additions and 128 deletions

View File

@@ -1,10 +1,12 @@
package com.digitalperson.cloud;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.digitalperson.BuildConfig;
import com.digitalperson.R;
import org.json.JSONArray;
import org.json.JSONException;
@@ -30,6 +32,7 @@ public class CloudApiManager {
private CloudApiListener mListener;
private Handler mMainHandler; // 用于在主线程执行UI更新
private JSONArray mConversationHistory; // 存储对话历史
private boolean mEnableStreaming = true; // 默认启用流式输出
public interface CloudApiListener {
void onLLMResponseReceived(String response);
@@ -38,10 +41,37 @@ public class CloudApiManager {
void onError(String errorMessage);
}
public CloudApiManager(CloudApiListener listener) {
public CloudApiManager(CloudApiListener listener, Context context) {
this.mListener = listener;
this.mMainHandler = new Handler(Looper.getMainLooper()); // 初始化主线程Handler
this.mConversationHistory = new JSONArray(); // 初始化对话历史
// 添加 system message要求回答简洁
try {
JSONObject systemMessage = new JSONObject();
systemMessage.put("role", "system");
String systemPrompt = context.getString(R.string.system_prompt);
systemMessage.put("content", systemPrompt);
mConversationHistory.put(systemMessage);
} catch (JSONException e) {
Log.e(TAG, "Failed to add system message: " + e.getMessage());
}
}
/**
* 设置是否启用流式输出
* @param enableStreaming true: 启用流式输出false: 禁用流式输出(整段输出)
*/
public void setEnableStreaming(boolean enableStreaming) {
this.mEnableStreaming = enableStreaming;
}
/**
* 获取当前是否启用流式输出
* @return true: 启用流式输出false: 禁用流式输出(整段输出)
*/
public boolean isEnableStreaming() {
return mEnableStreaming;
}
public void callLLM(String userInput) {
@@ -64,7 +94,7 @@ public class CloudApiManager {
JSONObject requestBody = new JSONObject();
requestBody.put("model", LLM_MODEL);
requestBody.put("messages", mConversationHistory);
requestBody.put("stream", true); // 启用流式响应
requestBody.put("stream", mEnableStreaming); // 根据配置决定是否启用流式响应
String jsonBody = requestBody.toString();
@@ -84,47 +114,74 @@ public class CloudApiManager {
Log.d(TAG, "LLM Response Code: " + responseCode);
if (responseCode == 200) {
// 逐行读取流式响应
try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
Log.d(TAG, "LLM Streaming Line: " + line);
// 处理SSE格式的响应
if (line.startsWith("data: ")) {
String dataPart = line.substring(6);
if (dataPart.equals("[DONE]")) {
// 流式响应结束
break;
}
if (mEnableStreaming) {
// 逐行读取流式响应
try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
Log.d(TAG, "LLM Streaming Line: " + line);
try {
// 解析JSON
JSONObject chunkObj = new JSONObject(dataPart);
JSONArray choices = chunkObj.getJSONArray("choices");
if (choices.length() > 0) {
JSONObject choice = choices.getJSONObject(0);
JSONObject delta = choice.getJSONObject("delta");
if (delta.has("content")) {
String chunkContent = delta.getString("content");
accumulatedContent.append(chunkContent);
// 处理SSE格式的响应
if (line.startsWith("data: ")) {
String dataPart = line.substring(6);
if (dataPart.equals("[DONE]")) {
// 流式响应结束
break;
}
try {
// 解析JSON
JSONObject chunkObj = new JSONObject(dataPart);
JSONArray choices = chunkObj.getJSONArray("choices");
if (choices.length() > 0) {
JSONObject choice = choices.getJSONObject(0);
JSONObject delta = choice.getJSONObject("delta");
// 发送流式chunk到监听器
if (mListener != null) {
mMainHandler.post(() -> {
mListener.onLLMStreamingChunkReceived(chunkContent);
});
if (delta.has("content")) {
String chunkContent = delta.getString("content");
accumulatedContent.append(chunkContent);
// 发送流式chunk到监听器
if (mListener != null) {
mMainHandler.post(() -> {
mListener.onLLMStreamingChunkReceived(chunkContent);
});
}
}
}
} catch (JSONException e) {
Log.e(TAG, "Failed to parse streaming chunk: " + e.getMessage());
}
} catch (JSONException e) {
Log.e(TAG, "Failed to parse streaming chunk: " + e.getMessage());
}
fullResponse.append(line).append("\n");
}
}
} else {
// 读取完整响应
try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
fullResponse.append(line);
}
}
// 解析完整JSON响应
try {
JSONObject responseObj = new JSONObject(fullResponse.toString());
JSONArray choices = responseObj.getJSONArray("choices");
if (choices.length() > 0) {
JSONObject choice = choices.getJSONObject(0);
JSONObject message = choice.getJSONObject("message");
if (message.has("content")) {
String content = message.getString("content");
accumulatedContent.append(content);
}
}
fullResponse.append(line).append("\n");
} catch (JSONException e) {
Log.e(TAG, "Failed to parse full response: " + e.getMessage());
}
}