Skip to content

Commit

Permalink
Merge branch 'main' into try_multiversion_build
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgerrits authored Oct 22, 2024
2 parents 8c6c6be + 5391804 commit b4df095
Show file tree
Hide file tree
Showing 31 changed files with 278 additions and 216 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/issue-user-responded.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Remove awaiting-op-response label if op responded
on:
issue_comment:
types: [created]
jobs:
label_issues:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- run: gh issue edit "$NUMBER" --remove-label "$LABELS"
if: ${{ github.event.comment.user.login == github.event.issue.user.login && contains(github.event.issue.labels.*.name, 'awaiting-op-response') }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number }}
LABELS: awaiting-op-response
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import importlib.metadata

TRACE_LOGGER_NAME = "autogen_agentchat"
EVENT_LOGGER_NAME = "autogen_agentchat.events"

__version__ = importlib.metadata.version("autogen_agentchat")
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
from ._base_chat_agent import (
BaseChatAgent,
BaseMessage,
BaseToolUseChatAgent,
ChatMessage,
MultiModalMessage,
StopMessage,
TextMessage,
ToolCallMessage,
ToolCallResultMessage,
)
from ._code_executor_agent import CodeExecutorAgent
from ._coding_assistant_agent import CodingAssistantAgent
from ._tool_use_assistant_agent import ToolUseAssistantAgent

__all__ = [
"BaseChatAgent",
"BaseMessage",
"BaseToolUseChatAgent",
"ChatMessage",
"CodeExecutorAgent",
"CodingAssistantAgent",
"MultiModalMessage",
"StopMessage",
"TextMessage",
"ToolCallMessage",
"ToolCallResultMessage",
"ToolUseAssistantAgent",
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from autogen_core.base import CancellationToken
from autogen_core.components.code_executor import CodeBlock, CodeExecutor, extract_markdown_code_blocks

from ._base_chat_agent import BaseChatAgent, ChatMessage, TextMessage
from ..base import BaseChatAgent
from ..messages import ChatMessage, TextMessage


class CodeExecutorAgent(BaseChatAgent):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
UserMessage,
)

from ._base_chat_agent import BaseChatAgent, ChatMessage, MultiModalMessage, StopMessage, TextMessage
from ..base import BaseChatAgent
from ..messages import ChatMessage, MultiModalMessage, StopMessage, TextMessage


class CodingAssistantAgent(BaseChatAgent):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
)
from autogen_core.components.tools import Tool

