refactor(llm): 重构AI代理平台服务接口和实现
- 修改LLMAiAgentPlatformService.query方法签名,添加TTS相关参数 - 更新FlowActionExecutorService中的AI代理平台调用逻辑 - 移除FlowHttpNodeHandler中的formData清理逻辑 - 简化FlowNodeParamPreparer中参数准备逻辑 - 更新LLMAiAgentPlatformOperateService参数解析结构 - 修改LlmChatService.chat方法添加TTS配置参数 - 实现TTS播放控制功能,支持配置化语音合成参数 - 更新意图识别和工作流调用服务兼容性适配
This commit is contained in:
parent
37d800be69
commit
0dbed4006c
@ -3,5 +3,5 @@ package com.cmvr.llm.service;
|
|||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
|
|
||||||
public interface LLMAiAgentPlatformService {
|
public interface LLMAiAgentPlatformService {
|
||||||
JSONObject query(String action, String text, String apiKey);
|
JSONObject query(String action, String text, String apiKey, Boolean invokeTts, JSONObject config);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -90,7 +90,9 @@ public class WorkflowInvokeServiceCopy {
|
|||||||
appKey, // apiKey (header用)
|
appKey, // apiKey (header用)
|
||||||
appId, // apiId (body的AppKey)
|
appId, // apiId (body的AppKey)
|
||||||
"user_123", // userId
|
"user_123", // userId
|
||||||
body
|
body,
|
||||||
|
false,
|
||||||
|
null
|
||||||
);
|
);
|
||||||
System.out.println( result);
|
System.out.println( result);
|
||||||
System.out.println("总耗时:" + (System.currentTimeMillis() - startTime));
|
System.out.println("总耗时:" + (System.currentTimeMillis() - startTime));
|
||||||
|
|||||||
@ -14,7 +14,7 @@ import java.util.Map;
|
|||||||
public class LLMAiAgentPlatformServiceImpl implements LLMAiAgentPlatformService {
|
public class LLMAiAgentPlatformServiceImpl implements LLMAiAgentPlatformService {
|
||||||
private final LlmChatService llmChatService;
|
private final LlmChatService llmChatService;
|
||||||
@Override
|
@Override
|
||||||
public JSONObject query(String action, String text, String apiKey) {
|
public JSONObject query(String action, String text, String apiKey, Boolean invokeTts, JSONObject config) {
|
||||||
Map<String, Object> body = new HashMap<>();
|
Map<String, Object> body = new HashMap<>();
|
||||||
body.put("Query", text);
|
body.put("Query", text);
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
@ -24,7 +24,9 @@ public class LLMAiAgentPlatformServiceImpl implements LLMAiAgentPlatformService
|
|||||||
apiKey, // apiKey (header用)
|
apiKey, // apiKey (header用)
|
||||||
apiKey, // apiId (body的AppKey)
|
apiKey, // apiId (body的AppKey)
|
||||||
"user_123", // userId
|
"user_123", // userId
|
||||||
body
|
body,
|
||||||
|
invokeTts,
|
||||||
|
config
|
||||||
);
|
);
|
||||||
System.out.println( result);
|
System.out.println( result);
|
||||||
System.out.println("总耗时:" + (System.currentTimeMillis() - startTime));
|
System.out.println("总耗时:" + (System.currentTimeMillis() - startTime));
|
||||||
|
|||||||
@ -36,7 +36,9 @@ public class LLMIntentRecognitionServiceImpl implements LLMIntentRecognitionServ
|
|||||||
acg.getAppKey(), // apiKey (header用)
|
acg.getAppKey(), // apiKey (header用)
|
||||||
acg.getAppId(), // apiId (body的AppKey)
|
acg.getAppId(), // apiId (body的AppKey)
|
||||||
"user_123", // userId
|
"user_123", // userId
|
||||||
body
|
body,
|
||||||
|
false,
|
||||||
|
null
|
||||||
);
|
);
|
||||||
System.out.println( result);
|
System.out.println( result);
|
||||||
System.out.println("总耗时:" + (System.currentTimeMillis() - startTime));
|
System.out.println("总耗时:" + (System.currentTimeMillis() - startTime));
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package com.cmvr.llm.util;
|
package com.cmvr.llm.util;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.cmvr.llm.service.LLMAiTtsService;
|
import com.cmvr.llm.service.LLMAiTtsService;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
@ -50,7 +51,7 @@ public class LlmChatService {
|
|||||||
* @param body 请求体,必须包含Query字段,可选Name等
|
* @param body 请求体,必须包含Query字段,可选Name等
|
||||||
* @return 完整AI回复字符串
|
* @return 完整AI回复字符串
|
||||||
*/
|
*/
|
||||||
public String chat(String apiKey, String apiId, String userId, Map<String, Object> body) throws IOException {
|
public String chat(String apiKey, String apiId, String userId, Map<String, Object> body, boolean playTts, JSONObject ttsConfig) throws IOException {
|
||||||
String cacheKey = apiKey + "|" + apiId + "|" + userId;
|
String cacheKey = apiKey + "|" + apiId + "|" + userId;
|
||||||
|
|
||||||
// 获取或创建会话(严格保持原createConversation逻辑)
|
// 获取或创建会话(严格保持原createConversation逻辑)
|
||||||
@ -78,7 +79,7 @@ public class LlmChatService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 执行流式请求并同步返回完整结果
|
// 执行流式请求并同步返回完整结果
|
||||||
return executeSseRequestSync(apiKey, requestBody);
|
return executeSseRequestSync(apiKey, requestBody, playTts, ttsConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -130,7 +131,7 @@ public class LlmChatService {
|
|||||||
/**
|
/**
|
||||||
* 执行SSE请求 - 完全保持原executeSseRequest逻辑,改为同步返回
|
* 执行SSE请求 - 完全保持原executeSseRequest逻辑,改为同步返回
|
||||||
*/
|
*/
|
||||||
private String executeSseRequestSync(String apiKey, Map<String, Object> requestBody) throws IOException {
|
private String executeSseRequestSync(String apiKey, Map<String, Object> requestBody, boolean playTts, JSONObject ttsConfig) throws IOException {
|
||||||
String jsonBody = objectMapper.writeValueAsString(requestBody);
|
String jsonBody = objectMapper.writeValueAsString(requestBody);
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@ -286,6 +287,7 @@ public class LlmChatService {
|
|||||||
private void processBufferIfNeed() {
|
private void processBufferIfNeed() {
|
||||||
if (interrupted.get()) return;
|
if (interrupted.get()) return;
|
||||||
if (!true) return;
|
if (!true) return;
|
||||||
|
if (!playTts) return;
|
||||||
|
|
||||||
sentenceExecutor.submit(() -> {
|
sentenceExecutor.submit(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -316,7 +318,7 @@ public class LlmChatService {
|
|||||||
|
|
||||||
if (!sentence.isEmpty()) {
|
if (!sentence.isEmpty()) {
|
||||||
if (interrupted.get()) return;
|
if (interrupted.get()) return;
|
||||||
yourAsyncMethod(sentence);
|
yourAsyncMethod(sentence, ttsConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理完继续循环,看是否还有新句子
|
// 处理完继续循环,看是否还有新句子
|
||||||
@ -355,10 +357,10 @@ public class LlmChatService {
|
|||||||
return fullContent.toString();
|
return fullContent.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void yourAsyncMethod(String sentence) {
|
private void yourAsyncMethod(String sentence, JSONObject ttsConfig) {
|
||||||
try {
|
try {
|
||||||
System.out.println("正在处理句子: " + sentence);
|
System.out.println("正在处理句子: " + sentence);
|
||||||
llmAiTtsService.play("http://127.0.0.1:8080/tts/play", sentence, "x4_yezi", "50", "80");
|
llmAiTtsService.play(ttsConfig.getString("url"), sentence, ttsConfig.getString("voice"), ttsConfig.getString("speed"), ttsConfig.getString("volume"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,7 +40,6 @@ public class FlowHttpNodeHandler implements FlowNodeTypeHandler {
|
|||||||
Object bodyCfg = null;
|
Object bodyCfg = null;
|
||||||
if (!"json".equals(body.getString("bodyType"))) {
|
if (!"json".equals(body.getString("bodyType"))) {
|
||||||
JSONObject formData = body.getJSONObject("formData");
|
JSONObject formData = body.getJSONObject("formData");
|
||||||
formData.remove("_self_");
|
|
||||||
bodyCfg = formData;
|
bodyCfg = formData;
|
||||||
} else {
|
} else {
|
||||||
bodyCfg = body.get("json");
|
bodyCfg = body.get("json");
|
||||||
|
|||||||
@ -124,11 +124,8 @@ public class FlowNodeParamPreparer {
|
|||||||
input.put(param.getName(), val);
|
input.put(param.getName(), val);
|
||||||
}
|
}
|
||||||
if (!CollectionUtil.isEmpty(param.getChildren())) {
|
if (!CollectionUtil.isEmpty(param.getChildren())) {
|
||||||
JSONObject inputParams = getInputParams(graph, param.getChildren(), rootMessage);
|
|
||||||
// 添加 _self_ 参数,用于在子参数中引用父参数
|
|
||||||
inputParams.put("_self_",input.get(param.getName()));
|
|
||||||
// 添加子参数
|
// 添加子参数
|
||||||
input.put(param.getName(), inputParams);
|
input.put(param.getName(), getInputParams(graph, param.getChildren(), rootMessage));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return input;
|
return input;
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class LLMAiAgentPlatformOperateService implements LLMOperateService {
|
public class LLMAiAgentPlatformOperateService implements LLMOperateService {
|
||||||
|
|
||||||
@ -26,10 +27,11 @@ public class LLMAiAgentPlatformOperateService implements LLMOperateService {
|
|||||||
public TaskNodeExecuteResult execute(TaskNodeExecuteMessage message) {
|
public TaskNodeExecuteResult execute(TaskNodeExecuteMessage message) {
|
||||||
ActionEnum action = message.getAction();
|
ActionEnum action = message.getAction();
|
||||||
JSONObject inputParams = message.getInputParams();
|
JSONObject inputParams = message.getInputParams();
|
||||||
String text = inputParams.getString("text");
|
JSONObject config = inputParams.getJSONObject("config");
|
||||||
String apiKey = inputParams.getString("apiKey");
|
String text = config.getString("text");
|
||||||
|
String apiKey = config.getString("apiKey");
|
||||||
JSONObject output = llmAiAgentPlatformService.query(action.name(), text, apiKey);
|
Boolean invokeTts = inputParams.getBoolean("invokeTts");
|
||||||
|
JSONObject output =llmAiAgentPlatformService.query(action.name(), text, apiKey, invokeTts, inputParams.getJSONObject("tts"));
|
||||||
|
|
||||||
return TaskNodeExecuteResult.success(output);
|
return TaskNodeExecuteResult.success(output);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -120,7 +120,7 @@ public class FlowActionExecutorService {
|
|||||||
case GENERATE_ADVANCED_AUDIO:
|
case GENERATE_ADVANCED_AUDIO:
|
||||||
return "LLM 高级音频生成 OK";
|
return "LLM 高级音频生成 OK";
|
||||||
case AI_AGENT_PLATFORM:
|
case AI_AGENT_PLATFORM:
|
||||||
return llmAiAgentPlatformService.query(action.getAction(), req.getPayload().getString("text"), req.getPayload().getString("apiKey")).toString();
|
return llmAiAgentPlatformService.query(action.getAction(), req.getPayload().getJSONObject("config").getString("text"), req.getPayload().getJSONObject("config").getString("apiKey"), req.getPayload().getBoolean("invokeTts"), req.getPayload().getJSONObject("tts")).toString();
|
||||||
default:
|
default:
|
||||||
throw new UnsupportedOperationException("未实现的 LLM Action: " + action);
|
throw new UnsupportedOperationException("未实现的 LLM Action: " + action);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user