搭配生成 添加sessionId
This commit is contained in:
@@ -30,7 +30,7 @@ public class ChatController {
|
|||||||
@Parameter(description = "用户输入的消息内容", example = "你好,请介绍一下自己")
|
@Parameter(description = "用户输入的消息内容", example = "你好,请介绍一下自己")
|
||||||
@RequestParam(required = false) String message,
|
@RequestParam(required = false) String message,
|
||||||
|
|
||||||
@Parameter(description = "会话ID", example = "123456", required = true)
|
@Parameter(description = "会话ID", example = "1740968404", required = true)
|
||||||
@RequestParam Long sessionId,
|
@RequestParam Long sessionId,
|
||||||
|
|
||||||
@Parameter(description = "性别", example = "male | female", required = true)
|
@Parameter(description = "性别", example = "male | female", required = true)
|
||||||
|
|||||||
@@ -52,4 +52,11 @@ public class RequestOutfitDTO {
|
|||||||
)
|
)
|
||||||
private int num = 1;
|
private int num = 1;
|
||||||
|
|
||||||
|
@Schema(
|
||||||
|
description = "会话id",
|
||||||
|
example = "1740968404",
|
||||||
|
requiredMode = Schema.RequiredMode.REQUIRED
|
||||||
|
)
|
||||||
|
private String sessionId;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,11 +30,12 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
*/
|
*/
|
||||||
public SseEmitter streamChat(String message, Long sessionId, String gender) {
|
public SseEmitter streamChat(String message, Long sessionId, String gender) {
|
||||||
SseEmitter emitter = new SseEmitter(0L);
|
SseEmitter emitter = new SseEmitter(0L);
|
||||||
|
Long userId = UserContext.getUserHolder().getId();
|
||||||
|
|
||||||
CompletableFuture.runAsync(() -> {
|
CompletableFuture.runAsync(() -> {
|
||||||
try {
|
try {
|
||||||
// 连接第三方AI服务
|
// 连接第三方AI服务
|
||||||
processAIStream(message, sessionId.toString(), gender, emitter);
|
processAIStream(message, userId, sessionId.toString(), gender, emitter);
|
||||||
emitter.complete();
|
emitter.complete();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("AI流式对话处理失败", e);
|
log.error("AI流式对话处理失败", e);
|
||||||
@@ -48,14 +49,14 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
/**
|
/**
|
||||||
* 处理第三方AI服务的流式响应
|
* 处理第三方AI服务的流式响应
|
||||||
*/
|
*/
|
||||||
private void processAIStream(String message, String sessionId, String gender, SseEmitter emitter) {
|
private void processAIStream(String message, Long userId, String sessionId, String gender, SseEmitter emitter) {
|
||||||
HttpURLConnection connection = null;
|
HttpURLConnection connection = null;
|
||||||
try {
|
try {
|
||||||
// 1. 创建AI服务连接
|
// 1. 创建AI服务连接
|
||||||
connection = createAIConnection();
|
connection = createAIConnection();
|
||||||
|
|
||||||
// 2. 发送请求到AI服务
|
// 2. 发送请求到AI服务
|
||||||
sendToAI(connection, message, sessionId, gender);
|
sendToAI(connection, message, userId, sessionId, gender);
|
||||||
|
|
||||||
// 3. 流式读取并转发响应
|
// 3. 流式读取并转发响应
|
||||||
streamAIResponse(connection, emitter, sessionId);
|
streamAIResponse(connection, emitter, sessionId);
|
||||||
@@ -89,13 +90,12 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
/**
|
/**
|
||||||
* 发送请求到AI服务
|
* 发送请求到AI服务
|
||||||
*/
|
*/
|
||||||
private void sendToAI(HttpURLConnection connection, String message, String sessionId, String gender)
|
private void sendToAI(HttpURLConnection connection, String message, Long userId, String sessionId, String gender)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
Long userId = UserContext.getUserHolder().getId();
|
|
||||||
// 构建AI服务请求体
|
// 构建AI服务请求体
|
||||||
JSONObject requestBody = new JSONObject();
|
JSONObject requestBody = new JSONObject();
|
||||||
requestBody.put("user_message", message);
|
requestBody.put("user_message", message);
|
||||||
requestBody.put("user_id", userId);
|
requestBody.put("user_id", userId.toString());
|
||||||
requestBody.put("session_id", sessionId);
|
requestBody.put("session_id", sessionId);
|
||||||
requestBody.put("gender", gender); // 启用流式输出
|
requestBody.put("gender", gender); // 启用流式输出
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,8 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
|||||||
// 请求需要顾客id, 生成的数量,风格
|
// 请求需要顾客id, 生成的数量,风格
|
||||||
|
|
||||||
StylistPathEnum stylistPathEnum = StylistPathEnum.of(requestOutfitDTO.getStylist());
|
StylistPathEnum stylistPathEnum = StylistPathEnum.of(requestOutfitDTO.getStylist());
|
||||||
Map<String, Object> params = setRequestOutfitParams(requestOutfitDTO.getCustomerId(), requestOutfitDTO.getNum(), stylistPathEnum.getPath(), requestOutfitDTO.getGender());
|
Map<String, Object> params = setRequestOutfitParams(requestOutfitDTO.getCustomerId(), requestOutfitDTO.getNum(),
|
||||||
|
stylistPathEnum.getPath(), requestOutfitDTO.getGender(), requestOutfitDTO.getSessionId());
|
||||||
|
|
||||||
OutfitRequest outfitRequest = new OutfitRequest();
|
OutfitRequest outfitRequest = new OutfitRequest();
|
||||||
outfitRequest.setCustomerId(requestOutfitDTO.getCustomerId());
|
outfitRequest.setCustomerId(requestOutfitDTO.getCustomerId());
|
||||||
@@ -60,6 +61,7 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
|||||||
outfitRequest.setGender(requestOutfitDTO.getGender());
|
outfitRequest.setGender(requestOutfitDTO.getGender());
|
||||||
outfitRequestMapper.insert(outfitRequest);
|
outfitRequestMapper.insert(outfitRequest);
|
||||||
|
|
||||||
|
log.info("agent request params: {}", JSON.toJSONString(params));
|
||||||
String response = SendRequestUtil.sendPostWithRetry(CommonConstants.REQUEST_OUTFIT, JSON.toJSONString(params));
|
String response = SendRequestUtil.sendPostWithRetry(CommonConstants.REQUEST_OUTFIT, JSON.toJSONString(params));
|
||||||
|
|
||||||
JSONObject jsonObject = JSONObject.parseObject(response); // todo 确认这里的status的取值
|
JSONObject jsonObject = JSONObject.parseObject(response); // todo 确认这里的status的取值
|
||||||
@@ -92,7 +94,7 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> setRequestOutfitParams(Long customerId, int num, String stylistPath, String gender) {
|
private Map<String, Object> setRequestOutfitParams(Long customerId, int num, String stylistPath, String gender, String sessionId) {
|
||||||
HashMap<String, Object> params = new HashMap<>();
|
HashMap<String, Object> params = new HashMap<>();
|
||||||
params.put("user_id", customerId.toString());
|
params.put("user_id", customerId.toString());
|
||||||
params.put("num_outfits", num);
|
params.put("num_outfits", num);
|
||||||
@@ -100,6 +102,7 @@ public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> implements
|
|||||||
params.put("callback_url", webhookDomain);
|
params.put("callback_url", webhookDomain);
|
||||||
params.put("gender", gender);
|
params.put("gender", gender);
|
||||||
params.put("max_len", 9);
|
params.put("max_len", 9);
|
||||||
|
params.put("session_id", sessionId);
|
||||||
|
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user