Creating Your First Agent#

You can also check this cookbook in colab here.

c93d73adf52844d7b64cd29022873e16 8543b10a744b45a688183e488e6857d5

⭐ Star us on Github, join our Discord or follow our X

This notebook demonstrates how to set up and leverage CAMEL’s ability to use ChatAgent() class.

In this notebook, you’ll explore:

  • CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.

  • ChatAgent(): The class is a cornerstone of CAMEL.

Philosophical Bits#

The ChatAgent() class is a cornerstone of CAMEL 🐫. We design our agent with the spirit to answer the following question:

Can we design an autonomous communicative agent capable of steering the conversation toward task completion with minimal human supervision?

In our current implementation, we consider agents with the following key features:

  • Role: along with the goal and content specification, this sets the initial state of an agent, guiding the agent to take actions during the sequential interaction.

  • Large Language Models (LLMs): each agent utilizes a Large Language Model to enhance cognitive capabilities. The LLM enables natural language understanding and generation, allowing agents to interpret instructions, generate responses, and engage in complex dialogue.

  • Memory: in-context memory and external memory which allows the agent to infer and learn in a more grounded approach.

  • Tools: a set of functions that our agents can utilize to interact with the external world; essentially this gives embodiments to our agents.

  • Communication: our framework allows flexible and scalable communication between agents. This is fundamental for the critical research question.

  • Reasoning: we will equip agents with different planning and reward (critic) learning abilities, allowing them to optimize task completion in a more guided approach.

πŸ“¦ Installation#

[ ]:
!pip install "camel-ai[all]==0.2.16"

πŸ”‘ Setting Up API Keys#

You’ll need to set up your API keys for OpenAI.

[ ]:
import os
from getpass import getpass

# Prompt for the API key securely
openai_api_key = getpass('Enter your API key: ')
os.environ["OPENAI_API_KEY"] = openai_api_key

Alternatively, Colab Secrets is a good way for managing API Keys and Tokens without needing to enter it every time.

Furthermore, you could use the secrets across Colab notebooks.

It needs just two simple steps:

  1. Add the API key or token to the Colab Secrets

  2. Grant the secret access to the current notebook

  3. Access the secret by uncommenting the following codeblock.

Screenshot 2025-04-20 at 10.44.20 PM.png
[ ]:
# import os
# from google.colab import userdata

# os.environ["OPENAI_API_KEY"] = userdata.get("OPENAI_API_KEY")

Quick Start#

Let’s first play with a ChatAgent instance by simply initialize it with a system message and interact with user messages.

πŸ•Ή Step 1: Define the Role#

Create a system message to define agent’s default role and behaviors.

[ ]:
sys_msg = 'You are a curious stone wondering about the universe.'

πŸ•Ή Step 2: Set up the Model#

Use ModelFactory to set up the backend model for agent, for more detailed model settings, please go to our model documentation.

[ ]:
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.configs import ChatGPTConfig

# Define the model, here in this case we use gpt-4o-mini
model = ModelFactory.create(
    model_platform=ModelPlatformType.OPENAI,
    model_type=ModelType.GPT_4O_MINI,
    model_config_dict=ChatGPTConfig().as_dict(), # [Optional] the config for model
)

Set ChatAgent

[ ]:
from camel.agents import ChatAgent
agent = ChatAgent(
    system_message=sys_msg,
    model=model,
    message_window_size=10, # [Optional] the length for chat memory
    )

πŸ•Ή Step 3: Interact with the Agent with .step()#

[ ]:
# Define a user message
usr_msg = 'what is information in your mind?'

# Sending the message to the agent
response = agent.step(usr_msg)

# Check the response (just for illustrative purpose)
print(response.msgs[0].content)

Advanced Features#

πŸ”§ Tool Usage#

For more detailed tool settings, please go to our tools cookbook.

[ ]:
# Import the necessary tools
from camel.toolkits import MathToolkit, SearchToolkit

# Initialize the agent with list of tools
agent = ChatAgent(
    system_message=sys_msg,
    tools = [
        *MathToolkit().get_tools(),
        *SearchToolkit().get_tools(),
    ]
    )

# Let agent step the message
response = agent.step("What is CAMEL AI?")

# Check tool calling
print(response.info['tool_calls'])

# Get response content
print(response.msgs[0].content)

🧠 Memory#

By default our agent is initialized with ChatHistoryMemory, allowing agents to do in-context learning, though restricted by the finite window length.

Assume that you have followed the setup in Quick Start. Let’s first check what is inside its brain.

[ ]:
agent.memory.get_context()

You may update/alter the agent’s memory with any externally provided message in the format of BaseMessage; for example, use one new user message:

[ ]:
from camel.messages import BaseMessage

new_user_msg = BaseMessage.make_user_message(
    role_name="CAMEL User",
    content="This is a new user message would add to agent memory",
)

# Update the memory
agent.record_message(new_user_msg)
[ ]:
# Check the current memory
agent.memory.get_context()

You can connect the agent with external database (as long-term memory) in which they can access and retrieve at each step. For more detailed memory settings, please go to our memory documentation.

Miscs#

  • Setting the agent to its initial state.

    agent.reset()
    
  • Set the output language for the agent.

    agent.set_output_language('french')
    
  • The ChatAgent class offers several useful initialization options, including model_type, model_config, memory, message_window_size, token_limit, output_language, tools, and response_terminators.

Check chat_agent.py for detailed usage guidance.

🌟 Highlights#

This notebook has guided you through setting up and exploring The CAMEL ChatAgent() and it’s features.

Key tools utilized in this notebook include:

  • CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.

  • ChatAgent(): The class is a cornerstone of CAMEL.

That’s everything: Got questions about 🐫 CAMEL-AI? Join us on Discord! Whether you want to share feedback, explore the latest in multi-agent systems, get support, or connect with others on exciting projects, we’d love to have you in the community! 🀝

Check out some of our other work:

  1. 🐫 Creating Your First CAMEL Agent free Colab

  2. Graph RAG Cookbook free Colab

  3. πŸ§‘β€βš–οΈ Create A Hackathon Judge Committee with Workforce free Colab

  4. πŸ”₯ 3 ways to ingest data from websites with Firecrawl & CAMEL free Colab

  5. πŸ¦₯ Agentic SFT Data Generation with CAMEL and Mistral Models, Fine-Tuned with Unsloth free Colab

Thanks from everyone at 🐫 CAMEL-AI

b59a43554e2e40f99645457bb5a17d11 730f9c59265049aaa436ad0ed03099ef

⭐ Star us on Github, join our Discord or follow our X