Spaces:
Sleeping
Sleeping
File size: 13,051 Bytes
494c89b | 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | #!/usr/bin/env python3
"""
Kiro LLM API Server
OpenAI-compatible API server that uses Kiro tokens to access Claude via CodeWhisperer.
Supports token pool rotation, automatic refresh, and ban detection.
Usage:
python -m autoreg.llm.llm_server
# or
uvicorn autoreg.llm.llm_server:app --host 0.0.0.0 --port 8421
Endpoints:
GET /v1/models - List available models
POST /v1/chat/completions - Chat completions (streaming supported)
GET /health - Health check
GET /pool/status - Token pool status
"""
import asyncio
import json
import os
import sys
import time
import uuid
from datetime import datetime
from pathlib import Path
from typing import AsyncGenerator, Optional, Dict, Any, List
from fastapi import FastAPI, HTTPException, Request, Header
from fastapi.responses import StreamingResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
# Add parent to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from llm.token_pool import TokenPool
from llm.codewhisperer_client import CodeWhispererClient
from services.quota_service import QuotaService
# ============================================================================
# Configuration
# ============================================================================
API_PORT = int(os.environ.get("KIRO_LLM_PORT", 8421))
API_HOST = os.environ.get("KIRO_LLM_HOST", "0.0.0.0")
API_KEY = os.environ.get("KIRO_LLM_API_KEY", "") # Optional API key protection
# ============================================================================
# Pydantic Models (OpenAI-compatible)
# ============================================================================
class Message(BaseModel):
role: str
content: str
class ChatCompletionRequest(BaseModel):
model: str = "claude-sonnet-4-20250514"
messages: List[Message]
stream: bool = False
max_tokens: Optional[int] = 4096
temperature: Optional[float] = 0.7
top_p: Optional[float] = None
stop: Optional[List[str]] = None
class ChatCompletionChoice(BaseModel):
index: int
message: Message
finish_reason: str
class Usage(BaseModel):
prompt_tokens: int
completion_tokens: int
total_tokens: int
class ChatCompletionResponse(BaseModel):
id: str
object: str = "chat.completion"
created: int
model: str
choices: List[ChatCompletionChoice]
usage: Usage
# ============================================================================
# FastAPI Application
# ============================================================================
app = FastAPI(
title="Kiro LLM API",
description="OpenAI-compatible API using Kiro/CodeWhisperer tokens",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Global instances
token_pool: Optional[TokenPool] = None
cw_client: Optional[CodeWhispererClient] = None
quota_service: Optional[QuotaService] = None
# ============================================================================
# Startup/Shutdown
# ============================================================================
@app.on_event("startup")
async def startup():
global token_pool, cw_client, quota_service
print("=" * 60)
print("Kiro LLM API Server Starting...")
print("=" * 60)
# Initialize token pool
token_pool = TokenPool()
await token_pool.load_tokens()
# Initialize CodeWhisperer client
cw_client = CodeWhispererClient(token_pool)
# Initialize quota service
quota_service = QuotaService()
print(f"\nServer ready at http://{API_HOST}:{API_PORT}")
print(f"OpenAI-compatible endpoint: http://{API_HOST}:{API_PORT}/v1/chat/completions")
print("=" * 60)
@app.on_event("shutdown")
async def shutdown():
print("\nShutting down Kiro LLM API Server...")
# ============================================================================
# Auth Middleware
# ============================================================================
async def verify_api_key(authorization: Optional[str] = Header(None)):
"""Verify API key if configured."""
if not API_KEY:
return True
if not authorization:
raise HTTPException(status_code=401, detail="Missing Authorization header")
# Support both "Bearer <key>" and just "<key>"
key = authorization.replace("Bearer ", "").strip()
if key != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
return True
# ============================================================================
# Endpoints
# ============================================================================
@app.get("/")
async def root():
"""Root endpoint."""
return {
"service": "Kiro LLM API",
"version": "1.0.0",
"status": "running",
"endpoints": {
"models": "/v1/models",
"chat": "/v1/chat/completions",
"health": "/health",
"pool": "/pool/status"
}
}
@app.get("/v1/models")
async def list_models():
"""List available models (OpenAI-compatible)."""
models = [
{"id": "claude-opus-4.5", "credit": "2.2x", "desc": "Most capable model"},
{"id": "claude-sonnet-4.5", "credit": "1.3x", "desc": "Latest Sonnet"},
{"id": "claude-sonnet-4", "credit": "1.3x", "desc": "Hybrid reasoning"},
{"id": "claude-haiku-4.5", "credit": "0.4x", "desc": "Fast & cheap"},
{"id": "auto", "credit": "1x", "desc": "Auto-select model"},
]
return {
"object": "list",
"data": [
{
"id": m["id"],
"object": "model",
"created": int(time.time()),
"owned_by": "anthropic",
"permission": [],
"root": m["id"],
"parent": None,
"description": f"{m['desc']} ({m['credit']} credit)"
}
for m in models
]
}
@app.post("/v1/chat/completions")
async def chat_completions(
request: ChatCompletionRequest,
authorization: Optional[str] = Header(None)
):
"""OpenAI-compatible chat completions endpoint."""
await verify_api_key(authorization)
if not cw_client:
raise HTTPException(status_code=503, detail="Service not initialized")
if not token_pool or token_pool.available_count == 0:
raise HTTPException(status_code=503, detail="No available tokens in pool")
request_id = f"chatcmpl-{uuid.uuid4().hex[:24]}"
created = int(time.time())
if request.stream:
return StreamingResponse(
generate_stream(request, request_id, created),
media_type="text/event-stream"
)
else:
return await generate_response(request, request_id, created)
async def generate_stream(
request: ChatCompletionRequest,
request_id: str,
created: int
) -> AsyncGenerator[str, None]:
"""Generate streaming response."""
try:
async for chunk in cw_client.generate_stream(
messages=request.messages,
model=request.model,
max_tokens=request.max_tokens or 4096,
temperature=request.temperature or 0.7
):
data = {
"id": request_id,
"object": "chat.completion.chunk",
"created": created,
"model": request.model,
"choices": [{
"index": 0,
"delta": {"content": chunk},
"finish_reason": None
}]
}
yield f"data: {json.dumps(data)}\n\n"
# Final chunk
data = {
"id": request_id,
"object": "chat.completion.chunk",
"created": created,
"model": request.model,
"choices": [{
"index": 0,
"delta": {},
"finish_reason": "stop"
}]
}
yield f"data: {json.dumps(data)}\n\n"
yield "data: [DONE]\n\n"
except Exception as e:
error_data = {
"error": {
"message": str(e),
"type": "server_error",
"code": "internal_error"
}
}
yield f"data: {json.dumps(error_data)}\n\n"
async def generate_response(
request: ChatCompletionRequest,
request_id: str,
created: int
) -> ChatCompletionResponse:
"""Generate non-streaming response."""
content = await cw_client.generate(
messages=request.messages,
model=request.model,
max_tokens=request.max_tokens or 4096,
temperature=request.temperature or 0.7
)
# Estimate tokens (rough approximation)
prompt_tokens = sum(len(m.content.split()) * 1.3 for m in request.messages)
completion_tokens = len(content.split()) * 1.3
return ChatCompletionResponse(
id=request_id,
created=created,
model=request.model,
choices=[ChatCompletionChoice(
index=0,
message=Message(role="assistant", content=content),
finish_reason="stop"
)],
usage=Usage(
prompt_tokens=int(prompt_tokens),
completion_tokens=int(completion_tokens),
total_tokens=int(prompt_tokens + completion_tokens)
)
)
@app.get("/health")
async def health():
"""Health check endpoint."""
return {
"status": "healthy",
"timestamp": datetime.now().isoformat(),
"pool": {
"total": token_pool.total_count if token_pool else 0,
"available": token_pool.available_count if token_pool else 0,
"banned": token_pool.banned_count if token_pool else 0
}
}
@app.get("/pool/status")
async def pool_status():
"""Get detailed token pool status."""
if not token_pool:
raise HTTPException(status_code=503, detail="Service not initialized")
return token_pool.get_status()
@app.post("/pool/refresh")
async def pool_refresh():
"""Refresh all tokens in pool."""
if not token_pool:
raise HTTPException(status_code=503, detail="Service not initialized")
refreshed = await token_pool.refresh_all()
return {"refreshed": refreshed, "total": token_pool.total_count}
@app.post("/pool/reload")
async def pool_reload():
"""Reload tokens from disk."""
if not token_pool:
raise HTTPException(status_code=503, detail="Service not initialized")
count = await token_pool.load_tokens()
return {"loaded": count}
@app.get("/pool/quotas")
async def pool_quotas():
"""Get quota information for all tokens in pool."""
if not token_pool or not quota_service:
raise HTTPException(status_code=503, detail="Service not initialized")
quotas = []
for token in token_pool.tokens:
try:
info = quota_service.get_quota(token.access_token, token.auth_method)
quota_data = {
"account": token.account_name or token.email,
"email": info.email,
"subscription": info.subscription_type,
"days_until_reset": info.days_until_reset,
"error": info.error
}
if info.usage:
quota_data.update({
"limit": info.usage.limit,
"used": info.usage.used,
"remaining": info.usage.remaining,
"percent_used": round(info.usage.percent_used, 1),
"total_remaining": info.usage.total_remaining
})
quotas.append(quota_data)
except Exception as e:
quotas.append({
"account": token.account_name or token.email,
"error": str(e)
})
return {
"quotas": quotas,
"total_tokens": len(quotas),
"total_remaining": sum(q.get("total_remaining", 0) for q in quotas if not q.get("error"))
}
# ============================================================================
# Server Control
# ============================================================================
@app.post("/shutdown")
async def shutdown_server():
"""Gracefully shutdown the server."""
import signal
import os
# Schedule shutdown after response is sent
async def do_shutdown():
await asyncio.sleep(0.5)
os.kill(os.getpid(), signal.SIGTERM)
asyncio.create_task(do_shutdown())
return {"status": "shutting_down"}
# ============================================================================
# Main
# ============================================================================
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host=API_HOST, port=API_PORT)
|