Skip to content

armgit / camel

Public Imported from GitHub
0

đŸ« CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

7.8 KB
Raw Blame

title: "Societies" description: "Collaborative agent frameworks in CAMEL: autonomous social behaviors, role-based task solving, and turn-based agent societies." icon: people-group

doc_code_map: - "camel/societies/**/*.py"

The society module simulates agent social behaviors and collaborative workflows.
It powers autonomous, multi-role agents that can plan, debate, critique, and solve tasks together, minimizing human intervention while maximizing alignment with your goals. Task: An objective or idea, given as a simple prompt.
AI User: The role responsible for providing instructions or challenges.
AI Assistant: The role tasked with generating solutions, plans, or step-by-step responses.
Critic (optional): An agent that reviews or critiques the assistant's responses for quality control.

Turn-based, prompt-engineered, zero-role-flip agent collaboration.
  • Guards against role-flipping, infinite loops, vague responses
  • Structured, strict turn-taking—user and assistant never switch
  • Supports optional task planners, critics, and meta-reasoning
  • Every message follows a system-enforced structure
Built-in Prompt Rules:
  • Never forget you are <ASSISTANT_ROLE>, I am <USER_ROLE>
  • Never flip roles or instruct me
  • Decline impossible or unsafe requests, explain why
  • Always answer as: Solution: <YOUR_SOLUTION>
  • Always end with: Next request.

đŸ§© RolePlaying Attributes

Attribute Type Description
assistant_role_namestrName of assistant's role
user_role_namestrName of user's role
critic_role_namestrName of critic's role (optional)
task_promptstrPrompt for the main task
with_task_specifyboolEnable task specification agent
with_task_plannerboolEnable task planner agent
with_critic_in_the_loopboolInclude critic in conversation loop
critic_criteriastrHow the critic scores/evaluates outputs
modelBaseModelBackendModel backend for responses
task_typeTaskTypeType/category of the task
assistant_agent_kwargsDictExtra options for assistant agent
user_agent_kwargsDictExtra options for user agent
task_specify_agent_kwargsDictExtra options for task specify agent
task_planner_agent_kwargsDictExtra options for task planner agent
critic_kwargsDictExtra options for critic agent
sys_msg_generator_kwargsDictOptions for system message generator
extend_sys_msg_meta_dictsList[Dict]Extra metadata for system messages
extend_task_specify_meta_dictDictExtra metadata for task specification
output_languagestrTarget output language
assistant_agentChatAgentCustom ChatAgent to use as assistant (optional)
user_agentChatAgentCustom ChatAgent to use as user (optional)
Example: Turn-based multi-agent chat with custom roles and live output colors.

~~~python from colorama import Fore from camel.societies import RolePlaying from camel.utils import print_text_animated

def main(model=None, chat_turn_limit=50) -> None: # Initialize a session for developing a trading bot task_prompt = "Develop a trading bot for the stock market" role_play_session = RolePlaying( assistant_role_name="Python Programmer", assistant_agent_kwargs=dict(model=model), user_role_name="Stock Trader", user_agent_kwargs=dict(model=model), task_prompt=task_prompt, with_task_specify=True, task_specify_agent_kwargs=dict(model=model), )

# Print initial system messages
print(
    Fore.GREEN
    + f"AI Assistant sys message:\\n{role_play_session.assistant_sys_msg}\\n"
)
print(
    Fore.BLUE + f"AI User sys message:\\n{role_play_session.user_sys_msg}\\n"
)

print(Fore.YELLOW + f"Original task prompt:\\n{task_prompt}\\n")
print(
    Fore.CYAN
    + "Specified task prompt:"
    + f"\\n{role_play_session.specified_task_prompt}\\n"
)
print(Fore.RED + f"Final task prompt:\\n{role_play_session.task_prompt}\\n")

n = 0
input_msg = role_play_session.init_chat()

# Turn-based simulation
while n < chat_turn_limit:
    n += 1
    assistant_response, user_response = role_play_session.step(input_msg)

    if assistant_response.terminated:
        print(
            Fore.GREEN
            + (
                "AI Assistant terminated. Reason: "
                f"{assistant_response.info['termination_reasons']}."
            )
        )
        break
    if user_response.terminated:
        print(
            Fore.GREEN
            + (
                "AI User terminated. "
                f"Reason: {user_response.info['termination_reasons']}."
            )
        )
        break

    print_text_animated(
        Fore.BLUE + f"AI User:\\n\\n{user_response.msg.content}\\n"
    )
    print_text_animated(
        Fore.GREEN + "AI Assistant:\\n\\n"
        f"{assistant_response.msg.content}\\n"
    )

    if "CAMEL_TASK_DONE" in user_response.msg.content:
        break

    input_msg = assistant_response.msg

if name == "main": main()

</Card>

<AccordionGroup>
  <Accordion title="Tips & Best Practices">
    <ul>
      <li>Use <b>RolePlaying</b> for most multi-agent conversations, with or without a critic.</li>
      <li>Define specific roles and prompt-guardrails for your agents—structure is everything!</li>
      <li>Leverage the <code>with_task_specify</code> and <code>with_task_planner</code> options for highly complex tasks.</li>
      <li>Monitor for infinite loops—every agent response should have a clear next step or end.</li>
    </ul>
  </Accordion>
  <Accordion title="More Examples & Advanced Use">
    <ul>
      <li>Check <code>[examples/society/](https://github.com/camel-ai/camel/tree/master/examples/runtimes)</code> in the CAMEL repo for advanced agent society demos.</li>
      <li>Explore <b>critic-in-the-loop</b> setups for higher accuracy and safety.</li>
      <li>Integrate toolkits or external APIs into agent society loops for real-world workflows.</li>
    </ul>
  </Accordion>
</AccordionGroup>