← 返回首页 📖 📖 Wiki知识库
📐 学习维度 本篇是 📖 📖 Wiki知识库 🏠 首页 🛤️ 学习路径 📚 全部文章

AI Coding 助手与代码补全大模型深度实战 - 11. Spring Boot 工程化实现

本文是 AI Coding 助手与代码补全大模型深度实战 的分章阅读版。

11. Spring Boot 工程化实现

11.1 整体架构


┌──────────────────────────────────────────────────────────┐
│                IDE Plugin / REST API                     │
└────────────────────────────┬─────────────────────────────┘
                             │
                  ┌──────────▼──────────┐
                  │ CodingFacade        │
                  └──────────┬──────────┘
                             │
       ┌─────────────────────┼─────────────────────┐
       │                     │                     │
┌──────▼──────┐    ┌─────────▼─────────┐  ┌────────▼────────┐
│ Completion  │    │ Chat / Edit       │  │ Agent           │
│ - FIM       │    │ - Repo Context    │  │ - Multi-step    │
│ - Context   │    │ - RAG             │  │ - Tool Calling  │
└─────────────┘    └─────────┬─────────┘  └────────┬────────┘
                             │                     │
                  ┌──────────▼─────────────────────▼──┐
                  │      Model Router                 │
                  │ - DeepSeek-Coder / Qwen / Claude  │
                  └──────────┬────────────────────────┘
                             │
                  ┌──────────▼──────────┐
                  │ Code Indexer        │
                  │ - AST Chunker       │
                  │ - Vector Store      │
                  └─────────────────────┘

11.2 Completion Controller


@RestController
@RequestMapping("/api/completion")
public class CompletionController {

    private final CompletionFacade facade;
    
    @PostMapping
    public CompletionResponse complete(@RequestBody CompletionRequest req, Principal principal) {
        return facade.complete(req, principal.getName());
    }
    
    @PostMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> stream(@RequestBody CompletionRequest req, Principal principal) {
        return facade.streamComplete(req, principal.getName())
            .map(token -> ServerSentEvent.<String>builder(token).event("token").build());
    }
}

public record CompletionRequest(
    String filePath,
    String fileContent,
    int cursorLine,
    int cursorColumn,
    String language,
    CompletionType type,  // INLINE / MULTILINE / BLOCK
    boolean enableRAG
) {}

public record CompletionResponse(
    String completion,
    String model,
    int tokens,
    long latencyMs
) {}

11.3 Chat Controller


@RestController
@RequestMapping("/api/chat")
public class CodingChatController {

    private final CodingChatService chatService;
    
    @PostMapping
    public Flux<ServerSentEvent<String>> chat(@RequestBody ChatRequest req, Principal principal) {
        return chatService.chat(req, principal.getName())
            .map(token -> ServerSentEvent.<String>builder(token).event("token").build());
    }
}

public record ChatRequest(
    String sessionId,
    String message,
    List<String> selectedFiles,
    String selectionText,
    ChatMode mode  // ASK / EDIT / GENERATE
) {}

11.4 Completion Service


@Service
public class CompletionFacade {

    private final FIMCompletionService fimService;
    private final RepoLevelContextBuilder contextBuilder;
    private final ModelRouter modelRouter;
    private final CompletionCache cache;
    
    public CompletionResponse complete(CompletionRequest req, String userId) {
        long start = System.currentTimeMillis();
        
        // 1. 检查缓存
        Optional<CompletionResponse> cached = cache.lookup(req);
        if (cached.isPresent()) {
            return cached.get();
        }
        
        // 2. 提取光标上下文
        CursorContext cursor = extractCursor(req);
        
        // 3. (可选) RAG 增强
        if (req.enableRAG()) {
            String ragContext = contextBuilder.buildRepoContext(req, cursor);
            cursor = cursor.withExtraContext(ragContext);
        }
        
        // 4. 选择模型
        ModelChoice model = modelRouter.choose(req.language(), req.type());
        
        // 5. FIM 调用
        String completion = fimService.complete(
            cursor.prefix(), cursor.suffix(), model
        );
        
        // 6. 后处理 (移除 markdown,截断)
        completion = postProcess(completion, req);
        
        long latency = System.currentTimeMillis() - start;
        CompletionResponse resp = new CompletionResponse(completion, model.name(), 
            estimateTokens(completion), latency);
        
        // 7. 缓存
        cache.put(req, resp);
        
        return resp;
    }
    
    private String postProcess(String completion, CompletionRequest req) {
        // 移除可能的 ``` 代码块标记
        completion = completion.replaceAll("```[a-z]*\\n?", "").replaceAll("```", "");
        
        // 截断到合理长度
        if (completion.length() > 500) {
            completion = completion.substring(0, 500);
        }
        
        return completion;
    }
}

11.5 模型路由


@Service
public class ModelRouter {

    private final Map<String, CodeModelClient> models;
    
    public ModelChoice choose(String language, CompletionType type) {
        // 简单策略:本地模型优先,复杂任务用云端
        if (type == CompletionType.INLINE) {
            // 单行:本地小模型
            return ModelChoice.of("deepseek-coder-1.3b", false);
        }
        if (type == CompletionType.MULTILINE) {
            // 多行:本地中模型
            return ModelChoice.of("deepseek-coder-6.7b", false);
        }
        // 块级:云端大模型
        return ModelChoice.of("deepseek-coder-v2", true);
    }
}

11.6 配置


ai:
  coding:
    completion:
      provider: deepseek
      deepseek:
        api-key: ${DEEPSEEK_API_KEY}
        model: deepseek-coder
        endpoint: https://api.deepseek.com/coder
      qwen:
        api-key: ${DASHSCOPE_API_KEY}
        model: qwen2.5-coder-32b-instruct
      local:
        enabled: true
        model: deepseek-coder-6.7b
        endpoint: http://localhost:8000
    chat:
      provider: claude
      claude:
        api-key: ${ANTHROPIC_API_KEY}
        model: claude-3-5-sonnet-20241022
    agent:
      max-iterations: 20
      max-context-tokens: 100000
      sandbox-enabled: true
    rag:
      enabled: true
      embedding-model: bge-code-1.5
      chunk-strategy: ast
      max-snippets: 10
    cache:
      enabled: true
      ttl-hours: 6


相关页面