Files
Aether/_deprecated_py_src/api/public/openai.py
fawney19 1d9c77522a refactor: 移除 Python 后端源码,全面迁移至 Rust gateway 架构
- 删除全部 Python 源码 (src/) 及 Alembic 迁移脚本,归档至 _deprecated_py_src/
- 重构 Rust gateway ai_pipeline: 拆分 planner/finalize 模块,新增 contracts/adaptation 层
- 重组 handlers 模块为 admin/public/proxy/internal/shared 子模块结构
- 新增 executor 模块,引入 Rust 原生数据库迁移 (aether-data/migrations)
- 简化 CI/Docker 构建流程,移除 base image 二级构建,统一为单一 app image
- 移除 Python 相关基础设施文件 (entrypoint.sh, gunicorn_conf.py, Dockerfile.base)
2026-04-03 16:26:16 +08:00

96 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
OpenAI API 端点
- /v1/chat/completions - OpenAI Chat API
- /v1/responses - OpenAI Responses API (CLI)
- /v1/responses/compact - OpenAI Responses Compaction API (CLI)
注意: /v1/models 端点由 models.py 统一处理,根据请求头返回对应格式
"""
from typing import Any
from fastapi import APIRouter, Depends, Request
from sqlalchemy.orm import Session
from src.api.base.pipeline import get_pipeline
from src.database import get_db
router = APIRouter(tags=["OpenAI API"])
pipeline = get_pipeline()
async def _run_openai_route_shell(adapter: Any, http_request: Request, db: Session) -> Any:
return await pipeline.run(
adapter=adapter,
http_request=http_request,
db=db,
mode=adapter.mode,
)
@router.post("/v1/chat/completions")
async def create_chat_completion(
http_request: Request,
db: Session = Depends(get_db),
) -> Any:
"""
OpenAI Chat Completions API
兼容 OpenAI Chat Completions API 格式的代理接口。
**认证方式**: Bearer TokenAPI Key 或 JWT Token
**请求格式**:
```json
{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hello"}],
"stream": false
}
```
**支持的参数**: model, messages, stream, temperature, max_tokens 等标准 OpenAI 参数
"""
from src.api.handlers.openai import OpenAIChatAdapter
adapter = OpenAIChatAdapter()
return await _run_openai_route_shell(adapter, http_request, db)
@router.post("/v1/responses/compact")
async def create_responses_compact(
http_request: Request,
db: Session = Depends(get_db),
) -> Any:
"""
OpenAI Responses Compaction API (CLI)
用于压缩/总结之前的 responses永远非流式。
Codex CLI 使用 compact 模型后缀(如 gpt-5-compact时调用此端点。
**认证方式**: Bearer TokenAPI Key 或 JWT Token
"""
from src.api.handlers.openai_cli import OpenAICompactAdapter
adapter = OpenAICompactAdapter()
return await _run_openai_route_shell(adapter, http_request, db)
@router.post("/v1/responses")
async def create_responses(
http_request: Request,
db: Session = Depends(get_db),
) -> Any:
"""
OpenAI Responses API (CLI)
兼容 OpenAI Codex CLI 使用的 Responses API 格式,请求透传到上游。
**认证方式**: Bearer TokenAPI Key 或 JWT Token
"""
from src.api.handlers.openai_cli import OpenAICliAdapter
adapter = OpenAICliAdapter()
return await _run_openai_route_shell(adapter, http_request, db)