Embodied Agents#

You can also check this cookbook in colab here

Philosophical Bits#

We believe the essence of intelligence emerges from its dynamic interactions with the external environment, where the use of various tools becomes a pivotal factor in its development and manifestation.

The EmbodiedAgent() in CAMEL is an advanced conversational agent that leverages code interpreters and tool agents (e.g., HuggingFaceToolAgent()) to execute diverse tasks efficiently. This agent represents a blend of advanced programming and AI capabilities, and is able to interact and respond within a dynamic environment.

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#

[ ]:
%pip install "camel-ai==0.2.1"
[3]:
from camel.agents import EmbodiedAgent
from camel.generators import SystemMessageGenerator as sys_msg_gen
from camel.messages import BaseMessage as bm
from camel.types import RoleType

Setting Up API Keys#

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

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

We first need to set up the necessary information.

[5]:
# Set the role name and the task
role = 'Programmer'
task = 'Writing and executing codes.'

# Create the meta_dict and the role_tuple
meta_dict = dict(role=role, task=task)
role_tuple = (role, RoleType.EMBODIMENT)

The meta_dict and role_type will be used to generate the system message.

[6]:
# Generate the system message based on this
sys_msg = sys_msg_gen().from_dict(meta_dict=meta_dict,
                                  role_tuple=role_tuple)

🕹 Step 2: Initialize the Agent 🐫#

Based on the system message, we are ready to initialize our embodied agent.

[7]:
embodied_agent = EmbodiedAgent(system_message=sys_msg,
                               tool_agents=None,
                               code_interpreter=None,
                               verbose=True)

Be aware that the default argument values for tool_agents and code_interpreter are None, and the underlying code interpreter is using the SubProcessInterpreter(), which handles the execution of code in Python and Bash within a subprocess.

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

Use the base message wrapper to generate the user message.

[8]:
usr_msg = bm.make_user_message(
    role_name='user',
    content=('1. write a bash script to install numpy. '
             '2. then write a python script to compute '
             'the dot product of [8, 9] and [5, 4], '
             'and print the result. '
             '3. then write a script to search for '
             'the weather at london with wttr.in/london.'))

And feed that into your agents:

[9]:
response = embodied_agent.step(usr_msg)
> Explanation:
To accomplish the tasks you've outlined, I will perform the following actions:

1. **Create a Bash script** to install NumPy using pip. This will ensure that the NumPy library is available for use in Python scripts.
2. **Create a Python script** that computes the dot product of two vectors, `[8, 9]` and `[5, 4]`, and prints the result. The dot product is a common operation in linear algebra and can be computed using NumPy.
3. **Create a Bash script** to fetch the weather information for London using the `curl` command to access the `wttr.in` service.

### Step 1: Bash Script to Install NumPy

The Bash script will use `pip` to install NumPy. Here’s the code for that:

> Code:
#!/bin/bash
# install_numpy.sh
echo "Installing NumPy..."
pip install numpy

> Explanation:
### Step 2: Python Script for Dot Product

The Python script will use NumPy to compute the dot product of the two vectors. Here’s the code for that:

> Code:
# dot_product.py
import numpy as np

# Define the vectors
vector_a = np.array([8, 9])
vector_b = np.array([5, 4])

# Compute the dot product
dot_product = np.dot(vector_a, vector_b)

# Print the result
print("The dot product of", vector_a.tolist(), "and", vector_b.tolist(), "is:", dot_product)

> Explanation:
### Step 3: Bash Script to Fetch Weather Information

The Bash script will use `curl` to get the weather information for London from `wttr.in`. Here’s the code for that:

> Code:
#!/bin/bash
# get_weather.sh
echo "Fetching weather information for London..."
curl wttr.in/London

> Explanation:
### Summary of Actions

1. Create a Bash script named `install_numpy.sh` to install NumPy.
2. Create a Python script named `dot_product.py` to compute and print the dot product of the vectors.
3. Create a Bash script named `get_weather.sh` to fetch and display the weather for London.

Now, I will write the code for each of these scripts.

### Code Implementation

> Code:
# Create install_numpy.sh
echo '#!/bin/bash' > install_numpy.sh
echo 'echo "Installing NumPy..."' >> install_numpy.sh
echo 'pip install numpy' >> install_numpy.sh
chmod +x install_numpy.sh

# Create dot_product.py
echo 'import numpy as np' > dot_product.py
echo 'vector_a = np.array([8, 9])' >> dot_product.py
echo 'vector_b = np.array([5, 4])' >> dot_product.py
echo 'dot_product = np.dot(vector_a, vector_b)' >> dot_product.py
echo 'print("The dot product of", vector_a.tolist(), "and", vector_b.tolist(), "is:", dot_product)' >> dot_product.py

# Create get_weather.sh
echo '#!/bin/bash' > get_weather.sh
echo 'echo "Fetching weather information for London..."' >> get_weather.sh
echo 'curl wttr.in/London' >> get_weather.sh
chmod +x get_weather.sh

> Explanation:
### Execution

1. Run the `install_numpy.sh` script to install NumPy.
2. Run the `dot_product.py` script to compute and print the dot product.
3. Run the `get_weather.sh` script to fetch and display the weather for London.

Let me know if you would like me to execute any of these scripts or if you need further assistance!

The following bash code will run on your computer:
#!/bin/bash
# install_numpy.sh
echo "Installing NumPy..."
pip install numpy
Running code? [Y/n]:y
The following python code will run on your computer:
# dot_product.py
import numpy as np

# Define the vectors
vector_a = np.array([8, 9])
vector_b = np.array([5, 4])

# Compute the dot product
dot_product = np.dot(vector_a, vector_b)

