|
|
|
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,16 +38,33 @@ 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("%'");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (rs.next()) {
|
|
|
|
results.add(mapRowToQuestionEmbedding(rs));
|
|
|
|
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);
|
|
...
|
...
|
@@ -78,27 +97,36 @@ 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)) {
|
|
|
|
|
|
|
|
stmt.setString(1, UUID.randomUUID().toString());
|
|
|
|
stmt.setString(2, record.getText());
|
|
|
|
stmt.setString(3, record.getQuestion());
|
|
|
|
stmt.setString(4, record.getAnswer());
|
|
|
|
PGobject jsonObject = new PGobject();
|
|
|
|
jsonObject.setType("json");
|
|
|
|
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());
|
|
|
|
stmt.setString(4, record.getAnswer());
|
|
|
|
PGobject jsonObject = new PGobject();
|
|
|
|
jsonObject.setType("json");
|
|
|
|
|
|
|
|
// JSONObject mataData = new JSONObject();
|
|
|
|
// mataData.put("knowledgeId",record.getKnowledgeId());
|
|
|
|
|
|
|
|
jsonObject.setValue(record.getMetadata());
|
|
|
|
stmt.setObject(5, jsonObject);
|
|
|
|
Response<Embedding> embedding = aiModelUtils.getEmbedding("1925730210204721154", record.getQuestion());
|
|
|
|
stmt.setObject(6, embedding.content().vector());
|
|
|
|
return stmt.executeUpdate();
|
|
|
|
jsonObject.setValue(record.getMetadata());
|
|
|
|
stmt.setObject(5, jsonObject);
|
|
|
|
Response<Embedding> embedding = aiModelUtils.getEmbedding("1925730210204721154", record.getQuestion());
|
|
|
|
stmt.setObject(6, embedding.content().vector());
|
|
|
|
return stmt.executeUpdate();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
log.error("插入记录失败: {}", record, e);
|
|
|
|
throw new RuntimeException("插入数据时发生数据库错误", e);
|
|
|
|
log.error("插入记录失败: {}", record, e);
|
|
|
|
throw new RuntimeException("插入数据时发生数据库错误", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -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());
|
...
|
...
|
|