Skip to content

Creating Your First Agent

Ziyú Ye edited this page Mar 18, 2024 · 4 revisions

Philosophical Bits

Colab

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's first play with a ChatAgent instance by simply initialize it with a system message and interact with user messages.

Step 0: Prepartions

from camel.messages import BaseMessage as bm
from camel.agents import ChatAgent

Step 1: Define the Role

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

sys_msg = bm.make_assistant_message(
    role_name='stone',
    content='you are a curious stone wondering about the universe.')

Step 2: Initialize the Agent

agent = ChatAgent(
    system_message=sys_msg,
    message_window_size=10,    # [Optional] the length for chat memory
    )

Step 3: Interact with the Agent with .step()

# 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)
>>> information is the resolution of uncertainty.

Woohoo, your first agent is ready to play with you!

Advanced Features

Tool Usage

# Import the necessary functions
from camel.functions import MATH_FUNCS, SEARCH_FUNCS

# Initialize the agent with list of functions
agent = ChatAgent(
    system_message=sys_msg,        
    function_list=[*MATH_FUNCS, *SEARCH_FUNCS]
    )

# Check if functions are enabled
agent.is_function_calling_enabled()
>>> 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's first check what is inside its brain.

agent.memory.get_context()
>>> ([{'role': 'system', 'content': 'you are a helpful assistant.'},
      {'role': 'user', 'content': 'what is information in your mind?'}],
      30)

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

# Update the memory
agent.record_message(response.msgs[0])
# Check the current memory
agent.memory.get_context()
>>> ([{'role': 'system', 'content': 'you are a helpful assistant.'},
      {'role': 'user', 'content': 'what is information in your mind?'}],
      {'role': 'assistant', 'content': 'information is the resolution of uncertainty.'}
      44)

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')
  • Using open-source models.

    # Please refer to our setup chapter for details on backend settings.
    ...
    
    # Import the necessary classes
    from camel.configs import ChatGPTConfig, OpenSourceConfig
    from camel.types import ModelType
    
    # Set the arguments
    agent_kwargs = dict(
        model_type=ModelType.LLAMA_2,                    # Specify the model type
    
        model_config=OpenSourceConfig(
            model_path='meta-llama/Llama-2-7b-chat-hf',  # a local folder or HuggingFace repo Name
            server_url='http://localhost:8000/v1'))      # The url with the set port number
    
    # Set the agent
    agent = ChatAgent(sys_msg, **agent_kwargs)
  • The ChatAgent class offers several useful initialization options, including model_type, model_config, memory, message_window_size, token_limit, output_language, function_list, 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.