File size: 1,130 Bytes
420fa8a
 
 
 
 
a2de729
 
420fa8a
 
 
 
 
a2de729
 
 
316b9d4
 
 
a2de729
 
420fa8a
316b9d4
 
 
 
 
 
38f3034
316b9d4
 
 
 
420fa8a
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
from typing import Optional
from dotenv import load_dotenv
from google.oauth2 import service_account
import vertexai
import os
import json
import base64

load_dotenv()
VERTEXAI_PROJECT = os.environ["VERTEXAI_PROJECT"]


def decode_service_key():
    encoded_key = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
    original_service_key = json.loads(base64.b64decode(encoded_key).decode('utf-8'))
    if original_service_key:
        return original_service_key
    return None


def initialize_vertexai_params(location: Optional[str] = "us-central1"):
    credentials = decode_service_key()
    creds_file_name = os.getcwd() + "/.config/application_default_credentials.json"
    with open(creds_file_name, 'w', encoding='utf-8') as file:
        json.dump(credentials, file, ensure_ascii=False, indent=4)

    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = creds_file_name

    service_account.Credentials.from_service_account_file(
        filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"],
        scopes=["https://www.googleapis.com/auth/cloud-platform"],
    )
    vertexai.init(project=VERTEXAI_PROJECT, location=location)