from ._base_chat_agent import (
BaseToolUseChatAgent,
from ..base import BaseToolUseChatAgent
from ..messages import (
ChatMessage,
MultiModalMessage,
StopMessage,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from ._base_chat_agent import BaseChatAgent, BaseToolUseChatAgent
from ._base_task import TaskResult, TaskRunner
from ._base_team import Team
from ._base_termination import TerminatedException, TerminationCondition

__all__ = [
"BaseChatAgent",
"BaseToolUseChatAgent",
"Team",
"TerminatedException",
"TerminationCondition",
"TaskResult",
"TaskRunner",
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,13 @@
from typing import List, Sequence

from autogen_core.base import CancellationToken
from autogen_core.components import FunctionCall, Image
from autogen_core.components.models import FunctionExecutionResult
from autogen_core.components.tools import Tool
from pydantic import BaseModel

from ..messages import ChatMessage
from ._base_task import TaskResult, TaskRunner

class BaseMessage(BaseModel):
"""A base message."""

source: str
"""The name of the agent that sent this message."""


class TextMessage(BaseMessage):
"""A text message."""

content: str
"""The content of the message."""


class MultiModalMessage(BaseMessage):
"""A multimodal message."""

content: List[str | Image]
"""The content of the message."""


class ToolCallMessage(BaseMessage):
"""A message containing a list of function calls."""

content: List[FunctionCall]
"""The list of function calls."""


class ToolCallResultMessage(BaseMessage):
"""A message containing the results of function calls."""

content: List[FunctionExecutionResult]
"""The list of function execution results."""


class StopMessage(BaseMessage):
"""A message requesting stop of a conversation."""

content: str
"""The content for the stop message."""


ChatMessage = TextMessage | MultiModalMessage | StopMessage | ToolCallMessage | ToolCallResultMessage
"""A message used by agents in a team."""


class BaseChatAgent(ABC):
class BaseChatAgent(TaskRunner, ABC):
"""Base class for a chat agent that can participant in a team."""

def __init__(self, name: str, description: str) -> None:
Expand All @@ -81,6 +35,12 @@ async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token:
"""Handle incoming messages and return a response message."""
...

async def run(
self, task: str, *, source: str = "user", cancellation_token: CancellationToken | None = None
) -> TaskResult:
# TODO: Implement this method.
raise NotImplementedError


class BaseToolUseChatAgent(BaseChatAgent):
"""Base class for a chat agent that can use tools.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from dataclasses import dataclass
from typing import Protocol, Sequence

from ..messages import ChatMessage


@dataclass
class TaskResult:
"""Result of running a task."""

messages: Sequence[ChatMessage]
"""Messages produced by the task."""


class TaskRunner(Protocol):
"""A task runner."""

async def run(self, task: str) -> TaskResult:
"""Run the task."""
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Protocol

from ._base_task import TaskResult, TaskRunner
from ._base_termination import TerminationCondition


class Team(TaskRunner, Protocol):
async def run(self, task: str, *, termination_condition: TerminationCondition | None = None) -> TaskResult:
"""Run the team on a given task until the termination condition is met."""
...
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from abc import ABC, abstractmethod
from typing import List, Sequence

from ..agents import ChatMessage, MultiModalMessage, StopMessage, TextMessage
from ..messages import ChatMessage, StopMessage


class TerminatedException(BaseException): ...
Expand Down Expand Up @@ -127,89 +127,3 @@ async def __call__(self, messages: Sequence[ChatMessage]) -> StopMessage | None:
async def reset(self) -> None:
for condition in self._conditions:
await condition.reset()


class StopMessageTermination(TerminationCondition):
"""Terminate the conversation if a StopMessage is received."""

def __init__(self) -> None:
self._terminated = False

@property
def terminated(self) -> bool:
return self._terminated

async def __call__(self, messages: Sequence[ChatMessage]) -> StopMessage | None:
if self._terminated:
raise TerminatedException("Termination condition has already been reached")
for message in messages:
if isinstance(message, StopMessage):
self._terminated = True
return StopMessage(content="Stop message received", source="StopMessageTermination")
return None

async def reset(self) -> None:
self._terminated = False


class MaxMessageTermination(TerminationCondition):
"""Terminate the conversation after a maximum number of messages have been exchanged.
Args:
max_messages: The maximum number of messages allowed in the conversation.
"""

def __init__(self, max_messages: int) -> None:
self._max_messages = max_messages
self._message_count = 0

@property
def terminated(self) -> bool:
return self._message_count >= self._max_messages

async def __call__(self, messages: Sequence[ChatMessage]) -> StopMessage | None:
if self.terminated:
raise TerminatedException("Termination condition has already been reached")
self._message_count += len(messages)
if self._message_count >= self._max_messages:
return StopMessage(
content=f"Maximal number of messages {self._max_messages} reached, current message count: {self._message_count}",
source="MaxMessageTermination",
)
return None

async def reset(self) -> None:
self._message_count = 0


class TextMentionTermination(TerminationCondition):
"""Terminate the conversation if a specific text is mentioned.
Args:
text: The text to look for in the messages.
"""

def __init__(self, text: str) -> None:
self._text = text
self._terminated = False

@property
def terminated(self) -> bool:
return self._terminated

async def __call__(self, messages: Sequence[ChatMessage]) -> StopMessage | None:
if self._terminated:
raise TerminatedException("Termination condition has already been reached")
for message in messages:
if isinstance(message, TextMessage | StopMessage) and self._text in message.content:
self._terminated = True
return StopMessage(content=f"Text '{self._text}' mentioned", source="TextMentionTermination")
elif isinstance(message, MultiModalMessage):
for item in message.content:
if isinstance(item, str) and self._text in item:
self._terminated = True
return StopMessage(content=f"Text '{self._text}' mentioned", source="TextMentionTermination")
return None

async def reset(self) -> None:
self._terminated = False
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from datetime import datetime

from ..agents import ChatMessage, StopMessage, TextMessage
from ..messages import ChatMessage, StopMessage, TextMessage
from ..teams._events import (
ContentPublishEvent,
SelectSpeakerEvent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from typing import List

from autogen_core.components import FunctionCall, Image
from autogen_core.components.models import FunctionExecutionResult
from pydantic import BaseModel


class BaseMessage(BaseModel):
"""A base message."""

source: str
"""The name of the agent that sent this message."""


class TextMessage(BaseMessage):
"""A text message."""

content: str
"""The content of the message."""


class MultiModalMessage(BaseMessage):
"""A multimodal message."""

content: List[str | Image]
"""The content of the message."""


class ToolCallMessage(BaseMessage):
"""A message containing a list of function calls."""

content: List[FunctionCall]
"""The list of function calls."""


class ToolCallResultMessage(BaseMessage):
"""A message containing the results of function calls."""

content: List[FunctionExecutionResult]
"""The list of function execution results."""


class StopMessage(BaseMessage):
"""A message requesting stop of a conversation."""

content: str
"""The content for the stop message."""


ChatMessage = TextMessage | MultiModalMessage | StopMessage | ToolCallMessage | ToolCallResultMessage
"""A message used by agents in a team."""


__all__ = [
"BaseMessage",
"TextMessage",
"MultiModalMessage",
"ToolCallMessage",
"ToolCallResultMessage",
"StopMessage",
"ChatMessage",
]
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from ._group_chat._round_robin_group_chat import RoundRobinGroupChat
from ._group_chat._selector_group_chat import SelectorGroupChat
from ._termination import MaxMessageTermination, StopMessageTermination, TerminationCondition, TextMentionTermination
from ._terminations import MaxMessageTermination, StopMessageTermination, TextMentionTermination

__all__ = [
"TerminationCondition",
"MaxMessageTermination",
"TextMentionTermination",
"StopMessageTermination",
Expand Down
Loading

0 comments on commit b4df095

Please sign in to comment.