正在显示
12 个修改的文件
包含
204 行增加
和
47 行删除
| @@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | @@ -6,6 +6,7 @@ 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; |
| @@ -62,7 +63,15 @@ public class AiragLogController extends JeecgController<AiragLog, IAiragLogServi | @@ -62,7 +63,15 @@ public class AiragLogController extends JeecgController<AiragLog, IAiragLogServi | ||
| 62 | HttpServletRequest req) { | 63 | HttpServletRequest req) { |
| 63 | QueryWrapper<AiragLog> queryWrapper = QueryGenerator.initQueryWrapper(airagLog, req.getParameterMap()); | 64 | QueryWrapper<AiragLog> queryWrapper = QueryGenerator.initQueryWrapper(airagLog, req.getParameterMap()); |
| 64 | Page<AiragLog> page = new Page<AiragLog>(pageNo, pageSize); | 65 | Page<AiragLog> page = new Page<AiragLog>(pageNo, pageSize); |
| 65 | - IPage<AiragLog> pageList = airagLogService.page(page, queryWrapper); | 66 | +// IPage<AiragLog> pageList = airagLogService.page(page, queryWrapper); |
| 67 | + IPage<AiragLog> pageList = airagLogService.pageList(airagLog,page); | ||
| 68 | + /*if(airagLog != null && StringUtils.isNotBlank(airagLog.getName())) { | ||
| 69 | + QueryWrapper<AiragModel> queryWrapper2 = new QueryWrapper<>(); | ||
| 70 | + queryWrapper2.eq("model_id", airagLog.getName()); | ||
| 71 | + IPage<AiragLog> page1 = airagLogService.list1(airagLog,page); | ||
| 72 | + return Result.ok(page1); | ||
| 73 | + }*/ | ||
| 74 | + | ||
| 66 | List<AiragModel> list = airagModelService.list(); | 75 | List<AiragModel> list = airagModelService.list(); |
| 67 | Map<String,String> nameMap = new HashMap<String,String>(); | 76 | Map<String,String> nameMap = new HashMap<String,String>(); |
| 68 | for(AiragModel airagModel : list){ | 77 | for(AiragModel airagModel : list){ |
| @@ -131,14 +131,14 @@ public class EmbeddingsController { | @@ -131,14 +131,14 @@ public class EmbeddingsController { | ||
| 131 | @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST}) | 131 | @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST}) |
| 132 | public Result<String> edit(@RequestBody Embeddings embeddings) { | 132 | public Result<String> edit(@RequestBody Embeddings embeddings) { |
| 133 | // embeddingsService.updateById(Embeddings); | 133 | // embeddingsService.updateById(Embeddings); |
| 134 | - Map<String, Object> metadata = embeddings.getMetadata(); | ||
| 135 | - SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator(); | ||
| 136 | - metadata.put("docName", embeddings.getDocName()); | ||
| 137 | - String docId = String.valueOf(snowflakeGenerator.next()); | ||
| 138 | - metadata.put("docId", docId); // 自动生成唯一文档ID | ||
| 139 | - metadata.put("index", "0"); | ||
| 140 | - // 2. 设置到embeddings对象 | ||
| 141 | - embeddings.setMetadata(metadata); | 134 | +// Map<String, Object> metadata = embeddings.getMetadata(); |
| 135 | +// SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator(); | ||
| 136 | +// metadata.put("docName", embeddings.getDocName()); | ||
| 137 | +// String docId = String.valueOf(snowflakeGenerator.next()); | ||
| 138 | +// metadata.put("docId", docId); // 自动生成唯一文档ID | ||
| 139 | +// metadata.put("index", "0"); | ||
| 140 | +// // 2. 设置到embeddings对象 | ||
| 141 | +// embeddings.setMetadata(metadata); | ||
| 142 | embeddingsService.update(embeddings); | 142 | embeddingsService.update(embeddings); |
| 143 | return Result.OK("编辑成功!"); | 143 | return Result.OK("编辑成功!"); |
| 144 | } | 144 | } |
| 1 | package org.jeecg.modules.airag.app.controller; | 1 | package org.jeecg.modules.airag.app.controller; |
| 2 | 2 | ||
| 3 | +import dev.langchain4j.internal.Json; | ||
| 3 | import lombok.extern.slf4j.Slf4j; | 4 | import lombok.extern.slf4j.Slf4j; |
| 4 | import org.apache.commons.lang3.StringUtils; | 5 | import org.apache.commons.lang3.StringUtils; |
| 5 | import org.jeecg.common.api.vo.Result; | 6 | import org.jeecg.common.api.vo.Result; |
| 7 | +import org.jeecg.modules.airag.app.entity.AiragLog; | ||
| 6 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; | 8 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; |
| 7 | import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService; | 9 | import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService; |
| 8 | import org.jeecg.modules.airag.app.utils.JsonUtils; | 10 | import org.jeecg.modules.airag.app.utils.JsonUtils; |
| 9 | import org.jeecg.modules.airag.llm.entity.AiragKnowledge; | 11 | import org.jeecg.modules.airag.llm.entity.AiragKnowledge; |
| 12 | +import org.jeecg.modules.airag.llm.entity.AiragModel; | ||
| 10 | import org.jeecg.modules.airag.llm.service.IAiragKnowledgeService; | 13 | import org.jeecg.modules.airag.llm.service.IAiragKnowledgeService; |
| 11 | import org.springframework.beans.factory.annotation.Autowired; | 14 | import org.springframework.beans.factory.annotation.Autowired; |
| 12 | import org.springframework.transaction.annotation.Transactional; | 15 | import org.springframework.transaction.annotation.Transactional; |
| 13 | import org.springframework.web.bind.annotation.*; | 16 | import org.springframework.web.bind.annotation.*; |
| 14 | import org.springframework.web.multipart.MultipartFile; | 17 | import org.springframework.web.multipart.MultipartFile; |
| 15 | 18 | ||
| 19 | +import java.util.HashMap; | ||
| 16 | import java.util.List; | 20 | import java.util.List; |
| 17 | import java.util.Map; | 21 | import java.util.Map; |
| 18 | import java.util.stream.Collectors; | 22 | import java.util.stream.Collectors; |
| @@ -27,8 +31,8 @@ public class QuestionEmbeddingController { | @@ -27,8 +31,8 @@ public class QuestionEmbeddingController { | ||
| 27 | private IAiragKnowledgeService airagKnowledgeService; | 31 | private IAiragKnowledgeService airagKnowledgeService; |
| 28 | 32 | ||
| 29 | @GetMapping("/list") | 33 | @GetMapping("/list") |
| 30 | - public Result<List<QuestionEmbedding>> findAll() { | ||
| 31 | - List<QuestionEmbedding> list = questionEmbeddingService.findAll(); | 34 | + public Result<List<QuestionEmbedding>> findAll(QuestionEmbedding questionEmbedding) { |
| 35 | + List<QuestionEmbedding> list = questionEmbeddingService.findAll(questionEmbedding); | ||
| 32 | Map<String, String> airagKnowledgeMap = airagKnowledgeService.list() | 36 | Map<String, String> airagKnowledgeMap = airagKnowledgeService.list() |
| 33 | .stream() | 37 | .stream() |
| 34 | .collect(Collectors.toMap(AiragKnowledge::getId, AiragKnowledge::getName)); | 38 | .collect(Collectors.toMap(AiragKnowledge::getId, AiragKnowledge::getName)); |
| @@ -44,6 +48,7 @@ public class QuestionEmbeddingController { | @@ -44,6 +48,7 @@ public class QuestionEmbeddingController { | ||
| 44 | } | 48 | } |
| 45 | 49 | ||
| 46 | }); | 50 | }); |
| 51 | + | ||
| 47 | return Result.OK(list); | 52 | return Result.OK(list); |
| 48 | } | 53 | } |
| 49 | 54 | ||
| @@ -58,12 +63,47 @@ public class QuestionEmbeddingController { | @@ -58,12 +63,47 @@ public class QuestionEmbeddingController { | ||
| 58 | 63 | ||
| 59 | @PostMapping("/add") | 64 | @PostMapping("/add") |
| 60 | public Result<String> insert(@RequestBody QuestionEmbedding record) { | 65 | public Result<String> insert(@RequestBody QuestionEmbedding record) { |
| 66 | + Map<String, String> airagKnowledgeMap = airagKnowledgeService.list() | ||
| 67 | + .stream() | ||
| 68 | + .collect(Collectors.toMap(AiragKnowledge::getId, AiragKnowledge::getName)); | ||
| 69 | + | ||
| 70 | + // 处理knowledgeId和knowledgeName的赋值 | ||
| 71 | + String knowledgeId = record.getKnowledgeId(); | ||
| 72 | + if (StringUtils.isNotBlank(knowledgeId)) { | ||
| 73 | + String knowledgeName = airagKnowledgeMap.get(record.getKnowledgeId()); | ||
| 74 | + record.setKnowledgeName(knowledgeName); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + | ||
| 61 | int result = questionEmbeddingService.insert(record); | 78 | int result = questionEmbeddingService.insert(record); |
| 62 | return result > 0 ? Result.OK("添加成功!") : Result.error("添加失败"); | 79 | return result > 0 ? Result.OK("添加成功!") : Result.error("添加失败"); |
| 63 | } | 80 | } |
| 64 | 81 | ||
| 65 | @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST}) | 82 | @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST}) |
| 66 | public Result<String> update(@RequestBody QuestionEmbedding record) { | 83 | public Result<String> update(@RequestBody QuestionEmbedding record) { |
| 84 | + //获取matadata | ||
| 85 | + QuestionEmbedding existRecord = questionEmbeddingService.findById(record.getId()); | ||
| 86 | + | ||
| 87 | + | ||
| 88 | + Map<String, String> airagKnowledgeMap = airagKnowledgeService.list() | ||
| 89 | + .stream() | ||
| 90 | + .collect(Collectors.toMap(AiragKnowledge::getId, AiragKnowledge::getName)); | ||
| 91 | + | ||
| 92 | + // 处理knowledgeId和knowledgeName的赋值 | ||
| 93 | + String knowledgeId = record.getKnowledgeId(); | ||
| 94 | + if (StringUtils.isNotBlank(knowledgeId)) { | ||
| 95 | + String knowledgeName = airagKnowledgeMap.get(record.getKnowledgeId()); | ||
| 96 | + record.setKnowledgeName(knowledgeName); | ||
| 97 | + | ||
| 98 | + String existMetadata = existRecord.getMetadata(); | ||
| 99 | + Map<String, String> jsonMap = new HashMap<>(); | ||
| 100 | + if (StringUtils.isNotBlank(existMetadata)) { | ||
| 101 | + jsonMap = JsonUtils.jsonUtils(existMetadata); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + jsonMap.put("knowledgeId", record.getKnowledgeId()); | ||
| 105 | + record.setMetadata(Json.toJson(jsonMap)); | ||
| 106 | + } | ||
| 67 | int result = questionEmbeddingService.update(record); | 107 | int result = questionEmbeddingService.update(record); |
| 68 | return result > 0 ? Result.OK("编辑成功!") : Result.error("编辑失败"); | 108 | return result > 0 ? Result.OK("编辑成功!") : Result.error("编辑失败"); |
| 69 | } | 109 | } |
| 1 | package org.jeecg.modules.airag.app.mapper; | 1 | package org.jeecg.modules.airag.app.mapper; |
| 2 | 2 | ||
| 3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; | 3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| 4 | +import com.baomidou.mybatisplus.core.metadata.IPage; | ||
| 5 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 6 | +import io.lettuce.core.dynamic.annotation.Param; | ||
| 4 | import org.jeecg.modules.airag.app.entity.AiragLog; | 7 | import org.jeecg.modules.airag.app.entity.AiragLog; |
| 5 | 8 | ||
| 9 | +import java.util.List; | ||
| 10 | + | ||
| 6 | /** | 11 | /** |
| 7 | * @Description: 日志管理 | 12 | * @Description: 日志管理 |
| 8 | * @Author: jeecg-boot | 13 | * @Author: jeecg-boot |
| @@ -10,5 +15,7 @@ import org.jeecg.modules.airag.app.entity.AiragLog; | @@ -10,5 +15,7 @@ import org.jeecg.modules.airag.app.entity.AiragLog; | ||
| 10 | * @Version: V1.0 | 15 | * @Version: V1.0 |
| 11 | */ | 16 | */ |
| 12 | public interface AiragLogMapper extends BaseMapper<AiragLog> { | 17 | public interface AiragLogMapper extends BaseMapper<AiragLog> { |
| 18 | + IPage<AiragLog> list1(@Param("param1")AiragLog airagLog, Page<AiragLog> page); | ||
| 13 | 19 | ||
| 20 | + IPage<AiragLog> pageList(@Param("param1") AiragLog airagLog, Page<AiragLog> page); | ||
| 14 | } | 21 | } |
| 1 | package org.jeecg.modules.airag.app.mapper; | 1 | package org.jeecg.modules.airag.app.mapper; |
| 2 | 2 | ||
| 3 | +import ch.qos.logback.core.net.SyslogOutputStream; | ||
| 3 | import cn.hutool.core.lang.generator.SnowflakeGenerator; | 4 | import cn.hutool.core.lang.generator.SnowflakeGenerator; |
| 4 | import com.alibaba.fastjson2.JSONObject; | 5 | import com.alibaba.fastjson2.JSONObject; |
| 5 | import com.fasterxml.jackson.core.JsonProcessingException; | 6 | import com.fasterxml.jackson.core.JsonProcessingException; |
| @@ -32,26 +33,36 @@ public class PgVectorMapper { | @@ -32,26 +33,36 @@ public class PgVectorMapper { | ||
| 32 | // 查询所有向量记录 | 33 | // 查询所有向量记录 |
| 33 | public List<Embeddings> findAll(Embeddings embeddings) { | 34 | public List<Embeddings> findAll(Embeddings embeddings) { |
| 34 | List<Embeddings> results = new ArrayList<>(); | 35 | List<Embeddings> results = new ArrayList<>(); |
| 35 | - String sql = "SELECT * FROM embeddings WHERE 1 =1 "; | ||
| 36 | - if (StringUtils.isNotBlank(embeddings.getId())){ | ||
| 37 | - sql += " and embedding_id = '" + embeddings.getId() + "'"; | ||
| 38 | - } | 36 | + StringBuilder sql = new StringBuilder("SELECT * FROM embeddings WHERE 1=1"); |
| 37 | + List<Object> params = new ArrayList<>(); // 存储参数值 | ||
| 39 | 38 | ||
| 40 | - if (StringUtils.isNotBlank(embeddings.getKnowledgeId())){ | ||
| 41 | - sql += " and metadata ->> 'knowledgeId' = '" + embeddings.getKnowledgeId() + "'"; | 39 | + // 动态构建查询条件 |
| 40 | + if (StringUtils.isNotBlank(embeddings.getKnowledgeId())) { | ||
| 41 | + sql.append(" AND metadata ->> 'knowledgeId' = ?"); | ||
| 42 | + params.add(embeddings.getKnowledgeId()); | ||
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | - if(StringUtils.isNotBlank(embeddings.getText())){ | ||
| 45 | - sql += " and text = '" + embeddings.getText() + "'"; | 45 | + if (StringUtils.isNotBlank(embeddings.getText())) { |
| 46 | + sql.append(" AND text ILIKE ?"); // 使用 ILIKE 进行不区分大小写的模糊匹配 | ||
| 47 | + params.add("%" + embeddings.getText() + "%"); | ||
| 46 | } | 48 | } |
| 47 | - System.out.println("sql = " + sql); | 49 | + |
| 50 | + | ||
| 51 | + System.out.println("SQL: " + sql.toString()); | ||
| 52 | + | ||
| 48 | try (Connection conn = getConnection(); | 53 | try (Connection conn = getConnection(); |
| 49 | - PreparedStatement stmt = conn.prepareStatement(sql); | ||
| 50 | - ResultSet rs = stmt.executeQuery()) { | 54 | + PreparedStatement stmt = conn.prepareStatement(sql.toString())) { |
| 51 | 55 | ||
| 56 | + // 设置参数值 | ||
| 57 | + for (int i = 0; i < params.size(); i++) { | ||
| 58 | + stmt.setObject(i + 1, params.get(i)); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + try (ResultSet rs = stmt.executeQuery()) { | ||
| 52 | while (rs.next()) { | 62 | while (rs.next()) { |
| 53 | results.add(mapRowToEmbeddings(rs)); | 63 | results.add(mapRowToEmbeddings(rs)); |
| 54 | } | 64 | } |
| 65 | + } | ||
| 55 | } catch (SQLException e) { | 66 | } catch (SQLException e) { |
| 56 | log.error("查询所有向量记录失败", e); | 67 | log.error("查询所有向量记录失败", e); |
| 57 | throw new RuntimeException("查询向量数据时发生数据库错误", e); | 68 | throw new RuntimeException("查询向量数据时发生数据库错误", e); |
| @@ -81,7 +92,7 @@ public class PgVectorMapper { | @@ -81,7 +92,7 @@ public class PgVectorMapper { | ||
| 81 | 92 | ||
| 82 | // 插入新向量记录 | 93 | // 插入新向量记录 |
| 83 | public int insert(Embeddings record) { | 94 | public int insert(Embeddings record) { |
| 84 | - /* Map<String, Object> metadata = new LinkedHashMap<>(); | 95 | + /*Map<String, Object> metadata = new LinkedHashMap<>(); |
| 85 | 96 | ||
| 86 | // 按固定顺序添加字段 | 97 | // 按固定顺序添加字段 |
| 87 | metadata.put("docId", UUID.randomUUID().toString()); | 98 | metadata.put("docId", UUID.randomUUID().toString()); |
| @@ -89,10 +100,10 @@ public class PgVectorMapper { | @@ -89,10 +100,10 @@ public class PgVectorMapper { | ||
| 89 | metadata.put("docName", record.getDocName()); | 100 | metadata.put("docName", record.getDocName()); |
| 90 | metadata.put("index", 0); // 确保是整数 | 101 | metadata.put("index", 0); // 确保是整数 |
| 91 | record.setMetadata(metadata); | 102 | record.setMetadata(metadata); |
| 92 | - | 103 | +*/ |
| 93 | // 自动生成向量(这里需要调用嵌入模型) | 104 | // 自动生成向量(这里需要调用嵌入模型) |
| 94 | float[] embedding = generateEmbedding(record.getText()); | 105 | float[] embedding = generateEmbedding(record.getText()); |
| 95 | - record.setEmbedding(embedding);*/ | 106 | + record.setEmbedding(embedding); |
| 96 | 107 | ||
| 97 | 108 | ||
| 98 | String sql = "INSERT INTO embeddings (embedding_id, embedding, text, metadata) VALUES (?, ?, ?, ?::jsonb)"; | 109 | String sql = "INSERT INTO embeddings (embedding_id, embedding, text, metadata) VALUES (?, ?, ?, ?::jsonb)"; |
| @@ -123,10 +134,21 @@ public class PgVectorMapper { | @@ -123,10 +134,21 @@ public class PgVectorMapper { | ||
| 123 | JSONObject mataData = new JSONObject(); | 134 | JSONObject mataData = new JSONObject(); |
| 124 | mataData.put("knowledgeId", record.getKnowledgeId()); // 使用前端传入的知识库ID | 135 | mataData.put("knowledgeId", record.getKnowledgeId()); // 使用前端传入的知识库ID |
| 125 | mataData.put("docName", record.getDocName()); | 136 | mataData.put("docName", record.getDocName()); |
| 137 | + | ||
| 138 | + //获取record数据中的docId | ||
| 139 | + Map<String, Object> map = record.getMetadata(); | ||
| 140 | + System.out.println("map = " + map); | ||
| 126 | mataData.put("docId", record.getDocId()); // 自动生成唯一文档ID | 141 | mataData.put("docId", record.getDocId()); // 自动生成唯一文档ID |
| 127 | mataData.put("index", "0"); | 142 | mataData.put("index", "0"); |
| 143 | + System.out.println("原始数据: " + mataData); | ||
| 128 | 144 | ||
| 129 | - PGobject jsonObject = new PGobject(); | 145 | + |
| 146 | + PGobject jsonObject = new PGobject();/* | ||
| 147 | + System.out.println("原始数据: " + mataData); // 检查原始对象 | ||
| 148 | + String jsonStr = mataData.toJSONString(); | ||
| 149 | + System.out.println("JSON字符串: " + jsonStr); // 检查序列化后的JSON | ||
| 150 | + jsonObject.setValue(jsonStr); | ||
| 151 | + System.out.println("存入后的值: " + jsonObject.getValue());*/ // 检查存入后的值 | ||
| 130 | jsonObject.setType("json"); | 152 | jsonObject.setType("json"); |
| 131 | jsonObject.setValue(mataData.toJSONString()); | 153 | jsonObject.setValue(mataData.toJSONString()); |
| 132 | stmt.setObject(1, new PGvector(record.getEmbedding())); | 154 | stmt.setObject(1, new PGvector(record.getEmbedding())); |
| @@ -213,7 +235,7 @@ public class PgVectorMapper { | @@ -213,7 +235,7 @@ public class PgVectorMapper { | ||
| 213 | } | 235 | } |
| 214 | } | 236 | } |
| 215 | 237 | ||
| 216 | - /*// 自动生成嵌入向量的方法(需根据您的嵌入模型实现) | 238 | + // 自动生成嵌入向量的方法(需根据您的嵌入模型实现) |
| 217 | private float[] generateEmbedding(String text) { | 239 | private float[] generateEmbedding(String text) { |
| 218 | // 改为生成 768 维向量 | 240 | // 改为生成 768 维向量 |
| 219 | float[] embedding = new float[768]; // OpenAI 标准维度是 1536,这里改为 768 | 241 | float[] embedding = new float[768]; // OpenAI 标准维度是 1536,这里改为 768 |
| @@ -230,13 +252,4 @@ public class PgVectorMapper { | @@ -230,13 +252,4 @@ public class PgVectorMapper { | ||
| 230 | return embedding; | 252 | return embedding; |
| 231 | } | 253 | } |
| 232 | 254 | ||
| 233 | - private String getKnowledgeId(Embeddings record) { | ||
| 234 | - // 1. 优先使用前端传入的knowledgeId(如果存在) | ||
| 235 | - if (record.getMetadata() != null && record.getMetadata().containsKey("knowledgeId")) { | ||
| 236 | - return String.valueOf(record.getMetadata().get("knowledgeId")); | ||
| 237 | - } | ||
| 238 | - | ||
| 239 | - // 2. 使用配置的默认值 | ||
| 240 | - return "default_knowledge"; // 实际应从配置读取 | ||
| 241 | - }*/ | ||
| 242 | } | 255 | } |
| 1 | package org.jeecg.modules.airag.app.mapper; | 1 | package org.jeecg.modules.airag.app.mapper; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.lang.generator.SnowflakeGenerator; | ||
| 3 | import com.alibaba.fastjson2.JSONObject; | 4 | import com.alibaba.fastjson2.JSONObject; |
| 4 | import com.fasterxml.jackson.core.JsonProcessingException; | 5 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | import com.fasterxml.jackson.core.type.TypeReference; | 6 | import com.fasterxml.jackson.core.type.TypeReference; |
| @@ -7,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; | @@ -7,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 7 | import com.pgvector.PGvector; | 8 | import com.pgvector.PGvector; |
| 8 | import dev.langchain4j.data.embedding.Embedding; | 9 | import dev.langchain4j.data.embedding.Embedding; |
| 9 | import dev.langchain4j.model.output.Response; | 10 | import dev.langchain4j.model.output.Response; |
| 11 | +import io.minio.messages.Metadata; | ||
| 10 | import lombok.extern.slf4j.Slf4j; | 12 | import lombok.extern.slf4j.Slf4j; |
| 11 | import org.apache.commons.lang3.StringUtils; | 13 | import org.apache.commons.lang3.StringUtils; |
| 12 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; | 14 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; |
| @@ -36,17 +38,34 @@ public class QuestionEmbeddingMapper { | @@ -36,17 +38,34 @@ public class QuestionEmbeddingMapper { | ||
| 36 | } | 38 | } |
| 37 | 39 | ||
| 38 | // 查询所有记录 | 40 | // 查询所有记录 |
| 39 | - public List<QuestionEmbedding> findAll() { | 41 | + public List<QuestionEmbedding> findAll(QuestionEmbedding questionEmbedding) { |
| 40 | List<QuestionEmbedding> results = new ArrayList<>(); | 42 | List<QuestionEmbedding> results = new ArrayList<>(); |
| 41 | - String sql = "SELECT * FROM question_embedding"; | 43 | + StringBuilder sql = new StringBuilder("select * from question_embedding where 1 = 1"); |
| 44 | + List<Object> params = new ArrayList<>(); | ||
| 42 | 45 | ||
| 43 | - try (Connection conn = getConnection(); | ||
| 44 | - PreparedStatement stmt = conn.prepareStatement(sql); | ||
| 45 | - ResultSet rs = stmt.executeQuery()) { | 46 | + if (StringUtils.isNotBlank(questionEmbedding.getKnowledgeId())) { |
| 47 | + sql.append(" AND metadata ->> 'knowledgeId' = ?"); | ||
| 48 | + params.add(questionEmbedding.getKnowledgeId()); | ||
| 49 | + } | ||
| 50 | + if(StringUtils.isNotBlank(questionEmbedding.getQuestion())){ | ||
| 51 | + sql.append(" and question like '%").append(questionEmbedding.getQuestion()).append("%'"); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + if(StringUtils.isNotBlank(questionEmbedding.getAnswer())){ | ||
| 55 | + sql.append(" and answer like '%").append(questionEmbedding.getAnswer()).append("%'"); | ||
| 56 | + } | ||
| 57 | + try(Connection conn = getConnection(); | ||
| 58 | + PreparedStatement stmt = conn.prepareStatement(sql.toString())){ | ||
| 59 | + // 设置参数值 | ||
| 60 | + for (int i = 0; i < params.size(); i++) { | ||
| 61 | + stmt.setObject(i + 1, params.get(i)); | ||
| 62 | + } | ||
| 46 | 63 | ||
| 64 | + try (ResultSet rs = stmt.executeQuery()) { | ||
| 47 | while (rs.next()) { | 65 | while (rs.next()) { |
| 48 | results.add(mapRowToQuestionEmbedding(rs)); | 66 | results.add(mapRowToQuestionEmbedding(rs)); |
| 49 | } | 67 | } |
| 68 | + } | ||
| 50 | } catch (SQLException e) { | 69 | } catch (SQLException e) { |
| 51 | log.error("查询所有记录失败", e); | 70 | log.error("查询所有记录失败", e); |
| 52 | throw new RuntimeException("查询数据时发生数据库错误", e); | 71 | throw new RuntimeException("查询数据时发生数据库错误", e); |
| @@ -78,9 +97,18 @@ public class QuestionEmbeddingMapper { | @@ -78,9 +97,18 @@ public class QuestionEmbeddingMapper { | ||
| 78 | public int insert(QuestionEmbedding record) { | 97 | public int insert(QuestionEmbedding record) { |
| 79 | String sql = "INSERT INTO question_embedding (id, text, question, answer, metadata,embedding) VALUES (?, ?, ?, ?, ?::jsonb,?)"; | 98 | String sql = "INSERT INTO question_embedding (id, text, question, answer, metadata,embedding) VALUES (?, ?, ?, ?, ?::jsonb,?)"; |
| 80 | 99 | ||
| 100 | + | ||
| 81 | try (Connection conn = getConnection(); | 101 | try (Connection conn = getConnection(); |
| 82 | PreparedStatement stmt = conn.prepareStatement(sql)) { | 102 | PreparedStatement stmt = conn.prepareStatement(sql)) { |
| 103 | + Map<String,Object> matadata = new LinkedHashMap<String,Object>(); | ||
| 104 | + SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator(); | ||
| 83 | 105 | ||
| 106 | + String docId = String.valueOf(snowflakeGenerator.next()); | ||
| 107 | + matadata.put("docId",docId); | ||
| 108 | + matadata.put("docName",""); | ||
| 109 | + matadata.put("knowledgeId",record.getKnowledgeId()); | ||
| 110 | + | ||
| 111 | + record.setMetadata(toJson(matadata)); | ||
| 84 | stmt.setString(1, UUID.randomUUID().toString()); | 112 | stmt.setString(1, UUID.randomUUID().toString()); |
| 85 | stmt.setString(2, record.getText()); | 113 | stmt.setString(2, record.getText()); |
| 86 | stmt.setString(3, record.getQuestion()); | 114 | stmt.setString(3, record.getQuestion()); |
| @@ -109,17 +137,18 @@ public class QuestionEmbeddingMapper { | @@ -109,17 +137,18 @@ public class QuestionEmbeddingMapper { | ||
| 109 | try (Connection conn = getConnection(); | 137 | try (Connection conn = getConnection(); |
| 110 | PreparedStatement stmt = conn.prepareStatement(sql)) { | 138 | PreparedStatement stmt = conn.prepareStatement(sql)) { |
| 111 | 139 | ||
| 140 | + | ||
| 112 | stmt.setString(1, record.getText()); | 141 | stmt.setString(1, record.getText()); |
| 113 | stmt.setString(2, record.getQuestion()); | 142 | stmt.setString(2, record.getQuestion()); |
| 114 | stmt.setString(3, record.getAnswer()); | 143 | stmt.setString(3, record.getAnswer()); |
| 115 | - PGobject jsonObject = new PGobject(); | 144 | + /* PGobject jsonObject = new PGobject(); |
| 116 | jsonObject.setType("json"); | 145 | jsonObject.setType("json"); |
| 117 | 146 | ||
| 118 | JSONObject mataData = new JSONObject(); | 147 | JSONObject mataData = new JSONObject(); |
| 119 | // mataData.put("knowledgeId",record.getKnowledgeId()); | 148 | // mataData.put("knowledgeId",record.getKnowledgeId()); |
| 120 | 149 | ||
| 121 | - jsonObject.setValue(mataData.toJSONString()); | ||
| 122 | - stmt.setObject(4, jsonObject); | 150 | + jsonObject.setValue(mataData.toJSONString());*/ |
| 151 | + stmt.setObject(4, record.getMetadata()); | ||
| 123 | 152 | ||
| 124 | Response<Embedding> embedding = aiModelUtils.getEmbedding("1925730210204721154", record.getQuestion()); | 153 | Response<Embedding> embedding = aiModelUtils.getEmbedding("1925730210204721154", record.getQuestion()); |
| 125 | stmt.setObject(5, embedding.content().vector()); | 154 | stmt.setObject(5, embedding.content().vector()); |
| @@ -2,4 +2,20 @@ | @@ -2,4 +2,20 @@ | ||
| 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| 3 | <mapper namespace="org.jeecg.modules.airag.app.mapper.AiragLogMapper"> | 3 | <mapper namespace="org.jeecg.modules.airag.app.mapper.AiragLogMapper"> |
| 4 | 4 | ||
| 5 | + <select id="list1" | ||
| 6 | + resultType="org.jeecg.modules.airag.app.entity.AiragLog"> | ||
| 7 | + select * from airag_log | ||
| 8 | + where model_id = #{param1.name} | ||
| 9 | + </select> | ||
| 10 | + | ||
| 11 | + <select id="pageList" resultType="org.jeecg.modules.airag.app.entity.AiragLog"> | ||
| 12 | + select * from airag_log | ||
| 13 | + where 1=1 | ||
| 14 | + <if test="param1.name != '' and param1.name != null"> | ||
| 15 | + and model_id = #{param1.name} | ||
| 16 | + </if> | ||
| 17 | + <if test="param1.question != '' and param1.question != null"> | ||
| 18 | + and question LIKE CONCAT('%', #{param1.question}, '%') | ||
| 19 | + </if> | ||
| 20 | + </select> | ||
| 5 | </mapper> | 21 | </mapper> |
| 1 | package org.jeecg.modules.airag.app.service; | 1 | package org.jeecg.modules.airag.app.service; |
| 2 | 2 | ||
| 3 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
| 4 | +import com.baomidou.mybatisplus.core.metadata.IPage; | ||
| 5 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 3 | import com.baomidou.mybatisplus.extension.service.IService; | 6 | import com.baomidou.mybatisplus.extension.service.IService; |
| 7 | +import org.apache.poi.ss.formula.functions.T; | ||
| 4 | import org.jeecg.modules.airag.app.entity.AiragLog; | 8 | import org.jeecg.modules.airag.app.entity.AiragLog; |
| 5 | 9 | ||
| 6 | import java.util.List; | 10 | import java.util.List; |
| @@ -14,4 +18,10 @@ import java.util.List; | @@ -14,4 +18,10 @@ import java.util.List; | ||
| 14 | public interface IAiragLogService extends IService<AiragLog> { | 18 | public interface IAiragLogService extends IService<AiragLog> { |
| 15 | // List<AiragLog> getLogListWithModelName(); | 19 | // List<AiragLog> getLogListWithModelName(); |
| 16 | void saveToQuestionLibrary(AiragLog log); | 20 | void saveToQuestionLibrary(AiragLog log); |
| 21 | + | ||
| 22 | + IPage<AiragLog> list1(AiragLog airagLog,Page<AiragLog> page); | ||
| 23 | + | ||
| 24 | + IPage<AiragLog> pageList(AiragLog airagLog, Page<AiragLog> page); | ||
| 25 | + | ||
| 26 | +// List<T> list(Page<AiragLog> page, QueryWrapper<AiragLog> queryWrapper); | ||
| 17 | } | 27 | } |
| @@ -7,7 +7,7 @@ import org.springframework.web.multipart.MultipartFile; | @@ -7,7 +7,7 @@ import org.springframework.web.multipart.MultipartFile; | ||
| 7 | import java.util.List; | 7 | import java.util.List; |
| 8 | 8 | ||
| 9 | public interface IQuestionEmbeddingService { | 9 | public interface IQuestionEmbeddingService { |
| 10 | - List<QuestionEmbedding> findAll(); | 10 | + List<QuestionEmbedding> findAll(QuestionEmbedding questionEmbedding); |
| 11 | QuestionEmbedding findById(String id); | 11 | QuestionEmbedding findById(String id); |
| 12 | int insert(QuestionEmbedding record); | 12 | int insert(QuestionEmbedding record); |
| 13 | int update(QuestionEmbedding record); | 13 | int update(QuestionEmbedding record); |
| 1 | package org.jeecg.modules.airag.app.service.impl; | 1 | package org.jeecg.modules.airag.app.service.impl; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.lang.generator.SnowflakeGenerator; | ||
| 4 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
| 5 | +import com.baomidou.mybatisplus.core.metadata.IPage; | ||
| 6 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
| 3 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 7 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 9 | +import com.fasterxml.jackson.databind.util.ObjectBuffer; | ||
| 10 | +import io.minio.messages.Metadata; | ||
| 11 | +import org.apache.poi.ss.formula.functions.T; | ||
| 4 | import org.jeecg.modules.airag.app.entity.AiragLog; | 12 | import org.jeecg.modules.airag.app.entity.AiragLog; |
| 5 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; | 13 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; |
| 6 | import org.jeecg.modules.airag.app.mapper.AiragLogMapper; | 14 | import org.jeecg.modules.airag.app.mapper.AiragLogMapper; |
| 7 | import org.jeecg.modules.airag.app.mapper.QuestionEmbeddingMapper; | 15 | import org.jeecg.modules.airag.app.mapper.QuestionEmbeddingMapper; |
| 8 | import org.jeecg.modules.airag.app.service.IAiragLogService; | 16 | import org.jeecg.modules.airag.app.service.IAiragLogService; |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | 17 | import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; | ||
| 10 | import org.springframework.stereotype.Service; | 19 | import org.springframework.stereotype.Service; |
| 11 | 20 | ||
| 21 | +import java.util.HashMap; | ||
| 22 | +import java.util.LinkedHashMap; | ||
| 12 | import java.util.List; | 23 | import java.util.List; |
| 24 | +import java.util.Map; | ||
| 13 | 25 | ||
| 14 | /** | 26 | /** |
| 15 | * @Description: 日志管理 | 27 | * @Description: 日志管理 |
| @@ -22,6 +34,9 @@ public class AiragLogServiceImpl extends ServiceImpl<AiragLogMapper, AiragLog> i | @@ -22,6 +34,9 @@ public class AiragLogServiceImpl extends ServiceImpl<AiragLogMapper, AiragLog> i | ||
| 22 | @Autowired | 34 | @Autowired |
| 23 | private QuestionEmbeddingMapper questionEmbeddingMapper; | 35 | private QuestionEmbeddingMapper questionEmbeddingMapper; |
| 24 | 36 | ||
| 37 | + @Autowired | ||
| 38 | + private AiragLogMapper airagLogMapper; | ||
| 39 | + | ||
| 25 | // @Override | 40 | // @Override |
| 26 | // public List<AiragLog> getLogListWithModelName() { | 41 | // public List<AiragLog> getLogListWithModelName() { |
| 27 | // List<AiragLog> logList = this.list(); | 42 | // List<AiragLog> logList = this.list(); |
| @@ -41,7 +56,24 @@ public class AiragLogServiceImpl extends ServiceImpl<AiragLogMapper, AiragLog> i | @@ -41,7 +56,24 @@ public class AiragLogServiceImpl extends ServiceImpl<AiragLogMapper, AiragLog> i | ||
| 41 | QuestionEmbedding questionEmbedding = new QuestionEmbedding(); | 56 | QuestionEmbedding questionEmbedding = new QuestionEmbedding(); |
| 42 | questionEmbedding.setQuestion(log.getQuestion()); | 57 | questionEmbedding.setQuestion(log.getQuestion()); |
| 43 | questionEmbedding.setAnswer(log.getAnswer()); | 58 | questionEmbedding.setAnswer(log.getAnswer()); |
| 59 | + | ||
| 60 | + | ||
| 44 | questionEmbeddingMapper.insert(questionEmbedding); | 61 | questionEmbeddingMapper.insert(questionEmbedding); |
| 45 | } | 62 | } |
| 63 | + | ||
| 64 | + @Override | ||
| 65 | + public IPage<AiragLog> list1(AiragLog airagLog,Page<AiragLog> page) { | ||
| 66 | + return airagLogMapper.list1(airagLog,page); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + @Override | ||
| 70 | + public IPage<AiragLog> pageList(AiragLog airagLog, Page<AiragLog> page) { | ||
| 71 | + return airagLogMapper.pageList(airagLog,page); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + /* @Override | ||
| 75 | + public List<T> list(Page<AiragLog> page, QueryWrapper<AiragLog> queryWrapper) { | ||
| 76 | + | ||
| 77 | + }*/ | ||
| 46 | } | 78 | } |
| 47 | 79 |
| @@ -64,8 +64,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | @@ -64,8 +64,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService { | ||
| 64 | 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}"); | 64 | 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 | 65 | ||
| 66 | @Override | 66 | @Override |
| 67 | - public List<QuestionEmbedding> findAll() { | ||
| 68 | - return questionEmbeddingMapper.findAll(); | 67 | + public List<QuestionEmbedding> findAll(QuestionEmbedding questionEmbedding) { |
| 68 | + return questionEmbeddingMapper.findAll(questionEmbedding); | ||
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | @Override | 71 | @Override |
-
请 注册 或 登录 后发表评论