mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-14 12:23:39 +00:00
refactor(DashscopeServiceImpl): 移除静态变量并改进日志输出逻辑
将静态变量改为实例变量以避免并发问题 重构日志输出逻辑,仅在最后响应时输出完整内容 添加异常堆栈打印以方便调试
This commit is contained in:
@@ -28,9 +28,7 @@ import org.springframework.stereotype.Service;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class DashscopeServiceImpl implements DashscopeService {
|
public class DashscopeServiceImpl implements DashscopeService {
|
||||||
|
|
||||||
private static StringBuilder reasoningContent = new StringBuilder();
|
private boolean isFirstPrint;
|
||||||
private static StringBuilder finalContent = new StringBuilder();
|
|
||||||
private static boolean isFirstPrint = true;
|
|
||||||
|
|
||||||
@Value("${dashscope.model}")
|
@Value("${dashscope.model}")
|
||||||
private String serviceModel;
|
private String serviceModel;
|
||||||
@@ -67,11 +65,11 @@ public class DashscopeServiceImpl implements DashscopeService {
|
|||||||
Flowable<MultiModalConversationResult> result = conv.streamCall(
|
Flowable<MultiModalConversationResult> result = conv.streamCall(
|
||||||
param);
|
param);
|
||||||
|
|
||||||
reasoningContent = new StringBuilder();
|
StringBuilder reasoningContent = new StringBuilder();
|
||||||
finalContent = new StringBuilder();
|
StringBuilder finalContent = new StringBuilder();
|
||||||
isFirstPrint = true;
|
isFirstPrint = true;
|
||||||
|
|
||||||
result.blockingForEach(DashscopeServiceImpl::handleGenerationResult);
|
result.blockingForEach(message -> handleGenerationResult(message, reasoningContent, finalContent));
|
||||||
|
|
||||||
return finalContent.toString().replaceAll("[\n\r\s]", "");
|
return finalContent.toString().replaceAll("[\n\r\s]", "");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -106,11 +104,11 @@ public class DashscopeServiceImpl implements DashscopeService {
|
|||||||
Flowable<MultiModalConversationResult> result = conv.streamCall(
|
Flowable<MultiModalConversationResult> result = conv.streamCall(
|
||||||
param);
|
param);
|
||||||
|
|
||||||
reasoningContent = new StringBuilder();
|
StringBuilder reasoningContent = new StringBuilder();
|
||||||
finalContent = new StringBuilder();
|
StringBuilder finalContent = new StringBuilder();
|
||||||
isFirstPrint = true;
|
isFirstPrint = true;
|
||||||
|
|
||||||
result.blockingForEach(DashscopeServiceImpl::handleGenerationResult);
|
result.blockingForEach(message -> handleGenerationResult(message, reasoningContent, finalContent));
|
||||||
|
|
||||||
return finalContent.toString().replaceAll("[\n\r\s]", "");
|
return finalContent.toString().replaceAll("[\n\r\s]", "");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -120,30 +118,33 @@ public class DashscopeServiceImpl implements DashscopeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void handleGenerationResult(MultiModalConversationResult message) {
|
private void handleGenerationResult(MultiModalConversationResult message, StringBuilder reasoningContent, StringBuilder finalContent) {
|
||||||
|
|
||||||
String re = message.getOutput().getChoices().get(0).getMessage().getReasoningContent();
|
String re = message.getOutput().getChoices().get(0).getMessage().getReasoningContent();
|
||||||
String reasoning = Objects.isNull(re) ? "" : re; // 默认值
|
String reasoning = Objects.isNull(re) ? "" : re;
|
||||||
|
|
||||||
List<Map<String, Object>> content = message.getOutput().getChoices().get(0).getMessage()
|
List<Map<String, Object>> content = message.getOutput().getChoices().get(0).getMessage()
|
||||||
.getContent();
|
.getContent();
|
||||||
if (!reasoning.isEmpty()) {
|
if (!reasoning.isEmpty()) {
|
||||||
reasoningContent.append(reasoning);
|
reasoningContent.append(reasoning);
|
||||||
if (isFirstPrint) {
|
|
||||||
System.out.println("====================思考过程====================");
|
|
||||||
isFirstPrint = false;
|
|
||||||
}
|
|
||||||
System.out.print(reasoning);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Objects.nonNull(content) && !content.isEmpty()) {
|
if (Objects.nonNull(content) && !content.isEmpty()) {
|
||||||
Object text = content.get(0).get("text");
|
Object text = content.get(0).get("text");
|
||||||
finalContent.append(text);
|
finalContent.append(text);
|
||||||
if (!isFirstPrint) {
|
}
|
||||||
System.out.println("\n====================完整回复====================");
|
|
||||||
isFirstPrint = true;
|
// 检查是否是最后一个响应
|
||||||
|
if (message.getOutput().getChoices().get(0).getFinishReason() != null) {
|
||||||
|
// 输出思考过程
|
||||||
|
if (reasoningContent.length() > 0) {
|
||||||
|
System.out.println("====================思考过程====================");
|
||||||
|
System.out.println(reasoningContent.toString());
|
||||||
|
}
|
||||||
|
// 输出完整回复
|
||||||
|
if (finalContent.length() > 0) {
|
||||||
|
System.out.println("====================完整回复====================");
|
||||||
|
System.out.println(finalContent.toString());
|
||||||
}
|
}
|
||||||
System.out.print(text);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,6 +129,7 @@ public class DealFileService {
|
|||||||
.eq(KnowledgeAttach::getVectorStatus, DealStatus.STATUS_20)
|
.eq(KnowledgeAttach::getVectorStatus, DealStatus.STATUS_20)
|
||||||
.eq(KnowledgeAttach::getId, attachItem.getId()));
|
.eq(KnowledgeAttach::getId, attachItem.getId()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
//设置处理失败
|
//设置处理失败
|
||||||
attachMapper.update(new LambdaUpdateWrapper<KnowledgeAttach>()
|
attachMapper.update(new LambdaUpdateWrapper<KnowledgeAttach>()
|
||||||
.set(KnowledgeAttach::getVectorStatus, DealStatus.STATUS_40)
|
.set(KnowledgeAttach::getVectorStatus, DealStatus.STATUS_40)
|
||||||
|
|||||||
Reference in New Issue
Block a user