-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow extending and modifying SendMessage tool in Agency class
- Loading branch information
Showing
11 changed files
with
337 additions
and
125 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from agency_swarm.threads.thread import Thread | ||
from typing import ClassVar, Optional, List, Type | ||
from pydantic import Field, field_validator, model_validator | ||
from .SendMessageBase import SendMessageBase | ||
|
||
class SendMessage(SendMessageBase): | ||
"""Use this tool to facilitate direct, synchronous communication between specialized agents within your agency. When you send a message using this tool, you receive a response exclusively from the designated recipient agent. To continue the dialogue, invoke this tool again with the desired recipient agent and your follow-up message. Remember, communication here is synchronous; the recipient agent won't perform any tasks post-response. You are responsible for relaying the recipient agent's responses back to the user, as the user does not have direct access to these replies. Keep engaging with the tool for continuous interaction until the task is fully resolved. Do not send more than 1 message at a time.""" | ||
message: str = Field( | ||
..., | ||
description="Specify the task required for the recipient agent to complete. Focus on clarifying what the task entails, rather than providing exact instructions." | ||
) | ||
my_primary_instructions: str = Field( | ||
..., | ||
description=( | ||
"Please repeat your primary instructions step-by-step, including both completed " | ||
"and the following next steps that you need to perform. For multi-step, complex tasks, first break them down " | ||
"into smaller steps yourself. Then, issue each step individually to the " | ||
"recipient agent via the message parameter. Each identified step should be " | ||
"sent in a separate message. Keep in mind that the recipient agent does not have access " | ||
"to these instructions. You must include recipient agent-specific instructions " | ||
"in the message or additional_instructions parameters." | ||
) | ||
) | ||
message_files: Optional[List[str]] = Field( | ||
default=None, | ||
description="A list of file IDs to be sent as attachments to this message. Only use this if you have the file ID that starts with 'file-'.", | ||
examples=["file-1234", "file-5678"] | ||
) | ||
additional_instructions: Optional[str] = Field( | ||
default=None, | ||
description="Additional context or instructions from the conversation needed by the recipient agent to complete the task." | ||
) | ||
|
||
|
||
@model_validator(mode='after') | ||
def validate_files(self): | ||
if hasattr(self, 'message') and "file-" in self.message or (self.additional_instructions and "file-" in self.additional_instructions): | ||
if not self.message_files: | ||
raise ValueError("You must include file IDs in message_files parameter.") | ||
return self | ||
|
||
@field_validator('additional_instructions', mode='before') | ||
@classmethod | ||
def validate_additional_instructions(cls, value): | ||
if isinstance(value, list): | ||
return "\n".join(value) | ||
return value | ||
|
||
|
||
def run(self): | ||
thread: Thread = self._agents_and_threads[self._caller_agent.name][self.recipient.value] | ||
|
||
message = thread.get_completion(message=self.message, | ||
message_files=self.message_files, | ||
event_handler=self._event_handler, | ||
yield_messages=not self._event_handler, | ||
additional_instructions=self.additional_instructions, | ||
) | ||
|
||
return message or "" |
16 changes: 16 additions & 0 deletions
16
agency_swarm/tools/send_message/SendMessageAsyncThreading.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import ClassVar, Type | ||
from agency_swarm.threads.thread_async import ThreadAsync | ||
from .SendMessage import SendMessage | ||
|
||
class SendMessageAsyncThreading(SendMessage): | ||
"""Use this tool for asynchronous communication with other agents within your agency. Initiate tasks by messaging, and check status and responses later with the 'GetResponse' tool. Relay responses to the user, who instructs on status checks. Continue until task completion.""" | ||
_thread_type: ClassVar[Type[ThreadAsync]] = ThreadAsync | ||
|
||
def run(self): | ||
thread: ThreadAsync = self._agents_and_threads[self._caller_agent.name][self.recipient.value] | ||
|
||
message = thread.get_completion_async(message=self.message, | ||
message_files=self.message_files, | ||
additional_instructions=self.additional_instructions) | ||
|
||
return message or "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from agency_swarm.threads.thread import Thread | ||
from typing import ClassVar, Optional, List, Type | ||
from pydantic import Field, field_validator, model_validator | ||
from agency_swarm.tools import BaseTool | ||
from abc import ABC | ||
|
||
class SendMessageBase(BaseTool, ABC): | ||
"""Use this tool to facilitate direct, synchronous communication between specialized agents within your agency. When you send a message using this tool, you receive a response exclusively from the designated recipient agent. To continue the dialogue, invoke this tool again with the desired recipient agent and your follow-up message. Remember, communication here is synchronous; the recipient agent won't perform any tasks post-response. You are responsible for relaying the recipient agent's responses back to the user, as the user does not have direct access to these replies. Keep engaging with the tool for continuous interaction until the task is fully resolved. Do not send more than 1 message at a time.""" | ||
|
||
recipient: str = Field(..., description="Recipient agent that you want to send the message to. This field will be overriden inside the agency class.") | ||
|
||
_agents_and_threads: ClassVar = None | ||
_thread_type: ClassVar[Type[Thread]] = Thread # thread type assigned by the agency class | ||
|
||
@classmethod | ||
def __init_subclass__(cls, **kwargs): | ||
super().__init_subclass__(**kwargs) | ||
if not cls.__name__.startswith("SendMessage"): | ||
raise TypeError(f"Class name '{cls.__name__}' must start with 'SendMessage'.") |
Oops, something went wrong.