- 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
1.8 KiB
1.8 KiB
重构计划:统一 backend/llm.py 和 backend/main.py
目标
消除 llm.py 和 main.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 构建逻辑 |