parent
d23884d5d3
commit
f3e27dbdf1
@ -0,0 +1,84 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from swarms import Agent, OpenAIChat
|
||||||
|
from swarms.agents.multion_agent import MultiOnAgent
|
||||||
|
from swarms.memory.chroma_db import ChromaDB
|
||||||
|
from swarms.tools.tool import tool
|
||||||
|
from swarms.utils.code_interpreter import SubprocessCodeInterpreter
|
||||||
|
|
||||||
|
# Load the environment variables
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
|
||||||
|
# Memory
|
||||||
|
chroma_db = ChromaDB()
|
||||||
|
|
||||||
|
|
||||||
|
# MultiOntool
|
||||||
|
@tool
|
||||||
|
def multion_tool(
|
||||||
|
task: str,
|
||||||
|
api_key: str = os.environ.get("MULTION_API_KEY"),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Executes a task using the MultiOnAgent.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task (str): The task to be executed.
|
||||||
|
api_key (str, optional): The API key for the MultiOnAgent. Defaults to the value of the MULTION_API_KEY environment variable.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The result of the task execution.
|
||||||
|
"""
|
||||||
|
multion = MultiOnAgent(multion_api_key=api_key)
|
||||||
|
return multion(task)
|
||||||
|
|
||||||
|
|
||||||
|
# Execute the interpreter tool
|
||||||
|
@tool
|
||||||
|
def execute_interpreter_tool(code: str,):
|
||||||
|
"""
|
||||||
|
Executes a single command using the interpreter.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task (str): The command to be executed.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None
|
||||||
|
"""
|
||||||
|
out = SubprocessCodeInterpreter(debug_mode=True)
|
||||||
|
out = out.run(code)
|
||||||
|
return code
|
||||||
|
|
||||||
|
|
||||||
|
# Get the API key from the environment
|
||||||
|
api_key = os.environ.get("OPENAI_API_KEY")
|
||||||
|
|
||||||
|
# Initialize the language model
|
||||||
|
llm = OpenAIChat(
|
||||||
|
temperature=0.5,
|
||||||
|
openai_api_key=api_key,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize the workflow
|
||||||
|
agent = Agent(
|
||||||
|
agent_name="Research Agent",
|
||||||
|
agent_description="An agent that performs research tasks.",
|
||||||
|
system_prompt="Perform a research task.",
|
||||||
|
llm=llm,
|
||||||
|
max_loops=1,
|
||||||
|
dashboard=True,
|
||||||
|
# tools=[multion_tool, execute_interpreter_tool],
|
||||||
|
verbose=True,
|
||||||
|
long_term_memory=chroma_db,
|
||||||
|
stopping_token="done",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Run the workflow on a task
|
||||||
|
out = agent.run(
|
||||||
|
"Generate a 10,000 word blog on health and wellness, and say done"
|
||||||
|
" when you are done"
|
||||||
|
)
|
||||||
|
print(out)
|
@ -0,0 +1,19 @@
|
|||||||
|
from swarms.structs.agent import Agent
|
||||||
|
from swarms.structs.message_pool import MessagePool
|
||||||
|
from swarms import OpenAIChat
|
||||||
|
|
||||||
|
agent1 = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
agent2 = Agent(llm=OpenAIChat(), agent_name="agent2")
|
||||||
|
agent3 = Agent(llm=OpenAIChat(), agent_name="agent3")
|
||||||
|
|
||||||
|
moderator = Agent(agent_name="moderator")
|
||||||
|
agents = [agent1, agent2, agent3]
|
||||||
|
message_pool = MessagePool(
|
||||||
|
agents=agents, moderator=moderator, turns=5
|
||||||
|
)
|
||||||
|
message_pool.add(agent=agent1, content="Hello, agent2!", turn=1)
|
||||||
|
message_pool.add(agent=agent2, content="Hello, agent1!", turn=1)
|
||||||
|
message_pool.add(agent=agent3, content="Hello, agent1!", turn=1)
|
||||||
|
message_pool.get_all_messages()
|
||||||
|
message_pool.get_visible_messages(agent=agent1, turn=1)
|
||||||
|
message_pool.get_visible_messages(agent=agent2, turn=1)
|
@ -0,0 +1,69 @@
|
|||||||
|
import os
|
||||||
|
import multion
|
||||||
|
|
||||||
|
from swarms.models.base_llm import AbstractLLM
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
# Load environment variables
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
# Muliton key
|
||||||
|
MULTION_API_KEY = os.getenv("MULTION_API_KEY")
|
||||||
|
|
||||||
|
|
||||||
|
class MultiOnAgent(AbstractLLM):
|
||||||
|
"""
|
||||||
|
Represents a multi-on agent that performs browsing tasks.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
max_steps (int): The maximum number of steps to perform during browsing.
|
||||||
|
starting_url (str): The starting URL for browsing.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
max_steps (int): The maximum number of steps to perform during browsing.
|
||||||
|
starting_url (str): The starting URL for browsing.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
multion_api_key: str = MULTION_API_KEY,
|
||||||
|
max_steps: int = 4,
|
||||||
|
starting_url: str = "https://www.google.com",
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.multion_api_key = multion_api_key
|
||||||
|
self.max_steps = max_steps
|
||||||
|
self.starting_url = starting_url
|
||||||
|
|
||||||
|
self.multion = multion.login(
|
||||||
|
use_api=True,
|
||||||
|
multion_api_key=str(multion_api_key),
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
def run(self, task: str, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Runs a browsing task.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task (str): The task to perform during browsing.
|
||||||
|
*args: Additional positional arguments.
|
||||||
|
**kwargs: Additional keyword arguments.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: The response from the browsing task.
|
||||||
|
"""
|
||||||
|
response = self.multion.browse(
|
||||||
|
{
|
||||||
|
"cmd": task,
|
||||||
|
"url": self.starting_url,
|
||||||
|
"maxSteps": self.max_steps,
|
||||||
|
},
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
return response.result, response.status, response.lastUrl
|
@ -0,0 +1,214 @@
|
|||||||
|
import hashlib
|
||||||
|
from time import time_ns
|
||||||
|
from typing import Callable, List, Optional, Sequence, Union
|
||||||
|
|
||||||
|
from swarms.structs.agent import Agent
|
||||||
|
from swarms.structs.base_swarm import BaseSwarm
|
||||||
|
from swarms.utils.loguru_logger import logger
|
||||||
|
|
||||||
|
|
||||||
|
def _hash(input: str):
|
||||||
|
"""
|
||||||
|
Hashes the input string using SHA256 algorithm.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
input (str): The string to be hashed.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The hexadecimal representation of the hash value.
|
||||||
|
"""
|
||||||
|
hex_dig = hashlib.sha256(input.encode("utf-8")).hexdigest()
|
||||||
|
return hex_dig
|
||||||
|
|
||||||
|
|
||||||
|
def msg_hash(
|
||||||
|
agent: Agent, content: str, turn: int, msg_type: str = "text"
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Generate a hash value for a message.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
agent (Agent): The agent sending the message.
|
||||||
|
content (str): The content of the message.
|
||||||
|
turn (int): The turn number of the message.
|
||||||
|
msg_type (str, optional): The type of the message. Defaults to "text".
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int: The hash value of the message.
|
||||||
|
"""
|
||||||
|
time = time_ns()
|
||||||
|
return _hash(
|
||||||
|
f"agent: {agent.agent_name}\ncontent: {content}\ntimestamp:"
|
||||||
|
f" {str(time)}\nturn: {turn}\nmsg_type: {msg_type}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class MessagePool(BaseSwarm):
|
||||||
|
"""
|
||||||
|
A class representing a message pool for agents in a swarm.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
agents (Optional[Sequence[Agent]]): The list of agents in the swarm.
|
||||||
|
moderator (Optional[Agent]): The moderator agent.
|
||||||
|
turns (Optional[int]): The number of turns.
|
||||||
|
routing_function (Optional[Callable]): The routing function for message distribution.
|
||||||
|
show_names (Optional[bool]): Flag indicating whether to show agent names.
|
||||||
|
messages (List[Dict]): The list of messages in the pool.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
>>> from swarms.structs.agent import Agent
|
||||||
|
>>> from swarms.structs.message_pool import MessagePool
|
||||||
|
>>> agent1 = Agent(agent_name="agent1")
|
||||||
|
>>> agent2 = Agent(agent_name="agent2")
|
||||||
|
>>> agent3 = Agent(agent_name="agent3")
|
||||||
|
>>> moderator = Agent(agent_name="moderator")
|
||||||
|
>>> agents = [agent1, agent2, agent3]
|
||||||
|
>>> message_pool = MessagePool(agents=agents, moderator=moderator, turns=5)
|
||||||
|
>>> message_pool.add(agent=agent1, content="Hello, agent2!", turn=1)
|
||||||
|
>>> message_pool.add(agent=agent2, content="Hello, agent1!", turn=1)
|
||||||
|
>>> message_pool.add(agent=agent3, content="Hello, agent1!", turn=1)
|
||||||
|
>>> message_pool.get_all_messages()
|
||||||
|
[{'agent': Agent(agent_name='agent1'), 'content': 'Hello, agent2!', 'turn': 1, 'visible_to': 'all', 'logged': True}, {'agent': Agent(agent_name='agent2'), 'content': 'Hello, agent1!', 'turn': 1, 'visible_to': 'all', 'logged': True}, {'agent': Agent(agent_name='agent3'), 'content': 'Hello, agent1!', 'turn': 1, 'visible_to': 'all', 'logged': True}]
|
||||||
|
>>> message_pool.get_visible_messages(agent=agent1, turn=1)
|
||||||
|
[{'agent': Agent(agent_name='agent1'), 'content': 'Hello, agent2!', 'turn': 1, 'visible_to': 'all', 'logged': True}, {'agent': Agent(agent_name='agent2'), 'content': 'Hello, agent1!', 'turn': 1, 'visible_to': 'all', 'logged': True}, {'agent': Agent(agent_name='agent3'), 'content': 'Hello, agent1!', 'turn': 1, 'visible_to': 'all', 'logged': True}]
|
||||||
|
>>> message_pool.get_visible_messages(agent=agent2, turn=1)
|
||||||
|
[{'agent': Agent(agent_name='agent1'), 'content': 'Hello, agent2!', 'turn': 1, 'visible_to': 'all', 'logged': True}, {'agent': Agent(agent_name='agent2'), 'content': 'Hello, agent1!', 'turn': 1, 'visible_to': 'all', 'logged': True}, {'agent': Agent(agent_name='agent3'), 'content': 'Hello, agent1!', 'turn': 1, 'visible_to': 'all', 'logged': True}]
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
agents: Optional[Sequence[Agent]] = None,
|
||||||
|
moderator: Optional[Agent] = None,
|
||||||
|
turns: Optional[int] = 5,
|
||||||
|
routing_function: Optional[Callable] = None,
|
||||||
|
show_names: Optional[bool] = False,
|
||||||
|
autosave: Optional[bool] = False,
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.agent = agents
|
||||||
|
self.moderator = moderator
|
||||||
|
self.turns = turns
|
||||||
|
self.routing_function = routing_function
|
||||||
|
self.show_names = show_names
|
||||||
|
self.autosave = autosave
|
||||||
|
|
||||||
|
self.messages = []
|
||||||
|
|
||||||
|
logger.info("MessagePool initialized")
|
||||||
|
logger.info(f"Number of agents: {len(agents)}")
|
||||||
|
logger.info(
|
||||||
|
f"Agents: {[agent.agent_name for agent in agents]}"
|
||||||
|
)
|
||||||
|
logger.info(f"moderator: {moderator.agent_name} is available")
|
||||||
|
logger.info(f"Number of turns: {turns}")
|
||||||
|
|
||||||
|
def add(
|
||||||
|
self,
|
||||||
|
agent: Agent,
|
||||||
|
content: str,
|
||||||
|
turn: int,
|
||||||
|
visible_to: Union[str, List[str]] = "all",
|
||||||
|
logged: bool = True,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Add a message to the pool.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
agent (Agent): The agent sending the message.
|
||||||
|
content (str): The content of the message.
|
||||||
|
turn (int): The turn number.
|
||||||
|
visible_to (Union[str, List[str]], optional): The agents who can see the message. Defaults to "all".
|
||||||
|
logged (bool, optional): Flag indicating whether the message should be logged. Defaults to True.
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.messages.append(
|
||||||
|
{
|
||||||
|
"agent": agent,
|
||||||
|
"content": content,
|
||||||
|
"turn": turn,
|
||||||
|
"visible_to": visible_to,
|
||||||
|
"logged": logged,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
logger.info(f"Message added: {content}")
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
"""
|
||||||
|
Reset the message pool.
|
||||||
|
"""
|
||||||
|
self.messages = []
|
||||||
|
logger.info("MessagePool reset")
|
||||||
|
|
||||||
|
def last_turn(self):
|
||||||
|
"""
|
||||||
|
Get the last turn number.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int: The last turn number.
|
||||||
|
"""
|
||||||
|
if len(self.messages) == 0:
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
return self.messages[-1]["turn"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def last_message(self):
|
||||||
|
"""
|
||||||
|
Get the last message in the pool.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: The last message.
|
||||||
|
"""
|
||||||
|
if len(self.messages) == 0:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return self.messages[-1]
|
||||||
|
|
||||||
|
def get_all_messages(self):
|
||||||
|
"""
|
||||||
|
Get all messages in the pool.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Dict]: The list of all messages.
|
||||||
|
"""
|
||||||
|
return self.messages
|
||||||
|
|
||||||
|
def get_visible_messages(self, agent: Agent, turn: int):
|
||||||
|
"""
|
||||||
|
Get the visible messages for a given agent and turn.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
agent (Agent): The agent.
|
||||||
|
turn (int): The turn number.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Dict]: The list of visible messages.
|
||||||
|
"""
|
||||||
|
# Get the messages before the current turn
|
||||||
|
prev_messages = [
|
||||||
|
message
|
||||||
|
for message in self.messages
|
||||||
|
if message["turn"] < turn
|
||||||
|
]
|
||||||
|
|
||||||
|
visible_messages = []
|
||||||
|
for message in prev_messages:
|
||||||
|
if (
|
||||||
|
message["visible_to"] == "all"
|
||||||
|
or agent.agent_name in message["visible_to"]
|
||||||
|
):
|
||||||
|
visible_messages.append(message)
|
||||||
|
return visible_messages
|
||||||
|
|
||||||
|
def query(self, query: str):
|
||||||
|
"""
|
||||||
|
Query a message from the messages list and then pass it to the moderator
|
||||||
|
"""
|
||||||
|
return [
|
||||||
|
(mod, content)
|
||||||
|
for mod, content in self.messages
|
||||||
|
if mod == self.moderator
|
||||||
|
]
|
@ -0,0 +1,10 @@
|
|||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
logger = logger.add(
|
||||||
|
"MessagePool.log",
|
||||||
|
level="INFO",
|
||||||
|
colorize=True,
|
||||||
|
format="<green>{time}</green> <level>{message}</level>",
|
||||||
|
backtrace=True,
|
||||||
|
diagnose=True,
|
||||||
|
)
|
@ -0,0 +1,117 @@
|
|||||||
|
from swarms.structs.agent import Agent
|
||||||
|
from swarms.structs.message_pool import MessagePool
|
||||||
|
from swarms import OpenAIChat
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_pool_initialization():
|
||||||
|
agent1 = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
agent2 = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
moderator = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
agents = [agent1, agent2]
|
||||||
|
message_pool = MessagePool(
|
||||||
|
agents=agents, moderator=moderator, turns=5
|
||||||
|
)
|
||||||
|
|
||||||
|
assert message_pool.agent == agents
|
||||||
|
assert message_pool.moderator == moderator
|
||||||
|
assert message_pool.turns == 5
|
||||||
|
assert message_pool.messages == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_pool_add():
|
||||||
|
agent1 = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
message_pool = MessagePool(
|
||||||
|
agents=[agent1], moderator=agent1, turns=5
|
||||||
|
)
|
||||||
|
message_pool.add(agent=agent1, content="Hello, world!", turn=1)
|
||||||
|
|
||||||
|
assert message_pool.messages == [
|
||||||
|
{
|
||||||
|
"agent": agent1,
|
||||||
|
"content": "Hello, world!",
|
||||||
|
"turn": 1,
|
||||||
|
"visible_to": "all",
|
||||||
|
"logged": True,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_pool_reset():
|
||||||
|
agent1 = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
message_pool = MessagePool(
|
||||||
|
agents=[agent1], moderator=agent1, turns=5
|
||||||
|
)
|
||||||
|
message_pool.add(agent=agent1, content="Hello, world!", turn=1)
|
||||||
|
message_pool.reset()
|
||||||
|
|
||||||
|
assert message_pool.messages == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_pool_last_turn():
|
||||||
|
agent1 = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
message_pool = MessagePool(
|
||||||
|
agents=[agent1], moderator=agent1, turns=5
|
||||||
|
)
|
||||||
|
message_pool.add(agent=agent1, content="Hello, world!", turn=1)
|
||||||
|
|
||||||
|
assert message_pool.last_turn() == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_pool_last_message():
|
||||||
|
agent1 = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
message_pool = MessagePool(
|
||||||
|
agents=[agent1], moderator=agent1, turns=5
|
||||||
|
)
|
||||||
|
message_pool.add(agent=agent1, content="Hello, world!", turn=1)
|
||||||
|
|
||||||
|
assert message_pool.last_message == {
|
||||||
|
"agent": agent1,
|
||||||
|
"content": "Hello, world!",
|
||||||
|
"turn": 1,
|
||||||
|
"visible_to": "all",
|
||||||
|
"logged": True,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_pool_get_all_messages():
|
||||||
|
agent1 = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
message_pool = MessagePool(
|
||||||
|
agents=[agent1], moderator=agent1, turns=5
|
||||||
|
)
|
||||||
|
message_pool.add(agent=agent1, content="Hello, world!", turn=1)
|
||||||
|
|
||||||
|
assert message_pool.get_all_messages() == [
|
||||||
|
{
|
||||||
|
"agent": agent1,
|
||||||
|
"content": "Hello, world!",
|
||||||
|
"turn": 1,
|
||||||
|
"visible_to": "all",
|
||||||
|
"logged": True,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_pool_get_visible_messages():
|
||||||
|
agent1 = Agent(llm=OpenAIChat(), agent_name="agent1")
|
||||||
|
agent2 = Agent(agent_name="agent2")
|
||||||
|
message_pool = MessagePool(
|
||||||
|
agents=[agent1, agent2], moderator=agent1, turns=5
|
||||||
|
)
|
||||||
|
message_pool.add(
|
||||||
|
agent=agent1,
|
||||||
|
content="Hello, agent2!",
|
||||||
|
turn=1,
|
||||||
|
visible_to=[agent2.agent_name],
|
||||||
|
)
|
||||||
|
|
||||||
|
assert message_pool.get_visible_messages(
|
||||||
|
agent=agent2, turn=2
|
||||||
|
) == [
|
||||||
|
{
|
||||||
|
"agent": agent1,
|
||||||
|
"content": "Hello, agent2!",
|
||||||
|
"turn": 1,
|
||||||
|
"visible_to": [agent2.agent_name],
|
||||||
|
"logged": True,
|
||||||
|
}
|
||||||
|
]
|
Loading…
Reference in new issue