Skip to content

Commit

Permalink
[6.7.9]
Browse files Browse the repository at this point in the history
  • Loading branch information
kyegomez committed Dec 23, 2024
1 parent da3f129 commit df76859
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
3 changes: 1 addition & 2 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import secrets
import signal
import sys
import traceback
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timedelta
Expand Down Expand Up @@ -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__":
Expand Down
3 changes: 1 addition & 2 deletions group_chat_example.py
Original file line number Diff line number Diff line change
@@ -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__":
Expand Down Expand Up @@ -61,7 +61,6 @@
name="Investment Advisory",
description="Financial, tax, and stock analysis group",
agents=agents,
speaker_fn=expertise_based,
)

history = chat.run(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
Expand Down
60 changes: 57 additions & 3 deletions swarms/structs/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -128,9 +162,14 @@ def _get_response_sync(
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(
Expand All @@ -150,6 +189,21 @@ def _get_response_sync(
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.
Expand Down
13 changes: 8 additions & 5 deletions swarms/structs/swarm_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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
Expand All @@ -13,7 +14,6 @@

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)

Expand Down Expand Up @@ -507,7 +507,10 @@ def swarm_router(
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}")

0 comments on commit df76859

Please sign in to comment.