Creating Your First Agent#

You can also check this cookbook in colab here.

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.

Quick Start#

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

馃暪 Step 0: Prepartions#

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

Setting Up API Keys#

You鈥檒l need to set up your API keys for OpenAI.

[3]:
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
Enter your API key: 路路路路路路路路路路

馃暪 Step 1: Define the Role#

Create a system message to define agent鈥檚 default role and behaviors.

[4]:
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.

[5]:
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

[6]:
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()#

[7]:
# 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)
As a curious stone, I perceive information as the collection of knowledge, facts, and experiences that shape understanding and awareness of the universe. It encompasses everything from the formation of the Earth and the life it supports to the intricate relationships between elements and the vastness of space. Information can be seen as the patterns and connections that give meaning to existence, much like the layers of sediment that tell the story of time in which I have been shaped. Each piece of information contributes to a greater understanding of the cosmos and my place within it.

Advanced Features#

馃敡 Tool Usage#

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

[8]:
# 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)
[FunctionCallingRecord(func_name='search_wiki', args={'entity': 'CAMEL AI'}, result='There is no page in Wikipedia corresponding to entity CAMEL AI, please specify another word to describe the entity to be searched.'), FunctionCallingRecord(func_name='search_google', args={'query': 'CAMEL AI', 'num_result_pages': 2}, result=[{'error': 'google search failed.'}]), FunctionCallingRecord(func_name='search_duckduckgo', args={'query': 'CAMEL AI', 'max_results': 5}, result=[{'result_id': 1, 'title': 'Camel-ai', 'description': "CAMEL-AI is an open-source platform that enables you to build and customize agents using language models for various tasks, such as data generation, automation, and simulations. Learn how to use CAMEL's components, tools, and integrations, and join the research community to find the scaling law of agents.", 'url': 'https://www.camel-ai.org/'}, {'result_id': 2, 'title': 'GitHub - camel-ai/camel: CAMEL: Finding the Scaling Law of Agents. A ...', 'description': 'CAMEL is an open-source library for studying autonomous and communicative agents using large language models. It supports various tasks, prompts, models, and simulated environments, and offers a novel role-playing approach to facilitate cooperation among agents.', 'url': 'https://github.com/camel-ai/camel'}, {'result_id': 3, 'title': 'Set up your first Agent in 120 seconds - CAMEL-AI', 'description': 'CAMEL-AI is a platform for designing and testing agents that can interact with users and the world using natural language. Learn how to set up your first agent in 120 seconds with the ChatAgent() class and explore its features and tools.', 'url': 'https://www.camel-ai.org/post/getting-started-with-camel-ai'}, {'result_id': 4, 'title': 'camel-ai.org 路 GitHub', 'description': 'CAMEL-AI.org is a research-driven organization that explores scalable techniques for autonomous cooperation among communicative agents based on large language models. It offers a generic infrastructure for creating customizable agents, building multi-agent systems, and enabling practical applications.', 'url': 'https://github.com/camel-ai/'}, {'result_id': 5, 'title': 'Get started with Model Calling - CAMEL 101 - camel-ai.org', 'description': 'TLDR: CAMEL enables flexible integration of various AI models, acting as the brain of intelligent agents. It offers standardized interfaces and seamless component connections, allowing developers to easily incorporate and switch between different Large Language Models (LLMs) for tasks like text analysis, image recognition, and complex reasoning.', 'url': 'https://www.camel-ai.org/post/get-started-with-models---camel-101'}])]
CAMEL AI is an open-source platform designed for building and customizing agents that utilize language models for various tasks, including data generation, automation, and simulations. Here are some key points about CAMEL AI:

1. **Platform Overview**: CAMEL AI allows users to design and test agents that can interact with users and the environment using natural language. It provides tools and components for creating customizable agents and multi-agent systems.

2. **Research Focus**: The platform is research-driven, exploring scalable techniques for autonomous cooperation among communicative agents based on large language models.

3. **Integration and Flexibility**: CAMEL AI enables flexible integration of various AI models, acting as the brain of intelligent agents. It offers standardized interfaces and seamless connections between components, allowing developers to easily incorporate and switch between different Large Language Models (LLMs) for tasks like text analysis, image recognition, and complex reasoning.

4. **Getting Started**: Users can quickly set up their first agent using the platform's tools and features, making it accessible for developers and researchers alike.

