正在显示
6 个修改的文件
包含
66 行增加
和
39 行删除
| @@ -6,13 +6,16 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | @@ -6,13 +6,16 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 6 | import io.swagger.v3.oas.annotations.Operation; | 6 | import io.swagger.v3.oas.annotations.Operation; |
| 7 | import io.swagger.v3.oas.annotations.tags.Tag; | 7 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 8 | import lombok.extern.slf4j.Slf4j; | 8 | import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.apache.commons.lang3.StringUtils; | ||
| 9 | import org.apache.shiro.authz.annotation.RequiresPermissions; | 10 | import org.apache.shiro.authz.annotation.RequiresPermissions; |
| 10 | import org.jeecg.common.api.vo.Result; | 11 | import org.jeecg.common.api.vo.Result; |
| 11 | import org.jeecg.common.aspect.annotation.AutoLog; | 12 | import org.jeecg.common.aspect.annotation.AutoLog; |
| 12 | import org.jeecg.common.system.base.controller.JeecgController; | 13 | import org.jeecg.common.system.base.controller.JeecgController; |
| 13 | import org.jeecg.common.system.query.QueryGenerator; | 14 | import org.jeecg.common.system.query.QueryGenerator; |
| 15 | +import org.jeecg.modules.airag.airagchatsetting.entity.AiragChatsetting; | ||
| 14 | import org.jeecg.modules.airag.app.entity.AiragButton; | 16 | import org.jeecg.modules.airag.app.entity.AiragButton; |
| 15 | import org.jeecg.modules.airag.app.service.IAiragButtonService; | 17 | import org.jeecg.modules.airag.app.service.IAiragButtonService; |
| 18 | +import org.jeecg.modules.airag.app.service.RedisChatSetService; | ||
| 16 | import org.springframework.beans.factory.annotation.Autowired; | 19 | import org.springframework.beans.factory.annotation.Autowired; |
| 17 | import org.springframework.web.bind.annotation.*; | 20 | import org.springframework.web.bind.annotation.*; |
| 18 | import org.springframework.web.servlet.ModelAndView; | 21 | import org.springframework.web.servlet.ModelAndView; |
| @@ -35,6 +38,8 @@ import java.util.List; | @@ -35,6 +38,8 @@ import java.util.List; | ||
| 35 | public class AiragButtonController extends JeecgController<AiragButton, IAiragButtonService> { | 38 | public class AiragButtonController extends JeecgController<AiragButton, IAiragButtonService> { |
| 36 | @Autowired | 39 | @Autowired |
| 37 | private IAiragButtonService airagButtonService; | 40 | private IAiragButtonService airagButtonService; |
| 41 | + @Autowired | ||
| 42 | + private RedisChatSetService redisChatSetService; | ||
| 38 | 43 | ||
| 39 | /** | 44 | /** |
| 40 | * 分页列表查询 | 45 | * 分页列表查询 |
| @@ -67,12 +72,20 @@ public class AiragButtonController extends JeecgController<AiragButton, IAiragBu | @@ -67,12 +72,20 @@ public class AiragButtonController extends JeecgController<AiragButton, IAiragBu | ||
| 67 | @Operation(summary="按钮表单-智能助手按钮列表") | 72 | @Operation(summary="按钮表单-智能助手按钮列表") |
| 68 | @GetMapping(value = "/buttonList") | 73 | @GetMapping(value = "/buttonList") |
| 69 | public Result<List<AiragButton>> buttonList() { | 74 | public Result<List<AiragButton>> buttonList() { |
| 75 | + AiragChatsetting chatSetting = redisChatSetService.getChatSetting(); | ||
| 76 | + String buttonId = chatSetting.getButtonId(); | ||
| 77 | + if (StringUtils.isNotBlank(buttonId)) { | ||
| 78 | + String[] split = buttonId.split(","); | ||
| 70 | QueryWrapper<AiragButton> queryWrapper = new QueryWrapper<>(); | 79 | QueryWrapper<AiragButton> queryWrapper = new QueryWrapper<>(); |
| 71 | - queryWrapper.eq("button_switch", "Y"); | 80 | + queryWrapper.in("id", split); |
| 72 | List<AiragButton> list = airagButtonService.list(queryWrapper); | 81 | List<AiragButton> list = airagButtonService.list(queryWrapper); |
| 82 | + | ||
| 73 | return Result.OK(list); | 83 | return Result.OK(list); |
| 74 | } | 84 | } |
| 75 | 85 | ||
| 86 | + return Result.OK(); | ||
| 87 | + } | ||
| 88 | + | ||
| 76 | 89 | ||
| 77 | /** | 90 | /** |
| 78 | * 添加 | 91 | * 添加 |
| @@ -2,6 +2,7 @@ package org.jeecg.modules.airag.app.service; | @@ -2,6 +2,7 @@ package org.jeecg.modules.airag.app.service; | ||
| 2 | 2 | ||
| 3 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 3 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 4 | import org.jeecg.common.api.vo.Result; | 4 | import org.jeecg.common.api.vo.Result; |
| 5 | +import org.jeecg.modules.airag.airagchatsetting.entity.AiragChatsetting; | ||
| 5 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; | 6 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; |
| 6 | import org.springframework.web.multipart.MultipartFile; | 7 | import org.springframework.web.multipart.MultipartFile; |
| 7 | 8 | ||
| @@ -15,7 +16,7 @@ public interface IQuestionEmbeddingService { | @@ -15,7 +16,7 @@ public interface IQuestionEmbeddingService { | ||
| 15 | int update(QuestionEmbedding record); | 16 | int update(QuestionEmbedding record); |
| 16 | int removeByIds(List<String> ids); | 17 | int removeByIds(List<String> ids); |
| 17 | int deleteById(String id); | 18 | int deleteById(String id); |
| 18 | - List<QuestionEmbedding> similaritySearchByQuestion(String question, int limit, Double minSimilarity); | 19 | + List<QuestionEmbedding> similaritySearchByQuestion(String question, int limit, Double minSimilarity, AiragChatsetting chatSetting); |
| 19 | List<QuestionEmbedding> similaritySearch(float[] vector, int limit); | 20 | List<QuestionEmbedding> similaritySearch(float[] vector, int limit); |
| 20 | 21 | ||
| 21 | Result<?> processZipUpload(MultipartFile file, String knowledgeId); | 22 | Result<?> processZipUpload(MultipartFile file, String knowledgeId); |
| @@ -2,6 +2,7 @@ package org.jeecg.modules.airag.app.service; | @@ -2,6 +2,7 @@ package org.jeecg.modules.airag.app.service; | ||
| 2 | 2 | ||
| 3 | import lombok.extern.log4j.Log4j2; | 3 | import lombok.extern.log4j.Log4j2; |
| 4 | import org.jeecg.modules.airag.airagchatsetting.entity.AiragChatsetting; | 4 | import org.jeecg.modules.airag.airagchatsetting.entity.AiragChatsetting; |
| 5 | +import org.jeecg.modules.airag.airagchatsetting.service.IAiragChatsettingService; | ||
| 5 | import org.springframework.beans.factory.annotation.Autowired; | 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.data.redis.core.RedisTemplate; | 7 | import org.springframework.data.redis.core.RedisTemplate; |
| 7 | import org.springframework.stereotype.Service; | 8 | import org.springframework.stereotype.Service; |
| @@ -13,6 +14,8 @@ public class RedisChatSetService { | @@ -13,6 +14,8 @@ public class RedisChatSetService { | ||
| 13 | 14 | ||
| 14 | @Autowired | 15 | @Autowired |
| 15 | private RedisTemplate<String, Object> redisTemplate; | 16 | private RedisTemplate<String, Object> redisTemplate; |
| 17 | + @Autowired | ||
| 18 | + private IAiragChatsettingService airagChatsettingService; | ||
| 16 | 19 | ||
| 17 | // 存值 | 20 | // 存值 |
| 18 | public void saveChatSetting(AiragChatsetting setting) { | 21 | public void saveChatSetting(AiragChatsetting setting) { |
| @@ -23,6 +26,10 @@ public class RedisChatSetService { | @@ -23,6 +26,10 @@ public class RedisChatSetService { | ||
| 23 | // 取值 | 26 | // 取值 |
| 24 | public AiragChatsetting getChatSetting() { | 27 | public AiragChatsetting getChatSetting() { |
| 25 | AiragChatsetting chatsetting = (AiragChatsetting) redisTemplate.opsForValue().get(CHAT_SETTING_KEY); | 28 | AiragChatsetting chatsetting = (AiragChatsetting) redisTemplate.opsForValue().get(CHAT_SETTING_KEY); |
| 29 | + if (chatsetting == null) { | ||
| 30 | + chatsetting = airagChatsettingService.getOne(null); | ||
| 31 | + redisTemplate.opsForValue().set(CHAT_SETTING_KEY, chatsetting); | ||
| 32 | + } | ||
| 26 | log.info("获取配置成功:{}" , chatsetting); | 33 | log.info("获取配置成功:{}" , chatsetting); |
| 27 | return chatsetting; | 34 | return chatsetting; |
| 28 | } | 35 | } |
| @@ -4,9 +4,11 @@ import com.alibaba.fastjson2.JSONObject; | @@ -4,9 +4,11 @@ import com.alibaba.fastjson2.JSONObject; | ||
| 4 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 4 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 5 | import dev.langchain4j.data.embedding.Embedding; | 5 | import dev.langchain4j.data.embedding.Embedding; |
| 6 | import dev.langchain4j.model.output.Response; | 6 | import dev.langchain4j.model.output.Response; |
| 7 | +import org.jeecg.modules.airag.airagchatsetting.entity.AiragChatsetting; | ||
| 7 | import org.jeecg.modules.airag.app.entity.Embeddings; | 8 | import org.jeecg.modules.airag.app.entity.Embeddings; |
| 8 | import org.jeecg.modules.airag.app.mapper.PgVectorMapper; | 9 | import org.jeecg.modules.airag.app.mapper.PgVectorMapper; |
| 9 | import org.jeecg.modules.airag.app.service.IEmbeddingsService; | 10 | import org.jeecg.modules.airag.app.service.IEmbeddingsService; |
| 11 | +import org.jeecg.modules.airag.app.service.RedisChatSetService; | ||
| 10 | import org.jeecg.modules.airag.app.utils.AiModelUtils; | 12 | import org.jeecg.modules.airag.app.utils.AiModelUtils; |
| 11 | import org.postgresql.util.PGobject; | 13 | import org.postgresql.util.PGobject; |
| 12 | import org.springframework.beans.factory.annotation.Autowired; | 14 | import org.springframework.beans.factory.annotation.Autowired; |
| @@ -29,8 +31,8 @@ public class IEmbeddingsServiceImpl implements IEmbeddingsService { | @@ -29,8 +31,8 @@ public class IEmbeddingsServiceImpl implements IEmbeddingsService { | ||
| 29 | private PgVectorMapper pgVectorMapper; | 31 | private PgVectorMapper pgVectorMapper; |
| 30 | @Autowired | 32 | @Autowired |
| 31 | private AiModelUtils aiModelUtils; | 33 | private AiModelUtils aiModelUtils; |
| 32 | - @Value("${jeecg.ai-chat.embedId}") | ||
| 33 | - private String embedId; | 34 | + @Autowired |
| 35 | + private RedisChatSetService redisChatSetService; | ||
| 34 | 36 | ||
| 35 | @Override | 37 | @Override |
| 36 | public Page<Embeddings> findAll(Page<Embeddings> page, Embeddings embeddings) { | 38 | public Page<Embeddings> findAll(Page<Embeddings> page, Embeddings embeddings) { |
| @@ -47,8 +49,9 @@ public class IEmbeddingsServiceImpl implements IEmbeddingsService { | @@ -47,8 +49,9 @@ public class IEmbeddingsServiceImpl implements IEmbeddingsService { | ||
| 47 | } | 49 | } |
| 48 | 50 | ||
| 49 | public int insert(Embeddings record) { | 51 | public int insert(Embeddings record) { |
| 52 | + AiragChatsetting chatSetting = redisChatSetService.getChatSetting(); | ||
| 50 | record.setId(UUID.randomUUID().toString()); | 53 | record.setId(UUID.randomUUID().toString()); |
| 51 | - Response<Embedding> embedding = aiModelUtils.getEmbedding(embedId, record.getText()); | 54 | + Response<Embedding> embedding = aiModelUtils.getEmbedding(chatSetting.getEmbeddingId(), record.getText()); |
| 52 | record.setEmbedding(embedding.content().vector()); | 55 | record.setEmbedding(embedding.content().vector()); |
| 53 | return pgVectorMapper.insert(record); | 56 | return pgVectorMapper.insert(record); |
| 54 | } | 57 | } |
| @@ -65,7 +68,8 @@ public class IEmbeddingsServiceImpl implements IEmbeddingsService { | @@ -65,7 +68,8 @@ public class IEmbeddingsServiceImpl implements IEmbeddingsService { | ||
| 65 | jsonObject.setValue(mataData.toJSONString()); | 68 | jsonObject.setValue(mataData.toJSONString()); |
| 66 | 69 | ||
| 67 | record.setMetadata(mataData); | 70 | record.setMetadata(mataData); |
| 68 | - Response<Embedding> embedding = aiModelUtils.getEmbedding(embedId, record.getText()); | 71 | + AiragChatsetting chatSetting = redisChatSetService.getChatSetting(); |
| 72 | + Response<Embedding> embedding = aiModelUtils.getEmbedding(chatSetting.getEmbeddingId(), record.getText()); | ||
| 69 | 73 | ||
| 70 | record.setEmbedding(embedding.content().vector()); | 74 | record.setEmbedding(embedding.content().vector()); |
| 71 | 75 |
| 1 | package org.jeecg.modules.airag.app.service.impl; | 1 | package org.jeecg.modules.airag.app.service.impl; |
| 2 | 2 | ||
| 3 | -import com.baomidou.dynamic.datasource.annotation.DS; | ||
| 4 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 3 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 5 | -import com.fasterxml.jackson.core.JsonProcessingException; | ||
| 6 | import org.apache.commons.lang3.StringUtils; | 4 | import org.apache.commons.lang3.StringUtils; |
| 7 | -import org.apache.poi.hwpf.usermodel.CharacterRun; | ||
| 8 | -import org.apache.poi.hwpf.HWPFDocument; | ||
| 9 | -import org.apache.poi.hwpf.usermodel.Paragraph; | ||
| 10 | -import org.apache.poi.hwpf.usermodel.Range; | ||
| 11 | import dev.langchain4j.data.document.Document; | 5 | import dev.langchain4j.data.document.Document; |
| 12 | import dev.langchain4j.data.document.DocumentSplitter; | 6 | import dev.langchain4j.data.document.DocumentSplitter; |
| 13 | import dev.langchain4j.data.document.splitter.DocumentByParagraphSplitter; | 7 | import dev.langchain4j.data.document.splitter.DocumentByParagraphSplitter; |
| @@ -17,27 +11,20 @@ import dev.langchain4j.model.output.Response; | @@ -17,27 +11,20 @@ import dev.langchain4j.model.output.Response; | ||
| 17 | import org.apache.commons.io.FilenameUtils; | 11 | import org.apache.commons.io.FilenameUtils; |
| 18 | import org.apache.poi.xwpf.usermodel.*; | 12 | import org.apache.poi.xwpf.usermodel.*; |
| 19 | import org.jeecg.common.api.vo.Result; | 13 | import org.jeecg.common.api.vo.Result; |
| 14 | +import org.jeecg.modules.airag.airagchatsetting.entity.AiragChatsetting; | ||
| 20 | import org.jeecg.modules.airag.app.entity.Embeddings; | 15 | import org.jeecg.modules.airag.app.entity.Embeddings; |
| 21 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; | 16 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; |
| 22 | -import org.jeecg.modules.airag.app.mapper.EmbeddingsMapper; | ||
| 23 | import org.jeecg.modules.airag.app.mapper.PgVectorMapper; | 17 | import org.jeecg.modules.airag.app.mapper.PgVectorMapper; |
| 24 | import org.jeecg.modules.airag.app.mapper.QuestionEmbeddingMapper; | 18 | import org.jeecg.modules.airag.app.mapper.QuestionEmbeddingMapper; |
| 25 | import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService; | 19 | import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService; |
| 20 | +import org.jeecg.modules.airag.app.service.RedisChatSetService; | ||
| 26 | import org.jeecg.modules.airag.app.utils.AiModelUtils; | 21 | import org.jeecg.modules.airag.app.utils.AiModelUtils; |
| 27 | -import org.jeecg.modules.airag.common.handler.IAIChatHandler; | ||
| 28 | -import org.postgresql.util.PGobject; | ||
| 29 | import org.slf4j.Logger; | 22 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; | 23 | import org.slf4j.LoggerFactory; |
| 31 | import org.springframework.beans.factory.annotation.Autowired; | 24 | import org.springframework.beans.factory.annotation.Autowired; |
| 32 | import org.springframework.beans.factory.annotation.Value; | 25 | import org.springframework.beans.factory.annotation.Value; |
| 33 | import org.springframework.stereotype.Service; | 26 | import org.springframework.stereotype.Service; |
| 34 | import org.springframework.web.multipart.MultipartFile; | 27 | import org.springframework.web.multipart.MultipartFile; |
| 35 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 36 | -import com.pgvector.PGvector; | ||
| 37 | -import java.sql.Connection; | ||
| 38 | -import java.sql.DriverManager; | ||
| 39 | -import java.sql.PreparedStatement; | ||
| 40 | -import java.sql.SQLException; | ||
| 41 | 28 | ||
| 42 | import java.io.File; | 29 | import java.io.File; |
| 43 | import java.io.FileInputStream; | 30 | import java.io.FileInputStream; |
| @@ -69,8 +56,10 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | @@ -69,8 +56,10 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | ||
| 69 | 56 | ||
| 70 | @Value("${jeecg.upload.path}") | 57 | @Value("${jeecg.upload.path}") |
| 71 | private String uploadPath; | 58 | private String uploadPath; |
| 72 | - @Value("${jeecg.ai-chat.embedId}") | ||
| 73 | - private String embedId; | 59 | + @Autowired |
| 60 | + private RedisChatSetService redisChatSetService; | ||
| 61 | +// @Value("${jeecg.ai-chat.embedId}") | ||
| 62 | +// private String embedId; | ||
| 74 | 63 | ||
| 75 | private static final Set<String> ALLOWED_EXTENSIONS = Set.of("txt", "doc", "docx"); | 64 | private static final Set<String> ALLOWED_EXTENSIONS = Set.of("txt", "doc", "docx"); |
| 76 | private static final Pattern UUID_PATTERN = Pattern.compile("_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"); | 65 | private static final Pattern UUID_PATTERN = Pattern.compile("_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"); |
| @@ -95,7 +84,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | @@ -95,7 +84,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | ||
| 95 | @Override | 84 | @Override |
| 96 | public int insert(QuestionEmbedding record) { | 85 | public int insert(QuestionEmbedding record) { |
| 97 | if (StringUtils.isNotBlank(record.getQuestion())){ | 86 | if (StringUtils.isNotBlank(record.getQuestion())){ |
| 98 | - Response<Embedding> embedding = aiModelUtils.getEmbedding(embedId, record.getQuestion()); | 87 | + AiragChatsetting chatSetting = redisChatSetService.getChatSetting(); |
| 88 | + Response<Embedding> embedding = aiModelUtils.getEmbedding(chatSetting.getEmbeddingId(), record.getQuestion()); | ||
| 99 | record.setEmbedding(embedding.content().vector()); | 89 | record.setEmbedding(embedding.content().vector()); |
| 100 | } | 90 | } |
| 101 | 91 | ||
| @@ -106,7 +96,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | @@ -106,7 +96,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | ||
| 106 | @Override | 96 | @Override |
| 107 | public int update(QuestionEmbedding record) { | 97 | public int update(QuestionEmbedding record) { |
| 108 | if (StringUtils.isNotBlank(record.getQuestion())){ | 98 | if (StringUtils.isNotBlank(record.getQuestion())){ |
| 109 | - Response<Embedding> embedding = aiModelUtils.getEmbedding(embedId, record.getQuestion()); | 99 | + AiragChatsetting chatSetting = redisChatSetService.getChatSetting(); |
| 100 | + Response<Embedding> embedding = aiModelUtils.getEmbedding(chatSetting.getEmbeddingId(), record.getQuestion()); | ||
| 110 | record.setEmbedding(embedding.content().vector()); | 101 | record.setEmbedding(embedding.content().vector()); |
| 111 | } | 102 | } |
| 112 | return questionEmbeddingMapper.update(record); | 103 | return questionEmbeddingMapper.update(record); |
| @@ -123,8 +114,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | @@ -123,8 +114,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | ||
| 123 | } | 114 | } |
| 124 | 115 | ||
| 125 | @Override | 116 | @Override |
| 126 | - public List<QuestionEmbedding> similaritySearchByQuestion(String question, int limit, Double minSimilarity) { | ||
| 127 | - Response<Embedding> embedding = aiModelUtils.getEmbedding(embedId, question); | 117 | + public List<QuestionEmbedding> similaritySearchByQuestion(String question, int limit, Double minSimilarity, AiragChatsetting chatSetting) { |
| 118 | + Response<Embedding> embedding = aiModelUtils.getEmbedding(chatSetting.getEmbeddingId(), question); | ||
| 128 | return questionEmbeddingMapper.similaritySearchByQuestion(embedding.content().vector(), limit, minSimilarity); | 119 | return questionEmbeddingMapper.similaritySearchByQuestion(embedding.content().vector(), limit, minSimilarity); |
| 129 | } | 120 | } |
| 130 | 121 | ||
| @@ -202,7 +193,7 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | @@ -202,7 +193,7 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | ||
| 202 | } | 193 | } |
| 203 | private void saveToEmbeddingsTable(List<String> segments, String originalFileName, String storedFileName, String knowledgeId) { | 194 | private void saveToEmbeddingsTable(List<String> segments, String originalFileName, String storedFileName, String knowledgeId) { |
| 204 | if (segments.isEmpty()) return; | 195 | if (segments.isEmpty()) return; |
| 205 | - | 196 | + AiragChatsetting chatSetting = redisChatSetService.getChatSetting(); |
| 206 | String displayFileName = removeUuidSuffix(originalFileName); | 197 | String displayFileName = removeUuidSuffix(originalFileName); |
| 207 | displayFileName = FilenameUtils.removeExtension(displayFileName); | 198 | displayFileName = FilenameUtils.removeExtension(displayFileName); |
| 208 | 199 | ||
| @@ -218,7 +209,7 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | @@ -218,7 +209,7 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | ||
| 218 | String answer = segment.trim(); | 209 | String answer = segment.trim(); |
| 219 | 210 | ||
| 220 | // 获取 embedding | 211 | // 获取 embedding |
| 221 | - Response<Embedding> embeddingResponse = aiModelUtils.getEmbedding(embedId, answer); | 212 | + Response<Embedding> embeddingResponse = aiModelUtils.getEmbedding(chatSetting.getEmbeddingId(), answer); |
| 222 | float[] embeddingVector = embeddingResponse.content().vector(); | 213 | float[] embeddingVector = embeddingResponse.content().vector(); |
| 223 | 214 | ||
| 224 | Map<String, Object> metadata = new HashMap<>(); | 215 | Map<String, Object> metadata = new HashMap<>(); |
| @@ -390,8 +381,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | @@ -390,8 +381,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | ||
| 390 | 381 | ||
| 391 | 382 | ||
| 392 | log.info("保存分段: title={}, content_length={}", question, segment.length()); | 383 | log.info("保存分段: title={}, content_length={}", question, segment.length()); |
| 393 | - | ||
| 394 | - Response<Embedding> embeddingResponse = aiModelUtils.getEmbedding(embedId, record.getQuestion()); | 384 | + AiragChatsetting chatSetting = redisChatSetService.getChatSetting(); |
| 385 | + Response<Embedding> embeddingResponse = aiModelUtils.getEmbedding(chatSetting.getEmbeddingId(), record.getQuestion()); | ||
| 395 | record.setEmbedding(embeddingResponse.content().vector()); | 386 | record.setEmbedding(embeddingResponse.content().vector()); |
| 396 | record.setKnowledgeId(knowledgeId); | 387 | record.setKnowledgeId(knowledgeId); |
| 397 | insert(record); | 388 | insert(record); |
| @@ -8,10 +8,12 @@ import dev.langchain4j.data.message.UserMessage; | @@ -8,10 +8,12 @@ import dev.langchain4j.data.message.UserMessage; | ||
| 8 | import dev.langchain4j.service.TokenStream; | 8 | import dev.langchain4j.service.TokenStream; |
| 9 | import lombok.extern.log4j.Log4j2; | 9 | import lombok.extern.log4j.Log4j2; |
| 10 | import org.apache.commons.lang3.StringUtils; | 10 | import org.apache.commons.lang3.StringUtils; |
| 11 | +import org.jeecg.modules.airag.airagchatsetting.entity.AiragChatsetting; | ||
| 11 | import org.jeecg.modules.airag.app.entity.AiragLog; | 12 | import org.jeecg.modules.airag.app.entity.AiragLog; |
| 12 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; | 13 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; |
| 13 | import org.jeecg.modules.airag.app.service.IAiragLogService; | 14 | import org.jeecg.modules.airag.app.service.IAiragLogService; |
| 14 | import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService; | 15 | import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService; |
| 16 | +import org.jeecg.modules.airag.app.service.RedisChatSetService; | ||
| 15 | import org.jeecg.modules.airag.app.utils.FileToBase64Util; | 17 | import org.jeecg.modules.airag.app.utils.FileToBase64Util; |
| 16 | import org.jeecg.modules.airag.common.handler.IAIChatHandler; | 18 | import org.jeecg.modules.airag.common.handler.IAIChatHandler; |
| 17 | import org.jeecg.modules.airag.llm.handler.EmbeddingHandler; | 19 | import org.jeecg.modules.airag.llm.handler.EmbeddingHandler; |
| @@ -39,21 +41,26 @@ public class AiragResponseServiceImpl implements AiragResponseService { | @@ -39,21 +41,26 @@ public class AiragResponseServiceImpl implements AiragResponseService { | ||
| 39 | private String uploadPath; | 41 | private String uploadPath; |
| 40 | @Autowired | 42 | @Autowired |
| 41 | private IAiragLogService airagLogService; | 43 | private IAiragLogService airagLogService; |
| 44 | + @Autowired | ||
| 45 | + private RedisChatSetService redisChatSetService; | ||
| 42 | 46 | ||
| 43 | @Override | 47 | @Override |
| 44 | public SseEmitter handleStreamRequest(String questionText, String code, Integer codeType, String user) { | 48 | public SseEmitter handleStreamRequest(String questionText, String code, Integer codeType, String user) { |
| 49 | + AiragChatsetting chatSetting = redisChatSetService.getChatSetting(); | ||
| 50 | + | ||
| 51 | + | ||
| 45 | SseEmitter emitter = new SseEmitter(300000L); | 52 | SseEmitter emitter = new SseEmitter(300000L); |
| 46 | - String modelId = "1926875898187878401"; | 53 | + String modelId = chatSetting.getLlmId(); |
| 47 | AiragLog logRecord = createLogRecord(questionText, code, codeType, user, modelId); | 54 | AiragLog logRecord = createLogRecord(questionText, code, codeType, user, modelId); |
| 48 | String cleanedQuestionText = cleanQuestionText(questionText); | 55 | String cleanedQuestionText = cleanQuestionText(questionText); |
| 49 | try { | 56 | try { |
| 50 | // 处理问题库匹配 | 57 | // 处理问题库匹配 |
| 51 | - if (handleQuestionEmbeddingMatch(emitter, cleanedQuestionText, logRecord)) { | 58 | + if (handleQuestionEmbeddingMatch(emitter, cleanedQuestionText, logRecord,chatSetting)) { |
| 52 | return emitter; | 59 | return emitter; |
| 53 | } | 60 | } |
| 54 | 61 | ||
| 55 | // 处理知识库搜索 | 62 | // 处理知识库搜索 |
| 56 | - handleKnowledgeBaseSearch(emitter, cleanedQuestionText, logRecord, modelId); | 63 | + handleKnowledgeBaseSearch(emitter, cleanedQuestionText, logRecord, modelId,chatSetting); |
| 57 | } catch (Exception e) { | 64 | } catch (Exception e) { |
| 58 | handleError(emitter, logRecord, e); | 65 | handleError(emitter, logRecord, e); |
| 59 | } | 66 | } |
| @@ -101,13 +108,15 @@ public class AiragResponseServiceImpl implements AiragResponseService { | @@ -101,13 +108,15 @@ public class AiragResponseServiceImpl implements AiragResponseService { | ||
| 101 | 108 | ||
| 102 | /** | 109 | /** |
| 103 | * 匹配问题库 | 110 | * 匹配问题库 |
| 111 | + * | ||
| 104 | * @param emitter 流式返回 | 112 | * @param emitter 流式返回 |
| 105 | * @param questionText 问题原文本 | 113 | * @param questionText 问题原文本 |
| 106 | * @param logRecord 日志对象 | 114 | * @param logRecord 日志对象 |
| 115 | + * @param chatSetting | ||
| 107 | * @return 返回是否匹配成功 | 116 | * @return 返回是否匹配成功 |
| 108 | */ | 117 | */ |
| 109 | - private boolean handleQuestionEmbeddingMatch(SseEmitter emitter, String questionText, AiragLog logRecord) throws Exception { | ||
| 110 | - List<QuestionEmbedding> questionEmbeddings = questionEmbeddingService.similaritySearchByQuestion(questionText, 1, 0.8); | 118 | + private boolean handleQuestionEmbeddingMatch(SseEmitter emitter, String questionText, AiragLog logRecord, AiragChatsetting chatSetting) throws Exception { |
| 119 | + List<QuestionEmbedding> questionEmbeddings = questionEmbeddingService.similaritySearchByQuestion(questionText, 1, 0.8, chatSetting); | ||
| 111 | if (questionEmbeddings.isEmpty()) { | 120 | if (questionEmbeddings.isEmpty()) { |
| 112 | return false; | 121 | return false; |
| 113 | } | 122 | } |
| @@ -140,13 +149,15 @@ public class AiragResponseServiceImpl implements AiragResponseService { | @@ -140,13 +149,15 @@ public class AiragResponseServiceImpl implements AiragResponseService { | ||
| 140 | 149 | ||
| 141 | /** | 150 | /** |
| 142 | * 知识库匹配 | 151 | * 知识库匹配 |
| 152 | + * | ||
| 143 | * @param emitter 流式返回 | 153 | * @param emitter 流式返回 |
| 144 | * @param questionText 问题原文本 | 154 | * @param questionText 问题原文本 |
| 145 | * @param logRecord 日志对象 | 155 | * @param logRecord 日志对象 |
| 146 | * @param modelId 模型id | 156 | * @param modelId 模型id |
| 157 | + * @param chatSetting | ||
| 147 | */ | 158 | */ |
| 148 | - private void handleKnowledgeBaseSearch(SseEmitter emitter, String questionText, AiragLog logRecord, String modelId) throws Exception { | ||
| 149 | - String knowId = "1926872137990148098"; | 159 | + private void handleKnowledgeBaseSearch(SseEmitter emitter, String questionText, AiragLog logRecord, String modelId, AiragChatsetting chatSetting) throws Exception { |
| 160 | + String knowId =chatSetting.getKnowledgeId(); | ||
| 150 | List<Map<String, Object>> maps = embeddingHandler.searchEmbedding(knowId, questionText, 2, 0.78); | 161 | List<Map<String, Object>> maps = embeddingHandler.searchEmbedding(knowId, questionText, 2, 0.78); |
| 151 | 162 | ||
| 152 | if (CollectionUtil.isEmpty(maps)) { | 163 | if (CollectionUtil.isEmpty(maps)) { |
| @@ -159,7 +170,7 @@ public class AiragResponseServiceImpl implements AiragResponseService { | @@ -159,7 +170,7 @@ public class AiragResponseServiceImpl implements AiragResponseService { | ||
| 159 | String fileName = generateFileDocName(firstMatch.get("metadata").toString()); | 170 | String fileName = generateFileDocName(firstMatch.get("metadata").toString()); |
| 160 | String storedFileName = generateFilePath(firstMatch.get("metadata").toString()); | 171 | String storedFileName = generateFilePath(firstMatch.get("metadata").toString()); |
| 161 | 172 | ||
| 162 | - String systemPrompt = createSystemPrompt(); | 173 | + String systemPrompt = chatSetting.getPrompt(); |
| 163 | String userPrompt = createUserPrompt(questionText, content); | 174 | String userPrompt = createUserPrompt(questionText, content); |
| 164 | 175 | ||
| 165 | processLLMResponse(emitter, logRecord, modelId, systemPrompt, userPrompt, firstMatch, fileName, storedFileName); | 176 | processLLMResponse(emitter, logRecord, modelId, systemPrompt, userPrompt, firstMatch, fileName, storedFileName); |
-
请 注册 或 登录 后发表评论