Repository navigation
Expand file tree
/
Copy pathcborg_api.py
More file actions
90 lines (74 loc) · 2.72 KB
/
Copy pathcborg_api.py
File metadata and controls
90 lines (74 loc) · 2.72 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
"""CBORG API client."""
import requests
from typing import List, Dict, Optional
from openai import OpenAI
class CBORGClient:
"""Client for interacting with CBORG API."""
def __init__(self, api_key: str, base_url: str = "https://api.cborg.lbl.gov"):
"""Initialize CBORG client."""
self.api_key = api_key
self.base_url = base_url
self.client = OpenAI(api_key=api_key, base_url=f"{base_url}/v1")
def get_models(self) -> List[str]:
"""
Get list of all available models.
Returns:
List of model IDs
"""
try:
models = self.client.models.list()
return [model.id for model in models.data]
except Exception as e:
raise Exception(f"Failed to fetch models: {e}")
def get_key_info(self) -> Optional[Dict]:
"""
Get API key information including spend and budget.
Returns:
Key info dict or None if not available
"""
try:
headers = {"Authorization": f"Bearer {self.api_key}"}
response = requests.get(f"{self.base_url}/key/info", headers=headers)
if response.status_code == 200:
return response.json()
else:
return None
except Exception:
return None
def get_spend_info(self) -> Optional[Dict]:
"""
Get spending information.
Returns:
Spend info dict with current_spend, budget_limit, remaining, etc.
None if not available
"""
key_info = self.get_key_info()
if key_info and 'info' in key_info:
info = key_info['info']
current_spend = info.get('spend', 0)
budget_limit = info.get('max_budget')
return {
'current_spend': current_spend,
'budget_limit': budget_limit,
'remaining': budget_limit - current_spend if budget_limit else None,
'reset_date': info.get('budget_reset_at'),
'key_alias': info.get('key_alias'),
'created_at': info.get('created_at'),
'updated_at': info.get('updated_at'),
'expires': info.get('expires'),
'blocked': info.get('blocked'),
'soft_budget_cooldown': info.get('soft_budget_cooldown'),
'model_spend': info.get('model_spend', {})
}
return None
def test_connection(self) -> bool:
"""
Test if the API connection is working.
Returns:
True if connection successful
"""
try:
models = self.get_models()
return len(models) > 0
except Exception:
return False