Skip to content

Commit

Permalink
Merge pull request #165 from RanaElwaseef21/feature/fixing-agent-name
Browse files Browse the repository at this point in the history
fixing agent key to accept numbers - issue #80
  • Loading branch information
cornelcroi authored Dec 26, 2024
2 parents 8811d7c + d714989 commit b0aa587
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
20 changes: 15 additions & 5 deletions python/src/multi_agent_orchestrator/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from multi_agent_orchestrator.types import ConversationMessage
from multi_agent_orchestrator.utils import Logger


@dataclass
class AgentProcessingResult:
user_input: str
Expand All @@ -13,6 +14,7 @@ class AgentProcessingResult:
session_id: str
additional_params: Dict[str, any] = field(default_factory=dict)


@dataclass
class AgentResponse:
metadata: AgentProcessingResult
Expand All @@ -25,6 +27,7 @@ def on_llm_new_token(self, token: str) -> None:
# Default implementation
pass


@dataclass
class AgentOptions:
name: str
Expand All @@ -44,18 +47,25 @@ def __init__(self, options: AgentOptions):
self.id = self.generate_key_from_name(options.name)
self.description = options.description
self.save_chat = options.save_chat
self.callbacks = options.callbacks if options.callbacks is not None else AgentCallbacks()
self.LOG_AGENT_DEBUG_TRACE = options.LOG_AGENT_DEBUG_TRACE if options.LOG_AGENT_DEBUG_TRACE is not None else False
self.callbacks = (
options.callbacks if options.callbacks is not None else AgentCallbacks()
)
self.LOG_AGENT_DEBUG_TRACE = (
options.LOG_AGENT_DEBUG_TRACE
if options.LOG_AGENT_DEBUG_TRACE is not None
else False
)

def is_streaming_enabled(self) -> bool:
return False

@staticmethod
def generate_key_from_name(name: str) -> str:
import re

# Remove special characters and replace spaces with hyphens
key = re.sub(r'[^a-zA-Z\s-]', '', name)
key = re.sub(r'\s+', '-', key)
key = re.sub(r"[^a-zA-Z0-9\s-]", "", name)
key = re.sub(r"\s+", "-", key)
return key.lower()

@abstractmethod
Expand All @@ -65,7 +75,7 @@ async def process_request(
user_id: str,
session_id: str,
chat_history: List[ConversationMessage],
additional_params: Optional[Dict[str, str]] = None
additional_params: Optional[Dict[str, str]] = None,
) -> Union[ConversationMessage, AsyncIterable[any]]:
pass

Expand Down
36 changes: 28 additions & 8 deletions python/src/tests/agents/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
from typing import Dict, List
from unittest.mock import Mock
from multi_agent_orchestrator.types import ConversationMessage
from multi_agent_orchestrator.agents import AgentProcessingResult, AgentResponse, AgentCallbacks, AgentOptions, Agent
from multi_agent_orchestrator.agents import (
AgentProcessingResult,
AgentResponse,
AgentCallbacks,
AgentOptions,
Agent,
)


class TestAgent:
@pytest.fixture
Expand All @@ -13,7 +20,7 @@ def mock_agent_options(self):
model_id="test-model",
region="us-west-2",
save_chat=True,
callbacks=None
callbacks=None,
)

@pytest.fixture
Expand All @@ -25,7 +32,7 @@ async def process_request(
user_id: str,
session_id: str,
chat_history: List[ConversationMessage],
additional_params: Dict[str, str] = None
additional_params: Dict[str, str] = None,
):
return ConversationMessage(role="assistant", content="Mock response")

Expand All @@ -37,7 +44,7 @@ def test_agent_processing_result(self):
agent_id="test-agent",
agent_name="Test Agent",
user_id="user123",
session_id="session456"
session_id="session456",
)
assert result.user_input == "Hello"
assert result.agent_id == "test-agent"
Expand All @@ -53,9 +60,11 @@ def test_agent_response(self):
agent_id="test-agent",
agent_name="Test Agent",
user_id="user123",
session_id="session456"
session_id="session456",
)
response = AgentResponse(
metadata=metadata, output="Hello, user!", streaming=False
)
response = AgentResponse(metadata=metadata, output="Hello, user!", streaming=False)
assert response.metadata == metadata
assert response.output == "Hello, user!"
assert response.streaming is False
Expand All @@ -82,6 +91,17 @@ def test_agent_initialization(self, mock_agent, mock_agent_options):
def test_generate_key_from_name(self):
assert Agent.generate_key_from_name("Test Agent") == "test-agent"
assert Agent.generate_key_from_name("Complex Name! @#$%") == "complex-name-"
assert Agent.generate_key_from_name("Agent123") == "agent123"
assert Agent.generate_key_from_name("Agent2-test") == "agent2-test"
assert Agent.generate_key_from_name("Agent4-test") == "agent4-test"
assert Agent.generate_key_from_name("Agent 123!") == "agent-123"
assert Agent.generate_key_from_name("Agent@#$%^&*()") == "agent"
assert Agent.generate_key_from_name("Trailing Space ") == "trailing-space-"
assert (
Agent.generate_key_from_name("123 Mixed Content 456!")
== "123-mixed-content-456"
)
assert Agent.generate_key_from_name("Mix@of123Symbols$") == "mixof123symbols"

@pytest.mark.asyncio
async def test_process_request(self, mock_agent):
Expand All @@ -90,8 +110,8 @@ async def test_process_request(self, mock_agent):
input_text="Hi",
user_id="user123",
session_id="session456",
chat_history=chat_history
chat_history=chat_history,
)
assert isinstance(result, ConversationMessage)
assert result.role == "assistant"
assert result.content == "Mock response"
assert result.content == "Mock response"
2 changes: 1 addition & 1 deletion typescript/src/agents/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export abstract class Agent {
private generateKeyFromName(name: string): string {
// Remove special characters and replace spaces with hyphens
const key = name
.replace(/[^a-zA-Z\s-]/g, "")
.replace(/[^a-zA-Z0-9\s-]/g, "")
.replace(/\s+/g, "-")
.toLowerCase();
return key;
Expand Down
36 changes: 36 additions & 0 deletions typescript/tests/agents/Agents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,42 @@ describe('Agents', () => {
const key = agent['generateKeyFromName']('UPPERCASE');
expect(key).toBe('uppercase');
});

it('should convert the key to lowercase and keep the numbers', () => {
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
const key = agent['generateKeyFromName']('Agent123');
expect(key).toBe('agent123');
});

it('should convert the key to lowercase', () => {
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
const key = agent['generateKeyFromName']('Agent2-test');
expect(key).toBe('agent2-test');
});

it('should handle multiple spaces', () => {
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
const key = agent['generateKeyFromName']('Agent 123!');
expect(key).toBe('agent-123');
});

it('should remove special characters', () => {
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
const key = agent['generateKeyFromName']('Agent@#$%^&*()');
expect(key).toBe('agent');
});

it('should handle mixed content with numbers', () => {
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
const key = agent['generateKeyFromName']('123 Mixed Content 456!');
expect(key).toBe('123-mixed-content-456');
});

it('should remove special characters from mixed symbols', () => {
const agent = new MockAgent({ name: 'Test Agent', description: 'Test description' });
const key = agent['generateKeyFromName']('Mix@of123Symbols$');
expect(key).toBe('mixof123symbols');
});
});

describe('constructor', () => {
Expand Down

0 comments on commit b0aa587

Please sign in to comment.