|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from azure.ai.inference.aio import ChatCompletionsClient |
| 7 | + |
| 8 | +from semantic_kernel.connectors.ai.azure_ai_inference.azure_ai_inference_prompt_execution_settings import ( |
| 9 | + AzureAIInferenceChatPromptExecutionSettings, |
| 10 | +) |
| 11 | +from semantic_kernel.connectors.ai.azure_ai_inference.services.azure_ai_inference_chat_completion import ( |
| 12 | + AzureAIInferenceChatCompletion, |
| 13 | +) |
| 14 | +from semantic_kernel.contents.chat_history import ChatHistory |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.asyncio |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "azure_ai_inference_service", |
| 20 | + [AzureAIInferenceChatCompletion.__name__], |
| 21 | + indirect=True, |
| 22 | +) |
| 23 | +@patch.object(ChatCompletionsClient, "complete", new_callable=AsyncMock) |
| 24 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.uninstrument") |
| 25 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.instrument") |
| 26 | +async def test_azure_ai_inference_chat_completion_instrumentation( |
| 27 | + mock_instrument, |
| 28 | + mock_uninstrument, |
| 29 | + mock_complete, |
| 30 | + azure_ai_inference_service, |
| 31 | + chat_history: ChatHistory, |
| 32 | + mock_azure_ai_inference_chat_completion_response, |
| 33 | + model_diagnostics_test_env, |
| 34 | +) -> None: |
| 35 | + """Test completion of AzureAIInferenceChatCompletion""" |
| 36 | + settings = AzureAIInferenceChatPromptExecutionSettings() |
| 37 | + |
| 38 | + mock_complete.return_value = mock_azure_ai_inference_chat_completion_response |
| 39 | + |
| 40 | + await azure_ai_inference_service.get_chat_message_contents(chat_history=chat_history, settings=settings) |
| 41 | + |
| 42 | + mock_instrument.assert_called_once_with(enable_content_recording=True) |
| 43 | + mock_uninstrument.assert_called_once() |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "azure_ai_inference_service", |
| 49 | + [ |
| 50 | + AzureAIInferenceChatCompletion.__name__, |
| 51 | + ], |
| 52 | + indirect=True, |
| 53 | +) |
| 54 | +@pytest.mark.parametrize( |
| 55 | + "override_env_param_dict", |
| 56 | + [ |
| 57 | + { |
| 58 | + "SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS": "False", |
| 59 | + "SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE": "False", |
| 60 | + }, |
| 61 | + ], |
| 62 | + indirect=True, |
| 63 | +) |
| 64 | +@patch.object(ChatCompletionsClient, "complete", new_callable=AsyncMock) |
| 65 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.uninstrument") |
| 66 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.instrument") |
| 67 | +async def test_azure_ai_inference_chat_completion_not_instrumentation( |
| 68 | + mock_instrument, |
| 69 | + mock_uninstrument, |
| 70 | + mock_complete, |
| 71 | + azure_ai_inference_service, |
| 72 | + chat_history: ChatHistory, |
| 73 | + mock_azure_ai_inference_chat_completion_response, |
| 74 | + model_diagnostics_test_env, |
| 75 | +) -> None: |
| 76 | + """Test completion of AzureAIInferenceChatCompletion""" |
| 77 | + settings = AzureAIInferenceChatPromptExecutionSettings() |
| 78 | + |
| 79 | + mock_complete.return_value = mock_azure_ai_inference_chat_completion_response |
| 80 | + |
| 81 | + await azure_ai_inference_service.get_chat_message_contents(chat_history=chat_history, settings=settings) |
| 82 | + |
| 83 | + mock_instrument.assert_not_called() |
| 84 | + mock_uninstrument.assert_not_called() |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +@pytest.mark.parametrize( |
| 89 | + "azure_ai_inference_service", |
| 90 | + [ |
| 91 | + AzureAIInferenceChatCompletion.__name__, |
| 92 | + ], |
| 93 | + indirect=True, |
| 94 | +) |
| 95 | +@pytest.mark.parametrize( |
| 96 | + "override_env_param_dict", |
| 97 | + [ |
| 98 | + { |
| 99 | + "SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS": "True", |
| 100 | + "SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE": "False", |
| 101 | + }, |
| 102 | + ], |
| 103 | + indirect=True, |
| 104 | +) |
| 105 | +@patch.object(ChatCompletionsClient, "complete", new_callable=AsyncMock) |
| 106 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.uninstrument") |
| 107 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.instrument") |
| 108 | +async def test_azure_ai_inference_chat_completion_instrumentation_without_sensitive( |
| 109 | + mock_instrument, |
| 110 | + mock_uninstrument, |
| 111 | + mock_complete, |
| 112 | + azure_ai_inference_service, |
| 113 | + chat_history: ChatHistory, |
| 114 | + mock_azure_ai_inference_chat_completion_response, |
| 115 | + model_diagnostics_test_env, |
| 116 | +) -> None: |
| 117 | + """Test completion of AzureAIInferenceChatCompletion""" |
| 118 | + settings = AzureAIInferenceChatPromptExecutionSettings() |
| 119 | + |
| 120 | + mock_complete.return_value = mock_azure_ai_inference_chat_completion_response |
| 121 | + |
| 122 | + await azure_ai_inference_service.get_chat_message_contents(chat_history=chat_history, settings=settings) |
| 123 | + |
| 124 | + mock_instrument.assert_called_once_with(enable_content_recording=False) |
| 125 | + mock_uninstrument.assert_called_once() |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.asyncio |
| 129 | +@pytest.mark.parametrize( |
| 130 | + "azure_ai_inference_service", |
| 131 | + [AzureAIInferenceChatCompletion.__name__], |
| 132 | + indirect=True, |
| 133 | +) |
| 134 | +@patch.object(ChatCompletionsClient, "complete", new_callable=AsyncMock) |
| 135 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.uninstrument") |
| 136 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.instrument") |
| 137 | +async def test_azure_ai_inference_streaming_chat_completion_instrumentation( |
| 138 | + mock_instrument, |
| 139 | + mock_uninstrument, |
| 140 | + mock_complete, |
| 141 | + azure_ai_inference_service, |
| 142 | + chat_history: ChatHistory, |
| 143 | + mock_azure_ai_inference_streaming_chat_completion_response, |
| 144 | + model_diagnostics_test_env, |
| 145 | +) -> None: |
| 146 | + """Test completion of AzureAIInferenceChatCompletion""" |
| 147 | + settings = AzureAIInferenceChatPromptExecutionSettings() |
| 148 | + |
| 149 | + mock_complete.return_value = mock_azure_ai_inference_streaming_chat_completion_response |
| 150 | + |
| 151 | + async for _ in azure_ai_inference_service.get_streaming_chat_message_contents( |
| 152 | + chat_history=chat_history, settings=settings |
| 153 | + ): |
| 154 | + pass |
| 155 | + |
| 156 | + mock_instrument.assert_called_once_with(enable_content_recording=True) |
| 157 | + mock_uninstrument.assert_called_once() |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.asyncio |
| 161 | +@pytest.mark.parametrize( |
| 162 | + "azure_ai_inference_service", |
| 163 | + [ |
| 164 | + AzureAIInferenceChatCompletion.__name__, |
| 165 | + ], |
| 166 | + indirect=True, |
| 167 | +) |
| 168 | +@pytest.mark.parametrize( |
| 169 | + "override_env_param_dict", |
| 170 | + [ |
| 171 | + { |
| 172 | + "SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS": "False", |
| 173 | + "SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE": "False", |
| 174 | + }, |
| 175 | + ], |
| 176 | + indirect=True, |
| 177 | +) |
| 178 | +@patch.object(ChatCompletionsClient, "complete", new_callable=AsyncMock) |
| 179 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.uninstrument") |
| 180 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.instrument") |
| 181 | +async def test_azure_ai_inference_streaming_chat_completion_not_instrumentation( |
| 182 | + mock_instrument, |
| 183 | + mock_uninstrument, |
| 184 | + mock_complete, |
| 185 | + azure_ai_inference_service, |
| 186 | + chat_history: ChatHistory, |
| 187 | + mock_azure_ai_inference_streaming_chat_completion_response, |
| 188 | + model_diagnostics_test_env, |
| 189 | +) -> None: |
| 190 | + """Test completion of AzureAIInferenceChatCompletion""" |
| 191 | + settings = AzureAIInferenceChatPromptExecutionSettings() |
| 192 | + |
| 193 | + mock_complete.return_value = mock_azure_ai_inference_streaming_chat_completion_response |
| 194 | + |
| 195 | + async for _ in azure_ai_inference_service.get_streaming_chat_message_contents( |
| 196 | + chat_history=chat_history, settings=settings |
| 197 | + ): |
| 198 | + pass |
| 199 | + |
| 200 | + mock_instrument.assert_not_called() |
| 201 | + mock_uninstrument.assert_not_called() |
| 202 | + |
| 203 | + |
| 204 | +@pytest.mark.asyncio |
| 205 | +@pytest.mark.parametrize( |
| 206 | + "azure_ai_inference_service", |
| 207 | + [ |
| 208 | + AzureAIInferenceChatCompletion.__name__, |
| 209 | + ], |
| 210 | + indirect=True, |
| 211 | +) |
| 212 | +@pytest.mark.parametrize( |
| 213 | + "override_env_param_dict", |
| 214 | + [ |
| 215 | + { |
| 216 | + "SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS": "True", |
| 217 | + "SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE": "False", |
| 218 | + }, |
| 219 | + ], |
| 220 | + indirect=True, |
| 221 | +) |
| 222 | +@patch.object(ChatCompletionsClient, "complete", new_callable=AsyncMock) |
| 223 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.uninstrument") |
| 224 | +@patch("azure.ai.inference.tracing.AIInferenceInstrumentor.instrument") |
| 225 | +async def test_azure_ai_inference_streaming_chat_completion_instrumentation_without_sensitive( |
| 226 | + mock_instrument, |
| 227 | + mock_uninstrument, |
| 228 | + mock_complete, |
| 229 | + azure_ai_inference_service, |
| 230 | + chat_history: ChatHistory, |
| 231 | + mock_azure_ai_inference_streaming_chat_completion_response, |
| 232 | + model_diagnostics_test_env, |
| 233 | +) -> None: |
| 234 | + """Test completion of AzureAIInferenceChatCompletion""" |
| 235 | + settings = AzureAIInferenceChatPromptExecutionSettings() |
| 236 | + |
| 237 | + mock_complete.return_value = mock_azure_ai_inference_streaming_chat_completion_response |
| 238 | + |
| 239 | + async for _ in azure_ai_inference_service.get_streaming_chat_message_contents( |
| 240 | + chat_history=chat_history, settings=settings |
| 241 | + ): |
| 242 | + pass |
| 243 | + |
| 244 | + mock_instrument.assert_called_once_with(enable_content_recording=False) |
| 245 | + mock_uninstrument.assert_called_once() |
0 commit comments