Skip to content

Commit 2bf8b08

Browse files
giulio-leonegiulio-leone
authored andcommitted
fix: add vertexai parameter validation and tests
1 parent 3ef6b52 commit 2bf8b08

6 files changed

Lines changed: 49 additions & 1 deletion

File tree

python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_chat_completion.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ def __init__(
119119
if not client:
120120
if google_ai_settings.use_vertexai and not google_ai_settings.cloud_project_id:
121121
raise ServiceInitializationError("Project ID must be provided when use_vertexai is True.")
122+
if google_ai_settings.use_vertexai and not google_ai_settings.cloud_region:
123+
raise ServiceInitializationError("Region must be provided when use_vertexai is True.")
122124
if not google_ai_settings.use_vertexai and not google_ai_settings.api_key:
123125
raise ServiceInitializationError("The API key is required when use_vertexai is False.")
124126

python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_completion.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def __init__(
9090
if not client:
9191
if google_ai_settings.use_vertexai and not google_ai_settings.cloud_project_id:
9292
raise ServiceInitializationError("Project ID must be provided when use_vertexai is True.")
93+
if google_ai_settings.use_vertexai and not google_ai_settings.cloud_region:
94+
raise ServiceInitializationError("Region must be provided when use_vertexai is True.")
9395
if not google_ai_settings.use_vertexai and not google_ai_settings.api_key:
9496
raise ServiceInitializationError("The API key is required when use_vertexai is False.")
9597

python/semantic_kernel/connectors/ai/google/google_ai/services/google_ai_text_embedding.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def __init__(
8181
if not client:
8282
if google_ai_settings.use_vertexai and not google_ai_settings.cloud_project_id:
8383
raise ServiceInitializationError("Project ID must be provided when use_vertexai is True.")
84+
if google_ai_settings.use_vertexai and not google_ai_settings.cloud_region:
85+
raise ServiceInitializationError("Region must be provided when use_vertexai is True.")
8486
if not google_ai_settings.use_vertexai and not google_ai_settings.api_key:
8587
raise ServiceInitializationError("The API key is required when use_vertexai is False.")
8688

python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_chat_completion.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ def test_google_ai_chat_completion_init_with_use_vertexai_missing_project_id(goo
7575
GoogleAIChatCompletion(use_vertexai=True, env_file_path="fake_env_file_path.env")
7676

7777

78+
@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_CLOUD_REGION"]], indirect=True)
79+
def test_google_ai_chat_completion_init_with_use_vertexai_missing_region(google_ai_unit_test_env) -> None:
80+
"""Test initialization of GoogleAIChatCompletion with use_vertexai true but missing region"""
81+
with pytest.raises(ServiceInitializationError, match="Region must be provided when use_vertexai is True."):
82+
GoogleAIChatCompletion(use_vertexai=True, env_file_path="fake_env_file_path.env")
83+
84+
85+
@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_API_KEY"]], indirect=True)
86+
def test_google_ai_chat_completion_init_with_use_vertexai_no_api_key(google_ai_unit_test_env) -> None:
87+
"""Test initialization of GoogleAIChatCompletion succeeds with use_vertexai=True and no api_key"""
88+
chat_completion = GoogleAIChatCompletion(use_vertexai=True)
89+
assert chat_completion.service_settings.use_vertexai is True
90+
91+
7892
def test_prompt_execution_settings_class(google_ai_unit_test_env) -> None:
7993
google_ai_chat_completion = GoogleAIChatCompletion()
8094
assert google_ai_chat_completion.get_prompt_execution_settings_class() == GoogleAIChatPromptExecutionSettings

python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_completion.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ def test_google_ai_text_completion_init_with_use_vertexai_missing_project_id(goo
6767
GoogleAITextCompletion(use_vertexai=True, env_file_path="fake_env_file_path.env")
6868

6969

70+
@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_CLOUD_REGION"]], indirect=True)
71+
def test_google_ai_text_completion_init_with_use_vertexai_missing_region(google_ai_unit_test_env) -> None:
72+
"""Test initialization of GoogleAITextCompletion with use_vertexai true but missing region"""
73+
with pytest.raises(ServiceInitializationError, match="Region must be provided when use_vertexai is True."):
74+
GoogleAITextCompletion(use_vertexai=True, env_file_path="fake_env_file_path.env")
75+
76+
77+
@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_API_KEY"]], indirect=True)
78+
def test_google_ai_text_completion_init_with_use_vertexai_no_api_key(google_ai_unit_test_env) -> None:
79+
"""Test initialization of GoogleAITextCompletion succeeds with use_vertexai=True and no api_key"""
80+
text_completion = GoogleAITextCompletion(use_vertexai=True)
81+
assert text_completion.service_settings.use_vertexai is True
82+
83+
7084
def test_prompt_execution_settings_class(google_ai_unit_test_env) -> None:
7185
google_ai_text_completion = GoogleAITextCompletion()
7286
assert google_ai_text_completion.get_prompt_execution_settings_class() == GoogleAITextPromptExecutionSettings

python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_text_embedding.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ def test_google_ai_text_embedding_init_with_empty_model_id(google_ai_unit_test_e
5757
def test_google_ai_text_embedding_init_with_empty_api_key(google_ai_unit_test_env) -> None:
5858
"""Test initialization of GoogleAITextEmbedding with an empty api_key"""
5959
with pytest.raises(ServiceInitializationError, match="The API key is required when use_vertexai is False."):
60-
GoogleAITextEmbedding(use_vertexai=True, env_file_path="fake_env_file_path.env")
60+
GoogleAITextEmbedding(env_file_path="fake_env_file_path.env")
61+
62+
63+
@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_API_KEY"]], indirect=True)
64+
def test_google_ai_text_embedding_init_with_use_vertexai_no_api_key(google_ai_unit_test_env) -> None:
65+
"""Test initialization of GoogleAITextEmbedding succeeds with use_vertexai=True and no api_key"""
66+
embedding = GoogleAITextEmbedding(use_vertexai=True)
67+
assert embedding.service_settings.use_vertexai is True
6168

6269

6370
@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_CLOUD_PROJECT_ID"]], indirect=True)
@@ -67,6 +74,13 @@ def test_google_ai_text_embedding_init_with_use_vertexai_missing_project_id(goog
6774
GoogleAITextEmbedding(use_vertexai=True, env_file_path="fake_env_file_path.env")
6875

6976

77+
@pytest.mark.parametrize("exclude_list", [["GOOGLE_AI_CLOUD_REGION"]], indirect=True)
78+
def test_google_ai_text_embedding_init_with_use_vertexai_missing_region(google_ai_unit_test_env) -> None:
79+
"""Test initialization of GoogleAITextEmbedding with use_vertexai true but missing region"""
80+
with pytest.raises(ServiceInitializationError, match="Region must be provided when use_vertexai is True."):
81+
GoogleAITextEmbedding(use_vertexai=True, env_file_path="fake_env_file_path.env")
82+
83+
7084
def test_prompt_execution_settings_class(google_ai_unit_test_env) -> None:
7185
google_ai_text_embedding = GoogleAITextEmbedding()
7286
assert google_ai_text_embedding.get_prompt_execution_settings_class() == GoogleAIEmbeddingPromptExecutionSettings

0 commit comments

Comments
 (0)