mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-03-13 20:53:42 +08:00
feat: 集成RagFlow基础聊天
This commit is contained in:
@@ -19,7 +19,9 @@ public enum ChatModeType {
|
||||
|
||||
IMAGE("image", "图片识别模型"),
|
||||
|
||||
FASTGPT("fastgpt", "FASTGPT");
|
||||
FASTGPT("fastgpt", "FASTGPT"),
|
||||
|
||||
RAGFLOW("ragflow", "RAGFLOW");
|
||||
|
||||
private final String code;
|
||||
private final String description;
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package org.ruoyi.chat.service.chat.impl;
|
||||
|
||||
|
||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||
import dev.langchain4j.model.chat.response.ChatResponse;
|
||||
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
|
||||
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.chat.enums.ChatModeType;
|
||||
import org.ruoyi.chat.service.chat.IChatService;
|
||||
import org.ruoyi.chat.support.ChatServiceHelper;
|
||||
import org.ruoyi.common.chat.request.ChatRequest;
|
||||
import org.ruoyi.domain.vo.ChatModelVo;
|
||||
import org.ruoyi.service.IChatModelService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
/**
|
||||
* RagFlow
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class RagFlowChatImpl implements IChatService {
|
||||
|
||||
@Autowired
|
||||
private IChatModelService chatModelService;
|
||||
|
||||
@Override
|
||||
public SseEmitter chat(ChatRequest chatRequest, SseEmitter emitter) {
|
||||
ChatModelVo chatModelVo = chatModelService.selectModelByName(chatRequest.getModel());
|
||||
StreamingChatModel chatModel = OpenAiStreamingChatModel.builder()
|
||||
.baseUrl(chatModelVo.getApiHost())
|
||||
.apiKey(chatModelVo.getApiKey())
|
||||
.modelName(chatModelVo.getModelName())
|
||||
.logRequests(true)
|
||||
.logResponses(true)
|
||||
.temperature(0.8)
|
||||
.build();
|
||||
// 发送流式消息
|
||||
try {
|
||||
chatModel.chat(chatRequest.getPrompt(), new StreamingChatResponseHandler() {
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void onPartialResponse(String partialResponse) {
|
||||
emitter.send(partialResponse);
|
||||
log.info("收到消息片段: {}", partialResponse);
|
||||
System.out.print(partialResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompleteResponse(ChatResponse completeResponse) {
|
||||
emitter.complete();
|
||||
log.info("消息结束,完整消息ID: {}", completeResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable error) {
|
||||
System.err.println("错误: " + error.getMessage());
|
||||
ChatServiceHelper.onStreamError(emitter, error.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("ragFlow 请求失败:{}", e.getMessage());
|
||||
// 同步异常直接通知失败
|
||||
ChatServiceHelper.onStreamError(emitter, e.getMessage());
|
||||
}
|
||||
|
||||
return emitter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCategory() {
|
||||
return ChatModeType.RAGFLOW.getCode();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user