feat(project): 实现AI项目初始化及文档解析功能

- 新增DocumentParserUtil工具类,支持PDF、Word、Excel、Markdown及文本解析
- 基于MinIO实现OssService,支持文件上传、下载、删除及URL生成
- 添加ProjectService实现,利用Spring AI ChatClient解析项目文档生成结构化数据
- 新增ProjectController,提供文件上传接口供项目初始化调用
- 配置开发环境application-dev.yaml,包含数据库、MinIO及Spring AI相关配置
- 添加pom.xml,集成必要依赖如Spring AI、MinIO、Apache POI、PDFBox、Tika和Flexmark等组件
This commit is contained in:
2026-03-26 17:59:18 +08:00
parent 4656090683
commit 852cbd60a0
6 changed files with 342 additions and 42 deletions

View File

@@ -1,6 +1,7 @@
package cn.yinlihupo.service.oss.impl;
import cn.yinlihupo.common.config.MinioConfig;
import cn.yinlihupo.common.util.DocumentParserUtil;
import cn.yinlihupo.service.oss.OssService;
import io.minio.*;
import io.minio.errors.*;
@@ -61,22 +62,43 @@ public class OssServiceImpl implements OssService {
@Override
public String readFileAsString(String fileUrl) {
try (InputStream inputStream = getFileInputStream(fileUrl);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return outputStream.toString(StandardCharsets.UTF_8);
try (InputStream inputStream = getFileInputStream(fileUrl)) {
// 从 URL 中提取文件名
String fileName = extractFileNameFromUrl(fileUrl);
// 使用文档解析工具类解析文件内容
return DocumentParserUtil.parse(inputStream, fileName);
} catch (Exception e) {
log.error("读取文件内容失败: {}", e.getMessage(), e);
throw new RuntimeException("读取文件内容失败: " + e.getMessage(), e);
}
}
/**
* 从 URL 中提取文件名
*
* @param fileUrl 文件 URL
* @return 文件名
*/
private String extractFileNameFromUrl(String fileUrl) {
try {
URL url = new URL(fileUrl);
String path = url.getPath();
// 去掉开头的 /
if (path.startsWith("/")) {
path = path.substring(1);
}
// 获取最后一个 / 后面的文件名
int lastSlashIndex = path.lastIndexOf('/');
if (lastSlashIndex >= 0) {
return path.substring(lastSlashIndex + 1);
}
return path;
} catch (Exception e) {
log.warn("从 URL 提取文件名失败: {}", fileUrl);
return "unknown";
}
}
@Override
public InputStream getFileInputStream(String fileUrl) {
try {

View File

@@ -147,8 +147,10 @@ public class ProjectServiceImpl implements ProjectService {
public ProjectInitResult generateProjectFromContent(String content) {
log.info("开始根据内容生成项目初始化数据");
PromptTemplate promptTemplate = new PromptTemplate(USER_PROMPT_TEMPLATE);
String userPrompt = promptTemplate.createMessage(java.util.Map.of("content", content)).toString();
// 构建用户提示词,直接将内容嵌入
String userPrompt = "请根据以下项目资料,生成完整的项目初始化结构化数据:\n\n" +
content + "\n\n" +
"请严格按照系统提示词中的JSON格式输出确保所有字段都包含合理的值。";
return chatClient.prompt()
.system(PROJECT_INIT_SYSTEM_PROMPT)