feat(ai-web): 知识库增加描述
This commit is contained in:
@@ -3,6 +3,7 @@ CREATE TABLE `service_ai_knowledge`
|
|||||||
`id` bigint NOT NULL,
|
`id` bigint NOT NULL,
|
||||||
`vector_source_id` varchar(100) NOT NULL,
|
`vector_source_id` varchar(100) NOT NULL,
|
||||||
`name` varchar(100) NOT NULL,
|
`name` varchar(100) NOT NULL,
|
||||||
|
`description` longtext NOT NULL,
|
||||||
`strategy` varchar(10) NOT NULL,
|
`strategy` varchar(10) NOT NULL,
|
||||||
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
`modified_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
`modified_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
|
|||||||
@@ -37,9 +37,18 @@ public class KnowledgeBaseController {
|
|||||||
@PostMapping("add")
|
@PostMapping("add")
|
||||||
public void add(
|
public void add(
|
||||||
@RequestParam("name") String name,
|
@RequestParam("name") String name,
|
||||||
|
@RequestParam("description") String description,
|
||||||
@RequestParam("strategy") String strategy
|
@RequestParam("strategy") String strategy
|
||||||
) throws ExecutionException, InterruptedException {
|
) throws ExecutionException, InterruptedException {
|
||||||
knowledgeBaseService.add(name, strategy);
|
knowledgeBaseService.add(name, description, strategy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("update_description")
|
||||||
|
public void updateDescription(
|
||||||
|
@RequestParam("id") Long id,
|
||||||
|
@RequestParam("description") String description
|
||||||
|
) {
|
||||||
|
knowledgeBaseService.updateDescription(id, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("name")
|
@GetMapping("name")
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ public class Knowledge {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private Long vectorSourceId;
|
private Long vectorSourceId;
|
||||||
private String name;
|
private String name;
|
||||||
|
private String description;
|
||||||
private String strategy;
|
private String strategy;
|
||||||
private Long createdTime;
|
private Long createdTime;
|
||||||
private Long modifiedTime;
|
private Long modifiedTime;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ public class KnowledgeVO {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private Long vectorSourceId;
|
private Long vectorSourceId;
|
||||||
private String name;
|
private String name;
|
||||||
|
private String description;
|
||||||
private String strategy;
|
private String strategy;
|
||||||
private Long size;
|
private Long size;
|
||||||
private Long points;
|
private Long points;
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import com.lanyuanxiaoyao.service.ai.web.entity.vo.KnowledgeVO;
|
|||||||
import com.lanyuanxiaoyao.service.common.Constants;
|
import com.lanyuanxiaoyao.service.common.Constants;
|
||||||
import io.qdrant.client.QdrantClient;
|
import io.qdrant.client.QdrantClient;
|
||||||
import io.qdrant.client.grpc.Collections;
|
import io.qdrant.client.grpc.Collections;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -32,14 +31,16 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
@Service
|
@Service
|
||||||
public class KnowledgeBaseService {
|
public class KnowledgeBaseService {
|
||||||
public static final String KNOWLEDGE_TABLE_NAME = Constants.DATABASE_NAME + ".service_ai_knowledge";
|
public static final String KNOWLEDGE_TABLE_NAME = Constants.DATABASE_NAME + ".service_ai_knowledge";
|
||||||
|
public static final String[] KNOWLEDGE_COLUMNS = new String[]{"id", "vector_source_id", "name", "description", "strategy", "created_time", "modified_time"};
|
||||||
private static final RowMapper<Knowledge> knowledgeMapper = (rs, row) -> {
|
private static final RowMapper<Knowledge> knowledgeMapper = (rs, row) -> {
|
||||||
Knowledge knowledge = new Knowledge();
|
Knowledge knowledge = new Knowledge();
|
||||||
knowledge.setId(rs.getLong(1));
|
knowledge.setId(rs.getLong(1));
|
||||||
knowledge.setVectorSourceId(rs.getLong(2));
|
knowledge.setVectorSourceId(rs.getLong(2));
|
||||||
knowledge.setName(rs.getString(3));
|
knowledge.setName(rs.getString(3));
|
||||||
knowledge.setStrategy(rs.getString(4));
|
knowledge.setDescription(rs.getString(4));
|
||||||
knowledge.setCreatedTime(rs.getTimestamp(5).getTime());
|
knowledge.setStrategy(rs.getString(5));
|
||||||
knowledge.setModifiedTime(rs.getTimestamp(6).getTime());
|
knowledge.setCreatedTime(rs.getTimestamp(6).getTime());
|
||||||
|
knowledge.setModifiedTime(rs.getTimestamp(7).getTime());
|
||||||
return knowledge;
|
return knowledge;
|
||||||
};
|
};
|
||||||
private final JdbcTemplate template;
|
private final JdbcTemplate template;
|
||||||
@@ -56,7 +57,7 @@ public class KnowledgeBaseService {
|
|||||||
|
|
||||||
public Knowledge get(Long id) {
|
public Knowledge get(Long id) {
|
||||||
return template.queryForObject(
|
return template.queryForObject(
|
||||||
SqlBuilder.select("id", "vector_source_id", "name", "strategy", "created_time", "modified_time")
|
SqlBuilder.select(KNOWLEDGE_COLUMNS)
|
||||||
.from(KNOWLEDGE_TABLE_NAME)
|
.from(KNOWLEDGE_TABLE_NAME)
|
||||||
.whereEq("id", "?")
|
.whereEq("id", "?")
|
||||||
.precompileSql(),
|
.precompileSql(),
|
||||||
@@ -66,7 +67,7 @@ public class KnowledgeBaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void add(String name, String strategy) throws ExecutionException, InterruptedException {
|
public void add(String name, String description, String strategy) throws ExecutionException, InterruptedException {
|
||||||
Integer count = template.queryForObject(
|
Integer count = template.queryForObject(
|
||||||
SqlBuilder.select("count(*)")
|
SqlBuilder.select("count(*)")
|
||||||
.from(KNOWLEDGE_TABLE_NAME)
|
.from(KNOWLEDGE_TABLE_NAME)
|
||||||
@@ -82,13 +83,14 @@ public class KnowledgeBaseService {
|
|||||||
long id = SnowflakeId.next();
|
long id = SnowflakeId.next();
|
||||||
long vectorSourceId = SnowflakeId.next();
|
long vectorSourceId = SnowflakeId.next();
|
||||||
template.update(
|
template.update(
|
||||||
SqlBuilder.insertInto(KNOWLEDGE_TABLE_NAME, "id", "vector_source_id", "name", "strategy")
|
SqlBuilder.insertInto(KNOWLEDGE_TABLE_NAME, "id", "vector_source_id", "name", "description", "strategy")
|
||||||
.values()
|
.values()
|
||||||
.addValue("?", "?", "?", "?")
|
.addValue("?", "?", "?", "?", "?")
|
||||||
.precompileSql(),
|
.precompileSql(),
|
||||||
id,
|
id,
|
||||||
vectorSourceId,
|
vectorSourceId,
|
||||||
name,
|
name,
|
||||||
|
description,
|
||||||
strategy
|
strategy
|
||||||
);
|
);
|
||||||
client.createCollectionAsync(
|
client.createCollectionAsync(
|
||||||
@@ -100,6 +102,18 @@ public class KnowledgeBaseService {
|
|||||||
).get();
|
).get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void updateDescription(Long id, String description) {
|
||||||
|
template.update(
|
||||||
|
SqlBuilder.update(KNOWLEDGE_TABLE_NAME)
|
||||||
|
.set("description", "?")
|
||||||
|
.whereEq("id", "?")
|
||||||
|
.precompileSql(),
|
||||||
|
description,
|
||||||
|
id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public String getName(Long id) {
|
public String getName(Long id) {
|
||||||
return template.queryForObject(
|
return template.queryForObject(
|
||||||
SqlBuilder.select("name")
|
SqlBuilder.select("name")
|
||||||
@@ -113,7 +127,7 @@ public class KnowledgeBaseService {
|
|||||||
|
|
||||||
public ImmutableList<KnowledgeVO> list() {
|
public ImmutableList<KnowledgeVO> list() {
|
||||||
return template.query(
|
return template.query(
|
||||||
SqlBuilder.select("id", "vector_source_id", "name", "strategy", "created_time", "modified_time")
|
SqlBuilder.select(KNOWLEDGE_COLUMNS)
|
||||||
.from(KNOWLEDGE_TABLE_NAME)
|
.from(KNOWLEDGE_TABLE_NAME)
|
||||||
.orderByDesc("created_time")
|
.orderByDesc("created_time")
|
||||||
.build(),
|
.build(),
|
||||||
@@ -127,6 +141,7 @@ public class KnowledgeBaseService {
|
|||||||
vo.setId(knowledge.getId());
|
vo.setId(knowledge.getId());
|
||||||
vo.setVectorSourceId(knowledge.getVectorSourceId());
|
vo.setVectorSourceId(knowledge.getVectorSourceId());
|
||||||
vo.setName(knowledge.getName());
|
vo.setName(knowledge.getName());
|
||||||
|
vo.setDescription(knowledge.getDescription());
|
||||||
vo.setPoints(info.getPointsCount());
|
vo.setPoints(info.getPointsCount());
|
||||||
vo.setSegments(info.getSegmentsCount());
|
vo.setSegments(info.getSegmentsCount());
|
||||||
vo.setStatus(info.getStatus().name());
|
vo.setStatus(info.getStatus().name());
|
||||||
@@ -165,7 +180,7 @@ public class KnowledgeBaseService {
|
|||||||
String text,
|
String text,
|
||||||
Integer limit,
|
Integer limit,
|
||||||
Double threshold
|
Double threshold
|
||||||
) throws ExecutionException, InterruptedException, IOException {
|
) throws ExecutionException, InterruptedException {
|
||||||
Knowledge knowledge = get(id);
|
Knowledge knowledge = get(id);
|
||||||
Boolean exists = client.collectionExistsAsync(String.valueOf(knowledge.getVectorSourceId())).get();
|
Boolean exists = client.collectionExistsAsync(String.valueOf(knowledge.getVectorSourceId())).get();
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
@@ -182,13 +197,6 @@ public class KnowledgeBaseService {
|
|||||||
.similarityThreshold(threshold)
|
.similarityThreshold(threshold)
|
||||||
.build()
|
.build()
|
||||||
);
|
);
|
||||||
// 如果只是一个知识库的话,似乎没有什么rerank的必要...
|
|
||||||
/* List<org.noear.solon.ai.rag.Document> rerankDocuments = rerankingModel.rerank(
|
|
||||||
text,
|
|
||||||
documents.stream()
|
|
||||||
.map(doc -> new org.noear.solon.ai.rag.Document(doc.getId(), doc.getText(), doc.getMetadata(), doc.getScore()))
|
|
||||||
.toList()
|
|
||||||
); */
|
|
||||||
return Lists.immutable.ofAll(documents)
|
return Lists.immutable.ofAll(documents)
|
||||||
.collect(Document::getText);
|
.collect(Document::getText);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,12 +50,20 @@ const Knowledge: React.FC = () => {
|
|||||||
type: 'input-text',
|
type: 'input-text',
|
||||||
name: 'name',
|
name: 'name',
|
||||||
label: '名称',
|
label: '名称',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'textarea',
|
||||||
|
name: 'description',
|
||||||
|
label: '描述',
|
||||||
|
required: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'select',
|
type: 'select',
|
||||||
name: 'strategy',
|
name: 'strategy',
|
||||||
label: '类型',
|
label: '类型',
|
||||||
value: 'Cosine',
|
value: 'Cosine',
|
||||||
|
required: true,
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
label: '文本',
|
label: '文本',
|
||||||
@@ -77,6 +85,11 @@ const Knowledge: React.FC = () => {
|
|||||||
{
|
{
|
||||||
name: 'name',
|
name: 'name',
|
||||||
label: '名称',
|
label: '名称',
|
||||||
|
width: 200,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'description',
|
||||||
|
label: '描述',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: '类型',
|
label: '类型',
|
||||||
@@ -99,8 +112,41 @@ const Knowledge: React.FC = () => {
|
|||||||
{
|
{
|
||||||
type: 'operation',
|
type: 'operation',
|
||||||
label: '操作',
|
label: '操作',
|
||||||
width: 150,
|
width: 200,
|
||||||
buttons: [
|
buttons: [
|
||||||
|
{
|
||||||
|
type: 'action',
|
||||||
|
label: '更新',
|
||||||
|
level: 'link',
|
||||||
|
size: 'sm',
|
||||||
|
actionType: 'dialog',
|
||||||
|
dialog: {
|
||||||
|
title: '更新描述',
|
||||||
|
body: {
|
||||||
|
debug: commonInfo.debug,
|
||||||
|
type: 'form',
|
||||||
|
api: {
|
||||||
|
method: 'post',
|
||||||
|
url: `${commonInfo.baseAiUrl}/knowledge/update_description`,
|
||||||
|
dataType: 'form',
|
||||||
|
},
|
||||||
|
mode: 'normal',
|
||||||
|
body: [
|
||||||
|
{
|
||||||
|
type: 'hidden',
|
||||||
|
name: "id",
|
||||||
|
// value: '${id}',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'textarea',
|
||||||
|
name: 'description',
|
||||||
|
label: '描述',
|
||||||
|
required: true,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
type: 'action',
|
type: 'action',
|
||||||
label: '详情',
|
label: '详情',
|
||||||
|
|||||||
@@ -281,6 +281,7 @@ export function crudCommonOptions() {
|
|||||||
resizable: false,
|
resizable: false,
|
||||||
syncLocation: false,
|
syncLocation: false,
|
||||||
silentPolling: true,
|
silentPolling: true,
|
||||||
|
columnsTogglable: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user