superdup95 commited on
Commit
34d557c
·
verified ·
1 Parent(s): 629bd6c

Create vertexLib.py

Browse files
Files changed (1) hide show
  1. vertexLib.py +54 -0
vertexLib.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import time
3
+ import requests
4
+ import jwt
5
+
6
+ def get_access_token(project_id, client_email, private_key):
7
+ current_time = int(time.time())
8
+ expiration_time = current_time + 600
9
+
10
+ claims = {
11
+ 'iss': client_email,
12
+ 'scope': 'https://www.googleapis.com/auth/cloud-platform',
13
+ 'aud': 'https://oauth2.googleapis.com/token',
14
+ 'exp': expiration_time,
15
+ 'iat': current_time
16
+ }
17
+
18
+ try:
19
+ signed_jwt = jwt.encode(
20
+ claims,
21
+ private_key,
22
+ algorithm='RS256'
23
+ )
24
+ except Exception as e:
25
+ return False, e
26
+
27
+ # Request the access token
28
+ response = requests.post(
29
+ 'https://oauth2.googleapis.com/token',
30
+ data={
31
+ 'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
32
+ 'assertion': signed_jwt
33
+ }
34
+ )
35
+
36
+ if response.status_code == 200:
37
+ access_token = response.json()['access_token']
38
+ return True, access_token
39
+ else:
40
+ return False, response.text
41
+
42
+ async def send_gcp_request(session, project_id, access_token, payload, region='us-east5', model='claude-3-5-sonnet@20240620'):
43
+ VERTEX_URL = f'https://{region}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{region}/publishers/anthropic/models/{model}:streamRawPredict'
44
+ headers = {
45
+ 'Authorization': f'Bearer {access_token}',
46
+ 'Content-Type': 'application/json; charset=utf-8'
47
+ }
48
+
49
+ async with session.post(url=VERTEX_URL, headers=headers, data=payload) as response:
50
+ if response.status != 200:
51
+ response_data = await response.text()
52
+ return json.loads(response_data)
53
+
54
+ return await response.json()