Embodied Agents#
You can also check this cookbook in colab here
β Star us on Github, join our Discord or follow our X
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.16"
[2]:
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.
[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#
We first need to set up the necessary information.
[4]:
# 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.
[5]:
# 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.
[6]:
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.
[7]:
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:
[8]:
response = embodied_agent.step(usr_msg)
> Explanation:
To accomplish the tasks you've outlined, I will perform the following actions:
1. **Write a Bash script** to install NumPy using `pip`. This will ensure that the necessary library is available for the Python script that follows.
2. **Write a Python script** to compute the dot product of the two lists `[8, 9]` and `[5, 4]`. The dot product is calculated as the sum of the products of the corresponding entries of the two sequences.
3. **Write a script** to fetch the weather information for London using the `wttr.in` service. This will provide a simple way to get weather data from the command line.
### Step 1: Bash Script to Install NumPy
I'll create a Bash script named `install_numpy.sh` that will check if `pip` is installed and then use it to install NumPy.
### Step 2: Python Script for Dot Product Calculation
I'll create a Python script named `dot_product.py` that will compute the dot product of the two lists and print the result.
### Step 3: Script to Fetch Weather Information
I'll create a Bash script named `get_weather.sh` that will use `curl` to fetch the weather information for London from `wttr.in`.
Now, let's implement these actions in code.
> Code:
# Step 1: Bash script to install NumPy
echo '#!/bin/bash' > install_numpy.sh
echo 'if command -v pip &> /dev/null; then' >> install_numpy.sh
echo ' echo "Installing NumPy..."' >> install_numpy.sh
echo ' pip install numpy' >> install_numpy.sh
echo 'else' >> install_numpy.sh
echo ' echo "pip is not installed. Please install pip first."' >> install_numpy.sh
echo 'fi' >> install_numpy.sh
chmod +x install_numpy.sh
# Step 2: Python script for dot product calculation
echo 'import numpy as np' > dot_product.py
echo 'a = np.array([8, 9])' >> dot_product.py
echo 'b = np.array([5, 4])' >> dot_product.py
echo 'dot_product = np.dot(a, b)' >> dot_product.py
echo 'print("Dot product:", dot_product)' >> dot_product.py
# Step 3: Bash script to fetch weather information
echo '#!/bin/bash' > get_weather.sh
echo 'curl wttr.in/london' >> get_weather.sh
chmod +x get_weather.sh
> Explanation:
### Explanation of the Code
1. **install_numpy.sh**: This script checks if `pip` is installed and installs NumPy if it is. It provides feedback if `pip` is not found.
2. **dot_product.py**: This Python script uses NumPy to calculate the dot product of two arrays and prints the result.
3. **get_weather.sh**: This script uses `curl` to fetch and display the weather information for London from `wttr.in`.
### Next Steps
I will execute these scripts in the appropriate order to ensure that NumPy is installed before running the Python script. After that, I will fetch the weather information for London.
Let's execute the installation script first.
> Code:
./install_numpy.sh
> Explanation:
After confirming that NumPy is installed, I will run the Python script:
> Code:
python3 dot_product.py
> Explanation:
Finally, I will run the weather script:
> Code:
./get_weather.sh
> Explanation:
Now, I will perform these actions.
The following bash code will run on your computer:
# Step 1: Bash script to install NumPy
echo '#!/bin/bash' > install_numpy.sh
echo 'if command -v pip &> /dev/null; then' >> install_numpy.sh
echo ' echo "Installing NumPy..."' >> install_numpy.sh
echo ' pip install numpy' >> install_numpy.sh
echo 'else' >> install_numpy.sh
echo ' echo "pip is not installed. Please install pip first."' >> install_numpy.sh
echo 'fi' >> install_numpy.sh
chmod +x install_numpy.sh
# Step 2: Python script for dot product calculation
echo 'import numpy as np' > dot_product.py
echo 'a = np.array([8, 9])' >> dot_product.py
echo 'b = np.array([5, 4])' >> dot_product.py
echo 'dot_product = np.dot(a, b)' >> dot_product.py
echo 'print("Dot product:", dot_product)' >> dot_product.py
# Step 3: Bash script to fetch weather information
echo '#!/bin/bash' > get_weather.sh
echo 'curl wttr.in/london' >> get_weather.sh
chmod +x get_weather.sh
Running code? [Y/n]:y
The following bash code will run on your computer:
./install_numpy.sh
Running code? [Y/n]:y
The following bash code will run on your computer:
python3 dot_product.py
Running code? [Y/n]:y
The following bash code will run on your computer:
./get_weather.sh
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
100 8906 100 8906 0 0 9068 0 --:--:-- --:--:-- --:--:-- 9069
100 8906 100 8906 0 0 9068 0 --:--:-- --:--:-- --:--:-- 9069
==================
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:
[ ]:
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: {
}