feat: rag-git的api
This commit is contained in:
@@ -5,6 +5,9 @@ import com.storm.dev.api.IRAGService;
|
||||
import com.storm.dev.api.response.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
||||
import org.redisson.api.RList;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.ai.chat.ChatResponse;
|
||||
@@ -20,10 +23,15 @@ import org.springframework.ai.transformer.splitter.TokenTextSplitter;
|
||||
import org.springframework.ai.vectorstore.PgVectorStore;
|
||||
import org.springframework.ai.vectorstore.SearchRequest;
|
||||
import org.springframework.ai.vectorstore.SimpleVectorStore;
|
||||
import org.springframework.core.io.PathResource;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -113,4 +121,67 @@ public class RAGController implements IRAGService {
|
||||
log.info("测试结果:{}", call);
|
||||
return call;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "analyze_git_repository", method = RequestMethod.POST)
|
||||
@Override
|
||||
public Response<String> analyzeGitRepository(@RequestParam String repoUrl, @RequestParam String userName, @RequestParam String token) throws Exception {
|
||||
String localPath = "./git-cloned-repo";
|
||||
String repoProjectName = extractProjectName(repoUrl);
|
||||
log.info("克隆路径:{}", new File(localPath).getAbsolutePath());
|
||||
|
||||
FileUtils.deleteDirectory(new File(localPath));
|
||||
|
||||
Git git = Git.cloneRepository()
|
||||
.setURI(repoUrl)
|
||||
.setDirectory(new File(localPath))
|
||||
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, token))
|
||||
.call();
|
||||
|
||||
Files.walkFileTree(Paths.get(localPath), new SimpleFileVisitor<>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
log.info("{} 遍历解析路径,上传知识库:{}", repoProjectName, file.getFileName());
|
||||
try {
|
||||
TikaDocumentReader reader = new TikaDocumentReader(new PathResource(file));
|
||||
List<Document> documents = reader.get();
|
||||
List<Document> documentSplitterList = tokenTextSplitter.apply(documents);
|
||||
|
||||
documents.forEach(doc -> doc.getMetadata().put("knowledge", repoProjectName));
|
||||
|
||||
documentSplitterList.forEach(doc -> doc.getMetadata().put("knowledge", repoProjectName));
|
||||
|
||||
pgVectorStore.accept(documentSplitterList);
|
||||
} catch (Exception e) {
|
||||
log.error("遍历解析路径,上传知识库失败:{}", file.getFileName());
|
||||
}
|
||||
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
|
||||
log.info("Failed to access file: {} - {}", file.toString(), exc.getMessage());
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
|
||||
FileUtils.deleteDirectory(new File(localPath));
|
||||
|
||||
RList<String> elements = redissonClient.getList("ragTag");
|
||||
if (!elements.contains(repoProjectName)) {
|
||||
elements.add(repoProjectName);
|
||||
}
|
||||
|
||||
git.close();
|
||||
|
||||
log.info("遍历解析路径,上传完成:{}", repoUrl);
|
||||
|
||||
return Response.<String>builder().code("0000").info("调用成功").build();
|
||||
}
|
||||
|
||||
private String extractProjectName(String repoUrl) {
|
||||
String[] parts = repoUrl.split("/");
|
||||
String projectNameWithGit = parts[parts.length - 1];
|
||||
return projectNameWithGit.replace(".git", "");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user