-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
342 lines (276 loc) · 11 KB
/
Copy pathserver.py
File metadata and controls
342 lines (276 loc) · 11 KB
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
"""
OpenAI Gateway - Proxy API pour partager l'accès LLM sans exposer la clé.
Expose une API compatible OpenAI qui forward vers Anthropic (ou LLM local).
"""
import os
import json
import httpx
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import StreamingResponse, JSONResponse
from pydantic import BaseModel
from typing import Optional
from dotenv import load_dotenv
load_dotenv()
# === Configuration ===
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY", "")
ANTHROPIC_BASE_URL = "https://api.anthropic.com"
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL", "claude-sonnet-4-20250514")
# Rate limiting simple (par IP)
REQUEST_LIMIT_PER_MINUTE = int(os.getenv("REQUEST_LIMIT_PER_MINUTE", "120"))
# === App ===
app = FastAPI(
title="OpenAI Gateway",
description="Proxy API pour Francine - forward vers Anthropic sans exposer la clé",
version="0.1.0"
)
# Tracking des requêtes (simple, en mémoire)
request_counts: dict[str, list[float]] = {}
# Stats globales (en mémoire, reset au redémarrage)
import time
stats = {
"start_time": time.time(),
"total_requests": 0,
"total_tokens_in": 0,
"total_tokens_out": 0,
"errors": 0,
"by_ip": {}, # ip -> {"requests": n, "tokens_in": n, "tokens_out": n}
"by_model": {}, # model -> {"requests": n, "tokens_in": n, "tokens_out": n}
}
# === Models ===
class ChatMessage(BaseModel):
role: str
content: Optional[str | list] = None
tool_calls: Optional[list] = None # Pour assistant avec tool calls
tool_call_id: Optional[str] = None # Pour tool results
name: Optional[str] = None # Nom du tool pour tool results
class ChatCompletionRequest(BaseModel):
model: Optional[str] = None
messages: list[ChatMessage]
max_tokens: Optional[int] = 4096
temperature: Optional[float] = 1.0
stream: Optional[bool] = False
tools: Optional[list] = None
# === Helpers ===
def check_rate_limit(client_ip: str) -> bool:
"""Vérifie le rate limit pour une IP."""
import time
now = time.time()
minute_ago = now - 60
if client_ip not in request_counts:
request_counts[client_ip] = []
# Nettoie les anciennes requêtes
request_counts[client_ip] = [t for t in request_counts[client_ip] if t > minute_ago]
if len(request_counts[client_ip]) >= REQUEST_LIMIT_PER_MINUTE:
return False
request_counts[client_ip].append(now)
return True
def convert_openai_tools_to_anthropic(openai_tools: list) -> list:
"""Convertit les tools OpenAI en format Anthropic."""
anthropic_tools = []
for tool in openai_tools:
if tool.get("type") == "function":
func = tool.get("function", {})
anthropic_tools.append({
"name": func.get("name"),
"description": func.get("description", ""),
"input_schema": func.get("parameters", {"type": "object", "properties": {}})
})
return anthropic_tools
def convert_openai_to_anthropic(request: ChatCompletionRequest) -> dict:
"""Convertit une requête OpenAI en format Anthropic."""
# Extrait le system prompt si présent
system = None
messages = []
for msg in request.messages:
if msg.role == "system":
system = msg.content if isinstance(msg.content, str) else str(msg.content)
elif msg.role == "assistant" and msg.tool_calls:
# Assistant avec tool calls -> Anthropic tool_use
content = []
if msg.content:
content.append({"type": "text", "text": msg.content})
for tc in msg.tool_calls:
func = tc.get("function", {})
content.append({
"type": "tool_use",
"id": tc.get("id"),
"name": func.get("name"),
"input": json.loads(func.get("arguments", "{}"))
})
messages.append({"role": "assistant", "content": content})
elif msg.role == "tool":
# Tool result -> Anthropic tool_result (dans un message user)
tool_result = {
"type": "tool_result",
"tool_use_id": msg.tool_call_id,
"content": msg.content if isinstance(msg.content, str) else str(msg.content)
}
# Merge avec le dernier message user si possible, sinon crée un nouveau
if messages and messages[-1]["role"] == "user" and isinstance(messages[-1]["content"], list):
messages[-1]["content"].append(tool_result)
else:
messages.append({"role": "user", "content": [tool_result]})
else:
messages.append({
"role": msg.role,
"content": msg.content if isinstance(msg.content, str) else str(msg.content) if msg.content else ""
})
payload = {
"model": request.model or DEFAULT_MODEL,
"max_tokens": request.max_tokens or 4096,
"messages": messages,
}
if system:
payload["system"] = system
if request.tools:
payload["tools"] = convert_openai_tools_to_anthropic(request.tools)
return payload
def convert_anthropic_to_openai(anthropic_response: dict, model: str) -> dict:
"""Convertit une réponse Anthropic en format OpenAI."""
content = ""
tool_calls = []
for block in anthropic_response.get("content", []):
if block.get("type") == "text":
content += block.get("text", "")
elif block.get("type") == "tool_use":
tool_calls.append({
"id": block.get("id"),
"type": "function",
"function": {
"name": block.get("name"),
"arguments": json.dumps(block.get("input", {}))
}
})
message = {
"role": "assistant",
"content": content if content else None,
}
if tool_calls:
message["tool_calls"] = tool_calls
return {
"id": anthropic_response.get("id", "chatcmpl-gateway"),
"object": "chat.completion",
"created": 0,
"model": model,
"choices": [{
"index": 0,
"message": message,
"finish_reason": "stop" if anthropic_response.get("stop_reason") == "end_turn" else "tool_calls"
}],
"usage": {
"prompt_tokens": anthropic_response.get("usage", {}).get("input_tokens", 0),
"completion_tokens": anthropic_response.get("usage", {}).get("output_tokens", 0),
"total_tokens": anthropic_response.get("usage", {}).get("input_tokens", 0) +
anthropic_response.get("usage", {}).get("output_tokens", 0)
}
}
# === Routes ===
@app.get("/")
async def root():
return {
"name": "OpenAI Gateway",
"version": "0.1.0",
"status": "running",
"backend": "anthropic" if ANTHROPIC_API_KEY else "not configured"
}
@app.get("/v1/models")
async def list_models():
"""Liste les modèles disponibles (format OpenAI)."""
return {
"object": "list",
"data": [
{"id": "claude-sonnet-4-20250514", "object": "model", "owned_by": "anthropic"},
{"id": "claude-opus-4-20250514", "object": "model", "owned_by": "anthropic"},
{"id": "claude-haiku-3-20240307", "object": "model", "owned_by": "anthropic"},
]
}
@app.post("/v1/chat/completions")
async def chat_completions(request: ChatCompletionRequest, req: Request):
"""Endpoint compatible OpenAI qui forward vers Anthropic."""
# Vérifie la clé API
if not ANTHROPIC_API_KEY:
raise HTTPException(status_code=500, detail="ANTHROPIC_API_KEY not configured")
# Rate limiting
client_ip = req.client.host if req.client else "unknown"
if not check_rate_limit(client_ip):
stats["errors"] += 1
raise HTTPException(status_code=429, detail="Rate limit exceeded")
# Convertit la requête
anthropic_payload = convert_openai_to_anthropic(request)
# Forward vers Anthropic
async with httpx.AsyncClient() as client:
try:
response = await client.post(
f"{ANTHROPIC_BASE_URL}/v1/messages",
headers={
"x-api-key": ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
json=anthropic_payload,
timeout=120.0
)
if response.status_code != 200:
error_detail = response.text
raise HTTPException(
status_code=response.status_code,
detail=f"Anthropic API error: {error_detail}"
)
anthropic_response = response.json()
except httpx.TimeoutException:
raise HTTPException(status_code=504, detail="Anthropic API timeout")
except httpx.RequestError as e:
raise HTTPException(status_code=502, detail=f"Anthropic API error: {str(e)}")
# Convertit la réponse
openai_response = convert_anthropic_to_openai(
anthropic_response,
request.model or DEFAULT_MODEL
)
# Update stats
model = request.model or DEFAULT_MODEL
tokens_in = anthropic_response.get("usage", {}).get("input_tokens", 0)
tokens_out = anthropic_response.get("usage", {}).get("output_tokens", 0)
stats["total_requests"] += 1
stats["total_tokens_in"] += tokens_in
stats["total_tokens_out"] += tokens_out
# Par IP
if client_ip not in stats["by_ip"]:
stats["by_ip"][client_ip] = {"requests": 0, "tokens_in": 0, "tokens_out": 0}
stats["by_ip"][client_ip]["requests"] += 1
stats["by_ip"][client_ip]["tokens_in"] += tokens_in
stats["by_ip"][client_ip]["tokens_out"] += tokens_out
# Par modèle
if model not in stats["by_model"]:
stats["by_model"][model] = {"requests": 0, "tokens_in": 0, "tokens_out": 0}
stats["by_model"][model]["requests"] += 1
stats["by_model"][model]["tokens_in"] += tokens_in
stats["by_model"][model]["tokens_out"] += tokens_out
return JSONResponse(content=openai_response)
# === Stats ===
@app.get("/stats")
async def get_stats():
"""Retourne les statistiques d'utilisation."""
uptime = time.time() - stats["start_time"]
hours = int(uptime // 3600)
minutes = int((uptime % 3600) // 60)
return {
"uptime": f"{hours}h {minutes}m",
"total_requests": stats["total_requests"],
"total_tokens": {
"input": stats["total_tokens_in"],
"output": stats["total_tokens_out"],
"total": stats["total_tokens_in"] + stats["total_tokens_out"]
},
"errors": stats["errors"],
"by_ip": stats["by_ip"],
"by_model": stats["by_model"]
}
# === Health check ===
@app.get("/health")
async def health():
return {"status": "healthy"}
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "8000"))
print(f"Starting OpenAI Gateway on port {port}...")
uvicorn.run(app, host="0.0.0.0", port=port)