chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import pytest
|
||||
|
||||
import semantic_kernel
|
||||
from semantic_kernel.utils.telemetry.model_diagnostics.model_diagnostics_settings import ModelDiagnosticSettings
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def model_diagnostics_unit_test_env(monkeypatch):
|
||||
"""Fixture to set environment variables for Model Diagnostics Unit Tests."""
|
||||
env_vars = {
|
||||
"SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS": "true",
|
||||
"SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE": "true",
|
||||
}
|
||||
|
||||
for key, value in env_vars.items():
|
||||
monkeypatch.setenv(key, value)
|
||||
|
||||
# Need to reload the settings to pick up the new environment variables since the
|
||||
# settings are loaded at import time and this fixture is called after the import
|
||||
semantic_kernel.utils.telemetry.agent_diagnostics.decorators.MODEL_DIAGNOSTICS_SETTINGS = ModelDiagnosticSettings()
|
||||
@@ -0,0 +1,43 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import pytest
|
||||
|
||||
from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatCompletionAgent
|
||||
from semantic_kernel.agents.open_ai.openai_assistant_agent import OpenAIAssistantAgent
|
||||
|
||||
pytestmark = pytest.mark.parametrize(
|
||||
"decorated_method, expected_attribute",
|
||||
[
|
||||
# region ChatCompletionAgent
|
||||
pytest.param(
|
||||
ChatCompletionAgent.invoke,
|
||||
"__agent_diagnostics__",
|
||||
id="ChatCompletionAgent.invoke",
|
||||
),
|
||||
pytest.param(
|
||||
ChatCompletionAgent.invoke_stream,
|
||||
"__agent_diagnostics__",
|
||||
id="ChatCompletionAgent.invoke_stream",
|
||||
),
|
||||
# endregion
|
||||
# region OpenAIAssistantAgent
|
||||
pytest.param(
|
||||
OpenAIAssistantAgent.invoke,
|
||||
"__agent_diagnostics__",
|
||||
id="OpenAIAssistantBase.invoke",
|
||||
),
|
||||
pytest.param(
|
||||
OpenAIAssistantAgent.invoke_stream,
|
||||
"__agent_diagnostics__",
|
||||
id="OpenAIAssistantBase.invoke_stream",
|
||||
),
|
||||
# endregion
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def test_decorated(decorated_method, expected_attribute):
|
||||
"""Test that the connectors are being decorated properly with the agent diagnostics decorators."""
|
||||
assert hasattr(decorated_method, expected_attribute) and getattr(decorated_method, expected_attribute), (
|
||||
f"{decorated_method} should be decorated with the appropriate agent diagnostics decorator."
|
||||
)
|
||||
@@ -0,0 +1,65 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatCompletionAgent, ChatHistoryAgentThread
|
||||
from semantic_kernel.exceptions.kernel_exceptions import KernelServiceNotFoundError
|
||||
|
||||
|
||||
@patch("semantic_kernel.utils.telemetry.agent_diagnostics.decorators.tracer")
|
||||
async def test_chat_completion_agent_get_response(
|
||||
mock_tracer,
|
||||
chat_history,
|
||||
model_diagnostics_unit_test_env,
|
||||
):
|
||||
# Arrange
|
||||
chat_completion_agent = ChatCompletionAgent()
|
||||
thread = ChatHistoryAgentThread(chat_history=chat_history)
|
||||
# Act
|
||||
with pytest.raises(KernelServiceNotFoundError):
|
||||
await chat_completion_agent.get_response(messages="test", thread=thread)
|
||||
# Assert
|
||||
mock_tracer.start_as_current_span.assert_called_once()
|
||||
args, _ = mock_tracer.start_as_current_span.call_args
|
||||
assert args[0] == f"invoke_agent {chat_completion_agent.name}"
|
||||
|
||||
|
||||
@patch("semantic_kernel.utils.telemetry.agent_diagnostics.decorators.tracer")
|
||||
async def test_chat_completion_agent_invoke(
|
||||
mock_tracer,
|
||||
chat_history,
|
||||
model_diagnostics_unit_test_env,
|
||||
):
|
||||
# Arrange
|
||||
chat_completion_agent = ChatCompletionAgent()
|
||||
thread = ChatHistoryAgentThread(chat_history=chat_history)
|
||||
# Act
|
||||
with pytest.raises(KernelServiceNotFoundError):
|
||||
async for _ in chat_completion_agent.invoke(messages="test", thread=thread):
|
||||
pass
|
||||
# Assert
|
||||
mock_tracer.start_as_current_span.assert_called_once()
|
||||
args, _ = mock_tracer.start_as_current_span.call_args
|
||||
assert args[0] == f"invoke_agent {chat_completion_agent.name}"
|
||||
|
||||
|
||||
@patch("semantic_kernel.utils.telemetry.agent_diagnostics.decorators.tracer")
|
||||
async def test_chat_completion_agent_invoke_stream(
|
||||
mock_tracer,
|
||||
chat_history,
|
||||
model_diagnostics_unit_test_env,
|
||||
):
|
||||
# Arrange
|
||||
chat_completion_agent = ChatCompletionAgent()
|
||||
thread = ChatHistoryAgentThread(chat_history=chat_history)
|
||||
# Act
|
||||
with pytest.raises(KernelServiceNotFoundError):
|
||||
async for _ in chat_completion_agent.invoke_stream(messages="test", thread=thread):
|
||||
pass
|
||||
# Assert
|
||||
mock_tracer.start_as_current_span.assert_called_once()
|
||||
args, _ = mock_tracer.start_as_current_span.call_args
|
||||
assert args[0] == f"invoke_agent {chat_completion_agent.name}"
|
||||
@@ -0,0 +1,128 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
from openai.types.beta.assistant import Assistant
|
||||
|
||||
from semantic_kernel.agents.open_ai.openai_assistant_agent import AssistantAgentThread, OpenAIAssistantAgent
|
||||
from semantic_kernel.contents.chat_message_content import ChatMessageContent
|
||||
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
|
||||
from semantic_kernel.contents.utils.author_role import AuthorRole
|
||||
|
||||
|
||||
@patch("semantic_kernel.utils.telemetry.agent_diagnostics.decorators.tracer")
|
||||
async def test_open_ai_assistant_agent_get_response(
|
||||
mock_tracer,
|
||||
chat_history,
|
||||
openai_unit_test_env,
|
||||
model_diagnostics_unit_test_env,
|
||||
):
|
||||
# Arrange
|
||||
client = AsyncMock(spec=AsyncOpenAI)
|
||||
definition = AsyncMock(spec=Assistant)
|
||||
definition.name = "agentName"
|
||||
definition.description = "agentDescription"
|
||||
definition.id = "agentId"
|
||||
definition.instructions = "agentInstructions"
|
||||
definition.tools = []
|
||||
definition.model = "agentModel"
|
||||
definition.temperature = 1.0
|
||||
definition.top_p = 1.0
|
||||
definition.metadata = {}
|
||||
openai_assistant_agent = OpenAIAssistantAgent(client=client, definition=definition)
|
||||
|
||||
thread = AsyncMock(spec=AssistantAgentThread)
|
||||
|
||||
async def fake_invoke(*args, **kwargs):
|
||||
yield True, ChatMessageContent(role=AuthorRole.ASSISTANT, content="content")
|
||||
|
||||
# Act
|
||||
with patch(
|
||||
"semantic_kernel.agents.open_ai.assistant_thread_actions.AssistantThreadActions.invoke",
|
||||
side_effect=fake_invoke,
|
||||
):
|
||||
await openai_assistant_agent.get_response(messages="message", thread=thread)
|
||||
# Assert
|
||||
mock_tracer.start_as_current_span.assert_called_once()
|
||||
args, _ = mock_tracer.start_as_current_span.call_args
|
||||
assert args[0] == f"invoke_agent {openai_assistant_agent.name}"
|
||||
|
||||
|
||||
@patch("semantic_kernel.utils.telemetry.agent_diagnostics.decorators.tracer")
|
||||
async def test_open_ai_assistant_agent_invoke(
|
||||
mock_tracer,
|
||||
chat_history,
|
||||
openai_unit_test_env,
|
||||
model_diagnostics_unit_test_env,
|
||||
):
|
||||
# Arrange
|
||||
client = AsyncMock(spec=AsyncOpenAI)
|
||||
definition = AsyncMock(spec=Assistant)
|
||||
definition.name = "agentName"
|
||||
definition.description = "agentDescription"
|
||||
definition.id = "agentId"
|
||||
definition.instructions = "agentInstructions"
|
||||
definition.tools = []
|
||||
definition.model = "agentModel"
|
||||
definition.temperature = 1.0
|
||||
definition.top_p = 1.0
|
||||
definition.metadata = {}
|
||||
openai_assistant_agent = OpenAIAssistantAgent(client=client, definition=definition)
|
||||
|
||||
thread = AsyncMock(spec=AssistantAgentThread)
|
||||
|
||||
async def fake_invoke(*args, **kwargs):
|
||||
yield True, ChatMessageContent(role=AuthorRole.ASSISTANT, content="content")
|
||||
|
||||
# Act
|
||||
with patch(
|
||||
"semantic_kernel.agents.open_ai.assistant_thread_actions.AssistantThreadActions.invoke",
|
||||
side_effect=fake_invoke,
|
||||
):
|
||||
async for item in openai_assistant_agent.invoke(messages="message", thread=thread):
|
||||
pass
|
||||
# Assert
|
||||
mock_tracer.start_as_current_span.assert_called_once()
|
||||
args, _ = mock_tracer.start_as_current_span.call_args
|
||||
assert args[0] == f"invoke_agent {openai_assistant_agent.name}"
|
||||
|
||||
|
||||
@patch("semantic_kernel.utils.telemetry.agent_diagnostics.decorators.tracer")
|
||||
async def test_open_ai_assistant_agent_invoke_stream(
|
||||
mock_tracer,
|
||||
chat_history,
|
||||
openai_unit_test_env,
|
||||
model_diagnostics_unit_test_env,
|
||||
):
|
||||
# Arrange
|
||||
client = AsyncMock(spec=AsyncOpenAI)
|
||||
definition = AsyncMock(spec=Assistant)
|
||||
definition.name = "agentName"
|
||||
definition.description = "agentDescription"
|
||||
definition.id = "agentId"
|
||||
definition.instructions = "agentInstructions"
|
||||
definition.tools = []
|
||||
definition.model = "agentModel"
|
||||
definition.temperature = 1.0
|
||||
definition.top_p = 1.0
|
||||
definition.metadata = {}
|
||||
openai_assistant_agent = OpenAIAssistantAgent(client=client, definition=definition)
|
||||
|
||||
thread = AsyncMock(spec=AssistantAgentThread)
|
||||
|
||||
async def fake_invoke(*args, **kwargs):
|
||||
yield StreamingChatMessageContent(role=AuthorRole.ASSISTANT, choice_index=0, content="content")
|
||||
|
||||
# Act
|
||||
with patch(
|
||||
"semantic_kernel.agents.open_ai.assistant_thread_actions.AssistantThreadActions.invoke_stream",
|
||||
side_effect=fake_invoke,
|
||||
):
|
||||
async for item in openai_assistant_agent.invoke_stream(messages="message", thread=thread):
|
||||
pass
|
||||
# Assert
|
||||
mock_tracer.start_as_current_span.assert_called_once()
|
||||
args, _ = mock_tracer.start_as_current_span.call_args
|
||||
assert args[0] == f"invoke_agent {openai_assistant_agent.name}"
|
||||
Reference in New Issue
Block a user