diff --git a/api/main.py b/api/main.py index 10123634..460e5ad8 100644 --- a/api/main.py +++ b/api/main.py @@ -2,7 +2,6 @@ import asyncio import os import secrets import signal -import sys import traceback from concurrent.futures import ThreadPoolExecutor from datetime import datetime, timedelta @@ -974,7 +973,7 @@ def run_server(): asyncio.run(server.startup()) except Exception as e: logger.error(f"Failed to start API: {str(e)}") - print(f"Error starting server: {str(e)}" + print(f"Error starting server: {str(e)}") if __name__ == "__main__": diff --git a/group_chat_example.py b/group_chat_example.py index d28bea9b..f2426896 100644 --- a/group_chat_example.py +++ b/group_chat_example.py @@ -1,7 +1,7 @@ import os from dotenv import load_dotenv from swarm_models import OpenAIChat -from swarms import Agent, GroupChat, expertise_based +from swarms import Agent, GroupChat if __name__ == "__main__": @@ -61,7 +61,6 @@ if __name__ == "__main__": name="Investment Advisory", description="Financial, tax, and stock analysis group", agents=agents, - speaker_fn=expertise_based, ) history = chat.run( diff --git a/pyproject.toml b/pyproject.toml index c8b174e4..a506e5bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "6.7.8" +version = "6.7.9" description = "Swarms - TGSC" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/groupchat.py b/swarms/structs/groupchat.py index 39fc4552..bac48e72 100644 --- a/swarms/structs/groupchat.py +++ b/swarms/structs/groupchat.py @@ -65,6 +65,40 @@ def random_selection(history: List[str], agent: Agent) -> bool: return random.choice([True, False]) +def custom_speaker(history: List[str], agent: Agent) -> bool: + """ + Custom speaker function with complex logic. + + Args: + history: Previous conversation messages + agent: Current agent being evaluated + + Returns: + bool: Whether agent should speak + """ + # No history - let everyone speak + if not history: + return True + + last_message = history[-1].lower() + + # Check for agent expertise keywords + expertise_relevant = any( + keyword in last_message + for keyword in agent.description.lower().split() + ) + + # Check for direct mentions + mentioned = agent.agent_name.lower() in last_message + + # Check if agent hasn't spoken recently + not_recent_speaker = not any( + agent.agent_name in msg for msg in history[-3:] + ) + + return expertise_relevant or mentioned or not_recent_speaker + + def most_recent(history: List[str], agent: Agent) -> bool: """ Most recent speaker function. @@ -128,9 +162,14 @@ class GroupChat: AgentResponse: The agent's response captured in a structured format. """ try: - context = f"""You are {agent.name} with role: {agent.system_prompt}. - Other agents: {[a.name for a in self.agents if a != agent]} - Previous messages: {[t.responses[-3:] for t in self.chat_history.turns[-3:]]}""" + # Provide the agent with information about the chat and other agents + chat_info = f"Chat Name: {self.name}\nChat Description: {self.description}\nAgents in Chat: {[a.agent_name for a in self.agents]}" + context = f"""You are {agent.agent_name} + Conversation History: + \n{chat_info} + Other agents: {[a.agent_name for a in self.agents if a != agent]} + Previous messages: {self.get_full_chat_history()} + """ # Updated line message = agent.run(context + prompt) return AgentResponse( @@ -150,6 +189,21 @@ class GroupChat: preceding_context=[], ) + def get_full_chat_history(self) -> str: + """ + Get the full chat history formatted for agent context. + + Returns: + str: The full chat history with sender names. + """ + messages = [] + for turn in self.chat_history.turns: + for response in turn.responses: + messages.append( + f"{response.agent_name}: {response.message}" + ) + return "\n".join(messages) + def get_recent_messages(self, n: int = 3) -> List[str]: """ Get the most recent messages in the chat. diff --git a/swarms/structs/swarm_builder.py b/swarms/structs/swarm_builder.py index 78d5ea34..8c1c1da8 100644 --- a/swarms/structs/swarm_builder.py +++ b/swarms/structs/swarm_builder.py @@ -2,6 +2,7 @@ import os import subprocess from typing import List, Optional +from loguru import logger from pydantic import BaseModel, Field from pydantic.v1 import validator from swarm_models import OpenAIChat @@ -13,7 +14,6 @@ from tenacity import ( from swarms.structs.agent import Agent from swarms.structs.swarm_router import SwarmRouter, SwarmType -from loguru import logger logger.add("swarm_builder.log", rotation="10 MB", backtrace=True) @@ -507,7 +507,10 @@ swarm = AutoSwarmBuilder( swarm_type="ConcurrentWorkflow", ) -result = swarm.run( - "Design a new AI accelerator chip optimized for transformer model inference..." -) -print(result) +try: + result = swarm.run( + "Design a new AI accelerator chip optimized for transformer model inference..." + ) + print(result) +except Exception as e: + print(f"An error occurred: {e}")