|
1
|
package org.jeecg.modules.airag.app.controller;
|
1
|
package org.jeecg.modules.airag.app.controller;
|
|
2
|
|
2
|
|
|
3
|
-import java.lang.reflect.Field;
|
|
|
|
4
|
-import java.util.Arrays;
|
|
|
|
5
|
-import java.util.List;
|
|
|
|
6
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
7
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
|
8
|
-import javax.sql.DataSource;
|
|
|
|
9
|
-import java.sql.Connection; // PostgreSQL JDBC 连接
|
|
|
|
10
|
-import java.sql.PreparedStatement;
|
|
|
|
11
|
-import java.sql.ResultSet;
|
|
|
|
12
|
-import java.sql.ResultSetMetaData;
|
|
|
|
13
|
-import java.sql.SQLException;
|
|
|
|
14
|
-import java.util.ArrayList;
|
|
|
|
15
|
-import java.util.HashMap;
|
|
|
|
16
|
-import java.util.List;
|
|
|
|
17
|
-import java.util.Map;
|
|
|
|
18
|
-
|
|
|
|
19
|
-import dev.langchain4j.store.embedding.EmbeddingStore; // LangChain4j 的 EmbeddingStore 接口
|
|
|
|
20
|
-import dev.langchain4j.store.embedding.pgvector.PgVectorEmbeddingStore;
|
|
|
|
21
|
-import dev.langchain4j.data.embedding.Embedding;
|
|
|
|
22
|
-import dev.langchain4j.data.segment.TextSegment;
|
|
|
|
23
|
-import dev.langchain4j.model.embedding.EmbeddingModel;
|
|
|
|
24
|
-import dev.langchain4j.model.output.Response;
|
|
|
|
25
|
-import dev.langchain4j.store.embedding.EmbeddingMatch;
|
|
|
|
26
|
-import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
|
|
|
|
27
|
-import dev.langchain4j.store.embedding.EmbeddingSearchResult;
|
|
|
|
28
|
-import dev.langchain4j.store.embedding.EmbeddingStore;
|
|
|
|
29
|
-import dev.langchain4j.store.embedding.pgvector.PgVectorEmbeddingStore;
|
|
|
|
30
|
-import org.jeecg.common.api.vo.Result;
|
|
|
|
31
|
-import org.jeecg.common.system.query.QueryGenerator;
|
|
|
|
32
|
-
|
|
|
|
33
|
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
3
|
+import cn.hutool.core.lang.generator.SnowflakeGenerator;
|
|
34
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
4
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
35
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
5
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
6
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
7
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
36
|
import lombok.extern.slf4j.Slf4j;
|
8
|
import lombok.extern.slf4j.Slf4j;
|
|
37
|
-
|
|
|
|
38
|
-//import org.jeecg.modules.airag.app.config.PostgreEmbeddingStore;
|
|
|
|
39
|
-import org.jeecg.modules.airag.app.entity.Embeddings;
|
9
|
+import org.apache.poi.xwpf.usermodel.*;
|
|
|
|
10
|
+import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
|
|
11
|
+import org.jeecg.common.api.vo.Result;
|
|
|
|
12
|
+import org.jeecg.common.aspect.annotation.AutoLog;
|
|
40
|
import org.jeecg.modules.airag.app.entity.Embeddings;
|
13
|
import org.jeecg.modules.airag.app.entity.Embeddings;
|
|
41
|
import org.jeecg.modules.airag.app.service.IEmbeddingsService;
|
14
|
import org.jeecg.modules.airag.app.service.IEmbeddingsService;
|
|
42
|
-import org.jeecg.common.system.base.controller.JeecgController;
|
|
|
|
43
|
-import org.jeecg.modules.airag.app.utils.AiModelUtils;
|
|
|
|
44
|
-import org.jeecg.modules.airag.llm.entity.AiragModel;
|
|
|
|
45
|
-import org.jeecg.modules.airag.llm.handler.EmbeddingHandler;
|
|
|
|
46
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
47
|
-import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
48
|
-import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
|
|
49
|
import org.springframework.web.bind.annotation.*;
|
16
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
17
|
+import org.springframework.web.multipart.MultipartFile;
|
|
50
|
import org.springframework.web.servlet.ModelAndView;
|
18
|
import org.springframework.web.servlet.ModelAndView;
|
|
51
|
-import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
52
|
-import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
53
|
-import org.jeecg.common.aspect.annotation.AutoLog;
|
|
|
|
54
|
-import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
|
|
55
|
|
19
|
|
|
56
|
- /**
|
|
|
|
57
|
- * @Description: Embeddings
|
|
|
|
58
|
- * @Author: jeecg-boot
|
|
|
|
59
|
- * @Date: 2025-05-26
|
|
|
|
60
|
- * @Version: V1.0
|
|
|
|
61
|
- */
|
20
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
21
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
22
|
+import java.sql.SQLException;
|
|
|
|
23
|
+import java.util.*;
|
|
|
|
24
|
+
|
|
|
|
25
|
+/**
|
|
|
|
26
|
+* @Description: Embeddings
|
|
|
|
27
|
+* @Author: jeecg-boot
|
|
|
|
28
|
+* @Date: 2025-05-26
|
|
|
|
29
|
+* @Version: V1.0
|
|
|
|
30
|
+*/
|
|
62
|
@Tag(name="Embeddings")
|
31
|
@Tag(name="Embeddings")
|
|
63
|
@RestController
|
32
|
@RestController
|
|
64
|
@RequestMapping("/embeddings/embeddings")
|
33
|
@RequestMapping("/embeddings/embeddings")
|
|
65
|
@Slf4j
|
34
|
@Slf4j
|
|
66
|
public class EmbeddingsController {
|
35
|
public class EmbeddingsController {
|
|
67
|
- @Autowired
|
|
|
|
68
|
- private IEmbeddingsService embeddingsService;
|
|
|
|
69
|
-
|
|
|
|
70
|
- @Autowired
|
|
|
|
71
|
- private AiModelUtils aiModelUtils;
|
|
|
|
72
|
-
|
|
|
|
73
|
- @Autowired
|
|
|
|
74
|
- private EmbeddingHandler embeddingHandler;
|
|
|
|
75
|
-
|
|
|
|
76
|
-
|
|
|
|
77
|
- /**
|
|
|
|
78
|
- * 分页列表查询
|
|
|
|
79
|
- *
|
|
|
|
80
|
- * @param Embeddings
|
|
|
|
81
|
- * @param pageNo
|
|
|
|
82
|
- * @param pageSize
|
|
|
|
83
|
- * @param req
|
|
|
|
84
|
- * @return
|
|
|
|
85
|
- */
|
|
|
|
86
|
- //@AutoLog(value = "Embeddings-分页列表查询")
|
|
|
|
87
|
- @Operation(summary="Embeddings-分页列表查询")
|
|
|
|
88
|
- @GetMapping(value = "/list")
|
|
|
|
89
|
- public Result<IPage<Embeddings>> queryPageList(Embeddings Embeddings,
|
|
|
|
90
|
- @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
|
|
|
|
91
|
- @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
|
|
|
|
92
|
- HttpServletRequest req) throws NoSuchFieldException, IllegalAccessException, SQLException {
|
|
|
|
93
|
-// AiragModel airagModel = new AiragModel();
|
|
|
|
94
|
-// airagModel.setId("1925730210204721154");
|
|
|
|
95
|
-// airagModel.setProvider("OLLAMA");
|
|
|
|
96
|
-// airagModel.setModelName("nomic-embed-text");
|
|
|
|
97
|
-// airagModel.setBaseUrl("http://localhost:11434");
|
|
|
|
98
|
-// EmbeddingStore<TextSegment> embedStore = embeddingHandler.getEmbedStore(airagModel);
|
|
|
|
99
|
-// embeddingHandler.searchEmbedding()
|
|
|
|
100
|
- Response<Embedding> embedding = aiModelUtils.getEmbedding("1925730210204721154", "33333");
|
|
|
|
101
|
-
|
|
|
|
102
|
- List<Embeddings> records = embeddingsService.findAll();
|
|
|
|
103
|
- for (Embeddings record : records) {
|
|
|
|
104
|
- System.out.println("record = " + record);
|
|
|
|
105
|
- }
|
|
|
|
106
|
- Page<Embeddings> page = new Page<Embeddings>(pageNo, pageSize);
|
|
|
|
107
|
- page.setRecords(records);
|
|
|
|
108
|
- return Result.OK(page);
|
|
|
|
109
|
-
|
|
|
|
110
|
- }
|
|
|
|
111
|
-
|
|
|
|
112
|
- /**
|
|
|
|
113
|
- * 添加
|
|
|
|
114
|
- *
|
|
|
|
115
|
- * @param Embeddings
|
|
|
|
116
|
- * @return
|
|
|
|
117
|
- */
|
|
|
|
118
|
- @AutoLog(value = "Embeddings-添加")
|
|
|
|
119
|
- @Operation(summary="Embeddings-添加")
|
|
|
|
120
|
- @RequiresPermissions("Embeddings:Embeddings:add")
|
|
|
|
121
|
- @PostMapping(value = "/add")
|
|
|
|
122
|
- public Result<String> add(@RequestBody Embeddings Embeddings) {
|
36
|
+ @Autowired
|
|
|
|
37
|
+ private IEmbeddingsService embeddingsService;
|
|
|
|
38
|
+
|
|
|
|
39
|
+ /*@Autowired
|
|
|
|
40
|
+ private AiModelUtils aiModelUtils;
|
|
|
|
41
|
+*/
|
|
|
|
42
|
+
|
|
|
|
43
|
+ /**
|
|
|
|
44
|
+ * 分页列表查询
|
|
|
|
45
|
+ *
|
|
|
|
46
|
+ * @param embeddings
|
|
|
|
47
|
+ * @param pageNo
|
|
|
|
48
|
+ * @param pageSize
|
|
|
|
49
|
+ * @param req
|
|
|
|
50
|
+ * @return
|
|
|
|
51
|
+ */
|
|
|
|
52
|
+ //@AutoLog(value = "Embeddings-分页列表查询")
|
|
|
|
53
|
+ @Operation(summary = "Embeddings-分页列表查询")
|
|
|
|
54
|
+ @GetMapping(value = "/list")
|
|
|
|
55
|
+ public Result<IPage<Embeddings>> queryPageList(Embeddings embeddings,
|
|
|
|
56
|
+ @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
|
|
|
|
57
|
+ @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
|
|
|
|
58
|
+ HttpServletRequest req) throws NoSuchFieldException, IllegalAccessException, SQLException {
|
|
|
|
59
|
+ //Response<Embedding> embedding = aiModelUtils.getEmbedding("1925730210204721154", "33333");
|
|
|
|
60
|
+
|
|
|
|
61
|
+ List<Embeddings> records = embeddingsService.findAll(embeddings);
|
|
|
|
62
|
+ for (Embeddings record : records) {
|
|
|
|
63
|
+ System.out.println("record = " + record);
|
|
|
|
64
|
+ }
|
|
|
|
65
|
+ Page<Embeddings> page = new Page<Embeddings>(pageNo, pageSize);
|
|
|
|
66
|
+ page.setRecords(records);
|
|
|
|
67
|
+ return Result.OK(page);
|
|
|
|
68
|
+ }
|
|
|
|
69
|
+
|
|
|
|
70
|
+ /**
|
|
|
|
71
|
+ * 添加
|
|
|
|
72
|
+ *
|
|
|
|
73
|
+ * @param embeddings
|
|
|
|
74
|
+ * @return
|
|
|
|
75
|
+ */
|
|
|
|
76
|
+ @AutoLog(value = "Embeddings-添加")
|
|
|
|
77
|
+ @Operation(summary = "Embeddings-添加")
|
|
|
|
78
|
+ @RequiresPermissions("embeddings:embeddings:add")
|
|
|
|
79
|
+ @PostMapping(value = "/add")
|
|
|
|
80
|
+ public Result<String> add(@RequestBody Embeddings embeddings) {
|
|
123
|
// embeddingsService.save(Embeddings);
|
81
|
// embeddingsService.save(Embeddings);
|
|
124
|
- return Result.OK("添加成功!");
|
|
|
|
125
|
- }
|
|
|
|
126
|
-
|
|
|
|
127
|
- /**
|
|
|
|
128
|
- * 编辑
|
|
|
|
129
|
- *
|
|
|
|
130
|
- * @param Embeddings
|
|
|
|
131
|
- * @return
|
|
|
|
132
|
- */
|
|
|
|
133
|
- @AutoLog(value = "Embeddings-编辑")
|
|
|
|
134
|
- @Operation(summary="Embeddings-编辑")
|
|
|
|
135
|
- @RequiresPermissions("Embeddings:Embeddings:edit")
|
|
|
|
136
|
- @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
|
|
|
|
137
|
- public Result<String> edit(@RequestBody Embeddings Embeddings) {
|
82
|
+ // 1. 构建完整的metadata
|
|
|
|
83
|
+ Map<String, Object> metadata = new HashMap<>();
|
|
|
|
84
|
+ SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
|
|
|
|
85
|
+ String knowledgeId = String.valueOf(snowflakeGenerator.next());
|
|
|
|
86
|
+ metadata.put("knowledgeId", knowledgeId); // 使用前端传入的知识库ID
|
|
|
|
87
|
+ metadata.put("docName", embeddings.getDocName());
|
|
|
|
88
|
+ String docId = String.valueOf(snowflakeGenerator.next());
|
|
|
|
89
|
+ metadata.put("docId", docId); // 自动生成唯一文档ID
|
|
|
|
90
|
+ metadata.put("index", "0"); // 默认索引位置为0
|
|
|
|
91
|
+ // 2. 设置到embeddings对象
|
|
|
|
92
|
+ embeddings.setMetadata(metadata);
|
|
|
|
93
|
+
|
|
|
|
94
|
+ /*// 3. 生成向量嵌入(实际项目中应调用嵌入模型API)
|
|
|
|
95
|
+ embeddings.setEmbedding(generateEmbedding(embeddings.getText()));
|
|
|
|
96
|
+*/
|
|
|
|
97
|
+ System.out.println(new SnowflakeGenerator().next());
|
|
|
|
98
|
+
|
|
|
|
99
|
+ embeddingsService.insert(embeddings);
|
|
|
|
100
|
+ return Result.OK("添加成功!");
|
|
|
|
101
|
+ }
|
|
|
|
102
|
+
|
|
|
|
103
|
+ /**
|
|
|
|
104
|
+ * 编辑
|
|
|
|
105
|
+ *
|
|
|
|
106
|
+ * @param embeddings
|
|
|
|
107
|
+ * @return
|
|
|
|
108
|
+ */
|
|
|
|
109
|
+ @AutoLog(value = "Embeddings-编辑")
|
|
|
|
110
|
+ @Operation(summary = "Embeddings-编辑")
|
|
|
|
111
|
+ @RequiresPermissions("embeddings:embeddings:edit")
|
|
|
|
112
|
+ @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
|
|
|
|
113
|
+ public Result<String> edit(@RequestBody Embeddings embeddings) {
|
|
138
|
// embeddingsService.updateById(Embeddings);
|
114
|
// embeddingsService.updateById(Embeddings);
|
|
139
|
- return Result.OK("编辑成功!");
|
|
|
|
140
|
- }
|
|
|
|
141
|
-
|
|
|
|
142
|
- /**
|
|
|
|
143
|
- * 通过id删除
|
|
|
|
144
|
- *
|
|
|
|
145
|
- * @param id
|
|
|
|
146
|
- * @return
|
|
|
|
147
|
- */
|
|
|
|
148
|
- @AutoLog(value = "Embeddings-通过id删除")
|
|
|
|
149
|
- @Operation(summary="Embeddings-通过id删除")
|
|
|
|
150
|
- @RequiresPermissions("Embeddings:Embeddings:delete")
|
|
|
|
151
|
- @DeleteMapping(value = "/delete")
|
|
|
|
152
|
- public Result<String> delete(@RequestParam(name="id",required=true) String id) {
|
|
|
|
153
|
-// embeddingsService.removeById(id);
|
|
|
|
154
|
- return Result.OK("删除成功!");
|
|
|
|
155
|
- }
|
|
|
|
156
|
-
|
|
|
|
157
|
- /**
|
|
|
|
158
|
- * 批量删除
|
|
|
|
159
|
- *
|
|
|
|
160
|
- * @param ids
|
|
|
|
161
|
- * @return
|
|
|
|
162
|
- */
|
|
|
|
163
|
- @AutoLog(value = "Embeddings-批量删除")
|
|
|
|
164
|
- @Operation(summary="Embeddings-批量删除")
|
|
|
|
165
|
- @RequiresPermissions("Embeddings:Embeddings:deleteBatch")
|
|
|
|
166
|
- @DeleteMapping(value = "/deleteBatch")
|
|
|
|
167
|
- public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
|
115
|
+ embeddingsService.update(embeddings);
|
|
|
|
116
|
+ return Result.OK("编辑成功!");
|
|
|
|
117
|
+ }
|
|
|
|
118
|
+
|
|
|
|
119
|
+ /**
|
|
|
|
120
|
+ * 通过id删除
|
|
|
|
121
|
+ *
|
|
|
|
122
|
+ * @param id
|
|
|
|
123
|
+ * @return
|
|
|
|
124
|
+ */
|
|
|
|
125
|
+ @AutoLog(value = "Embeddings-通过id删除")
|
|
|
|
126
|
+ @Operation(summary = "Embeddings-通过id删除")
|
|
|
|
127
|
+ @RequiresPermissions("embeddings:embeddings:delete")
|
|
|
|
128
|
+ @DeleteMapping(value = "/delete")
|
|
|
|
129
|
+ public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
|
|
|
|
130
|
+ //embeddingsService.removeById(id);
|
|
|
|
131
|
+ embeddingsService.deleteById(id);
|
|
|
|
132
|
+ return Result.OK("删除成功!");
|
|
|
|
133
|
+ }
|
|
|
|
134
|
+
|
|
|
|
135
|
+ /**
|
|
|
|
136
|
+ * 批量删除
|
|
|
|
137
|
+ *
|
|
|
|
138
|
+ * @param ids
|
|
|
|
139
|
+ * @return
|
|
|
|
140
|
+ */
|
|
|
|
141
|
+ @AutoLog(value = "Embeddings-批量删除")
|
|
|
|
142
|
+ @Operation(summary = "Embeddings-批量删除")
|
|
|
|
143
|
+ @RequiresPermissions("embeddings:embeddings:deleteBatch")
|
|
|
|
144
|
+ @DeleteMapping(value = "/deleteBatch")
|
|
|
|
145
|
+ public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
|
|
168
|
// this.embeddingsService.removeByIds(Arrays.asList(ids.split(",")));
|
146
|
// this.embeddingsService.removeByIds(Arrays.asList(ids.split(",")));
|
|
169
|
- return Result.OK("批量删除成功!");
|
|
|
|
170
|
- }
|
|
|
|
171
|
-
|
|
|
|
172
|
- /**
|
|
|
|
173
|
- * 通过id查询
|
|
|
|
174
|
- *
|
|
|
|
175
|
- * @param id
|
|
|
|
176
|
- * @return
|
|
|
|
177
|
- */
|
|
|
|
178
|
- //@AutoLog(value = "Embeddings-通过id查询")
|
|
|
|
179
|
- @Operation(summary="Embeddings-通过id查询")
|
|
|
|
180
|
- @GetMapping(value = "/queryById")
|
|
|
|
181
|
- public Result<Embeddings> queryById(@RequestParam(name="id",required=true) String id) {
|
147
|
+ return Result.OK("批量删除成功!");
|
|
|
|
148
|
+ }
|
|
|
|
149
|
+
|
|
|
|
150
|
+ /**
|
|
|
|
151
|
+ * 通过id查询
|
|
|
|
152
|
+ *
|
|
|
|
153
|
+ * @param id
|
|
|
|
154
|
+ * @return
|
|
|
|
155
|
+ */
|
|
|
|
156
|
+ //@AutoLog(value = "Embeddings-通过id查询")
|
|
|
|
157
|
+ @Operation(summary = "Embeddings-通过id查询")
|
|
|
|
158
|
+ @GetMapping(value = "/queryById")
|
|
|
|
159
|
+ public Result<Embeddings> queryById(@RequestParam(name = "id", required = true) String id) {
|
|
182
|
// Embeddings Embeddings = embeddingsService.getById(id);
|
160
|
// Embeddings Embeddings = embeddingsService.getById(id);
|
|
183
|
// if(Embeddings==null) {
|
161
|
// if(Embeddings==null) {
|
|
184
|
// return Result.error("未找到对应数据");
|
162
|
// return Result.error("未找到对应数据");
|
|
185
|
// }
|
163
|
// }
|
|
186
|
- return Result.OK();
|
|
|
|
187
|
- }
|
164
|
+ embeddingsService.findById(id);
|
|
|
|
165
|
+ return Result.OK();
|
|
|
|
166
|
+ }
|
|
188
|
|
167
|
|
|
189
|
/**
|
168
|
/**
|
|
190
|
- * 导出excel
|
|
|
|
191
|
- *
|
|
|
|
192
|
- * @param request
|
|
|
|
193
|
- * @param Embeddings
|
|
|
|
194
|
- */
|
169
|
+ * 导出excel
|
|
|
|
170
|
+ *
|
|
|
|
171
|
+ * @param request
|
|
|
|
172
|
+ * @param Embeddings
|
|
|
|
173
|
+ */
|
|
195
|
@RequiresPermissions("Embeddings:Embeddings:exportXls")
|
174
|
@RequiresPermissions("Embeddings:Embeddings:exportXls")
|
|
196
|
@RequestMapping(value = "/exportXls")
|
175
|
@RequestMapping(value = "/exportXls")
|
|
197
|
public ModelAndView exportXls(HttpServletRequest request, Embeddings Embeddings) {
|
176
|
public ModelAndView exportXls(HttpServletRequest request, Embeddings Embeddings) {
|
|
@@ -199,16 +178,118 @@ public class EmbeddingsController { |
|
@@ -199,16 +178,118 @@ public class EmbeddingsController { |
|
199
|
}
|
178
|
}
|
|
200
|
|
179
|
|
|
201
|
/**
|
180
|
/**
|
|
202
|
- * 通过excel导入数据
|
|
|
|
203
|
- *
|
|
|
|
204
|
- * @param request
|
|
|
|
205
|
- * @param response
|
|
|
|
206
|
- * @return
|
|
|
|
207
|
- */
|
181
|
+ * 通过excel导入数据
|
|
|
|
182
|
+ *
|
|
|
|
183
|
+ * @param request
|
|
|
|
184
|
+ * @param response
|
|
|
|
185
|
+ * @return
|
|
|
|
186
|
+ */
|
|
208
|
@RequiresPermissions("Embeddings:Embeddings:importExcel")
|
187
|
@RequiresPermissions("Embeddings:Embeddings:importExcel")
|
|
209
|
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
|
188
|
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
|
|
210
|
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
|
189
|
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
|
|
|
|
190
|
+
|
|
|
|
191
|
+
|
|
211
|
return null;
|
192
|
return null;
|
|
212
|
}
|
193
|
}
|
|
213
|
|
194
|
|
|
|
|
195
|
+
|
|
|
|
196
|
+ // EmbeddingsController.java 新增方法
|
|
|
|
197
|
+ @AutoLog(value = "Embeddings-导入Word文档")
|
|
|
|
198
|
+ @Operation(summary = "Embeddings-导入Word文档")
|
|
|
|
199
|
+ @RequiresPermissions("embeddings:embeddings:importWord")
|
|
|
|
200
|
+ @PostMapping(value = "/importWord")
|
|
|
|
201
|
+ public Result<Map<String, Object>> importWord(@RequestParam(value = "file", required = false) MultipartFile file) {
|
|
|
|
202
|
+ try {
|
|
|
|
203
|
+ // 1. 验证文件
|
|
|
|
204
|
+ if (file.isEmpty()) {
|
|
|
|
205
|
+ return Result.error("上传文件为空");
|
|
|
|
206
|
+ }
|
|
|
|
207
|
+
|
|
|
|
208
|
+ String fileName = file.getOriginalFilename();
|
|
|
|
209
|
+ String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
|
210
|
+
|
|
|
|
211
|
+ if (!"doc".equals(fileExtension) && !"docx".equals(fileExtension)) {
|
|
|
|
212
|
+ return Result.error("仅支持.doc和.docx格式的Word文档");
|
|
|
|
213
|
+ }
|
|
|
|
214
|
+
|
|
|
|
215
|
+
|
|
|
|
216
|
+ if (fileName != null) {
|
|
|
|
217
|
+ // 去掉后缀
|
|
|
|
218
|
+ int dotIndex = fileName.lastIndexOf('.');
|
|
|
|
219
|
+ fileName = fileName.substring(0, dotIndex);
|
|
|
|
220
|
+ }
|
|
|
|
221
|
+
|
|
|
|
222
|
+
|
|
|
|
223
|
+ // 2. 解析Word文档
|
|
|
|
224
|
+ String fullContent = readFullWordContent(file);
|
|
|
|
225
|
+
|
|
|
|
226
|
+ if (fullContent.isEmpty()) {
|
|
|
|
227
|
+ return Result.error("文档内容为空,无法导入");
|
|
|
|
228
|
+ }
|
|
|
|
229
|
+
|
|
|
|
230
|
+ // 3. 准备元数据
|
|
|
|
231
|
+ SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
|
|
|
|
232
|
+ String knowledgeId = String.valueOf(snowflakeGenerator.next());
|
|
|
|
233
|
+ String docId = String.valueOf(snowflakeGenerator.next());
|
|
|
|
234
|
+ String docName = fileName;
|
|
|
|
235
|
+
|
|
|
|
236
|
+ Map<String, Object> metadata = new HashMap<>();
|
|
|
|
237
|
+ metadata.put("knowledgeId", knowledgeId);
|
|
|
|
238
|
+ metadata.put("docName", docName);
|
|
|
|
239
|
+ metadata.put("docId", docId);
|
|
|
|
240
|
+
|
|
|
|
241
|
+ // 4. 创建并保存Embeddings对象
|
|
|
|
242
|
+ Embeddings embeddings = new Embeddings();
|
|
|
|
243
|
+
|
|
|
|
244
|
+ embeddings.setText(fullContent);
|
|
|
|
245
|
+ embeddings.setId(UUID.randomUUID().toString());
|
|
|
|
246
|
+ embeddings.setMetadata(metadata);
|
|
|
|
247
|
+ // 设置向量嵌入(实际项目中应调用模型API)
|
|
|
|
248
|
+ embeddings.setEmbedding(generateRandomEmbedding());
|
|
|
|
249
|
+
|
|
|
|
250
|
+ // 保存到数据库
|
|
|
|
251
|
+ embeddingsService.insert(embeddings);
|
|
|
|
252
|
+ return Result.OK("导入成功");
|
|
|
|
253
|
+
|
|
|
|
254
|
+ } catch (Exception e) {
|
|
|
|
255
|
+ log.error("Word导入失败", e);
|
|
|
|
256
|
+ return Result.error("导入失败: " + e.getMessage());
|
|
|
|
257
|
+ }
|
|
|
|
258
|
+ }
|
|
|
|
259
|
+
|
|
|
|
260
|
+ // 读取整个Word内容作为单个字符串
|
|
|
|
261
|
+ private String readFullWordContent(MultipartFile file) throws Exception {
|
|
|
|
262
|
+ try (XWPFDocument doc = new XWPFDocument(file.getInputStream())) {
|
|
|
|
263
|
+ StringBuilder fullContent = new StringBuilder();
|
|
|
|
264
|
+
|
|
|
|
265
|
+ // 读取所有段落文本
|
|
|
|
266
|
+ for (XWPFParagraph p : doc.getParagraphs()) {
|
|
|
|
267
|
+ fullContent.append(p.getText()).append("\n");
|
|
|
|
268
|
+ }
|
|
|
|
269
|
+
|
|
|
|
270
|
+ // 读取所有表格文本
|
|
|
|
271
|
+ for (XWPFTable table : doc.getTables()) {
|
|
|
|
272
|
+ for (XWPFTableRow row : table.getRows()) {
|
|
|
|
273
|
+ for (XWPFTableCell cell : row.getTableCells()) {
|
|
|
|
274
|
+ fullContent.append(cell.getText()).append("\t");
|
|
|
|
275
|
+ }
|
|
|
|
276
|
+ fullContent.append("\n");
|
|
|
|
277
|
+ }
|
|
|
|
278
|
+ }
|
|
|
|
279
|
+
|
|
|
|
280
|
+ return fullContent.toString();
|
|
|
|
281
|
+ }
|
|
|
|
282
|
+ }
|
|
|
|
283
|
+
|
|
|
|
284
|
+
|
|
|
|
285
|
+ // 生成随机向量(1536维)
|
|
|
|
286
|
+ private float[] generateRandomEmbedding() {
|
|
|
|
287
|
+ float[] embedding = new float[768];
|
|
|
|
288
|
+ Random random = new Random();
|
|
|
|
289
|
+ for (int i = 0; i < embedding.length; i++) {
|
|
|
|
290
|
+ embedding[i] = random.nextFloat() * 2 - 1;
|
|
|
|
291
|
+ }
|
|
|
|
292
|
+ return embedding;
|
|
|
|
293
|
+
|
|
|
|
294
|
+ }
|
|
214
|
} |
295
|
} |