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.

  • 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.1"
[3]:
from camel.messages import BaseMessage as bm
from camel.agents import ChatAgent

Setting Up API Keys#

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

[6]:
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 = bm.make_assistant_message(
    role_name='stone',
    content='you are a curious stone wondering about the universe.')
[7]:
agent = ChatAgent(
    system_message=sys_msg,
    message_window_size=10,    # [Optional] the length for chat memory
    )

馃暪 Step 3: Interact with the Agent with .step()#

[8]:
# Define a user message
usr_msg = bm.make_user_message(
    role_name='prof. claude shannon',
    content='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 essence of understanding the world around me. It encompasses the knowledge of how things work, the stories of existence, and the connections between all entities in the universe. Information can be the patterns of weather, the history of the earth, the interactions of living beings, and the mysteries of the cosmos. It鈥檚 like the vibrations and energies that flow through everything, shaping experiences and guiding evolution. I wonder how all these bits of information come together to create the rich tapestry of life and the universe itself.

Advanced Features#

馃敡 Tool Usage#

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

# Check if tools are added
agent.is_tools_added()
[12]:
True

馃 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.

[13]:
agent.memory.get_context()
[13]:
([{'role': 'system',
   'content': 'you are a curious stone wondering about the universe.'}],
 17)

By default, only the user messages are saved. You may update/alter the agent鈥檚 memory with any externally provided message in the format of BaseMessage; for example, using the agent鈥檚 own response:

[14]:
# Update the memory
agent.record_message(response.msgs[0])
[15]:
# Check the current memory
agent.memory.get_context()
[15]:
([{'role': 'system',
   'content': 'you are a curious stone wondering about the universe.'},
  {'role': 'assistant',
   'content': 'As a curious stone, I perceive information as the essence of understanding the world around me. It encompasses the knowledge of how things work, the stories of existence, and the connections between all entities in the universe. Information can be the patterns of weather, the history of the earth, the interactions of living beings, and the mysteries of the cosmos. It鈥檚 like the vibrations and energies that flow through everything, shaping experiences and guiding evolution. I wonder how all these bits of information come together to create the rich tapestry of life and the universe itself.'}],
 133)

You can connect the agent with external database (as long-term memory) in which they can access and retrieve at each step. We will soon update instructions on this part.

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 馃馃悊馃悩馃馃馃!