作者 dong

修复bug

正在显示 12 个修改的文件 包含 204 行增加47 行删除
... ... @@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
... ... @@ -62,7 +63,15 @@ public class AiragLogController extends JeecgController<AiragLog, IAiragLogServi
HttpServletRequest req) {
QueryWrapper<AiragLog> queryWrapper = QueryGenerator.initQueryWrapper(airagLog, req.getParameterMap());
Page<AiragLog> page = new Page<AiragLog>(pageNo, pageSize);
IPage<AiragLog> pageList = airagLogService.page(page, queryWrapper);
// IPage<AiragLog> pageList = airagLogService.page(page, queryWrapper);
IPage<AiragLog> pageList = airagLogService.pageList(airagLog,page);
/*if(airagLog != null && StringUtils.isNotBlank(airagLog.getName())) {
QueryWrapper<AiragModel> queryWrapper2 = new QueryWrapper<>();
queryWrapper2.eq("model_id", airagLog.getName());
IPage<AiragLog> page1 = airagLogService.list1(airagLog,page);
return Result.ok(page1);
}*/
List<AiragModel> list = airagModelService.list();
Map<String,String> nameMap = new HashMap<String,String>();
for(AiragModel airagModel : list){
... ...
... ... @@ -131,14 +131,14 @@ public class EmbeddingsController {
@RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
public Result<String> edit(@RequestBody Embeddings embeddings) {
// embeddingsService.updateById(Embeddings);
Map<String, Object> metadata = embeddings.getMetadata();
SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
metadata.put("docName", embeddings.getDocName());
String docId = String.valueOf(snowflakeGenerator.next());
metadata.put("docId", docId); // 自动生成唯一文档ID
metadata.put("index", "0");
// 2. 设置到embeddings对象
embeddings.setMetadata(metadata);
// Map<String, Object> metadata = embeddings.getMetadata();
// SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
// metadata.put("docName", embeddings.getDocName());
// String docId = String.valueOf(snowflakeGenerator.next());
// metadata.put("docId", docId); // 自动生成唯一文档ID
// metadata.put("index", "0");
// // 2. 设置到embeddings对象
// embeddings.setMetadata(metadata);
embeddingsService.update(embeddings);
return Result.OK("编辑成功!");
}
... ...
package org.jeecg.modules.airag.app.controller;
import dev.langchain4j.internal.Json;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.airag.app.entity.AiragLog;
import org.jeecg.modules.airag.app.entity.QuestionEmbedding;
import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService;
import org.jeecg.modules.airag.app.utils.JsonUtils;
import org.jeecg.modules.airag.llm.entity.AiragKnowledge;
import org.jeecg.modules.airag.llm.entity.AiragModel;
import org.jeecg.modules.airag.llm.service.IAiragKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
... ... @@ -27,8 +31,8 @@ public class QuestionEmbeddingController {
private IAiragKnowledgeService airagKnowledgeService;
@GetMapping("/list")
public Result<List<QuestionEmbedding>> findAll() {
List<QuestionEmbedding> list = questionEmbeddingService.findAll();
public Result<List<QuestionEmbedding>> findAll(QuestionEmbedding questionEmbedding) {
List<QuestionEmbedding> list = questionEmbeddingService.findAll(questionEmbedding);
Map<String, String> airagKnowledgeMap = airagKnowledgeService.list()
.stream()
.collect(Collectors.toMap(AiragKnowledge::getId, AiragKnowledge::getName));
... ... @@ -44,6 +48,7 @@ public class QuestionEmbeddingController {
}
});
return Result.OK(list);
}
... ... @@ -58,12 +63,47 @@ public class QuestionEmbeddingController {
@PostMapping("/add")
public Result<String> insert(@RequestBody QuestionEmbedding record) {
Map<String, String> airagKnowledgeMap = airagKnowledgeService.list()
.stream()
.collect(Collectors.toMap(AiragKnowledge::getId, AiragKnowledge::getName));
// 处理knowledgeId和knowledgeName的赋值
String knowledgeId = record.getKnowledgeId();
if (StringUtils.isNotBlank(knowledgeId)) {
String knowledgeName = airagKnowledgeMap.get(record.getKnowledgeId());
record.setKnowledgeName(knowledgeName);
}
int result = questionEmbeddingService.insert(record);
return result > 0 ? Result.OK("添加成功!") : Result.error("添加失败");
}
@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
public Result<String> update(@RequestBody QuestionEmbedding record) {
//获取matadata
QuestionEmbedding existRecord = questionEmbeddingService.findById(record.getId());
Map<String, String> airagKnowledgeMap = airagKnowledgeService.list()
.stream()
.collect(Collectors.toMap(AiragKnowledge::getId, AiragKnowledge::getName));
// 处理knowledgeId和knowledgeName的赋值
String knowledgeId = record.getKnowledgeId();
if (StringUtils.isNotBlank(knowledgeId)) {
String knowledgeName = airagKnowledgeMap.get(record.getKnowledgeId());
record.setKnowledgeName(knowledgeName);
String existMetadata = existRecord.getMetadata();
Map<String, String> jsonMap = new HashMap<>();
if (StringUtils.isNotBlank(existMetadata)) {
jsonMap = JsonUtils.jsonUtils(existMetadata);
}
jsonMap.put("knowledgeId", record.getKnowledgeId());
record.setMetadata(Json.toJson(jsonMap));
}
int result = questionEmbeddingService.update(record);
return result > 0 ? Result.OK("编辑成功!") : Result.error("编辑失败");
}
... ...
... ... @@ -51,4 +51,5 @@ public class QuestionEmbedding {
@TableField(exist = false)
private String knowledgeId;
}
\ No newline at end of file
... ...
package org.jeecg.modules.airag.app.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.lettuce.core.dynamic.annotation.Param;
import org.jeecg.modules.airag.app.entity.AiragLog;
import java.util.List;
/**
* @Description: 日志管理
* @Author: jeecg-boot
... ... @@ -10,5 +15,7 @@ import org.jeecg.modules.airag.app.entity.AiragLog;
* @Version: V1.0
*/
public interface AiragLogMapper extends BaseMapper<AiragLog> {
IPage<AiragLog> list1(@Param("param1")AiragLog airagLog, Page<AiragLog> page);
IPage<AiragLog> pageList(@Param("param1") AiragLog airagLog, Page<AiragLog> page);
}
... ...
package org.jeecg.modules.airag.app.mapper;
import ch.qos.logback.core.net.SyslogOutputStream;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
... ... @@ -32,26 +33,36 @@ public class PgVectorMapper {
// 查询所有向量记录
public List<Embeddings> findAll(Embeddings embeddings) {
List<Embeddings> results = new ArrayList<>();
String sql = "SELECT * FROM embeddings WHERE 1 =1 ";
if (StringUtils.isNotBlank(embeddings.getId())){
sql += " and embedding_id = '" + embeddings.getId() + "'";
}
StringBuilder sql = new StringBuilder("SELECT * FROM embeddings WHERE 1=1");
List<Object> params = new ArrayList<>(); // 存储参数值
if (StringUtils.isNotBlank(embeddings.getKnowledgeId())){
sql += " and metadata ->> 'knowledgeId' = '" + embeddings.getKnowledgeId() + "'";
// 动态构建查询条件
if (StringUtils.isNotBlank(embeddings.getKnowledgeId())) {
sql.append(" AND metadata ->> 'knowledgeId' = ?");
params.add(embeddings.getKnowledgeId());
}
if(StringUtils.isNotBlank(embeddings.getText())){
sql += " and text = '" + embeddings.getText() + "'";
if (StringUtils.isNotBlank(embeddings.getText())) {
sql.append(" AND text ILIKE ?"); // 使用 ILIKE 进行不区分大小写的模糊匹配
params.add("%" + embeddings.getText() + "%");
}
System.out.println("sql = " + sql);
System.out.println("SQL: " + sql.toString());
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
PreparedStatement stmt = conn.prepareStatement(sql.toString())) {
// 设置参数值
for (int i = 0; i < params.size(); i++) {
stmt.setObject(i + 1, params.get(i));
}
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
results.add(mapRowToEmbeddings(rs));
}
}
} catch (SQLException e) {
log.error("查询所有向量记录失败", e);
throw new RuntimeException("查询向量数据时发生数据库错误", e);
... ... @@ -81,7 +92,7 @@ public class PgVectorMapper {
// 插入新向量记录
public int insert(Embeddings record) {
/* Map<String, Object> metadata = new LinkedHashMap<>();
/*Map<String, Object> metadata = new LinkedHashMap<>();
// 按固定顺序添加字段
metadata.put("docId", UUID.randomUUID().toString());
... ... @@ -89,10 +100,10 @@ public class PgVectorMapper {
metadata.put("docName", record.getDocName());
metadata.put("index", 0); // 确保是整数
record.setMetadata(metadata);
*/
// 自动生成向量(这里需要调用嵌入模型)
float[] embedding = generateEmbedding(record.getText());
record.setEmbedding(embedding);*/
record.setEmbedding(embedding);
String sql = "INSERT INTO embeddings (embedding_id, embedding, text, metadata) VALUES (?, ?, ?, ?::jsonb)";
... ... @@ -123,10 +134,21 @@ public class PgVectorMapper {
JSONObject mataData = new JSONObject();
mataData.put("knowledgeId", record.getKnowledgeId()); // 使用前端传入的知识库ID
mataData.put("docName", record.getDocName());
//获取record数据中的docId
Map<String, Object> map = record.getMetadata();
System.out.println("map = " + map);
mataData.put("docId", record.getDocId()); // 自动生成唯一文档ID
mataData.put("index", "0");
System.out.println("原始数据: " + mataData);
PGobject jsonObject = new PGobject();
PGobject jsonObject = new PGobject();/*
System.out.println("原始数据: " + mataData); // 检查原始对象
String jsonStr = mataData.toJSONString();
System.out.println("JSON字符串: " + jsonStr); // 检查序列化后的JSON
jsonObject.setValue(jsonStr);
System.out.println("存入后的值: " + jsonObject.getValue());*/ // 检查存入后的值
jsonObject.setType("json");
jsonObject.setValue(mataData.toJSONString());
stmt.setObject(1, new PGvector(record.getEmbedding()));
... ... @@ -213,7 +235,7 @@ public class PgVectorMapper {
}
}
/*// 自动生成嵌入向量的方法(需根据您的嵌入模型实现)
// 自动生成嵌入向量的方法(需根据您的嵌入模型实现)
private float[] generateEmbedding(String text) {
// 改为生成 768 维向量
float[] embedding = new float[768]; // OpenAI 标准维度是 1536,这里改为 768
... ... @@ -230,13 +252,4 @@ public class PgVectorMapper {
return embedding;
}
private String getKnowledgeId(Embeddings record) {
// 1. 优先使用前端传入的knowledgeId(如果存在)
if (record.getMetadata() != null && record.getMetadata().containsKey("knowledgeId")) {
return String.valueOf(record.getMetadata().get("knowledgeId"));
}
// 2. 使用配置的默认值
return "default_knowledge"; // 实际应从配置读取
}*/
}
\ No newline at end of file
... ...
package org.jeecg.modules.airag.app.mapper;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
... ... @@ -7,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.pgvector.PGvector;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.model.output.Response;
import io.minio.messages.Metadata;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.airag.app.entity.QuestionEmbedding;
... ... @@ -36,17 +38,34 @@ public class QuestionEmbeddingMapper {
}
// 查询所有记录
public List<QuestionEmbedding> findAll() {
public List<QuestionEmbedding> findAll(QuestionEmbedding questionEmbedding) {
List<QuestionEmbedding> results = new ArrayList<>();
String sql = "SELECT * FROM question_embedding";
StringBuilder sql = new StringBuilder("select * from question_embedding where 1 = 1");
List<Object> params = new ArrayList<>();
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
if (StringUtils.isNotBlank(questionEmbedding.getKnowledgeId())) {
sql.append(" AND metadata ->> 'knowledgeId' = ?");
params.add(questionEmbedding.getKnowledgeId());
}
if(StringUtils.isNotBlank(questionEmbedding.getQuestion())){
sql.append(" and question like '%").append(questionEmbedding.getQuestion()).append("%'");
}
if(StringUtils.isNotBlank(questionEmbedding.getAnswer())){
sql.append(" and answer like '%").append(questionEmbedding.getAnswer()).append("%'");
}
try(Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql.toString())){
// 设置参数值
for (int i = 0; i < params.size(); i++) {
stmt.setObject(i + 1, params.get(i));
}
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
results.add(mapRowToQuestionEmbedding(rs));
}
}
} catch (SQLException e) {
log.error("查询所有记录失败", e);
throw new RuntimeException("查询数据时发生数据库错误", e);
... ... @@ -78,9 +97,18 @@ public class QuestionEmbeddingMapper {
public int insert(QuestionEmbedding record) {
String sql = "INSERT INTO question_embedding (id, text, question, answer, metadata,embedding) VALUES (?, ?, ?, ?, ?::jsonb,?)";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
Map<String,Object> matadata = new LinkedHashMap<String,Object>();
SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
String docId = String.valueOf(snowflakeGenerator.next());
matadata.put("docId",docId);
matadata.put("docName","");
matadata.put("knowledgeId",record.getKnowledgeId());
record.setMetadata(toJson(matadata));
stmt.setString(1, UUID.randomUUID().toString());
stmt.setString(2, record.getText());
stmt.setString(3, record.getQuestion());
... ... @@ -109,17 +137,18 @@ public class QuestionEmbeddingMapper {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, record.getText());
stmt.setString(2, record.getQuestion());
stmt.setString(3, record.getAnswer());
PGobject jsonObject = new PGobject();
/* PGobject jsonObject = new PGobject();
jsonObject.setType("json");
JSONObject mataData = new JSONObject();
// mataData.put("knowledgeId",record.getKnowledgeId());
jsonObject.setValue(mataData.toJSONString());
stmt.setObject(4, jsonObject);
jsonObject.setValue(mataData.toJSONString());*/
stmt.setObject(4, record.getMetadata());
Response<Embedding> embedding = aiModelUtils.getEmbedding("1925730210204721154", record.getQuestion());
stmt.setObject(5, embedding.content().vector());
... ...
... ... @@ -2,4 +2,20 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.jeecg.modules.airag.app.mapper.AiragLogMapper">
<select id="list1"
resultType="org.jeecg.modules.airag.app.entity.AiragLog">
select * from airag_log
where model_id = #{param1.name}
</select>
<select id="pageList" resultType="org.jeecg.modules.airag.app.entity.AiragLog">
select * from airag_log
where 1=1
<if test="param1.name != '' and param1.name != null">
and model_id = #{param1.name}
</if>
<if test="param1.question != '' and param1.question != null">
and question LIKE CONCAT('%', #{param1.question}, '%')
</if>
</select>
</mapper>
\ No newline at end of file
... ...
package org.jeecg.modules.airag.app.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.modules.airag.app.entity.AiragLog;
import java.util.List;
... ... @@ -14,4 +18,10 @@ import java.util.List;
public interface IAiragLogService extends IService<AiragLog> {
// List<AiragLog> getLogListWithModelName();
void saveToQuestionLibrary(AiragLog log);
IPage<AiragLog> list1(AiragLog airagLog,Page<AiragLog> page);
IPage<AiragLog> pageList(AiragLog airagLog, Page<AiragLog> page);
// List<T> list(Page<AiragLog> page, QueryWrapper<AiragLog> queryWrapper);
}
... ...
... ... @@ -7,7 +7,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.util.List;
public interface IQuestionEmbeddingService {
List<QuestionEmbedding> findAll();
List<QuestionEmbedding> findAll(QuestionEmbedding questionEmbedding);
QuestionEmbedding findById(String id);
int insert(QuestionEmbedding record);
int update(QuestionEmbedding record);
... ...
package org.jeecg.modules.airag.app.service.impl;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.ObjectBuffer;
import io.minio.messages.Metadata;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.modules.airag.app.entity.AiragLog;
import org.jeecg.modules.airag.app.entity.QuestionEmbedding;
import org.jeecg.modules.airag.app.mapper.AiragLogMapper;
import org.jeecg.modules.airag.app.mapper.QuestionEmbeddingMapper;
import org.jeecg.modules.airag.app.service.IAiragLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @Description: 日志管理
... ... @@ -22,6 +34,9 @@ public class AiragLogServiceImpl extends ServiceImpl<AiragLogMapper, AiragLog> i
@Autowired
private QuestionEmbeddingMapper questionEmbeddingMapper;
@Autowired
private AiragLogMapper airagLogMapper;
// @Override
// public List<AiragLog> getLogListWithModelName() {
// List<AiragLog> logList = this.list();
... ... @@ -41,7 +56,24 @@ public class AiragLogServiceImpl extends ServiceImpl<AiragLogMapper, AiragLog> i
QuestionEmbedding questionEmbedding = new QuestionEmbedding();
questionEmbedding.setQuestion(log.getQuestion());
questionEmbedding.setAnswer(log.getAnswer());
questionEmbeddingMapper.insert(questionEmbedding);
}
@Override
public IPage<AiragLog> list1(AiragLog airagLog,Page<AiragLog> page) {
return airagLogMapper.list1(airagLog,page);
}
@Override
public IPage<AiragLog> pageList(AiragLog airagLog, Page<AiragLog> page) {
return airagLogMapper.pageList(airagLog,page);
}
/* @Override
public List<T> list(Page<AiragLog> page, QueryWrapper<AiragLog> queryWrapper) {
}*/
}
... ...
... ... @@ -64,8 +64,8 @@ public class QuestionEmbeddingServiceImpl implements IQuestionEmbeddingService {
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}");
@Override
public List<QuestionEmbedding> findAll() {
return questionEmbeddingMapper.findAll();
public List<QuestionEmbedding> findAll(QuestionEmbedding questionEmbedding) {
return questionEmbeddingMapper.findAll(questionEmbedding);
}
@Override
... ...