- proxy_openai.py 추가: MLX 서버 SSE 패스스루 - chat.py: openai-compat 백엔드 타입 라우팅 추가 - backends.json: GPU=embed(bge-m3)만, 맥미니MLX=채팅(qwen3.5:35b-a3b) - LAN IP(192.168.1.122) 사용 (같은 서브넷, Tailscale 불필요) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
110 lines
3.3 KiB
Python
110 lines
3.3 KiB
Python
from typing import List, Optional
|
|
|
|
from fastapi import APIRouter, HTTPException, Request
|
|
from fastapi.responses import JSONResponse, StreamingResponse
|
|
from pydantic import BaseModel
|
|
|
|
from middleware.rate_limit import check_backend_rate_limit
|
|
from services import proxy_ollama, proxy_openai
|
|
from services.registry import registry
|
|
|
|
router = APIRouter(prefix="/v1", tags=["chat"])
|
|
|
|
|
|
class ChatMessage(BaseModel):
|
|
role: str
|
|
content: str
|
|
|
|
|
|
class ChatRequest(BaseModel):
|
|
model: str
|
|
messages: List[ChatMessage]
|
|
stream: bool = False
|
|
temperature: Optional[float] = None
|
|
max_tokens: Optional[int] = None
|
|
|
|
|
|
@router.post("/chat/completions")
|
|
async def chat_completions(body: ChatRequest, request: Request):
|
|
role = getattr(request.state, "role", "anonymous")
|
|
if role == "anonymous":
|
|
raise HTTPException(
|
|
status_code=401,
|
|
detail={"error": {"message": "Authentication required", "type": "auth_error", "code": "unauthorized"}},
|
|
)
|
|
|
|
# Resolve model to backend
|
|
result = registry.resolve_model(body.model, role)
|
|
if not result:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail={
|
|
"error": {
|
|
"message": f"Model '{body.model}' not found or not available",
|
|
"type": "invalid_request_error",
|
|
"code": "model_not_found",
|
|
}
|
|
},
|
|
)
|
|
|
|
backend, model_info = result
|
|
|
|
# Check rate limit
|
|
check_backend_rate_limit(backend.id)
|
|
|
|
# Record request for rate limiting
|
|
registry.record_request(backend.id)
|
|
|
|
messages = [{"role": m.role, "content": m.content} for m in body.messages]
|
|
kwargs = {}
|
|
if body.temperature is not None:
|
|
kwargs["temperature"] = body.temperature
|
|
|
|
# Route to appropriate proxy
|
|
if backend.type == "ollama":
|
|
if body.stream:
|
|
return StreamingResponse(
|
|
proxy_ollama.stream_chat(
|
|
backend.url, body.model, messages, **kwargs
|
|
),
|
|
media_type="text/event-stream",
|
|
headers={
|
|
"Cache-Control": "no-cache",
|
|
"X-Accel-Buffering": "no",
|
|
},
|
|
)
|
|
else:
|
|
result = await proxy_ollama.complete_chat(
|
|
backend.url, body.model, messages, **kwargs
|
|
)
|
|
return JSONResponse(content=result)
|
|
|
|
if backend.type == "openai-compat":
|
|
if body.stream:
|
|
return StreamingResponse(
|
|
proxy_openai.stream_chat(
|
|
backend.url, body.model, messages, **kwargs
|
|
),
|
|
media_type="text/event-stream",
|
|
headers={
|
|
"Cache-Control": "no-cache",
|
|
"X-Accel-Buffering": "no",
|
|
},
|
|
)
|
|
else:
|
|
result = await proxy_openai.complete_chat(
|
|
backend.url, body.model, messages, **kwargs
|
|
)
|
|
return JSONResponse(content=result)
|
|
|
|
raise HTTPException(
|
|
status_code=501,
|
|
detail={
|
|
"error": {
|
|
"message": f"Backend type '{backend.type}' not yet implemented",
|
|
"type": "api_error",
|
|
"code": "not_implemented",
|
|
}
|
|
},
|
|
)
|