AI 提问追问环境demo,以及评分处理

This commit is contained in:
2025-10-27 22:09:57 +08:00
parent 14b803609e
commit 45f3357962
2 changed files with 16 additions and 28 deletions

View File

@@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.vetti.common.ai.elevenLabs.ElevenLabsClient;
import com.vetti.common.ai.gpt.ChatGPTClient;
import com.vetti.common.ai.gpt.OpenAiStreamClient;
import com.vetti.common.ai.gpt.service.OpenAiStreamListenerService;
import com.vetti.common.ai.whisper.WhisperClient;
@@ -145,7 +146,7 @@ public class ChatWebSocketHandler {
String cacheResultText = cacheClientTts.get(clientId);
log.info("面试者回答信息为:{}", cacheResultText);
if (StrUtil.isEmpty(cacheResultText)) {
cacheResultText = "Hi.";
cacheResultText = "I am answering";
}
String promptJson = "";
if("YES".equals(startFlag)) {
@@ -294,27 +295,12 @@ public class ChatWebSocketHandler {
cacheMsgMapData.put(session.getId(),"");
}
log.info("结束AI提示词为:{}",promptJson);
OpenAiStreamClient aiStreamClient = SpringUtils.getBean(OpenAiStreamClient.class);
aiStreamClient.streamChat(promptJson, new OpenAiStreamListenerService() {
@Override
public void onMessage(String content) {
log.info("返回AI结果{}", content);
try {
//发送文件流数据
session.getBasicRemote().sendText(content);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
}
});
ChatGPTClient gptClient = SpringUtils.getBean(ChatGPTClient.class);
String resultMsg = gptClient.handleAiChat(promptJson);
Map<String, String> resultEntity = new HashMap<>();
resultEntity.put("msg", resultMsg);
resultEntity.put("dataType","score");
session.getBasicRemote().sendText(JSONUtil.toJsonStr(resultEntity));
}
}
} catch (Exception e) {

View File

@@ -1,5 +1,6 @@
package com.vetti.common.ai.gpt;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
@@ -71,11 +72,12 @@ public class ChatGPTClient {
*/
private String sendMessage(String prompt, String model,ObjectMapper objectMapper,HttpClient client,String role) throws IOException, InterruptedException {
// 创建消息列表
List<Map<String, String>> messages = new ArrayList<>();
Map<String, String> message = new HashMap<>();
message.put("role", role);
message.put("content", prompt);
messages.add(message);
List<Map> messages = JSONUtil.toList(prompt, Map.class);
// Map<String, String> message = new HashMap<>();
// message.put("role", role);
// message.put("content", prompt);
// messages.add(message);
return sendChatRequest(messages, model, 0.7,objectMapper,client);
}
@@ -89,7 +91,7 @@ public class ChatGPTClient {
* @throws IOException 网络或IO异常
* @throws InterruptedException 线程中断异常
*/
private String sendChatRequest(List<Map<String, String>> messages, String model, double temperature,ObjectMapper objectMapper,HttpClient client)
private String sendChatRequest(List<Map> messages, String model, double temperature,ObjectMapper objectMapper,HttpClient client)
throws IOException, InterruptedException {
// 构建请求体
Map<String, Object> requestBody = new HashMap<>();