Agents with Human-in-loop and Tool Approval from HumanLayer#

You can also check this cookbook in colab here

b5f2b8bc31ff4d09bc7a4f590b926355 7af5223f77844c188a6be93636817402

⭐ Star us on Github, join our Discord or follow our X

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.

In this notebook, you’ll explore:

  • CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.

  • HumanLayer: HumanLayer is an API and SDK that enables AI Agents to contact humans for feedback, input, and approvals.

  • Human-in-loop: The ability for agent to consult human during the execution of the task.

  • Human approval: The ability for agent ask approval to execute some tasks.

This cookbook demonstrates how to use HumanLayer functionality within CAMEL framework.

πŸ“¦ Installation#

First, install the CAMEL package with all its dependencies:

[ ]:
!pip install "camel-ai[all]==0.2.16"

Next, install humanlayer python SDK:

[ ]:
!pip install humanlayer

πŸ”‘ Setting Up API Keys#

Your can go to here to get API Key from OpenAI.

[ ]:
# Prompt for the API key securely
import os
from getpass import getpass

openai_api_key = getpass('Enter your API key: ')
os.environ["OPENAI_API_KEY"] = openai_api_key
Enter your API key: Β·Β·Β·Β·Β·Β·Β·Β·Β·Β·

Your can go to here to get API Key from HumanLayer.

[ ]:
humanlayer_api_key = getpass('Enter your HumanLayer API key: ')
os.environ["HUMANLAYER_API_KEY"] = humanlayer_api_key

πŸ‘¨ Tools that requires human approval#

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.

[4]:
from humanlayer.core.approval import HumanLayer
hl = HumanLayer(api_key=humanlayer_api_key, verbose=True)


# add can be called without approval
def add(x: int, y: int) -> int:
    """Add two numbers together."""
    return x + y


# but multiply must be approved by a human
@hl.require_approval()
def multiply(x: int, y: int) -> int:
    """multiply two numbers"""
    return x * y
/usr/local/lib/python3.11/dist-packages/pydantic/main.py:214: UserWarning: A custom validator is returning a value other than `self`.
Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.
See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.
  warnings.warn(

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.

Screenshot 2025-01-17 at 17.57.48.png
[6]:
from camel.toolkits import FunctionTool
from camel.agents import ChatAgent
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType

model = ModelFactory.create(
    model_platform=ModelPlatformType.OPENAI,
    model_type=ModelType.GPT_4O,
)

tools = [FunctionTool(add), FunctionTool(multiply)]

agent_with_tools = ChatAgent(
    model = model,
    tools=tools
)

# Interact with the agent
response = agent_with_tools.step("multiply 2 and 5, then add 32 to the result")
print("\n\n----------Result----------\n\n")
print(response.msgs[0].content)

WARNING:camel.agents.chat_agent:Overriding the configured tools in `BaseModelBackend` with the tools from `ChatAgent`.
HumanLayer: waiting for approval for multiply via humanlayer cloud
HumanLayer: human approved multiply


----------Result----------


The result of multiplying 2 and 5, then adding 32 to the result, is 42.

πŸ€– Human-in-loop interaction#

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:

[10]:
from camel.toolkits import HumanToolkit
human_toolkit = HumanToolkit()

agent = ChatAgent(
    system_message="You are a helpful assistant.",
    model=model,
    tools=[*human_toolkit.get_tools()],
)

response = agent.step(
    "Test me on the capital of some country, and comment on my answer."
)

print(response.msgs[0].content)
WARNING:camel.agents.chat_agent:Overriding the configured tools in `BaseModelBackend` with the tools from `ChatAgent`.
Question: What is the capital of France?
Your reply: Paris
Correct! The capital of France is indeed Paris. It's a beautiful city known for its art, fashion, and culture. Well done!

🌟 Highlights#

This notebook has guided you through setting up chat agents with the ability of Human-in-loop and Human approval.

Key tools utilized in this notebook include:

  • CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.

  • HumanLayer: HumanLayer is an API and SDK that enables AI Agents to contact humans for feedback, input, and approvals.

  • Human-in-loop: The ability for agent to consult human during the execution of the task.

  • Human approval: The ability for agent ask approval to execute some tasks.

That’s everything: Got questions about 🐫 CAMEL-AI? Join us on Discord! 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! 🀝

Check out some of our other work:

  1. 🐫 Creating Your First CAMEL Agent free Colab

  2. Graph RAG Cookbook free Colab

  3. πŸ§‘β€βš–οΈ Create A Hackathon Judge Committee with Workforce free Colab

  4. πŸ”₯ 3 ways to ingest data from websites with Firecrawl & CAMEL free Colab

  5. πŸ¦₯ Agentic SFT Data Generation with CAMEL and Mistral Models, Fine-Tuned with Unsloth free Colab

Thanks from everyone at 🐫 CAMEL-AI

8a326d5daacc47fdaa68ef894eff4d6d 6aff364fd8b447ee9d4bde141e9f68c8

⭐ Star us on Github, join our Discord or follow our X