初始化提交
This commit is contained in:
36
NBATransfer-backend/routes/v1_api.py
Normal file
36
NBATransfer-backend/routes/v1_api.py
Normal file
@@ -0,0 +1,36 @@
|
||||
"""对外公开的 API 路由 (v1) - 支持 API Key 认证"""
|
||||
from flask import Blueprint, request, jsonify, current_app
|
||||
from services.apikey_service import APIKeyService
|
||||
from services.v1_service import V1Service
|
||||
|
||||
v1_bp = Blueprint('v1_api', __name__)
|
||||
|
||||
@v1_bp.route('/chat/completions', methods=['POST'])
|
||||
def chat_completions():
|
||||
"""
|
||||
OpenAI 兼容的 Chat Completions 接口
|
||||
支持多模型,通过 modelapiservice 分发
|
||||
"""
|
||||
# 1. 认证
|
||||
auth_header = request.headers.get('Authorization')
|
||||
user, error = APIKeyService.authenticate_api_key(auth_header)
|
||||
|
||||
if error:
|
||||
return jsonify({'error': {'message': error, 'type': 'auth_error', 'code': 401}}), 401
|
||||
|
||||
# 2. 获取请求数据
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({'error': {'message': '无效的 JSON 请求体', 'type': 'invalid_request_error', 'code': 400}}), 400
|
||||
|
||||
result, status_code = V1Service.chat_completions(user, data)
|
||||
|
||||
if status_code == 200 and data.get('stream', False):
|
||||
return current_app.response_class(result, mimetype='text/event-stream')
|
||||
|
||||
return jsonify(result), status_code
|
||||
|
||||
@v1_bp.route('/text-to-image', methods=['POST'])
|
||||
def text_to_image_alias():
|
||||
"""兼容旧接口路径"""
|
||||
return chat_completions()
|
||||
Reference in New Issue
Block a user