大模型

ragflow4j 开发记录 - 2025-03-10

ragflow4j 开发记录 - 2025-03-10

大家好,我是Aaron。当前在实践”辅助AI编程”,目前有个比较火的词语是”干中学”。因此,我希望能通过一步步完善项目,来掌握RAG相关知识。因此正在开发一个名为ragflow4j(https://github.com/aaronshan/ragflow4j) 的项目。为了方便记录,我会在每天结束时让AI总结今天的开发进展,同步到公众号。

欢迎大家关注我关注公众号,第一时间收到更新推送,一步一步一起开发RAG引擎。

项目简介 📖

ragflow4j(https://github.com/aaronshan/ragflow4j) 是一个开源的 RAG(检索增强生成)引擎。与 Dify、Ragflow 等不同,它是用 Java 开发的。市面上类似的产品大多使用 Python 开发。我希望通过这个项目能帮助以 Java 为主要开发语言的开发者更好地理解 RAG 的整体机制。

今日工作内容 ✨

今天主要完成了Knowledge Service的整体架构设计和基础功能实现,主要包括以下几个方面:

  1. 1. RESTful API设计
  • 设计了符合RESTful规范的API接口

  • 实现了统一的请求响应格式

  • 添加了Swagger API文档支持

  1. 2. 知识库管理功能
  • 实现了KnowledgeController,提供文档上传、查询、删除等基础操作

  • 设计了文档处理的状态流转机制

  • 实现了异步文档处理流程

  1. 3. 核心代码展示
@RestController
@RequestMapping("/knowledge")
@Api(tags = "知识管理", description = "知识库文档管理接口")
public class KnowledgeController {
    private final KnowledgeService knowledgeService;
    private final VectorStore vectorStore;

    @PostMapping("/documents/upload")
    @ApiOperation("上传文档到知识库")
    public ResponseEntity<Document> uploadDocument(
            @RequestParam("file") MultipartFile file,
            @RequestParam("knowledgeBaseId") UUID knowledgeBaseId) {
        // 保存文件并创建文档记录
        Document document = knowledgeService.saveDocument(file, knowledgeBaseId);

        // 异步处理文档
        processDocumentAsync(document);

        return new ResponseEntity<>(document, HttpStatus.CREATED);
    }

    private CompletableFuture<Void> processDocumentAsync(Document document) {
        return CompletableFuture.runAsync(() -> {
            // 1. 解析文档
            String content = documentParser.parse(document);

            // 2. 切分文档
            List<String> chunks = documentSplitter.split(content);

            // 3. 向量化处理
            List<float[]> vectors = vectorizer.vectorize(chunks);

            // 4. 存储向量
            vectorStore.store(document.getId(), vectors, chunks);
        });
    }
}
  1. 4. 服务监控实现
  • 集成了Spring Boot Actuator

  • 添加了自定义健康检查指标

  • 实现了基础性能监控

  1. 5. 事务管理
  • 使用Spring声明式事务

  • 实现了分布式事务支持

  • 添加了事务回滚机制

  1. 6. 代码提交记录
commit c1f0cb414e0295aa60acf0ad3745c05ba0d59a84
feat(knowledge): 实现知识库管理功能
link https://github.com/aaronshan/ragflow4j/commit/c1f0cb414e0295aa60acf0ad3745c05ba0d59a84

明日开发重点🎯

  1. 1. LLM集成接口设计
  • 设计统一的模型调用接口

  • 实现Java 8异步调用封装

  • 设计模型结果缓存机制

  1. 2. OpenAI API集成
  • 实现OpenAI Java SDK封装

  • 配置API密钥管理

  • 实现请求限流机制

Tips:最近有些忙,可能进度不会保持之前预期的节奏,但会尽量保障每天有一些进展。

温馨提示:欢迎关注公众号【猿悟人生】获取最新开发进展

本文首发于微信公众号。原文:查看原文
微信搜一搜 AI拆解猿