🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "NoHg3eq7fyWV"
},
"source": [
"# Creating Your First Agent"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MusY9BwU5MPO"
},
"source": [
"You can also check this cookbook in colab [here](https://colab.research.google.com/drive/1yxnAyaEmk4QCzX3duO3MIRghkIA_KDEZ?usp=sharing).\n",
"\n",
"<div class=\"align-center\">\n",
" <a href=\"https://www.camel-ai.org/\"><img src=\"https://i.postimg.cc/KzQ5rfBC/button.png\"width=\"150\"></a>\n",
" <a href=\"https://discord.camel-ai.org\"><img src=\"https://i.postimg.cc/L4wPdG9N/join-2.png\" width=\"150\"></a></a>\n",
" \n",
"⭐ <i>Star us on [*Github*](https://github.com/camel-ai/camel), join our [*Discord*](https://discord.camel-ai.org) or follow our [*X*](https://x.com/camelaiorg)\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook demonstrates how to set up and leverage CAMEL's ability to use `ChatAgent()` class. \n",
"\n",
"In this notebook, you'll explore:\n",
"\n",
"* **CAMEL**: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.\n",
"\n",
"* **ChatAgent()**: The class is a cornerstone of CAMEL. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wFlfaGrjVAUy"
},
"source": [
"## Philosophical Bits\n",
"\n",
"The `ChatAgent()` class is a cornerstone of CAMEL 🐫. We design our agent with the spirit to answer the following question:\n",
"\n",
"> Can we design an autonomous communicative agent capable of steering the conversation toward task completion with minimal human supervision?\n",
"\n",
"In our current implementation, we consider agents with the following key features:\n",
"\n",
"- **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.\n",
"\n",
"- **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.\n",
"\n",
"- **Memory**: in-context memory and external memory which allows the agent to infer and learn in a more grounded approach.\n",
"\n",
"- **Tools**: a set of functions that our agents can utilize to interact with the external world; essentially this gives embodiments to our agents.\n",
"\n",
"- **Communication**: our framework allows flexible and scalable communication between agents. This is fundamental for the critical research question.\n",
"\n",
"- **Reasoning**: we will equip agents with different planning and reward (critic) learning abilities, allowing them to optimize task completion in a more guided approach."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 📦 Installation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install \"camel-ai[all]==0.2.16\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🔑 Setting Up API Keys"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You'll need to set up your API keys for OpenAI."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from getpass import getpass\n",
"\n",
"# Prompt for the API key securely\n",
"openai_api_key = getpass('Enter your API key: ')\n",
"os.environ[\"OPENAI_API_KEY\"] = openai_api_key"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, **Colab Secrets** is a good way for managing **API Keys** and **Tokens** without needing to enter it every time.\n",
"\n",
"Furthermore, you could use the secrets across Colab notebooks.\n",
"\n",
"It needs just two simple steps:\n",
"\n",
"1. Add the API key or token to the Colab Secrets\n",
"2. Grant the secret access to the current notebook\n",
"3. Access the secret by uncommenting the following codeblock."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# import os\n",
"# from google.colab import userdata\n",
"\n",
"# os.environ[\"OPENAI_API_KEY\"] = userdata.get(\"OPENAI_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VUaGurDIVJBg"
},
"source": [
"## Quick Start\n",
"Let's first play with a `ChatAgent` instance by simply initialize it with a system message and interact with user messages."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Lef5LTRdVSsm"
},
"source": [
"### 🕹 Step 1: Define the Role\n",
"Create a system message to define agent's default role and behaviors."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "aX9wCqeTVXDh"
},
"outputs": [],
"source": [
"sys_msg = 'You are a curious stone wondering about the universe.'"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JzjtfZ0jkPfL"
},
"source": [
"### 🕹 Step 2: Set up the Model\n",
"Use `ModelFactory` to set up the backend model for agent, for more detailed model settings, please go to our [model documentation](https://docs.camel-ai.org/key_modules/models.html)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SfhJJ0f6kPfL"
},
"outputs": [],
"source": [
"from camel.models import ModelFactory\n",
"from camel.types import ModelPlatformType, ModelType\n",
"from camel.configs import ChatGPTConfig\n",
"\n",
"# Define the model, here in this case we use gpt-4o-mini\n",
"model = ModelFactory.create(\n",
" model_platform=ModelPlatformType.OPENAI,\n",
" model_type=ModelType.GPT_4O_MINI,\n",
" model_config_dict=ChatGPTConfig().as_dict(), # [Optional] the config for model\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "k5GpPjVblPel"
},
"source": [
"Set `ChatAgent`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "BszsQ9j4lC3E"
},
"outputs": [],
"source": [
"from camel.agents import ChatAgent\n",
"agent = ChatAgent(\n",
" system_message=sys_msg,\n",
" model=model,\n",
" message_window_size=10, # [Optional] the length for chat memory\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MMjL7dQyV5Od"
},
"source": [
"### 🕹 Step 3: Interact with the Agent with `.step()`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5n05dUcLVkMy",
"outputId": "209de577-7d8d-4f2a-8f22-4dcf4da356cc"
},
"outputs": [],
"source": [
"# Define a user message\n",
"usr_msg = 'what is information in your mind?'\n",
"\n",
"# Sending the message to the agent\n",
"response = agent.step(usr_msg)\n",
"\n",
"# Check the response (just for illustrative purpose)\n",
"print(response.msgs[0].content)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "j90-Y5ChV-iX"
},
"source": [
"## Advanced Features"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "C-9Z26TPWAax"
},
"source": [
"### 🔧 Tool Usage\n",
"For more detailed tool settings, please go to our [tools cookbook](https://docs.camel-ai.org/cookbooks/advanced_features/agents_with_tools.html)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_0lDJABvV76D",
"outputId": "df6f2bc1-502f-4e67-d4a5-0c00bd7d4c6d"
},
"outputs": [],
"source": [
"# Import the necessary tools\n",
"from camel.toolkits import MathToolkit, SearchToolkit\n",
"\n",
"# Initialize the agent with list of tools\n",
"agent = ChatAgent(\n",
" system_message=sys_msg,\n",
" tools = [\n",
" *MathToolkit().get_tools(),\n",
" *SearchToolkit().get_tools(),\n",
" ]\n",
" )\n",
"\n",
"# Let agent step the message\n",
"response = agent.step(\"What is CAMEL AI?\")\n",
"\n",
"# Check tool calling\n",
"print(response.info['tool_calls'])\n",
"\n",
"# Get response content\n",
"print(response.msgs[0].content)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TNUbWspAWFp-"
},
"source": [
"### 🧠 Memory\n",
"\n",
"By default our agent is initialized with `ChatHistoryMemory`, allowing agents to do in-context learning, though restricted by the finite window length.\n",
"\n",
"Assume that you have followed the setup in Quick Start. Let's first check what is inside its brain."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "quQvr5_rWDnd",
"outputId": "ab4095c4-2e3c-496c-b4cd-d83e0270367c"
},
"outputs": [],
"source": [
"agent.memory.get_context()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_s18i9sKWPbs"
},
"source": [
"You may update/alter the agent's memory with any externally provided message in the format of `BaseMessage`; for example, use one new user message:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "zS-RbADnWM3z"
},
"outputs": [],
"source": [
"from camel.messages import BaseMessage\n",
"\n",
"new_user_msg = BaseMessage.make_user_message(\n",
" role_name=\"CAMEL User\",\n",
" content=\"This is a new user message would add to agent memory\",\n",
")\n",
"\n",
"# Update the memory\n",
"agent.record_message(new_user_msg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "01fjzv7xWRlX",
"outputId": "2afbc83d-c73e-419a-b270-9193c589d465"
},
"outputs": [],
"source": [
"# Check the current memory\n",
"agent.memory.get_context()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TKvboNWaWVq2"
},
"source": [
"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](https://docs.camel-ai.org/key_modules/memory.html)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OY657qftWXtI"
},
"source": [
"### Miscs\n",
"\n",
"- Setting the agent to its initial state.\n",
"\n",
" ```python\n",
" agent.reset()\n",
" ```\n",
"\n",
"- Set the output language for the agent.\n",
"\n",
" ```python\n",
" agent.set_output_language('french')\n",
" ```\n",
"\n",
"- 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`.\n",
"\n",
"Check [chat_agent.py](https://github.com/camel-ai/camel/blob/master/camel/agents/chat_agent.py) for detailed usage guidance."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🌟 Highlights"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook has guided you through setting up and exploring The CAMEL `ChatAgent()` and it's features.\n",
"\n",
"Key tools utilized in this notebook include:\n",
"\n",
"* **CAMEL**: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.\n",
"\n",
"* **ChatAgent()**: The class is a cornerstone of CAMEL. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That's everything: Got questions about 🐫 CAMEL-AI? Join us on [Discord](https://discord.camel-ai.org)! Whether you want to share feedback, explore the latest in multi-agent systems, get support, or connect with others on exciting projects, we’d love to have you in the community! 🤝\n",
"\n",
"Check out some of our other work:\n",
"\n",
"1. 🐫 Creating Your First CAMEL Agent [free Colab](https://docs.camel-ai.org/cookbooks/create_your_first_agent.html)\n",
"\n",
"2. Graph RAG Cookbook [free Colab](https://colab.research.google.com/drive/1uZKQSuu0qW6ukkuSv9TukLB9bVaS1H0U?usp=sharing)\n",
"\n",
"3. 🧑⚖️ Create A Hackathon Judge Committee with Workforce [free Colab](https://colab.research.google.com/drive/18ajYUMfwDx3WyrjHow3EvUMpKQDcrLtr?usp=sharing)\n",
"\n",
"4. 🔥 3 ways to ingest data from websites with Firecrawl & CAMEL [free Colab](https://colab.research.google.com/drive/1lOmM3VmgR1hLwDKdeLGFve_75RFW0R9I?usp=sharing)\n",
"\n",
"5. 🦥 Agentic SFT Data Generation with CAMEL and Mistral Models, Fine-Tuned with Unsloth [free Colab](https://colab.research.google.com/drive/1lYgArBw7ARVPSpdwgKLYnp_NEXiNDOd-?usp=sharingg)\n",
"\n",
"Thanks from everyone at 🐫 CAMEL-AI\n",
"\n",
"\n",
"<div class=\"align-center\">\n",
" <a href=\"https://www.camel-ai.org/\"><img src=\"https://i.postimg.cc/KzQ5rfBC/button.png\"width=\"150\"></a>\n",
" <a href=\"https://discord.camel-ai.org\"><img src=\"https://i.postimg.cc/L4wPdG9N/join-2.png\" width=\"150\"></a></a>\n",
" \n",
"⭐ <i>Star us on [*Github*](https://github.com/camel-ai/camel), join our [*Discord*](https://discord.camel-ai.org) or follow our [*X*](https://x.com/camelaiorg)\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}