-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_clients.py
572 lines (500 loc) · 20.8 KB
/
api_clients.py
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
"""
api_clients.py - Centralized API communication management for Hephia.
Provides unified interfaces for multiple API services while maintaining
provider-specific optimizations and requirements.
"""
import platform
import aiohttp
from aiohttp import UnixConnector
import os
from typing import Dict, Any, List, Optional, Union
import json
import asyncio
from abc import ABC, abstractmethod
from loggers import SystemLogger
class BaseAPIClient(ABC):
"""Base class for API clients with common functionality."""
def __init__(self, api_key: str, base_url: str, service_name: str):
self.api_key = api_key
self.base_url = base_url
self.service_name = service_name
self.max_retries = 3
self.retry_delay = 1 # seconds
async def _make_request(
self,
endpoint: str,
method: str = "POST",
payload: Optional[Dict] = None,
extra_headers: Optional[Dict] = None
) -> Dict[str, Any]:
"""Make API request with retry logic and error handling."""
headers = self._get_headers(extra_headers)
url = f"{self.base_url}/{endpoint.lstrip('/')}"
for attempt in range(self.max_retries):
try:
async with aiohttp.ClientSession() as session:
async with session.request(
method,
url,
headers=headers,
json=payload
) as response:
if response.status == 200:
SystemLogger.log_api_request(
self.service_name,
endpoint,
response.status
)
return await response.json()
error_text = await response.text()
SystemLogger.log_api_request(
self.service_name,
endpoint,
response.status,
error_text
)
if response.status == 429: # Rate limit
retry_after = int(response.headers.get('Retry-After', self.retry_delay))
SystemLogger.log_api_retry(
self.service_name,
attempt + 1,
self.max_retries,
f"Rate limited, waiting {retry_after}s"
)
await asyncio.sleep(retry_after)
continue
if response.status >= 500: # Server error, retry
delay = self.retry_delay * (attempt + 1)
SystemLogger.log_api_retry(
self.service_name,
attempt + 1,
self.max_retries,
f"Server error, waiting {delay}s"
)
await asyncio.sleep(delay)
continue
raise Exception(f"API error ({self.service_name}): Status {response.status}")
except Exception as e:
SystemLogger.log_api_retry(
self.service_name,
attempt + 1,
self.max_retries,
str(e)
)
if attempt == self.max_retries - 1:
raise
await asyncio.sleep(self.retry_delay * (attempt + 1))
@abstractmethod
def _get_headers(self, extra_headers: Optional[Dict] = None) -> Dict[str, str]:
"""Get headers specific to this provider."""
pass
@abstractmethod
def _extract_message_content(self, response: Dict[str, Any]) -> str:
"""Extract message content from provider-specific response format."""
pass
class OpenAIClient(BaseAPIClient):
"""Client for OpenAI API interactions."""
def __init__(self, api_key: str):
super().__init__(
api_key=api_key,
base_url="https://api.openai.com/v1",
service_name="OpenAI"
)
def _get_headers(self, extra_headers: Optional[Dict] = None) -> Dict[str, str]:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
if extra_headers:
headers.update(extra_headers)
return headers
def _extract_message_content(self, response: Dict[str, Any]) -> str:
return response["choices"][0]["message"]["content"]
async def create_completion(
self,
messages: List[Dict[str, str]],
model: str,
temperature: float = 0.7,
max_tokens: int = 150,
return_content_only: bool = False
) -> Union[Dict[str, Any], str]:
"""Create chat completion via OpenAI."""
payload = {
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens
}
response = await self._make_request("chat/completions", payload=payload)
return self._extract_message_content(response) if return_content_only else response
class AnthropicClient(BaseAPIClient):
"""Client for Anthropic API interactions."""
def __init__(self, api_key: str):
super().__init__(
api_key=api_key,
base_url="https://api.anthropic.com/v1",
service_name="Anthropic"
)
def _get_headers(self, extra_headers: Optional[Dict] = None) -> Dict[str, str]:
headers = {
"x-api-key": self.api_key,
"anthropic-version": "2023-06-01",
"content-type": "application/json"
}
if extra_headers:
headers.update(extra_headers)
return headers
def _extract_message_content(self, response: Dict[str, Any]) -> str:
return response["content"][0]["text"]
async def create_completion(
self,
messages: List[Dict[str, str]],
model: str,
temperature: float = 0.7,
max_tokens: int = 150,
return_content_only: bool = False
) -> Union[Dict[str, Any], str]:
"""Create chat completion via Anthropic."""
# Extract system message if present
system_message = next(
(msg["content"] for msg in messages if msg["role"] == "system"),
None
)
payload = {
"model": model,
"messages": [m for m in messages if m["role"] != "system"],
"temperature": temperature,
"max_tokens": max_tokens
}
if system_message:
payload["system"] = system_message
response = await self._make_request("messages", payload=payload)
return self._extract_message_content(response) if return_content_only else response
class GoogleClient(BaseAPIClient):
"""Client for Google AI interactions."""
def __init__(self, api_key: str):
super().__init__(
api_key=api_key,
base_url="https://generativelanguage.googleapis.com/v1",
service_name="Google"
)
def _get_headers(self, extra_headers: Optional[Dict] = None) -> Dict[str, str]:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
if extra_headers:
headers.update(extra_headers)
return headers
def _extract_message_content(self, response: Dict[str, Any]) -> str:
return response["candidates"][0]["content"]["parts"][0]["text"]
async def create_completion(
self,
messages: List[Dict[str, str]],
model: str,
temperature: float = 0.7,
max_tokens: int = 150,
return_content_only: bool = False
) -> Union[Dict[str, Any], str]:
"""Create chat completion via Google."""
formatted_messages = [{
"role": msg["role"],
"parts": [{"text": msg["content"]}]
} for msg in messages]
payload = {
"messages": formatted_messages,
"temperature": temperature,
"maxOutputTokens": max_tokens
}
response = await self._make_request(
f"models/{model}:generateContent",
payload=payload
)
return self._extract_message_content(response) if return_content_only else response
class OpenRouterClient(BaseAPIClient):
"""Client for OpenRouter API interactions."""
def __init__(self, api_key: str):
super().__init__(
api_key=api_key,
base_url="https://openrouter.ai/api/v1",
service_name="OpenRouter"
)
def _get_headers(self, extra_headers: Optional[Dict] = None) -> Dict[str, str]:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
"HTTP-Referer": "http://localhost:8000",
"X-Title": "Hephia Project"
}
if extra_headers:
headers.update(extra_headers)
return headers
def _extract_message_content(self, response: Dict[str, Any]) -> str:
return response["choices"][0]["message"]["content"]
async def create_completion(
self,
messages: List[Dict[str, str]],
model: str,
temperature: float = 0.7,
max_tokens: int = 150,
return_content_only: bool = False
) -> Union[Dict[str, Any], str]:
"""Create chat completion via OpenRouter."""
payload = {
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens
}
response = await self._make_request("chat/completions", payload=payload)
return self._extract_message_content(response) if return_content_only else response
class OpenPipeClient(BaseAPIClient):
"""Client for OpenPipe API interactions"""
def __init__(self, api_key: str):
super().__init__(
api_key=api_key,
base_url="https://api.openpipe.ai/api/v1",
service_name="OpenPipe"
)
def _get_headers(self, extra_headers: Optional[Dict] = None) -> Dict[str, str]:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
if extra_headers:
headers.update(extra_headers)
return headers
def _extract_message_content(self, response: Dict[str, Any]) -> str:
return response["choices"][0]["message"]["content"]
async def create_completion(
self,
messages: List[Dict[str, str]],
model: str,
temperature: float = 0.7,
max_tokens: int = 125,
return_content_only: bool = False
) -> Union[Dict[str, Any], str]:
"""Create chat completion via OpenPipe."""
payload = {
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens
}
response = await self._make_request("chat/completions", payload=payload)
return self._extract_message_content(response) if return_content_only else response
class PerplexityClient(BaseAPIClient):
"""Client for Perplexity API interactions."""
def __init__(self, api_key: str):
super().__init__(
api_key=api_key,
base_url="https://api.perplexity.ai",
service_name="Perplexity"
)
def _get_headers(self, extra_headers: Optional[Dict] = None) -> Dict[str, str]:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
if extra_headers:
headers.update(extra_headers)
return headers
def _extract_message_content(self, response: Dict[str, Any]) -> str:
return response["choices"][0]["message"]["content"]
async def create_completion(
self,
messages: List[Dict[str, str]],
model: str = "llama-3.1-sonar-small-128k-online",
temperature: float = 0.7,
max_tokens: int = 400,
return_content_only: bool = False
) -> Union[Dict[str, Any], str]:
payload = {
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens
}
response = await self._make_request("chat/completions", payload=payload)
return self._extract_message_content(response) if return_content_only else response
class UnixSocketClient(BaseAPIClient):
"""Base client for Unix socket communication."""
def __init__(self, socket_path: str, service_name: str):
super().__init__(
api_key="N/A",
base_url="http://localhost", # The actual hostname doesn't matter for Unix sockets
service_name=service_name
)
self.socket_path = socket_path
def _get_headers(self, extra_headers: Optional[Dict] = None) -> Dict[str, str]:
"""Get headers for Unix socket requests"""
headers = {"Content-Type": "application/json"}
if extra_headers:
headers.update(extra_headers)
return headers
def _extract_message_content(self, response: Dict[str, Any]) -> str:
"""Extract message from Unix socket response"""
return response["choices"][0]["message"]["content"]
async def _make_request(self, endpoint: str, method: str = "POST",
payload: Optional[Dict] = None,
extra_headers: Optional[Dict] = None) -> Dict[str, Any]:
"""Make request via Unix socket with retry logic."""
headers = self._get_headers(extra_headers)
for attempt in range(self.max_retries):
try:
connector = UnixConnector(path=self.socket_path)
async with aiohttp.ClientSession(connector=connector) as session:
url = f"http://localhost/{endpoint.lstrip('/')}"
async with session.request(
method,
url,
headers=headers,
json=payload
) as response:
if response.status == 200:
SystemLogger.log_api_request(
self.service_name,
endpoint,
response.status
)
return await response.json()
error_text = await response.text()
SystemLogger.log_api_request(
self.service_name,
endpoint,
response.status,
error_text
)
if response.status >= 500:
delay = self.retry_delay * (attempt + 1)
SystemLogger.log_api_retry(
self.service_name,
attempt + 1,
self.max_retries,
f"Server error, waiting {delay}s"
)
await asyncio.sleep(delay)
continue
raise Exception(f"Unix socket error: Status {response.status}")
except Exception as e:
SystemLogger.log_api_retry(
self.service_name,
attempt + 1,
self.max_retries,
str(e)
)
if attempt == self.max_retries - 1:
raise
await asyncio.sleep(self.retry_delay * (attempt + 1))
class Chapter2Client(BaseAPIClient):
"""Client for Chapter2 API supporting both HTTP and Unix socket."""
def __init__(self, socket_path: str = None, http_port: int = None):
self.is_unix = platform.system() != "Windows"
self.socket_path = socket_path or os.getenv("CHAPTER2_SOCKET_PATH", "/tmp/chapter2.sock")
self.http_port = http_port or int(os.getenv("CHAPTER2_HTTP_PORT", "6005"))
# Initialize base class with HTTP configuration first
super().__init__(
api_key="N/A",
base_url=f"http://localhost:{self.http_port}/v1",
service_name="Chapter2"
)
# Then check if we should use Unix socket
self.use_unix = self.is_unix and os.path.exists(self.socket_path)
if self.use_unix:
self.unix_client = UnixSocketClient(self.socket_path, "Chapter2")
else:
self.unix_client = None
def _get_headers(self, extra_headers: Optional[Dict] = None) -> Dict[str, str]:
"""Get headers for HTTP requests"""
headers = {"Content-Type": "application/json"}
if extra_headers:
headers.update(extra_headers)
return headers
def _extract_message_content(self, response: Dict[str, Any]) -> str:
"""Extract message content from response"""
return response["choices"][0]["message"]["content"]
async def create_completion(
self,
messages: List[Dict[str, str]],
model: str,
temperature: float = 0.7,
max_tokens: int = 150,
return_content_only: bool = False,
**kwargs
) -> Union[Dict[str, Any], str]:
"""Route completion request to appropriate client."""
if self.use_unix:
response = await self.unix_client._make_request(
"v1/chat/completions",
payload={
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
**kwargs
}
)
return self._extract_message_content(response) if return_content_only else response
return await super().create_completion(
messages=messages,
model=model,
temperature=temperature,
max_tokens=max_tokens,
return_content_only=return_content_only,
**kwargs
)
class APIManager:
"""
Central manager for all API clients.
Handles initialization and provides access to different services.
"""
def __init__(
self,
openai_key: Optional[str] = None,
anthropic_key: Optional[str] = None,
google_key: Optional[str] = None,
openrouter_key: Optional[str] = None,
openpipe_key: Optional[str] = None,
perplexity_key: Optional[str] = None
):
self.clients = {}
if openai_key:
self.clients["openai"] = OpenAIClient(openai_key)
if anthropic_key:
self.clients["anthropic"] = AnthropicClient(anthropic_key)
if google_key:
self.clients["google"] = GoogleClient(google_key)
if openrouter_key:
self.clients["openrouter"] = OpenRouterClient(openrouter_key)
if openpipe_key:
self.clients["openpipe"] = OpenPipeClient(openpipe_key)
if perplexity_key:
self.clients["perplexity"] = PerplexityClient(perplexity_key)
self.clients["chapter2"] = Chapter2Client()
@classmethod
def from_env(cls):
"""Create APIManager from environment variables."""
import os
return cls(
openai_key=os.getenv("OPENAI_API_KEY"),
anthropic_key=os.getenv("ANTHROPIC_API_KEY"),
google_key=os.getenv("GOOGLE_API_KEY"),
openrouter_key=os.getenv("OPENROUTER_API_KEY"),
openpipe_key=os.getenv("OPENPIPE_API_KEY"),
perplexity_key=os.getenv("PERPLEXITY_API_KEY")
)
def get_client(self, provider: str) -> BaseAPIClient:
"""Get specific client by provider name."""
if provider not in self.clients:
raise ValueError(f"Unknown provider: {provider}")
return self.clients[provider]
async def create_completion(
self,
provider: str,
messages: List[Dict[str, str]],
**kwargs
) -> Union[Dict[str, Any], str]:
"""Create completion using specified provider."""
if provider not in self.clients:
raise ValueError(f"Unknown provider: {provider}")
return await self.clients[provider].create_completion(messages, **kwargs)