mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-13 20:11:29 +00:00
Compare commits
6 Commits
v2.1.0
...
97ae5a46cd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97ae5a46cd | ||
|
|
baa664ac4f | ||
|
|
353fbf26b8 | ||
|
|
f79b4ec012 | ||
|
|
0a73cb4e17 | ||
|
|
d635e30b4a |
@@ -53,9 +53,13 @@ public interface IChatModelService {
|
|||||||
* 通过模型名称获取模型信息
|
* 通过模型名称获取模型信息
|
||||||
*/
|
*/
|
||||||
ChatModelVo selectModelByName(String modelName);
|
ChatModelVo selectModelByName(String modelName);
|
||||||
|
/**
|
||||||
|
* 通过模型分类获取模型信息
|
||||||
|
*/
|
||||||
|
ChatModelVo selectModelByCategory(String image);
|
||||||
/**
|
/**
|
||||||
* 获取ppt模型信息
|
* 获取ppt模型信息
|
||||||
*/
|
*/
|
||||||
ChatModel getPPT();
|
ChatModel getPPT();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,6 +129,13 @@ public class ChatModelServiceImpl implements IChatModelService {
|
|||||||
public ChatModelVo selectModelByName(String modelName) {
|
public ChatModelVo selectModelByName(String modelName) {
|
||||||
return baseMapper.selectVoOne(Wrappers.<ChatModel>lambdaQuery().eq(ChatModel::getModelName, modelName));
|
return baseMapper.selectVoOne(Wrappers.<ChatModel>lambdaQuery().eq(ChatModel::getModelName, modelName));
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 通过模型分类获取模型信息
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ChatModelVo selectModelByCategory(String category) {
|
||||||
|
return baseMapper.selectVoOne(Wrappers.<ChatModel>lambdaQuery().eq(ChatModel::getCategory, category));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChatModel getPPT() {
|
public ChatModel getPPT() {
|
||||||
|
|||||||
@@ -15,7 +15,9 @@ public enum ChatModeType {
|
|||||||
|
|
||||||
QIANWEN("qianwen", "通义千问"),
|
QIANWEN("qianwen", "通义千问"),
|
||||||
|
|
||||||
VECTOR("vector", "知识库向量模型");
|
VECTOR("vector", "知识库向量模型"),
|
||||||
|
|
||||||
|
IMAGE("image", "图片识别模型");
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
private final String description;
|
private final String description;
|
||||||
|
|||||||
@@ -0,0 +1,151 @@
|
|||||||
|
package org.ruoyi.chat.service.chat.impl;
|
||||||
|
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.ruoyi.chat.config.ChatConfig;
|
||||||
|
import org.ruoyi.chat.enums.ChatModeType;
|
||||||
|
import org.ruoyi.chat.listener.SSEEventSourceListener;
|
||||||
|
import org.ruoyi.chat.service.chat.IChatService;
|
||||||
|
import org.ruoyi.common.chat.entity.chat.ChatCompletion;
|
||||||
|
import org.ruoyi.common.chat.entity.chat.Message;
|
||||||
|
import org.ruoyi.common.chat.openai.OpenAiStreamClient;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片识别模型
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class ImageServiceImpl implements IChatService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IChatModelService chatModelService;
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
// @Override
|
||||||
|
// public SseEmitter chat(ChatRequest chatRequest, SseEmitter emitter) {
|
||||||
|
// ChatModelVo chatModelVo = chatModelService.selectModelByCategory("image");
|
||||||
|
//
|
||||||
|
// // 发送流式消息
|
||||||
|
//
|
||||||
|
// MultiModalConversation conv = new MultiModalConversation();
|
||||||
|
// MultiModalMessage systemMessage = MultiModalMessage.builder().role(Role.SYSTEM.getValue())
|
||||||
|
// .content(Arrays.asList(
|
||||||
|
// Collections.singletonMap("text",chatRequest.getSysPrompt()))).build();
|
||||||
|
// // 获取用户消息内容
|
||||||
|
// List<Message> messages = chatRequest.getMessages();
|
||||||
|
// MultiModalMessage userMessage = null;
|
||||||
|
// //漫长的格式转换
|
||||||
|
// // 遍历消息列表,提取文本内容
|
||||||
|
// if (messages != null && !messages.isEmpty()) {
|
||||||
|
// Object content = messages.get(messages.size() - 1).getContent();
|
||||||
|
// List<Map<String, Object>> contentList = new ArrayList<>();
|
||||||
|
// StringBuilder textContent = new StringBuilder();
|
||||||
|
// if (content instanceof List<?>) {
|
||||||
|
// for (Object item : (List<?>) content) {
|
||||||
|
// if (item instanceof Map<?, ?> mapItem) {
|
||||||
|
// String type = (String) mapItem.get("type");
|
||||||
|
// if ("text".equals(type)) {
|
||||||
|
// String text = (String) mapItem.get("text");
|
||||||
|
// if (text != null) {
|
||||||
|
// textContent.append(text).append(" ");
|
||||||
|
// }
|
||||||
|
// } else if ("image_url".equals(type)) {
|
||||||
|
// Map<String, String> imageUrl = (Map<String, String>) mapItem.get("image_url");
|
||||||
|
// contentList.add(Collections.singletonMap("image", imageUrl.get("url")));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// // 将拼接后的文本内容添加到 contentList
|
||||||
|
// if (textContent.length() > 0) {
|
||||||
|
// contentList.add(Collections.singletonMap("text", textContent.toString().trim()));
|
||||||
|
// }
|
||||||
|
// userMessage = MultiModalMessage.builder()
|
||||||
|
// .role(Role.USER.getValue())
|
||||||
|
// .content(contentList)
|
||||||
|
// .build();
|
||||||
|
// }
|
||||||
|
// MultiModalConversationParam param = MultiModalConversationParam.builder()
|
||||||
|
// .apiKey(chatModelVo.getApiKey())
|
||||||
|
// .model(chatModelVo.getModelName())
|
||||||
|
// .messages(Arrays.asList(systemMessage, userMessage))
|
||||||
|
// .incrementalOutput(true)
|
||||||
|
// .build();
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// final QwenStreamingResponseBuilder responseBuilder = new QwenStreamingResponseBuilder(param.getModel(),param.getIncrementalOutput() );
|
||||||
|
// conv.streamCall(param, new ResultCallback<>() {
|
||||||
|
// @SneakyThrows
|
||||||
|
// public void onEvent(MultiModalConversationResult result) {
|
||||||
|
//
|
||||||
|
// String delta = responseBuilder.append(result);
|
||||||
|
// if (Utils.isNotNullOrEmpty(delta)) {
|
||||||
|
//
|
||||||
|
// emitter.send(delta);
|
||||||
|
// log.info("收到消息片段: {}", delta);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// public void onComplete() {
|
||||||
|
// emitter.complete();
|
||||||
|
// log.info("消息结束", responseBuilder.build());
|
||||||
|
// }
|
||||||
|
// public void onError(Exception e) {
|
||||||
|
// log.info("请求失败", e.getMessage());
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// } catch (NoApiKeyException e) {
|
||||||
|
// emitter.send("请先配置API密钥");
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// } catch (UploadFileException e) {
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// return emitter;
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SseEmitter chat(ChatRequest chatRequest, SseEmitter emitter) {
|
||||||
|
// 从数据库获取 image 类型的模型配置
|
||||||
|
ChatModelVo chatModelVo = chatModelService.selectModelByCategory(ChatModeType.IMAGE.getCode());
|
||||||
|
if (chatModelVo == null) {
|
||||||
|
log.error("未找到 image 类型的模型配置");
|
||||||
|
emitter.completeWithError(new IllegalStateException("未找到 image 类型的模型配置"));
|
||||||
|
return emitter;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建 OpenAI 流客户端
|
||||||
|
OpenAiStreamClient openAiStreamClient = ChatConfig.createOpenAiStreamClient(chatModelVo.getApiHost(), chatModelVo.getApiKey());
|
||||||
|
List<Message> messages = chatRequest.getMessages();
|
||||||
|
|
||||||
|
// 创建 SSE 事件源监听器
|
||||||
|
SSEEventSourceListener listener = new SSEEventSourceListener(emitter, chatRequest.getUserId(), chatRequest.getSessionId());
|
||||||
|
|
||||||
|
// 构建聊天完成请求
|
||||||
|
ChatCompletion completion = ChatCompletion
|
||||||
|
.builder()
|
||||||
|
.messages(messages)
|
||||||
|
.model(chatModelVo.getModelName()) // 使用数据库中配置的模型名称
|
||||||
|
.stream(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 发起流式聊天完成请求
|
||||||
|
openAiStreamClient.streamChatCompletion(completion, listener);
|
||||||
|
|
||||||
|
return emitter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCategory() {
|
||||||
|
return ChatModeType.IMAGE.getCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,6 +35,8 @@ public class QianWenAiChatServiceImpl implements IChatService {
|
|||||||
.modelName(chatModelVo.getModelName())
|
.modelName(chatModelVo.getModelName())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 发送流式消息
|
// 发送流式消息
|
||||||
try {
|
try {
|
||||||
model.chat(chatRequest.getPrompt(), new StreamingChatResponseHandler() {
|
model.chat(chatRequest.getPrompt(), new StreamingChatResponseHandler() {
|
||||||
|
|||||||
@@ -125,7 +125,16 @@ public class SseServiceImpl implements ISseService {
|
|||||||
*/
|
*/
|
||||||
private void buildChatMessageList(ChatRequest chatRequest){
|
private void buildChatMessageList(ChatRequest chatRequest){
|
||||||
String sysPrompt;
|
String sysPrompt;
|
||||||
chatModelVo = chatModelService.selectModelByName(chatRequest.getModel());
|
// 矫正模型名称 如果是gpt-image 则查询image类型模型 获取模型名称
|
||||||
|
if(chatRequest.getModel().equals("gpt-image")) {
|
||||||
|
chatModelVo = chatModelService.selectModelByCategory("image");
|
||||||
|
if (chatModelVo == null) {
|
||||||
|
log.error("未找到image类型的模型配置");
|
||||||
|
throw new IllegalStateException("未找到image类型的模型配置");
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
chatModelVo = chatModelService.selectModelByName(chatRequest.getModel());
|
||||||
|
}
|
||||||
// 获取对话消息列表
|
// 获取对话消息列表
|
||||||
List<Message> messages = chatRequest.getMessages();
|
List<Message> messages = chatRequest.getMessages();
|
||||||
// 查询向量库相关信息加入到上下文
|
// 查询向量库相关信息加入到上下文
|
||||||
|
|||||||
@@ -132,8 +132,8 @@ INSERT INTO `chat_model` VALUES (1828324413241466880, '000000', 'vector', 'quent
|
|||||||
INSERT INTO `chat_model` VALUES (1828324413241466881, '000000', 'vector', 'baai/bge-m3', 'baai/bge-m3', 0.01, '2', '1', NULL, 'https://api.ppinfra.com/v3/openai', 'sk-xx', NULL, 103, 1, '2024-08-27 14:51:23', 1, '2025-05-24 17:33:11', 'BGE-M3 是一款具备多维度能力的文本嵌入模型,可同时实现密集检索、多向量检索和稀疏检索三大核心功能。该模型设计上兼容超过100种语言,并支持从短句到长达8192词元的长文本等多种输入形式。在跨语言检索任务中,BGE-M3展现出显著优势,其性能在MIRACL、MKQA等国际基准测试中位居前列。此外,针对长文档检索场景,该模型在MLDR、NarritiveQA等数据集上的表现同样达到行业领先水平。');
|
INSERT INTO `chat_model` VALUES (1828324413241466881, '000000', 'vector', 'baai/bge-m3', 'baai/bge-m3', 0.01, '2', '1', NULL, 'https://api.ppinfra.com/v3/openai', 'sk-xx', NULL, 103, 1, '2024-08-27 14:51:23', 1, '2025-05-24 17:33:11', 'BGE-M3 是一款具备多维度能力的文本嵌入模型,可同时实现密集检索、多向量检索和稀疏检索三大核心功能。该模型设计上兼容超过100种语言,并支持从短句到长达8192词元的长文本等多种输入形式。在跨语言检索任务中,BGE-M3展现出显著优势,其性能在MIRACL、MKQA等国际基准测试中位居前列。此外,针对长文档检索场景,该模型在MLDR、NarritiveQA等数据集上的表现同样达到行业领先水平。');
|
||||||
INSERT INTO `chat_model` VALUES (1859570229117022211, '000000', 'chat', 'deepseek/deepseek-v3-0324', 'deepseek/deepseek-v3-0324', 0.1, '1', '0', '', 'https://api.ppinfra.com/v3/openai/chat/completions', 'sk-xx', NULL, 103, 1, '2024-11-21 20:11:06', 1, '2025-05-24 17:56:22', 'DeepSeek V3 0324 是深度求索(DeepSeek)团队旗舰级对话模型系列的最新版本,采用混合专家(Mixture-of-Experts, MoE)架构,参数量达685B参数。');
|
INSERT INTO `chat_model` VALUES (1859570229117022211, '000000', 'chat', 'deepseek/deepseek-v3-0324', 'deepseek/deepseek-v3-0324', 0.1, '1', '0', '', 'https://api.ppinfra.com/v3/openai/chat/completions', 'sk-xx', NULL, 103, 1, '2024-11-21 20:11:06', 1, '2025-05-24 17:56:22', 'DeepSeek V3 0324 是深度求索(DeepSeek)团队旗舰级对话模型系列的最新版本,采用混合专家(Mixture-of-Experts, MoE)架构,参数量达685B参数。');
|
||||||
INSERT INTO `chat_model` VALUES (1859570229117022212, '000000', 'chat', 'deepseek/deepseek-r1', 'deepseek/deepseek-r1', 0.1, '1', '0', '', 'https://api.ppinfra.com/v3/openai/chat/completions', 'sk-xx', NULL, 103, 1, '2024-11-21 20:11:06', 1, '2025-05-24 17:56:14', 'DeepSeek R1是DeepSeek团队发布的最新开源模型,具备非常强悍的推理性能,尤其在数学、编程和推理任务上达到了与OpenAI的o1模型相当的水平。');
|
INSERT INTO `chat_model` VALUES (1859570229117022212, '000000', 'chat', 'deepseek/deepseek-r1', 'deepseek/deepseek-r1', 0.1, '1', '0', '', 'https://api.ppinfra.com/v3/openai/chat/completions', 'sk-xx', NULL, 103, 1, '2024-11-21 20:11:06', 1, '2025-05-24 17:56:14', 'DeepSeek R1是DeepSeek团队发布的最新开源模型,具备非常强悍的推理性能,尤其在数学、编程和推理任务上达到了与OpenAI的o1模型相当的水平。');
|
||||||
INSERT INTO `chat_model` VALUES (1926215622017482754, '000000', 'chat', 'gpt-4o-mini', 'gpt-4o-mini', 0.1, '1', '0', NULL, 'https://api.pandarobot.chat/v1/chat/completions/', 'sk-xx', NULL, 103, 1, '2025-05-24 17:56:06', 1, '2025-05-24 17:56:06', 'gpt 多模态模型');
|
INSERT INTO `chat_model` VALUES (1930184891812147202, '000000', 'image', 'qwen/qwen2.5-vl-72b-instruct', 'qwen/qwen2.5-vl-72b-instruct', 0.003, '2', '0', NULL, 'https://api.ppinfra.com/v3/openai/chat/completions', 'xx', NULL, 103, 1, '2025-06-04 16:48:34', 1, '2025-06-04 16:48:34', '视觉模型');
|
||||||
INSERT INTO `chat_model` VALUES (1926215622017482755, '000000', 'chat', 'gpt-4-all', 'gpt-4-all', 0.5, '2', '0', NULL, 'https://api.pandarobot.chat/v1/chat/completions/', 'sk-xx', NULL, 103, 1, '2025-05-24 17:56:06', 1, '2025-05-24 17:59:21', 'gpt 逆向多模态模型');
|
|
||||||
|
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Table structure for chat_pay_order
|
-- Table structure for chat_pay_order
|
||||||
|
|||||||
1
script/sql/update/20250605.sql
Normal file
1
script/sql/update/20250605.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
Reference in New Issue
Block a user