{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "ymsq1Lw0VEqT" }, "source": [ "# Agents with Human-in-loop and Tool Approval from HumanLayer" ] }, { "cell_type": "markdown", "metadata": { "id": "7V3aV16AmY0K" }, "source": [ "You can also check this cookbook in colab [here](https://colab.research.google.com/drive/1WF1Z6Ev6kTrifRLXXTTOZz6-QVRuj1uX?usp=sharing)" ] }, { "cell_type": "markdown", "metadata": { "id": "OkOJ--aQqJQK" }, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": { "id": "G5gE04UuPUWj" }, "source": [ "This notebook demonstrates how to set up and leverage CAMEL's ability to interact with user (for approval or comments) during the execution of the tasks.\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", "* **HumanLayer**: HumanLayer is an API and SDK that enables AI Agents to contact humans for feedback, input, and approvals.\n", "* **Human-in-loop**: The ability for agent to consult human during the execution of the task.\n", "* **Human approval**: The ability for agent ask approval to execute some tasks.\n", "\n", "This cookbook demonstrates how to use **HumanLayer** functionality within CAMEL framework." ] }, { "cell_type": "markdown", "metadata": { "id": "0J0_iW-YVcq2" }, "source": [ "## 📦 Installation" ] }, { "cell_type": "markdown", "metadata": { "id": "7p-JjpyNVcCT" }, "source": [ "First, install the CAMEL package with all its dependencies:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "0GXs2pruU9Vl" }, "outputs": [], "source": [ "!pip install \"camel-ai[all]==0.2.16\"" ] }, { "cell_type": "markdown", "metadata": { "id": "rnlxVTLs4spt" }, "source": [ "Next, install humanlayer python SDK:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "uEijqcLt4v1b" }, "outputs": [], "source": [ "!pip install humanlayer" ] }, { "cell_type": "markdown", "metadata": { "id": "lfNvFbhD6o8B" }, "source": [ "## 🔑 Setting Up API Keys" ] }, { "cell_type": "markdown", "metadata": { "id": "mf9iO_Haua-7" }, "source": [ "Your can go to [here](https://openai.com/index/openai-api/) to get API Key from OpenAI." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZhauVXPOua-8", "outputId": "f371a5dd-d8f9-4972-e666-b87cfc58453e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your API key: ··········\n" ] } ], "source": [ "# Prompt for the API key securely\n", "import os\n", "from getpass import getpass\n", "\n", "openai_api_key = getpass('Enter your API key: ')\n", "os.environ[\"OPENAI_API_KEY\"] = openai_api_key" ] }, { "cell_type": "markdown", "metadata": { "id": "rdMup8dLuEJH" }, "source": [ "Your can go to [here](https://app.humanlayer.dev/auth) to get API Key from HumanLayer." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "qk-JofPGufux" }, "outputs": [], "source": [ "humanlayer_api_key = getpass('Enter your HumanLayer API key: ')\n", "os.environ[\"HUMANLAYER_API_KEY\"] = humanlayer_api_key" ] }, { "cell_type": "markdown", "metadata": { "id": "NEUciNquON9_" }, "source": [ "## 👨 Tools that requires human approval" ] }, { "cell_type": "markdown", "metadata": { "id": "6f64VOMMP93d" }, "source": [ "In this section, we'll demonstrate how to define tools for Camel agent to use, and use **HumanLayer** to make some tools require human approval. First define two functions for agent to use, one of them requires human approval." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "m14JurOf6mQr", "outputId": "3b853e66-6a3f-495e-ab99-f95b135476c2" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.11/dist-packages/pydantic/main.py:214: UserWarning: A custom validator is returning a value other than `self`.\n", "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n", "See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.\n", " warnings.warn(\n" ] } ], "source": [ "from humanlayer.core.approval import HumanLayer\n", "hl = HumanLayer(api_key=humanlayer_api_key, verbose=True)\n", "\n", "\n", "# add can be called without approval\n", "def add(x: int, y: int) -> int:\n", " \"\"\"Add two numbers together.\"\"\"\n", " return x + y\n", "\n", "\n", "# but multiply must be approved by a human\n", "@hl.require_approval()\n", "def multiply(x: int, y: int) -> int:\n", " \"\"\"multiply two numbers\"\"\"\n", " return x * y" ] }, { "cell_type": "markdown", "metadata": { "id": "iTWKZwKp7UjO" }, "source": [ "Next we define the CAMEL agents, then run the computation commands. Here we will need to login HumanLayer cloud platform to approve for the agent to use the multiply function." ] }, { "cell_type": "markdown", "metadata": { "id": "5KsiJlRisi4g" }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3cW3hj3e7Y-1", "outputId": "167038a2-65c1-403e-f511-b576ebba6ba7" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING:camel.agents.chat_agent:Overriding the configured tools in `BaseModelBackend` with the tools from `ChatAgent`.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "HumanLayer: waiting for approval for multiply via humanlayer cloud\n", "HumanLayer: human approved multiply\n", "\n", "\n", "----------Result----------\n", "\n", "\n", "The result of multiplying 2 and 5, then adding 32 to the result, is 42.\n" ] } ], "source": [ "from camel.toolkits import FunctionTool\n", "from camel.agents import ChatAgent\n", "from camel.models import ModelFactory\n", "from camel.types import ModelPlatformType, ModelType\n", "\n", "model = ModelFactory.create(\n", " model_platform=ModelPlatformType.OPENAI,\n", " model_type=ModelType.GPT_4O,\n", ")\n", "\n", "tools = [FunctionTool(add), FunctionTool(multiply)]\n", "\n", "agent_with_tools = ChatAgent(\n", " model = model,\n", " tools=tools\n", ")\n", "\n", "# Interact with the agent\n", "response = agent_with_tools.step(\"multiply 2 and 5, then add 32 to the result\")\n", "print(\"\\n\\n----------Result----------\\n\\n\")\n", "print(response.msgs[0].content)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Sx706ii3-PN2" }, "source": [ "## 🤖 Human-in-loop interaction" ] }, { "cell_type": "markdown", "metadata": { "id": "d1qi4mdb-d6P" }, "source": [ "Sometimes we want the agent to ask user during the working process, in this case, we can equip agent with human toolkits, and be able to ask human via console. This example demonstrates the human-in-loop function:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xyujYUzN_M8Y", "outputId": "bbec9944-ecd9-42ee-d26e-27920a24b823" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING:camel.agents.chat_agent:Overriding the configured tools in `BaseModelBackend` with the tools from `ChatAgent`.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Question: What is the capital of France?\n", "Your reply: Paris\n", "Correct! The capital of France is indeed Paris. It's a beautiful city known for its art, fashion, and culture. Well done!\n" ] } ], "source": [ "from camel.toolkits import HumanToolkit\n", "human_toolkit = HumanToolkit()\n", "\n", "agent = ChatAgent(\n", " system_message=\"You are a helpful assistant.\",\n", " model=model,\n", " tools=[*human_toolkit.get_tools()],\n", ")\n", "\n", "response = agent.step(\n", " \"Test me on the capital of some country, and comment on my answer.\"\n", ")\n", "\n", "print(response.msgs[0].content)" ] }, { "cell_type": "markdown", "metadata": { "id": "flYNal6-R4yR" }, "source": [ "## 🌟 Highlights" ] }, { "cell_type": "markdown", "metadata": { "id": "SmkXhy4JR726" }, "source": [ "This notebook has guided you through setting up chat agents with the ability of **Human-in-loop** and **Human approval**.\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", "* **HumanLayer**: HumanLayer is an API and SDK that enables AI Agents to contact humans for feedback, input, and approvals.\n", "* **Human-in-loop**: The ability for agent to consult human during the execution of the task.\n", "* **Human approval**: The ability for agent ask approval to execute some tasks.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "7EZd8sutqorD" }, "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", "" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }