-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMakerSuite.py
69 lines (59 loc) · 2.74 KB
/
MakerSuite.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
import APIKey
gemini_models = ["gemini-1.5-pro", "gemini-1.0-ultra"]
async def check_makersuite(key: APIKey, session):
async with session.get(f"https://generativelanguage.googleapis.com/v1beta/models?key={key.api_key}") as response:
if response.status != 200:
return
check_billing = await test_makersuite_billing(key, session)
if not check_billing:
return
response_json = await response.json()
model_names = [model['name'].replace('models/', '').replace('-latest', '') for model in response_json['models']]
for model in gemini_models:
if model in model_names:
key.models.append(model)
return True
# rpm limit of 2 on nonbilling keys is hit or miss for me, and rpm isn't returned in headers like oai/anthro so have to check it like this unfortunately.
# google will also start terminating generation requests on high key batches (50+)
async def test_makersuite_billing(key: APIKey, session):
data = {
"generationConfig": {
"max_output_tokens": 1
},
"contents": {
"role": "user",
"parts": [
{
"text": "test\n" * 32000
}
]
}
}
async with session.post(f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent?key={key.api_key}", json=data) as response:
resp_json = await response.json()
if response.status != 429:
key.enabled_billing = True
else:
error_details = resp_json.get('error', {}).get('message', '')
# different type of 429 error compared to hitting the rpm limit, keys with this seem to never recover and are just perma 429'd, so we mark them as invalid
if "limit 'GenerateContent request limit per minute for a region' of service 'generativelanguage.googleapis.com' for consumer" in error_details:
return
return True
def pretty_print_makersuite_keys(keys):
model_counts = {model: [] for model in gemini_models}
total = 0
billing_count = 0
for key in keys:
for model in key.models:
model_counts[model].append(key)
total += 1
if key.enabled_billing:
billing_count += 1
print('-' * 90)
print(f'Validated {len(keys)} MakerSuite keys:')
for model, keys in model_counts.items():
print(f'\n{len(keys)} keys with model {model}:')
sorted_keys = sorted(keys, key=lambda x: not x.enabled_billing)
for key in sorted_keys:
print(f'{key.api_key}' + (' | billing enabled' if key.enabled_billing else ''))
print(f'\n--- Total Valid MakerSuite Keys: {total} ({billing_count} with billing enabled) ---\n')