-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathVertexAI.py
87 lines (69 loc) · 3.02 KB
/
VertexAI.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
import asyncio
import APIKey
import json
from google.oauth2 import service_account
from google.auth.transport.requests import Request
location = 'us-east5'
model = 'claude-3-opus@20240229'
async def check_vertexai(key: APIKey, session):
try:
with open(key.api_key, 'r') as file:
data = json.load(file)
if data.get('type') != 'service_account':
return
project_id = data.get('project_id')
if not project_id:
return
key.project_id = project_id
credentials = service_account.Credentials.from_service_account_file(
key.api_key,
scopes=['https://www.googleapis.com/auth/cloud-platform']
)
credentials.refresh(Request())
key.has_opus = await test_model_response(key, credentials.token, session)
return True
except Exception:
return
async def test_model_response(key: APIKey, access_token, session):
url = f"https://{location}-aiplatform.googleapis.com/v1/projects/{key.project_id}/locations/{location}/publishers/anthropic/models/{model}:rawPredict"
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json; charset=utf-8"
}
data = {
"anthropic_version": "vertex-2023-10-16",
"messages": [
{'role': 'user', 'content': ''}
],
"max_tokens": 0,
}
for i in range(5):
try:
async with session.post(url, headers=headers, json=data) as response:
resp = await response.json()
if response.status == 429:
print(f"Rate limited on vertexai model response, retrying in 5 seconds (attempt {i} of 5)")
await asyncio.sleep(5)
continue
# returned on non enabled models afaik, couldn't test on the anthropic models though since they're all enabled on my service account
elif response.status == 400 and resp.get('error', {}).get('status') == 'FAILED_PRECONDITION':
return False
elif response.status == 400 and resp.get('error', {}).get('type') == 'invalid_request_error':
return True
else:
return False
except Exception as e:
print(f"Error testing model response: {str(e)}")
return False
def pretty_print_vertexai_keys(keys):
print('-' * 90)
print(f'Validated {len(keys)} Google Vertex AI keys:')
keys_with_opus = [key for key in keys if key.has_opus]
keys_without_opus = [key for key in keys if not key.has_opus]
print(f'\nValid keys with Opus enabled: {len(keys_with_opus)}')
for key in keys_with_opus:
print(f'{key.api_key} | {key.project_id} | has opus')
print(f'\nValid keys without Opus enabled: {len(keys_without_opus)}')
for key in keys_without_opus:
print(f'{key.api_key} | {key.project_id}')
print(f'\n--- Total Valid Google Vertex AI Keys: {len(keys)} ({len(keys_with_opus)} with Opus enabled) ---\n')