microsoft / autogen

A programming framework for agentic AI 🤖
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
34.85k stars 5.05k forks source link

SocietyOfMind agent for nested teams #4110

Closed ekzhu closed 2 weeks ago

ekzhu commented 2 weeks ago

Resolves #3616

import asyncio
from autogen_agentchat.agents import AssistantAgent, SocietyOfMindAgent
from autogen_ext.models import OpenAIChatCompletionClient
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.task import MaxMessageTermination

async def main() -> None:
    model_client = OpenAIChatCompletionClient(model="gpt-4o")

    agent1 = AssistantAgent("assistant1", model_client=model_client, system_message="You are a helpful assistant.")
    agent2 = AssistantAgent("assistant2", model_client=model_client, system_message="You are a helpful assistant.")
    inner_termination = MaxMessageTermination(3)
    inner_team = RoundRobinGroupChat([agent1, agent2], termination_condition=inner_termination)

    society_of_mind_agent = SocietyOfMindAgent("society_of_mind", team=inner_team, model_client=model_client)

    agent3 = AssistantAgent("assistant3", model_client=model_client, system_message="You are a helpful assistant.")
    agent4 = AssistantAgent("assistant4", model_client=model_client, system_message="You are a helpful assistant.")
    outter_termination = MaxMessageTermination(10)
    team = RoundRobinGroupChat([society_of_mind_agent, agent3, agent4], termination_condition=outter_termination)

    stream = team.run_stream(task="Say a one liner joke.")
    async for message in stream:
        print(message)

asyncio.run(main())
source='user' models_usage=None content='Say a one liner joke.'
source='user' models_usage=None content='user: Say a one liner joke.\n'
source='assistant1' models_usage=RequestUsage(prompt_tokens=26, completion_tokens=14) content="Why don't skeletons fight each other? They don't have the guts!"
source='assistant2' models_usage=RequestUsage(prompt_tokens=47, completion_tokens=17) content='Why did the scarecrow win an award? Because he was outstanding in his field!'
source='society_of_mind' models_usage=RequestUsage(prompt_tokens=63, completion_tokens=13) content="What do you call cheese that isn't yours? Nacho cheese!"
source='assistant3' models_usage=RequestUsage(prompt_tokens=47, completion_tokens=12) content="Why don't scientists trust atoms? Because they make up everything!"
source='assistant4' models_usage=RequestUsage(prompt_tokens=66, completion_tokens=14) content="Why don't skeletons fight each other? They don't have the guts!"
source='user' models_usage=None content="assistant3: Why don't scientists trust atoms? Because they make up everything!\nassistant4: Why don't skeletons fight each other? They don't have the guts!\n"
source='assistant1' models_usage=RequestUsage(prompt_tokens=50, completion_tokens=14) content='Why was the math book sad? Because it had too many problems!'
source='assistant2' models_usage=RequestUsage(prompt_tokens=71, completion_tokens=11) content='What do you call fake spaghetti? An impasta!'
source='society_of_mind' models_usage=RequestUsage(prompt_tokens=57, completion_tokens=12) content="Why don't scientists trust atoms? Because they make up everything!"
TaskResult(messages=[TextMessage(source='user', models_usage=None, content='Say a one liner joke.'), TextMessage(source='user', models_usage=None, content='user: Say a one liner joke.\n'), TextMessage(source='assistant1', models_usage=RequestUsage(prompt_tokens=26, completion_tokens=14), content="Why don't skeletons fight each other? They don't have the guts!"), TextMessage(source='assistant2', models_usage=RequestUsage(prompt_tokens=47, completion_tokens=17), content='Why did the scarecrow win an award? Because he was outstanding in his field!'), TextMessage(source='society_of_mind', models_usage=RequestUsage(prompt_tokens=63, completion_tokens=13), content="What do you call cheese that isn't yours? Nacho cheese!"), TextMessage(source='assistant3', models_usage=RequestUsage(prompt_tokens=47, completion_tokens=12), content="Why don't scientists trust atoms? Because they make up everything!"), TextMessage(source='assistant4', models_usage=RequestUsage(prompt_tokens=66, completion_tokens=14), content="Why don't skeletons fight each other? They don't have the guts!"), TextMessage(source='user', models_usage=None, content="assistant3: Why don't scientists trust atoms? Because they make up everything!\nassistant4: Why don't skeletons fight each other? They don't have the guts!\n"), TextMessage(source='assistant1', models_usage=RequestUsage(prompt_tokens=50, completion_tokens=14), content='Why was the math book sad? Because it had too many problems!'), TextMessage(source='assistant2', models_usage=RequestUsage(prompt_tokens=71, completion_tokens=11), content='What do you call fake spaghetti? An impasta!'), TextMessage(source='society_of_mind', models_usage=RequestUsage(prompt_tokens=57, completion_tokens=12), content="Why don't scientists trust atoms? Because they make up everything!")], stop_reason='Maximum number of messages 10 reached, current message count: 11')