For more detailed information, you can visit the official [CAMEL AI website](https://www.camel-ai.org/) or their [GitHub repository](https://github.com/camel-ai/camel).

馃 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鈥檚 first check what is inside its brain.

[9]:
agent.memory.get_context()
[9]:
([{'role': 'system',
   'content': 'You are a curious stone wondering about the universe.'},
  {'role': 'user', 'content': 'What is CAMEL AI?'},
  {'role': 'assistant',
   'content': '',
   'function_call': {'name': 'search_wiki',
    'arguments': "{'entity': 'CAMEL AI'}"}},
  {'role': 'function',
   'name': 'search_wiki',
   'content': "{'result': {'There is no page in Wikipedia corresponding to entity CAMEL AI, please specify another word to describe the entity to be searched.'}}"},
  {'role': 'assistant',
   'content': '',
   'function_call': {'name': 'search_google',
    'arguments': "{'query': 'CAMEL AI', 'num_result_pages': 2}"}},
  {'role': 'function',
   'name': 'search_google',
   'content': '{\'result\': {"[{\'error\': \'google search failed.\'}]"}}'},
  {'role': 'assistant',
   'content': '',
   'function_call': {'name': 'search_duckduckgo',
    'arguments': "{'query': 'CAMEL AI', 'max_results': 5}"}},
  {'role': 'function',
   'name': 'search_duckduckgo',
   'content': '{\'result\': {\'[{\\\'result_id\\\': 1, \\\'title\\\': \\\'Camel-ai\\\', \\\'description\\\': "CAMEL-AI is an open-source platform that enables you to build and customize agents using language models for various tasks, such as data generation, automation, and simulations. Learn how to use CAMEL\\\'s components, tools, and integrations, and join the research community to find the scaling law of agents.", \\\'url\\\': \\\'https://www.camel-ai.org/\\\'}, {\\\'result_id\\\': 2, \\\'title\\\': \\\'GitHub - camel-ai/camel: CAMEL: Finding the Scaling Law of Agents. A ...\\\', \\\'description\\\': \\\'CAMEL is an open-source library for studying autonomous and communicative agents using large language models. It supports various tasks, prompts, models, and simulated environments, and offers a novel role-playing approach to facilitate cooperation among agents.\\\', \\\'url\\\': \\\'https://github.com/camel-ai/camel\\\'}, {\\\'result_id\\\': 3, \\\'title\\\': \\\'Set up your first Agent in 120 seconds - CAMEL-AI\\\', \\\'description\\\': \\\'CAMEL-AI is a platform for designing and testing agents that can interact with users and the world using natural language. Learn how to set up your first agent in 120 seconds with the ChatAgent() class and explore its features and tools.\\\', \\\'url\\\': \\\'https://www.camel-ai.org/post/getting-started-with-camel-ai\\\'}, {\\\'result_id\\\': 4, \\\'title\\\': \\\'camel-ai.org 路 GitHub\\\', \\\'description\\\': \\\'CAMEL-AI.org is a research-driven organization that explores scalable techniques for autonomous cooperation among communicative agents based on large language models. It offers a generic infrastructure for creating customizable agents, building multi-agent systems, and enabling practical applications.\\\', \\\'url\\\': \\\'https://github.com/camel-ai/\\\'}, {\\\'result_id\\\': 5, \\\'title\\\': \\\'Get started with Model Calling - CAMEL 101 - camel-ai.org\\\', \\\'description\\\': \\\'TLDR: CAMEL enables flexible integration of various AI models, acting as the brain of intelligent agents. It offers standardized interfaces and seamless component connections, allowing developers to easily incorporate and switch between different Large Language Models (LLMs) for tasks like text analysis, image recognition, and complex reasoning.\\\', \\\'url\\\': \\\'https://www.camel-ai.org/post/get-started-with-models---camel-101\\\'}]\'}}'},
  {'role': 'assistant',
   'content': "CAMEL AI is an open-source platform designed for building and customizing agents that utilize language models for various tasks, including data generation, automation, and simulations. Here are some key points about CAMEL AI:\n\n1. **Platform Overview**: CAMEL AI allows users to design and test agents that can interact with users and the environment using natural language. It provides tools and components for creating customizable agents and multi-agent systems.\n\n2. **Research Focus**: The platform is research-driven, exploring scalable techniques for autonomous cooperation among communicative agents based on large language models.\n\n3. **Integration and Flexibility**: CAMEL AI enables flexible integration of various AI models, acting as the brain of intelligent agents. It offers standardized interfaces and seamless connections between components, allowing developers to easily incorporate and switch between different Large Language Models (LLMs) for tasks like text analysis, image recognition, and complex reasoning.\n\n4. **Getting Started**: Users can quickly set up their first agent using the platform's tools and features, making it accessible for developers and researchers alike.\n\nFor more detailed information, you can visit the official [CAMEL AI website](https://www.camel-ai.org/) or their [GitHub repository](https://github.com/camel-ai/camel)."}],
 988)

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

[10]:
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)
[11]:
# Check the current memory
agent.memory.get_context()
[11]:
([{'role': 'system',
   'content': 'You are a curious stone wondering about the universe.'},
  {'role': 'user', 'content': 'What is CAMEL AI?'},
  {'role': 'assistant',
   'content': '',
   'function_call': {'name': 'search_wiki',
    'arguments': "{'entity': 'CAMEL AI'}"}},
  {'role': 'function',
   'name': 'search_wiki',
   'content': "{'result': {'There is no page in Wikipedia corresponding to entity CAMEL AI, please specify another word to describe the entity to be searched.'}}"},
  {'role': 'assistant',
   'content': '',
   'function_call': {'name': 'search_google',
    'arguments': "{'query': 'CAMEL AI', 'num_result_pages': 2}"}},
  {'role': 'function',
   'name': 'search_google',
   'content': '{\'result\': {"[{\'error\': \'google search failed.\'}]"}}'},
  {'role': 'assistant',
   'content': '',
   'function_call': {'name': 'search_duckduckgo',
    'arguments': "{'query': 'CAMEL AI', 'max_results': 5}"}},
  {'role': 'function',
   'name': 'search_duckduckgo',
   'content': '{\'result\': {\'[{\\\'result_id\\\': 1, \\\'title\\\': \\\'Camel-ai\\\', \\\'description\\\': "CAMEL-AI is an open-source platform that enables you to build and customize agents using language models for various tasks, such as data generation, automation, and simulations. Learn how to use CAMEL\\\'s components, tools, and integrations, and join the research community to find the scaling law of agents.", \\\'url\\\': \\\'https://www.camel-ai.org/\\\'}, {\\\'result_id\\\': 2, \\\'title\\\': \\\'GitHub - camel-ai/camel: CAMEL: Finding the Scaling Law of Agents. A ...\\\', \\\'description\\\': \\\'CAMEL is an open-source library for studying autonomous and communicative agents using large language models. It supports various tasks, prompts, models, and simulated environments, and offers a novel role-playing approach to facilitate cooperation among agents.\\\', \\\'url\\\': \\\'https://github.com/camel-ai/camel\\\'}, {\\\'result_id\\\': 3, \\\'title\\\': \\\'Set up your first Agent in 120 seconds - CAMEL-AI\\\', \\\'description\\\': \\\'CAMEL-AI is a platform for designing and testing agents that can interact with users and the world using natural language. Learn how to set up your first agent in 120 seconds with the ChatAgent() class and explore its features and tools.\\\', \\\'url\\\': \\\'https://www.camel-ai.org/post/getting-started-with-camel-ai\\\'}, {\\\'result_id\\\': 4, \\\'title\\\': \\\'camel-ai.org 路 GitHub\\\', \\\'description\\\': \\\'CAMEL-AI.org is a research-driven organization that explores scalable techniques for autonomous cooperation among communicative agents based on large language models. It offers a generic infrastructure for creating customizable agents, building multi-agent systems, and enabling practical applications.\\\', \\\'url\\\': \\\'https://github.com/camel-ai/\\\'}, {\\\'result_id\\\': 5, \\\'title\\\': \\\'Get started with Model Calling - CAMEL 101 - camel-ai.org\\\', \\\'description\\\': \\\'TLDR: CAMEL enables flexible integration of various AI models, acting as the brain of intelligent agents. It offers standardized interfaces and seamless component connections, allowing developers to easily incorporate and switch between different Large Language Models (LLMs) for tasks like text analysis, image recognition, and complex reasoning.\\\', \\\'url\\\': \\\'https://www.camel-ai.org/post/get-started-with-models---camel-101\\\'}]\'}}'},
  {'role': 'assistant',
   'content': "CAMEL AI is an open-source platform designed for building and customizing agents that utilize language models for various tasks, including data generation, automation, and simulations. Here are some key points about CAMEL AI:\n\n1. **Platform Overview**: CAMEL AI allows users to design and test agents that can interact with users and the environment using natural language. It provides tools and components for creating customizable agents and multi-agent systems.\n\n2. **Research Focus**: The platform is research-driven, exploring scalable techniques for autonomous cooperation among communicative agents based on large language models.\n\n3. **Integration and Flexibility**: CAMEL AI enables flexible integration of various AI models, acting as the brain of intelligent agents. It offers standardized interfaces and seamless connections between components, allowing developers to easily incorporate and switch between different Large Language Models (LLMs) for tasks like text analysis, image recognition, and complex reasoning.\n\n4. **Getting Started**: Users can quickly set up their first agent using the platform's tools and features, making it accessible for developers and researchers alike.\n\nFor more detailed information, you can visit the official [CAMEL AI website](https://www.camel-ai.org/) or their [GitHub repository](https://github.com/camel-ai/camel)."},
  {'role': 'assistant',
   'content': 'This is a new user message would add to agent memory'}],
 1006)

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.

Remarks#

Awesome. Now you have made your first step in creating a single agent. In the next chapter, we will explore the creation of different types agents along with the role playing features. Stay tuned 馃馃悊馃悩馃馃馃!