Files
llm-in-text/plans/refactor-backend.md

78 lines
1.8 KiB
Markdown
Raw Normal View History

# 重构计划:统一 backend/llm.py 和 backend/main.py
## 目标
消除 `llm.py``main.py` 之间的代码冗余,建立清晰的职责分离。
## 当前问题
```mermaid
graph LR
A[llm.py] -->|流式调用| B[Ollama API]
C[main.py] -->|非流式调用| B
A -.->|未被使用| D[❌ 冗余]
```
## 重构后架构
```mermaid
graph LR
A[main.py] -->|导入调用| B[llm.py]
B -->|非流式调用| C[Ollama API]
A --> D[FastAPI 路由处理]
```
## 具体步骤
### 步骤 1重构 llm.py
`stream_openai` 函数改为非流式调用,参考 main.py 的实现:
```python
# 新的 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
```python
# 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 构建逻辑 |