Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aexec method for Agents #1046

Open
wants to merge 7 commits into
base: mono/dev-newfeatures
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/core/swarmauri_core/ComponentBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ class ResourceTypes(Enum):
CONTROL_PANEL = "ControlPanel"
TASK_MGT_STRATEGY = "TaskMgtStrategy"
MAS = "Mas"
AGENT_API = "AgentAPI"
MAS_AGENT_API = "MasAgentAPI"


def generate_id() -> str:
return str(uuid4())


class ComponentBase(BaseModel):
name: Optional[str] = None
id: str = Field(default_factory=generate_id)
Expand Down
15 changes: 15 additions & 0 deletions pkgs/core/swarmauri_core/agent_apis/IAgentAPI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from abc import ABC, abstractmethod
from typing import Any, Coroutine, Dict


class IAgentAPI(ABC):

@abstractmethod
def invoke(self, agent_id: str, **kwargs: Dict[str, Any]) -> Any:
"""Invoke an agent synchronously."""
pass

@abstractmethod
async def ainvoke(self, agent_id: str, **kwargs: Dict[str, Any]) -> Any:
"""Invoke an agent asynchronously."""
pass
83 changes: 0 additions & 83 deletions pkgs/core/swarmauri_core/agent_apis/IAgentCommands.py

This file was deleted.

56 changes: 0 additions & 56 deletions pkgs/core/swarmauri_core/agent_apis/IAgentRouterCRUD.py

This file was deleted.

4 changes: 0 additions & 4 deletions pkgs/core/swarmauri_core/agent_apis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
from .IAgentCommands import IAgentCommands
from .IAgentRouterCRUD import IAgentRouterCRUD

__all__ = ['IAgentCommands', 'IAgentRouterCRUD']
21 changes: 21 additions & 0 deletions pkgs/core/swarmauri_core/mas_agent_apis/IMasAgentAPI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from abc import ABC, abstractmethod
from typing import Any


class IMasAgentAPI(ABC):
"""Interface for MAS-specific agent-local APIs."""

@abstractmethod
def send_message(self, message: Any) -> None:
"""Send a message to the MAS agent."""
pass

@abstractmethod
def subscribe(self, topic: str) -> None:
"""Subscribe to a topic."""
pass

@abstractmethod
def publish(self, topic: str) -> None:
"""Publish a message to a topic."""
pass
Empty file.
Empty file.
31 changes: 31 additions & 0 deletions pkgs/swarmauri/swarmauri/agent_apis/base/AgentAPIBase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Any, Dict, Literal, Optional

from pydantic import ConfigDict, Field
from swarmauri_core.ComponentBase import ComponentBase, ResourceTypes
from swarmauri_core.agent_apis.IAgentAPI import IAgentAPI
from swarmauri.service_registries.concrete.ServiceRegistry import ServiceRegistry


class AgentAPIBase(IAgentAPI, ComponentBase):

resource: Optional[str] = Field(default=ResourceTypes.AGENT_API.value, frozen=True)
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
type: Literal["AgentAPIBase"] = "AgentAPIBase"

agent_registry: ServiceRegistry

def invoke(self, agent_id: str, **kwargs: Dict[str, Any]) -> Any:
agent = self.agent_registry.get_service(agent_id)
if not agent:
raise ValueError(f"Agent with ID {agent_id} not found.")
return agent.exec(**kwargs)

async def ainvoke(self, agent_id: str, **kwargs: Dict[str, Any]) -> Any:
agent = self.agent_registry.get_service(agent_id)
if not agent:
raise ValueError(f"Agent with ID {agent_id} not found.")
if not hasattr(agent, "aexec"):
raise NotImplementedError(
f"Agent with ID {agent_id} does not support async execution."
)
return await agent.aexec(**kwargs)
Empty file.
10 changes: 10 additions & 0 deletions pkgs/swarmauri/swarmauri/agent_apis/concrete/AgentAPI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Literal
from swarmauri.agent_apis.base.AgentAPIBase import AgentAPIBase


class AgentAPI(AgentAPIBase):
"""
Concrete implementation of the AgentAPIBase.
"""

type: Literal["AgentAPI"] = "AgentAPI"
Empty file.
28 changes: 22 additions & 6 deletions pkgs/swarmauri/swarmauri/agents/base/AgentBase.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
from typing import Any, Optional, Dict, Union, Literal
from pydantic import ConfigDict, Field, field_validator
from pydantic import ConfigDict, Field
from swarmauri_core.typing import SubclassUnion
from swarmauri_core.ComponentBase import ComponentBase, ResourceTypes
from swarmauri_core.messages.IMessage import IMessage
from swarmauri_core.agents.IAgent import IAgent
from swarmauri.llms.base.LLMBase import LLMBase


