Files
llm-in-text/plans/refactor-backend.md
“ydy0615” c64ff7be45 feat(copilot): enhance prompt system and add Markdown rendering for ghost text
- Rewrite prompt builder with comprehensive rules for seamless text completion
- Implement Markdown parsing for ghost text with proper mark handling
- Update LLM parameters (temperature 0.7, repeat_penalty, think mode)
- Add CSS styles for formatted ghost text elements
- Add planning documentation for Copilot prompt system analysis
2026-02-13 22:00:26 +08:00

1.8 KiB
Raw Blame History

重构计划:统一 backend/llm.py 和 backend/main.py

目标

消除 llm.pymain.py 之间的代码冗余,建立清晰的职责分离。

当前问题

graph LR
    A[llm.py] -->|流式调用| B[Ollama API]
    C[main.py] -->|非流式调用| B
    A -.->|未被使用| D[❌ 冗余]

重构后架构

graph LR
    A[main.py] -->|导入调用| B[llm.py]
    B -->|非流式调用| C[Ollama API]
    A --> D[FastAPI 路由处理]

具体步骤

步骤 1重构 llm.py

stream_openai 函数改为非流式调用,参考 main.py 的实现:

# 新的 llm.py 结构
async def call_ollama(prompt: str) -> dict:
    # 非流式调用
    # 返回 {"content": str, "thinking": str}

关键改动:

  • 移除 AsyncGenerator 类型,改为返回 dict
  • 设置 stream=False
  • 使用 temperature=0.2(与 main.py 一致)
  • 返回 content 和 thinking 字段

步骤 2重构 main.py

导入并使用 llm.py

# main.py 改动
from llm import call_ollama

@app.post("/v1/completions")
async def create_completion(request: CompletionRequest):
    prompt = build_prompt(request.prefix, request.suffix)
    result = await call_ollama(prompt)
    # 使用 result["content"] 和 result["thinking"]

删除的代码:

  • 直接导入 ollama 的代码
  • 重复创建 AsyncClient 的代码
  • 重复的 API 调用逻辑
  • 重复的环境变量读取

步骤 3清理冗余

  • 移除 llm.py 中不再需要的 AsyncGenerator 导入
  • 移除 main.py 中重复的环境变量定义
  • 确保调试日志保留但不过度

文件职责划分

文件 职责
llm.py Ollama API 调用封装、模型配置
main.py FastAPI 路由、请求解析、响应格式化
prompt.py Prompt 构建逻辑