Spaces:
Runtime error
Runtime error
File size: 13,206 Bytes
b30d305 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | """
格式转换API
提供API格式转换的核心路由和处理逻辑
"""
import asyncio
import json
import time
from typing import Dict, Any, Optional, List
import httpx
from fastapi import APIRouter, HTTPException, Request, Response, Depends
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from channels.channel_manager import channel_manager, ChannelInfo
from formats.converter_factory import ConverterFactory, convert_request, convert_response
from src.utils.logger import setup_logger
from src.utils.exceptions import ChannelNotFoundError, ConversionError, APIError, TimeoutError
logger = setup_logger("conversion_api")
router = APIRouter()
class ChannelCreateRequest(BaseModel):
"""创建渠道请求"""
name: str = Field(..., description="渠道名称")
provider: str = Field(..., description="提供商 (openai/anthropic/gemini)")
base_url: str = Field(..., description="API基础URL")
api_key: str = Field(..., description="API密钥")
custom_key: str = Field(..., description="自定义key,用户调用时使用")
timeout: int = Field(30, description="超时时间")
max_retries: int = Field(3, description="最大重试次数")
class ChannelUpdateRequest(BaseModel):
"""更新渠道请求"""
name: Optional[str] = None
base_url: Optional[str] = None
api_key: Optional[str] = None
custom_key: Optional[str] = None
timeout: Optional[int] = None
max_retries: Optional[int] = None
enabled: Optional[bool] = None
async def detect_request_format(request_data: Dict[str, Any], path: str) -> str:
"""检测请求格式"""
# 基于URL路径检测
if "/openai/" in path or path.endswith("/chat/completions"):
return "openai"
elif "/anthropic/" in path or path.endswith("/messages"):
return "anthropic"
elif "/gemini/" in path or "generateContent" in path:
return "gemini"
# 基于请求数据结构检测
if "messages" in request_data and "model" in request_data:
if "system" in request_data:
return "anthropic"
else:
return "openai"
elif "contents" in request_data:
return "gemini"
# 默认返回openai格式
return "openai"
async def forward_request(
channel: ChannelInfo,
converted_data: Dict[str, Any],
headers: Dict[str, str],
method: str = "POST"
) -> Dict[str, Any]:
"""转发请求到目标API"""
# 构建请求URL
if channel.provider == "openai":
url = f"{channel.base_url.rstrip('/')}/chat/completions"
headers["Authorization"] = f"Bearer {channel.api_key}"
elif channel.provider == "anthropic":
url = f"{channel.base_url.rstrip('/')}/v1/messages"
headers["x-api-key"] = channel.api_key
headers["anthropic-version"] = "2023-06-01"
elif channel.provider == "gemini":
# Gemini需要从converted_data中提取模型名称
model = converted_data.get("model")
if not model:
raise ValueError("Model name is required for Gemini requests")
url = f"{channel.base_url.rstrip('/')}/models/{model}:generateContent?key={channel.api_key}"
else:
raise ValueError(f"Unsupported provider: {channel.provider}")
# 设置请求头
headers["Content-Type"] = "application/json"
try:
async with httpx.AsyncClient(timeout=channel.timeout) as client:
response = await client.request(
method=method,
url=url,
json=converted_data,
headers=headers
)
if response.status_code == 200:
return response.json()
else:
error_detail = f"API request failed with status {response.status_code}: {response.text}"
logger.error(error_detail)
raise APIError(error_detail)
except httpx.TimeoutException:
raise TimeoutError(f"Request timeout after {channel.timeout} seconds")
except Exception as e:
logger.error(f"Request failed: {e}")
raise APIError(f"Request failed: {e}")
# 渠道管理API
@router.post("/channels")
async def create_channel(request: ChannelCreateRequest):
"""创建新渠道"""
try:
channel_id = channel_manager.add_channel(
name=request.name,
provider=request.provider,
base_url=request.base_url,
api_key=request.api_key,
custom_key=request.custom_key,
timeout=request.timeout,
max_retries=request.max_retries
)
return {
"success": True,
"channel_id": channel_id,
"message": "Channel created successfully"
}
except Exception as e:
logger.error(f"Failed to create channel: {e}")
raise HTTPException(status_code=400, detail=str(e))
@router.get("/channels")
async def list_channels():
"""获取所有渠道"""
try:
channels = channel_manager.get_all_channels()
return {
"success": True,
"channels": [
{
"id": channel.id,
"name": channel.name,
"provider": channel.provider,
"base_url": channel.base_url,
"custom_key": channel.custom_key,
"enabled": channel.enabled,
"created_at": channel.created_at,
"updated_at": channel.updated_at
}
for channel in channels
]
}
except Exception as e:
logger.error(f"Failed to list channels: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/channels/{channel_id}")
async def get_channel(channel_id: str):
"""获取特定渠道信息"""
try:
channel = channel_manager.get_channel(channel_id)
if not channel:
raise HTTPException(status_code=404, detail="Channel not found")
return {
"success": True,
"channel": {
"id": channel.id,
"name": channel.name,
"provider": channel.provider,
"base_url": channel.base_url,
"custom_key": channel.custom_key,
"timeout": channel.timeout,
"max_retries": channel.max_retries,
"enabled": channel.enabled,
"created_at": channel.created_at,
"updated_at": channel.updated_at
}
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Failed to get channel: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.put("/channels/{channel_id}")
async def update_channel(channel_id: str, request: ChannelUpdateRequest):
"""更新渠道信息"""
try:
success = channel_manager.update_channel(
channel_id=channel_id,
name=request.name,
base_url=request.base_url,
api_key=request.api_key,
custom_key=request.custom_key,
timeout=request.timeout,
max_retries=request.max_retries,
enabled=request.enabled
)
if success:
return {
"success": True,
"message": "Channel updated successfully"
}
else:
raise HTTPException(status_code=404, detail="Channel not found")
except ChannelNotFoundError:
raise HTTPException(status_code=404, detail="Channel not found")
except Exception as e:
logger.error(f"Failed to update channel: {e}")
raise HTTPException(status_code=400, detail=str(e))
@router.delete("/channels/{channel_id}")
async def delete_channel(channel_id: str):
"""删除渠道"""
try:
success = channel_manager.delete_channel(channel_id)
if success:
return {
"success": True,
"message": "Channel deleted successfully"
}
else:
raise HTTPException(status_code=404, detail="Channel not found")
except ChannelNotFoundError:
raise HTTPException(status_code=404, detail="Channel not found")
except Exception as e:
logger.error(f"Failed to delete channel: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/channels/{channel_id}/test")
async def test_channel(channel_id: str):
"""测试渠道连接"""
try:
result = channel_manager.test_channel_connection(channel_id)
return {
"success": True,
"test_result": result
}
except ChannelNotFoundError:
raise HTTPException(status_code=404, detail="Channel not found")
except Exception as e:
logger.error(f"Failed to test channel: {e}")
raise HTTPException(status_code=500, detail=str(e))
# 格式转换API
@router.post("/openai/v1/chat/completions")
async def openai_chat_completions(request: Request):
"""OpenAI格式API端点"""
return await handle_conversion_request(request, "openai")
@router.post("/anthropic/v1/messages")
async def anthropic_messages(request: Request):
"""Anthropic格式API端点"""
return await handle_conversion_request(request, "anthropic")
@router.post("/gemini/v1beta/generateContent")
async def gemini_generate_content(request: Request):
"""Gemini格式API端点"""
return await handle_conversion_request(request, "gemini")
async def handle_conversion_request(request: Request, target_format: str):
"""处理转换请求的通用逻辑"""
try:
# 获取请求数据
request_data = await request.json()
headers = dict(request.headers)
# 检测源格式
source_format = await detect_request_format(request_data, str(request.url.path))
logger.info(f"Request URL path: {request.url.path}")
logger.info(f"Detected source_format: {source_format}, target_format: {target_format}")
logger.info(f"Converting request from {source_format} to {target_format}")
# 如果源格式和目标格式相同,直接转发
if source_format == target_format:
# 根据目标格式找到合适的渠道
channels = channel_manager.get_channels_by_provider(target_format)
if not channels:
raise HTTPException(
status_code=503,
detail=f"No available {target_format} channels configured"
)
# 选择第一个可用渠道
channel = channels[0]
# 直接转发请求
response_data = await forward_request(channel, request_data, headers)
return response_data
# 格式转换
conversion_result = convert_request(source_format, target_format, request_data, headers)
if not conversion_result.success:
raise HTTPException(
status_code=400,
detail=f"Request conversion failed: {conversion_result.error}"
)
# 找到目标格式的渠道
channels = channel_manager.get_channels_by_provider(target_format)
if not channels:
raise HTTPException(
status_code=503,
detail=f"No available {target_format} channels configured"
)
# 选择第一个可用渠道
channel = channels[0]
# 转发请求
response_data = await forward_request(channel, conversion_result.data, headers)
# 转换响应格式
response_conversion_result = convert_response(source_format, target_format, response_data)
if not response_conversion_result.success:
logger.warning(f"Response conversion failed: {response_conversion_result.error}")
# 如果响应转换失败,返回原始响应
return response_data
return response_conversion_result.data
except HTTPException:
raise
except Exception as e:
logger.error(f"Conversion request failed: {e}")
raise HTTPException(status_code=500, detail=f"Internal server error: {e}")
@router.get("/conversion/formats")
async def get_supported_formats():
"""获取支持的格式列表"""
return {
"success": True,
"formats": ConverterFactory.get_supported_formats()
}
@router.get("/conversion/statistics")
async def get_conversion_statistics():
"""获取转换统计信息"""
try:
channel_stats = channel_manager.get_channel_statistics()
return {
"success": True,
"statistics": {
"channels": channel_stats,
"supported_formats": ConverterFactory.get_supported_formats()
}
}
except Exception as e:
logger.error(f"Failed to get statistics: {e}")
raise HTTPException(status_code=500, detail=str(e)) |