|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, patch |
| 4 | + |
| 5 | +from semantic_kernel.connectors.ai.google.google_ai.google_ai_prompt_execution_settings import ( |
| 6 | + GoogleAIChatPromptExecutionSettings, |
| 7 | + GoogleAIEmbeddingPromptExecutionSettings, |
| 8 | + GoogleAITextPromptExecutionSettings, |
| 9 | +) |
| 10 | +from semantic_kernel.connectors.ai.google.google_ai.services.google_ai_chat_completion import GoogleAIChatCompletion |
| 11 | +from semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_completion import GoogleAITextCompletion |
| 12 | +from semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_embedding import GoogleAITextEmbedding |
| 13 | +from semantic_kernel.const import USER_AGENT |
| 14 | +from semantic_kernel.contents.chat_history import ChatHistory |
| 15 | + |
| 16 | + |
| 17 | +async def test_google_ai_chat_completion_user_agent(google_ai_unit_test_env): |
| 18 | + """Test that GoogleAIChatCompletion sends the User-Agent header.""" |
| 19 | + chat_history = ChatHistory() |
| 20 | + chat_history.add_user_message("hi") |
| 21 | + |
| 22 | + with patch( |
| 23 | + "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_chat_completion.Client" |
| 24 | + ) as mock_client: |
| 25 | + mock_instance = mock_client.return_value.__enter__.return_value |
| 26 | + mock_instance.aio.models.generate_content = AsyncMock() |
| 27 | + |
| 28 | + service = GoogleAIChatCompletion(gemini_model_id="gemini-3-flash-preview", api_key="test-api-key") |
| 29 | + |
| 30 | + await service.get_chat_message_contents( |
| 31 | + chat_history=chat_history, settings=GoogleAIChatPromptExecutionSettings() |
| 32 | + ) |
| 33 | + |
| 34 | + _, kwargs = mock_client.call_args |
| 35 | + assert "http_options" in kwargs |
| 36 | + assert kwargs["http_options"] is not None |
| 37 | + assert "headers" in kwargs["http_options"] |
| 38 | + assert USER_AGENT in kwargs["http_options"]["headers"] |
| 39 | + assert "semantic-kernel-python" in kwargs["http_options"]["headers"][USER_AGENT] |
| 40 | + |
| 41 | + |
| 42 | +async def test_google_ai_chat_completion_vertex_ai_user_agent(google_ai_unit_test_env): |
| 43 | + """Test that GoogleAIChatCompletion sends the User-Agent header with Vertex AI.""" |
| 44 | + chat_history = ChatHistory() |
| 45 | + chat_history.add_user_message("hi") |
| 46 | + |
| 47 | + with patch( |
| 48 | + "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_chat_completion.Client" |
| 49 | + ) as mock_client: |
| 50 | + mock_instance = mock_client.return_value.__enter__.return_value |
| 51 | + mock_instance.aio.models.generate_content = AsyncMock() |
| 52 | + |
| 53 | + service = GoogleAIChatCompletion( |
| 54 | + gemini_model_id="gemini-3-flash-preview", |
| 55 | + use_vertexai=True, |
| 56 | + project_id="test-project", |
| 57 | + region="test-region", |
| 58 | + ) |
| 59 | + |
| 60 | + await service.get_chat_message_contents( |
| 61 | + chat_history=chat_history, settings=GoogleAIChatPromptExecutionSettings() |
| 62 | + ) |
| 63 | + |
| 64 | + _, kwargs = mock_client.call_args |
| 65 | + assert "http_options" in kwargs |
| 66 | + assert kwargs["http_options"] is not None |
| 67 | + assert "headers" in kwargs["http_options"] |
| 68 | + assert USER_AGENT in kwargs["http_options"]["headers"] |
| 69 | + assert "semantic-kernel-python" in kwargs["http_options"]["headers"][USER_AGENT] |
| 70 | + |
| 71 | + |
| 72 | +async def test_google_ai_chat_completion_no_telemetry(google_ai_unit_test_env): |
| 73 | + """Test that GoogleAIChatCompletion does not send the User-Agent header when telemetry is disabled.""" |
| 74 | + chat_history = ChatHistory() |
| 75 | + chat_history.add_user_message("hi") |
| 76 | + |
| 77 | + with ( |
| 78 | + patch("semantic_kernel.connectors.ai.google.google_ai.services.google_ai_base.IS_TELEMETRY_ENABLED", False), |
| 79 | + patch( |
| 80 | + "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_chat_completion.Client" |
| 81 | + ) as mock_client, |
| 82 | + ): |
| 83 | + mock_instance = mock_client.return_value.__enter__.return_value |
| 84 | + mock_instance.aio.models.generate_content = AsyncMock() |
| 85 | + |
| 86 | + service = GoogleAIChatCompletion(gemini_model_id="gemini-3-flash-preview", api_key="test-api-key") |
| 87 | + |
| 88 | + await service.get_chat_message_contents( |
| 89 | + chat_history=chat_history, settings=GoogleAIChatPromptExecutionSettings() |
| 90 | + ) |
| 91 | + |
| 92 | + _, kwargs = mock_client.call_args |
| 93 | + assert "http_options" in kwargs |
| 94 | + assert kwargs["http_options"] is None |
| 95 | + |
| 96 | + |
| 97 | +async def test_google_ai_text_completion_user_agent(google_ai_unit_test_env): |
| 98 | + """Test that GoogleAITextCompletion sends the User-Agent header.""" |
| 99 | + with patch( |
| 100 | + "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_completion.Client" |
| 101 | + ) as mock_client: |
| 102 | + mock_instance = mock_client.return_value.__enter__.return_value |
| 103 | + mock_instance.aio.models.generate_content = AsyncMock() |
| 104 | + |
| 105 | + service = GoogleAITextCompletion(gemini_model_id="gemini-3-flash-preview", api_key="test-api-key") |
| 106 | + |
| 107 | + await service.get_text_contents(prompt="hi", settings=GoogleAITextPromptExecutionSettings()) |
| 108 | + |
| 109 | + _, kwargs = mock_client.call_args |
| 110 | + assert "http_options" in kwargs |
| 111 | + assert kwargs["http_options"] is not None |
| 112 | + assert "headers" in kwargs["http_options"] |
| 113 | + assert USER_AGENT in kwargs["http_options"]["headers"] |
| 114 | + assert "semantic-kernel-python" in kwargs["http_options"]["headers"][USER_AGENT] |
| 115 | + |
| 116 | + |
| 117 | +async def test_google_ai_text_completion_no_telemetry(google_ai_unit_test_env): |
| 118 | + """Test that GoogleAITextCompletion does not send the User-Agent header when telemetry is disabled.""" |
| 119 | + with ( |
| 120 | + patch("semantic_kernel.connectors.ai.google.google_ai.services.google_ai_base.IS_TELEMETRY_ENABLED", False), |
| 121 | + patch( |
| 122 | + "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_completion.Client" |
| 123 | + ) as mock_client, |
| 124 | + ): |
| 125 | + mock_instance = mock_client.return_value.__enter__.return_value |
| 126 | + mock_instance.aio.models.generate_content = AsyncMock() |
| 127 | + |
| 128 | + service = GoogleAITextCompletion(gemini_model_id="gemini-3-flash-preview", api_key="test-api-key") |
| 129 | + |
| 130 | + await service.get_text_contents(prompt="hi", settings=GoogleAITextPromptExecutionSettings()) |
| 131 | + |
| 132 | + _, kwargs = mock_client.call_args |
| 133 | + assert "http_options" in kwargs |
| 134 | + assert kwargs["http_options"] is None |
| 135 | + |
| 136 | + |
| 137 | +async def test_google_ai_text_embedding_user_agent(google_ai_unit_test_env): |
| 138 | + """Test that GoogleAITextEmbedding sends the User-Agent header.""" |
| 139 | + with patch( |
| 140 | + "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_embedding.Client" |
| 141 | + ) as mock_client: |
| 142 | + mock_instance = mock_client.return_value.__enter__.return_value |
| 143 | + mock_instance.aio.models.embed_content = AsyncMock() |
| 144 | + |
| 145 | + service = GoogleAITextEmbedding(embedding_model_id="gemini-embedding-2-preview", api_key="test-api-key") |
| 146 | + |
| 147 | + await service.generate_embeddings(texts=["hi"], settings=GoogleAIEmbeddingPromptExecutionSettings()) |
| 148 | + |
| 149 | + _, kwargs = mock_client.call_args |
| 150 | + assert "http_options" in kwargs |
| 151 | + assert kwargs["http_options"] is not None |
| 152 | + assert "headers" in kwargs["http_options"] |
| 153 | + assert USER_AGENT in kwargs["http_options"]["headers"] |
| 154 | + assert "semantic-kernel-python" in kwargs["http_options"]["headers"][USER_AGENT] |
| 155 | + |
| 156 | + |
| 157 | +async def test_google_ai_text_embedding_no_telemetry(google_ai_unit_test_env): |
| 158 | + """Test that GoogleAITextEmbedding does not send the User-Agent header when telemetry is disabled.""" |
| 159 | + with ( |
| 160 | + patch("semantic_kernel.connectors.ai.google.google_ai.services.google_ai_base.IS_TELEMETRY_ENABLED", False), |
| 161 | + patch("semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_embedding.Client") as mock_client, |
| 162 | + ): |
| 163 | + mock_instance = mock_client.return_value.__enter__.return_value |
| 164 | + mock_instance.aio.models.embed_content = AsyncMock() |
| 165 | + |
| 166 | + service = GoogleAITextEmbedding(embedding_model_id="gemini-embedding-2-preview", api_key="test-api-key") |
| 167 | + |
| 168 | + await service.generate_embeddings(texts=["hi"], settings=GoogleAIEmbeddingPromptExecutionSettings()) |
| 169 | + |
| 170 | + _, kwargs = mock_client.call_args |
| 171 | + assert "http_options" in kwargs |
| 172 | + assert kwargs["http_options"] is None |
0 commit comments