Skip to content

Commit

Permalink
fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
brnaba-aws committed Dec 19, 2024
1 parent 90e6fd5 commit e235e26
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
4 changes: 2 additions & 2 deletions python/src/multi_agent_orchestrator/agents/anthropic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def __init__(self, options: AnthropicAgentOptions):

if options.custom_system_prompt:
self.set_system_prompt(
options.custom_system_prompt.template,
options.custom_system_prompt.variables
options.custom_system_prompt.get('template'),
options.custom_system_prompt.get('variables')
)

def is_streaming_enabled(self) -> bool:
Expand Down
87 changes: 87 additions & 0 deletions python/src/tests/agents/test_anthropic_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import pytest
from unittest.mock import patch,MagicMock
from multi_agent_orchestrator.types import ConversationMessage, ParticipantRole
from multi_agent_orchestrator.agents import AnthropicAgent, AnthropicAgentOptions
from multi_agent_orchestrator.utils import Logger

logger = Logger()

@pytest.fixture
def mock_anthropic():
with patch('multi_agent_orchestrator.agents.anthropic_agent.AnthropicAgentOptions.client') as mock:
yield mock

def test_no_api_key_init(mock_anthropic):
try:
options = AnthropicAgentOptions(
name="TestAgent",
description="A test agent",
)

_anthropic_llm_agent = AnthropicAgent(options)
assert(_anthropic_llm_agent.api_key is not None)
except Exception as e:
assert(str(e) == "Anthropic API key or Anthropic client is required")

def test_custom_system_prompt_with_variable(mock_anthropic):
options = AnthropicAgentOptions(
api_key='test-api-key',
name="TestAgent",
description="A test agent",
custom_system_prompt={
'template': """This is my new prompt with this {{variable}}""",
'variables': {'variable': 'value'}
}
)

_anthropic_llm_agent = AnthropicAgent(options)
assert(_anthropic_llm_agent.system_prompt == 'This is my new prompt with this value')

def test_custom_system_prompt_with_wrong_variable(mock_anthropic):
options = AnthropicAgentOptions(
api_key='test-api-key',
name="TestAgent",
description="A test agent",
custom_system_prompt={
'template': """This is my new prompt with this {{variable}}""",
'variables': {'variableT': 'value'}
}
)

_anthropic_llm_agent = AnthropicAgent(options)
assert(_anthropic_llm_agent.system_prompt == 'This is my new prompt with this {{variable}}')

@pytest.mark.asyncio
async def test_process_request_single_response():
# Create a mock Anthropic client
with patch('anthropic.Anthropic') as MockAnthropic:
# Setup the mock instance that will be created
mock_instance = MagicMock()
mock_instance.messages.create.return_value = MagicMock(content=[MagicMock(text="Test response")])
MockAnthropic.return_value = mock_instance

options = AnthropicAgentOptions(
name="TestAgent",
description="A test agent",
api_key='test-api-key',
model_id="claude-3-sonnet-20240229",
)

anthropic_llm_agent = AnthropicAgent(options)
anthropic_llm_agent.client = mock_instance # mocking client

response = await anthropic_llm_agent.process_request('Test prompt', 'user', 'session', [], {})

# Verify the mock was called
mock_instance.messages.create.assert_called_once_with(
model="claude-3-sonnet-20240229",
max_tokens=1000,
messages=[{"role": "user", "content": "Test prompt"}],
system=anthropic_llm_agent.system_prompt,
temperature=0.1,
top_p=0.9,
stop_sequences=[]
)
assert isinstance(response, ConversationMessage)
assert response.content[0].get('text') == "Test response"
assert response.role == ParticipantRole.ASSISTANT.value
4 changes: 2 additions & 2 deletions python/src/tests/agents/test_lex_bot_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def mock_lex_client():
def lex_bot_agent(lex_bot_options, mock_lex_client):
return LexBotAgent(lex_bot_options)

def test_lex_bot_agent_initialization(lex_bot_options):
def test_lex_bot_agent_initialization(lex_bot_options, lex_bot_agent):
agent = LexBotAgent(lex_bot_options)
assert agent.bot_id == lex_bot_options.bot_id
assert agent.bot_alias_id == lex_bot_options.bot_alias_id
assert agent.locale_id == lex_bot_options.locale_id

def test_lex_bot_agent_initialization_missing_params():
def test_lex_bot_agent_initialization_missing_params(lex_bot_agent):
with pytest.raises(ValueError):
LexBotAgent(
LexBotAgentOptions(
Expand Down

0 comments on commit e235e26

Please sign in to comment.