class AgentBase(IAgent, ComponentBase):
llm: SubclassUnion[LLMBase]
resource: ResourceTypes = Field(default=ResourceTypes.AGENT.value)
model_config = ConfigDict(extra='forbid', arbitrary_types_allowed=True)
type: Literal['AgentBase'] = 'AgentBase'
resource: ResourceTypes = Field(default=ResourceTypes.AGENT.value)
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
type: Literal["AgentBase"] = "AgentBase"

def exec(
self,
input_str: Optional[Union[str, IMessage]] = "",
llm_kwargs: Optional[Dict] = {},
) -> Any:
raise NotImplementedError(
"The `exec` function has not been implemeneted on this class."
)

def exec(self, input_str: Optional[Union[str, IMessage]] = "", llm_kwargs: Optional[Dict] = {}) -> Any:
raise NotImplementedError('The `exec` function has not been implemeneted on this class.')
async def aexec(
self,
input_str: Optional[Union[str, IMessage]] = "",
llm_kwargs: Optional[Dict] = {},
) -> Any:
raise NotImplementedError(
"The `aexec` function has not been implemented on this class."
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from swarmauri_core.agents.IAgentConversation import IAgentConversation
from swarmauri.conversations.base.ConversationBase import ConversationBase


class AgentConversationMixin(IAgentConversation, BaseModel):
conversation: SubclassUnion[ConversationBase] # 🚧 Placeholder
model_config = ConfigDict(extra='forbid', arbitrary_types_allowed=True)
conversation: SubclassUnion[ConversationBase] # 🚧 Placeholder
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
7 changes: 3 additions & 4 deletions pkgs/swarmauri/swarmauri/agents/base/AgentRetrieveMixin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from abc import ABC
from typing import List
from pydantic import BaseModel, ConfigDict, field_validator, Field
from pydantic import BaseModel, ConfigDict, Field
from swarmauri.documents.concrete.Document import Document
from swarmauri_core.agents.IAgentRetrieve import IAgentRetrieve


class AgentRetrieveMixin(IAgentRetrieve, BaseModel):
last_retrieved: List[Document] = Field(default_factory=list)
model_config = ConfigDict(extra='forbid', arbitrary_types_allowed=True)

model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@


class AgentSystemContextMixin(IAgentSystemContext, BaseModel):
system_context: Union[SystemMessage, str]
system_context: Union[SystemMessage, str]

@field_validator('system_context', mode='before')
@field_validator("system_context", mode="before")
def set_system_context(cls, value: Union[str, SystemMessage]) -> SystemMessage:
if isinstance(value, str):
return SystemMessage(content=value)
return value
return value
4 changes: 2 additions & 2 deletions pkgs/swarmauri/swarmauri/agents/base/AgentToolMixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from swarmauri.toolkits.base.ToolkitBase import ToolkitBase
from swarmauri_core.agents.IAgentToolkit import IAgentToolkit


class AgentToolMixin(IAgentToolkit, BaseModel):
toolkit: SubclassUnion[ToolkitBase]
model_config = ConfigDict(extra='forbid', arbitrary_types_allowed=True)

model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from swarmauri_core.agents.IAgentVectorStore import IAgentVectorStore
from swarmauri.vector_stores.base.VectorStoreBase import VectorStoreBase


class AgentVectorStoreMixin(IAgentVectorStore, BaseModel):
vector_store: SubclassUnion[VectorStoreBase]
model_config = ConfigDict(extra='forbid', arbitrary_types_allowed=True)
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
28 changes: 19 additions & 9 deletions pkgs/swarmauri/swarmauri/agents/concrete/QAAgent.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
from typing import Any, Optional, Dict, Literal
from swarmauri.agents.base.AgentBase import AgentBase
from swarmauri.conversations.concrete.MaxSystemContextConversation import MaxSystemContextConversation
from swarmauri.conversations.concrete.MaxSystemContextConversation import (
MaxSystemContextConversation,
)
from swarmauri.messages.concrete.HumanMessage import HumanMessage


class QAAgent(AgentBase):
conversation: MaxSystemContextConversation = MaxSystemContextConversation(max_size=2)
type: Literal['QAAgent'] = 'QAAgent'
conversation: MaxSystemContextConversation = MaxSystemContextConversation(
max_size=2
)
type: Literal["QAAgent"] = "QAAgent"

def exec(
self, input_str: Optional[str] = "", llm_kwargs: Optional[Dict] = {}
) -> Any:

def exec(self,
input_str: Optional[str] = "",
llm_kwargs: Optional[Dict] = {}
) -> Any:

self.conversation.add_message(HumanMessage(content=input_str))
self.llm.predict(conversation=self.conversation, **llm_kwargs)


return self.conversation.get_last().content

async def aexec(
self, input_str: Optional[str] = "", llm_kwargs: Optional[Dict] = {}
) -> Any:
self.conversation.add_message(HumanMessage(content=input_str))
await self.llm.apredict(conversation=self.conversation, **llm_kwargs)
return self.conversation.get_last().content
Loading
Loading