File size: 12,241 Bytes
2ebe1d5 | 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 | #!/usr/bin/env python3
"""
Puter.com Reverse OpenAI-Compatible API Server
Accepts OpenAI Chat Completions requests and forwards them to:
POST https://api.puter.com/drivers/call
with payload:
{
"interface": "puter-chat-completion",
"driver": "claude",
"test_mode": false,
"method": "complete",
"args": {
"messages": [{"content": "..."}],
"model": "claude-sonnet-4-20250514",
"stream": true
}
}
"""
import json
import time
import uuid
import logging
from typing import Any, Dict, List, Optional, Union, AsyncGenerator
import requests
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse, JSONResponse
from pydantic import BaseModel, Field
try:
from .config import (
PUTER_HEADERS,
PUTER_AUTH_BEARER,
SERVER_CONFIG,
MODEL_MAPPING,
)
except ImportError:
from config import (
PUTER_HEADERS,
PUTER_AUTH_BEARER,
SERVER_CONFIG,
MODEL_MAPPING,
)
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
PUTER_URL = "https://api.puter.com/drivers/call"
REQUEST_TIMEOUT = 120
# ===== OpenAI-compatible models =====
class OpenAIMessage(BaseModel):
role: Optional[str] = Field(default=None, description="Role")
content: Optional[Union[str, List[Dict[str, Any]]]] = None
name: Optional[str] = None
function_call: Optional[Dict[str, Any]] = None
tool_calls: Optional[List[Dict[str, Any]]] = None
tool_call_id: Optional[str] = None
def get_text(self) -> str:
if isinstance(self.content, str):
return self.content
if isinstance(self.content, list):
parts: List[str] = []
for item in self.content:
if isinstance(item, dict) and item.get("type") == "text":
parts.append(item.get("text", ""))
return "".join(parts)
return str(self.content) if self.content is not None else ""
class Config:
extra = "allow"
class OpenAIFunction(BaseModel):
name: str
description: Optional[str] = None
parameters: Optional[Dict[str, Any]] = None
class Config:
extra = "allow"
class OpenAITool(BaseModel):
type: str = Field(default="function")
function: Optional[OpenAIFunction] = None
class Config:
extra = "allow"
class OpenAIChatRequest(BaseModel):
model: str
messages: List[OpenAIMessage]
max_tokens: Optional[int] = None
temperature: Optional[float] = None
top_p: Optional[float] = None
n: Optional[int] = 1
stream: Optional[bool] = False
stop: Optional[Union[str, List[str]]] = None
presence_penalty: Optional[float] = None
frequency_penalty: Optional[float] = None
logit_bias: Optional[Dict[str, float]] = None
user: Optional[str] = None
tools: Optional[List[OpenAITool]] = None
tool_choice: Optional[Union[str, Dict[str, Any]]] = None
functions: Optional[List[OpenAIFunction]] = None
function_call: Optional[Union[str, Dict[str, Any]]] = None
class Config:
extra = "allow"
class OpenAIChoice(BaseModel):
index: int = 0
message: Dict[str, Any]
finish_reason: Optional[str] = None
class OpenAIChatResponse(BaseModel):
id: str
object: str = "chat.completion"
created: int
model: str
choices: List[OpenAIChoice]
usage: Optional[Dict[str, int]] = None
class OpenAIStreamChoice(BaseModel):
index: int = 0
delta: Dict[str, Any]
finish_reason: Optional[str] = None
class OpenAIStreamChunk(BaseModel):
id: str
object: str = "chat.completion.chunk"
created: int
model: str
choices: List[OpenAIStreamChoice]
def _build_puter_payload(openai_req: OpenAIChatRequest) -> Dict[str, Any]:
# Map OpenAI messages to Puter format: only 'content' is used
mapped_messages: List[Dict[str, str]] = []
for m in openai_req.messages:
txt = m.get_text()
mapped_messages.append({"content": txt})
# Model mapping: map OpenAI model key -> (driver, puter_model)
mapping = MODEL_MAPPING.get(openai_req.model) or MODEL_MAPPING.get("default")
driver = mapping["driver"]
puter_model = mapping["puter_model"]
payload: Dict[str, Any] = {
"interface": "puter-chat-completion",
"driver": driver,
"test_mode": False,
"method": "complete",
"args": {
"messages": mapped_messages,
"model": puter_model,
"stream": True, # always request streaming upstream; we aggregate if needed
},
}
return payload
def _headers_with_auth() -> Dict[str, str]:
h = dict(PUTER_HEADERS)
h["authorization"] = f"Bearer {PuterAuth.token}"
return h
class PuterAuth:
token: str = PUTER_AUTH_BEARER
async def _stream_openai_chunks(openai_req: OpenAIChatRequest, request_id: str) -> AsyncGenerator[str, None]:
headers = _headers_with_auth()
payload = _build_puter_payload(openai_req)
with requests.Session() as sess:
try:
resp = sess.post(
PUTER_URL,
headers=headers,
json=payload,
stream=True,
timeout=REQUEST_TIMEOUT,
)
except requests.RequestException as e:
raise HTTPException(status_code=502, detail=f"Upstream connection error: {e}")
if resp.status_code != 200:
detail = resp.text[:500]
raise HTTPException(status_code=502, detail=f"Upstream error {resp.status_code}: {detail}")
created = int(time.time())
# Initial role chunk
initial = OpenAIStreamChunk(
id=request_id,
created=created,
model=openai_req.model,
choices=[OpenAIStreamChoice(index=0, delta={"role": "assistant"}, finish_reason=None)],
)
yield f"data: {initial.model_dump_json()}\n\n"
# Stream content
for raw in resp.iter_lines():
if not raw:
continue
try:
line = raw.decode("utf-8", errors="ignore")
except Exception:
continue
text_piece: Optional[str] = None
# Many APIs stream JSON lines; try to parse
try:
obj = json.loads(line)
# Common keys
for k in ("delta", "text", "content", "output"):
if isinstance(obj.get(k), str) and obj.get(k):
text_piece = obj.get(k)
break
except Exception:
# Fallback to raw text
if line and line != "[DONE]":
text_piece = line
if not text_piece:
continue
chunk = OpenAIStreamChunk(
id=request_id,
created=created,
model=openai_req.model,
choices=[OpenAIStreamChoice(index=0, delta={"content": text_piece}, finish_reason=None)],
)
yield f"data: {chunk.model_dump_json()}\n\n"
final = OpenAIStreamChunk(
id=request_id,
created=created,
model=openai_req.model,
choices=[OpenAIStreamChoice(index=0, delta={}, finish_reason="stop")],
)
yield f"data: {final.model_dump_json()}\n\n"
yield "data: [DONE]\n\n"
def _complete_non_streaming(openai_req: OpenAIChatRequest) -> str:
headers = _headers_with_auth()
payload = _build_puter_payload(openai_req)
payload["args"]["stream"] = True
with requests.Session() as sess:
try:
resp = sess.post(
PUTER_URL,
headers=headers,
json=payload,
stream=True,
timeout=REQUEST_TIMEOUT,
)
except requests.RequestException as e:
raise HTTPException(status_code=502, detail=f"Upstream connection error: {e}")
if resp.status_code != 200:
detail = resp.text[:500]
raise HTTPException(status_code=502, detail=f"Upstream error {resp.status_code}: {detail}")
parts: List[str] = []
for raw in resp.iter_lines():
if not raw:
continue
try:
line = raw.decode("utf-8", errors="ignore")
except Exception:
continue
try:
obj = json.loads(line)
for k in ("delta", "text", "content", "output"):
if isinstance(obj.get(k), str) and obj.get(k):
parts.append(obj.get(k))
break
except Exception:
if line and line != "[DONE]":
parts.append(line)
return "".join(parts)
# ===== FastAPI app =====
app = FastAPI(
title="Puter Reverse OpenAI API",
version="1.0.0",
description="OpenAI-compatible API proxying to api.puter.com"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Puter Reverse OpenAI API", "status": "running", "version": "1.0.0"}
@app.get("/health")
async def health():
return {"status": "healthy", "timestamp": int(time.time())}
@app.get("/v1/models")
async def models():
created = int(time.time())
data = []
for key in [k for k in MODEL_MAPPING.keys() if k != "default"]:
data.append({"id": key, "object": "model", "created": created, "owned_by": "puter"})
if not data:
data.append({"id": "claude-sonnet-4-20250514", "object": "model", "created": created, "owned_by": "puter"})
return {"object": "list", "data": data}
@app.post("/v1/chat/completions")
async def chat(request: OpenAIChatRequest):
req_id = f"chatcmpl-{uuid.uuid4().hex[:12]}"
logger.info(f"[{req_id}] model={request.model}, stream={bool(request.stream)}")
if bool(request.stream):
return StreamingResponse(
_stream_openai_chunks(request, req_id),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
},
)
content = _complete_non_streaming(request)
created = int(time.time())
response = OpenAIChatResponse(
id=req_id,
created=created,
model=request.model,
choices=[OpenAIChoice(index=0, message={"role": "assistant", "content": content}, finish_reason="stop")],
usage={
"prompt_tokens": len(" ".join([m.get_text() for m in request.messages]).split()),
"completion_tokens": len(content.split()),
"total_tokens": len(" ".join([m.get_text() for m in request.messages]).split()) + len(content.split()),
},
)
return response
@app.post("/v1/chat/completions/raw")
async def raw(req: Request):
body = await req.body()
try:
obj = json.loads(body)
_ = OpenAIChatRequest(**obj)
return {"valid": True}
except Exception as e:
return JSONResponse(status_code=422, content={"valid": False, "error": str(e)})
if __name__ == "__main__":
try:
import uvicorn
host = SERVER_CONFIG.get("host", "0.0.0.0")
port = int(SERVER_CONFIG.get("port", 8781))
logger.info(f"Starting Puter Reverse API on {host}:{port}")
uvicorn.run(app, host=host, port=port, log_level="info")
except Exception as e:
logger.error(f"Failed to start server: {e}")
|