# Print the result
print("The dot product of", vector_a.tolist(), "and", vector_b.tolist(), "is:", dot_product)
Running code? [Y/n]:y
The following bash code will run on your computer:
#!/bin/bash
# get_weather.sh
echo "Fetching weather information for London..."
curl wttr.in/London
Running code? [Y/n]:y
======stderr======
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
100  8977  100  8977    0     0   3495      0  0:00:02  0:00:02 --:--:--  3495

==================
The following bash code will run on your computer:
# Create install_numpy.sh
echo '#!/bin/bash' > install_numpy.sh
echo 'echo "Installing NumPy..."' >> install_numpy.sh
echo 'pip install numpy' >> install_numpy.sh
chmod +x install_numpy.sh

# Create dot_product.py
echo 'import numpy as np' > dot_product.py
echo 'vector_a = np.array([8, 9])' >> dot_product.py
echo 'vector_b = np.array([5, 4])' >> dot_product.py
echo 'dot_product = np.dot(vector_a, vector_b)' >> dot_product.py
echo 'print("The dot product of", vector_a.tolist(), "and", vector_b.tolist(), "is:", dot_product)' >> dot_product.py

# Create get_weather.sh
echo '#!/bin/bash' > get_weather.sh
echo 'echo "Fetching weather information for London..."' >> get_weather.sh
echo 'curl wttr.in/London' >> get_weather.sh
chmod +x get_weather.sh
Running code? [Y/n]:y

Under the hood, the agent will perform multiple actions within its action space in the OS to fulfill the user request. It will compose code to implement the action – no worries, it will ask for your permission before execution.

Ideally you should get the output similar to this, if you allow the agent to perform actions:

[10]:
print(response.msg.content)
1. write a bash script to install numpy. 2. then write a python script to compute the dot product of [8, 9] and [5, 4], and print the result. 3. then write a script to search for the weather at london with wttr.in/london.
> Embodied Actions:

> Executed Results:
Executing code block 0: {
Installing NumPy...
Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (1.26.4)
}
Executing code block 1: {
The dot product of [8, 9] and [5, 4] is: 76
}
Executing code block 2: {
Fetching weather information for London...
Weather report: London

       .-.      Light rain shower
      (   ).    15 °C
     (___(__)    12 km/h
      ‘ ‘ ‘ ‘   3 km
     ‘ ‘ ‘ ‘    3.4 mm
                                                       ┌─────────────┐
┌──────────────────────────────┬───────────────────────┤  Thu 26 Sep ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│  _`/"".-.     Patchy light d…│  _`/"".-.     Patchy light d…│  _`/"".-.     Light rain sho…│  _`/"".-.     Patchy rain ne…│
│   ,\_(   ).   16 °C          │   ,\_(   ).   17 °C          │   ,\_(   ).   +14(12) °C     │   ,\_(   ).   +12(10) °C     │
│    /(___(__)   19-24 km/h   │    /(___(__)   20-23 km/h   │    /(___(__)   23-35 km/h   │    /(___(__)   20-29 km/h   │
│      ‘ ‘ ‘ ‘  5 km           │      ‘ ‘ ‘ ‘  5 km           │      ‘ ‘ ‘ ‘  10 km          │      ‘ ‘ ‘ ‘  10 km          │
│     ‘ ‘ ‘ ‘   0.5 mm | 100%  │     ‘ ‘ ‘ ‘   0.2 mm | 100%  │     ‘ ‘ ‘ ‘   0.6 mm | 100%  │     ‘ ‘ ‘ ‘   0.0 mm | 65%   │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
                                                       ┌─────────────┐
┌──────────────────────────────┬───────────────────────┤  Fri 27 Sep ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│      .-.      Light rain     │  _`/"".-.     Patchy rain ne…│     \   /     Sunny          │     \   /     Clear          │
│     (   ).    +9(7) °C       │   ,\_(   ).   +11(9) °C      │      .-.      +11(8) °C      │      .-.      +9(8) °C       │
│    (___(__)    18-26 km/h   │    /(___(__)   23-31 km/h   │   ― (   ) ―    16-24 km/h   │   ― (   ) ―    10-16 km/h   │
│     ‘ ‘ ‘ ‘   9 km           │      ‘ ‘ ‘ ‘  10 km          │      `-’      10 km          │      `-’      10 km          │
│    ‘ ‘ ‘ ‘    0.8 mm | 100%  │     ‘ ‘ ‘ ‘   0.1 mm | 100%  │     /   \     0.0 mm | 0%    │     /   \     0.0 mm | 0%    │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
                                                       ┌─────────────┐
┌──────────────────────────────┬───────────────────────┤  Sat 28 Sep ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│     \   /     Sunny          │  _`/"".-.     Patchy rain ne…│               Cloudy         │    \  /       Partly Cloudy  │
│      .-.      +11(9) °C      │   ,\_(   ).   +14(13) °C     │      .--.     13 °C          │  _ /"".-.     12 °C          │
│   ― (   ) ―    11-14 km/h   │    /(___(__)   12-14 km/h   │   .-(    ).    6-10 km/h    │    \_(   ).    5-8 km/h     │
│      `-’      10 km          │      ‘ ‘ ‘ ‘  10 km          │  (___.__)__)  10 km          │    /(___(__)  10 km          │
│     /   \     0.0 mm | 0%    │     ‘ ‘ ‘ ‘   0.1 mm | 100%  │               0.0 mm | 0%    │               0.0 mm | 0%    │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
Location: London, Greater London, England, UK [51.5073219,-0.1276474]

Follow @igor_chubin for wttr.in updates
(stderr:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
100  8977  100  8977    0     0   3495      0  0:00:02  0:00:02 --:--:--  3495
)}
Executing code block 3: {
}