Skip to content

Commit 457b296

Browse files
authored
Merge pull request #728 from BassCoder2808/GeminiVertex
Authentication via VertexAI
2 parents b507f4d + af5ad8a commit 457b296

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/vanna/google/gemini_chat.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
23
from ..base import VannaBase
34

45

@@ -30,8 +31,29 @@ def __init__(self, config=None):
3031
self.chat_model = genai.GenerativeModel(model_name)
3132
else:
3233
# Authenticate using VertexAI
34+
import google.auth
35+
import vertexai
3336
from vertexai.generative_models import GenerativeModel
34-
self.chat_model = GenerativeModel(model_name)
37+
38+
json_file_path = config.get("google_credentials") # Assuming the JSON file path is provided in the config
39+
40+
if not json_file_path or not os.path.exists(json_file_path):
41+
raise FileNotFoundError(f"JSON credentials file not found at: {json_file_path}")
42+
43+
try:
44+
# Validate and set the JSON file path for GOOGLE_APPLICATION_CREDENTIALS
45+
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = json_file_path
46+
47+
# Initialize VertexAI with the credentials
48+
credentials, _ = google.auth.default()
49+
vertexai.init(credentials=credentials)
50+
self.chat_model = GenerativeModel(model_name)
51+
except google.auth.exceptions.DefaultCredentialsError as e:
52+
raise RuntimeError(f"Default credentials error: {e}")
53+
except google.auth.exceptions.TransportError as e:
54+
raise RuntimeError(f"Transport error during authentication: {e}")
55+
except Exception as e:
56+
raise RuntimeError(f"Failed to authenticate using JSON file: {e}")
3557

3658
def system_message(self, message: str) -> any:
3759
return message

0 commit comments

Comments
 (0)