DEV Community

Cover image for How to Build Tools For AI Agents With Human in The Loop in Python
Edwin Kys
Edwin Kys

Posted on • Edited on

How to Build Tools For AI Agents With Human in The Loop in Python

With LangChain, you can equip your LLM agent with tools to expand its capability way beyond a simple conversational interaction. For example, you can create an LLM agent that can automatically reply to emails you receive or an agent that can write and publish a blog post on your behalf on a certain trendy subject. The potential is quite limitless.

What are tools?

In the context of LLM agents, tools are functions or methods that you define in your source code, giving the agent the ability to perform specific tasks or access external resources. These tools extend the language model, allowing it to go beyond generating text in response to a prompt. Instead, it can interact with various functions, APIs, or databases as part of its workflow.

For example, tools might include functions for:

  • Data Retrieval: Accessing information from a database or a web API.
  • Calculations: Performing computations beyond the model's capabilities.
  • Content Generation: Generating or posting content, such as drafting blog posts.
  • Task Automation: Triggering actions like sending emails or updating records.

What does it look like in code?

As of LangChain version 0.3, defining tools in Python is straightforward. Tools are typically just standard Python functions that you decorate with @tool to make them accessible to your language model agent. This allows the agent to "call" these functions as part of its decision-making process.

Here’s a basic example illustrating how to create tools in LangChain. The actual functionality within each tool function is left out for simplicity.

from langchain_core.tools import tool

@tool
def publish_blog(topic: str):
    """Write and publish a blog post based on the topic."""
    # Add code to draft and publish the blog post.
    print(f"A blog post ({topic}) is published.")

@tool
def send_email(to: str, subject: str, body: str):
    """Sends an email with to the specified recipient."""
    # Add code to send an email using an email API.
    print(f"An email is sent to {to}.")
Enter fullscreen mode Exit fullscreen mode

Once we have our tools defined, we can bind them to our language model. The binding process typically involves specifying which functions are accessible to the model and defining the input and output formats, so the language model can correctly interact with each tool. The code below shows an example of how to bind tools to your language model.

import os
from langchain_openai import ChatOpenAI

# Replace this with your OpenAI API Key.
os.environ["OPENAI_API_KEY"] = "xxx"
llm = ChatOpenAI(model="gpt-4o-mini")

tools = [publish_blog, send_email]
llm_with_tools = llm.bind_tools(tools)
Enter fullscreen mode Exit fullscreen mode

The last step is to allow LangChain to call these tools using the output generated by the language model. In practice, this often requires output validation to ensure specific parameters for each tool and parsing the relevant information, such as data inputs. But with LangChain, we can use its chain functionality to simplify this process.

from langchain_core.messages import AIMessage

def call_tools(message: AIMessage):
    tool_map = {tool.name: tool for tool in tools}
    for tool_call in message.tool_calls:
        name, args = tool_call["name"], tool_call["args"]
        tool_map[name].invoke(args)

chain = llm_with_tools | call_tools
chain.invoke("Remind elon@x.com about the Monday meeting at 9AM")
Enter fullscreen mode Exit fullscreen mode

When we execute this code, our language model will first assess the available tools it can use to accomplish the task. It will then generate a message on the required operation with its parameters and pass it to the tool caller function. This function interprets the message, identifying which tool to activate based on the LLM’s output.

Adding human approval layer

Certain tools we provided to our LLM, such as publishing a blog post or sending an email, carry significant risks. To mitigate these risks, we can integrate a human approval layer into our chain. Instead of executing these high-stakes tools immediately upon request, our agent will wait for human approval. This extra step ensures that any critical action is carefully reviewed, allowing us to maintain control and safety over our agent's actions.

In this article, we’ll be using Phantasm, an open-source platform designed for human-in-the-loop workflows for AI agents. To get started with Phantasm, we’ll be using its Docker images and Python SDK. The Docker images allow us to quickly set up its components, while the Python SDK allows us to integrate Phantasm in our AI agent's source code.

Running Phantasm

# Pull the server and the dashboard images.
docker pull ghcr.io/phantasmlabs/phantasm/dashboard:latest
docker pull ghcr.io/phantasmlabs/phantasm/server:latest

# Run the server and the dashboard.
docker run -d -p 2515:2515 ghcr.io/phantasmlabs/phantasm/dashboard:latest
docker run -d -p 2505:2505 -p 2510:2510 ghcr.io/phantasmlabs/phantasm/server:latest start
Enter fullscreen mode Exit fullscreen mode

These commands will pull the necessary Docker images and launch both the server and dashboard in the background. To start receiving approval requests from our AI agent, we must first establish a connection with the coordinator server.

  1. Open http://localhost:2515 on your browser.
  2. Click on the Add Connection button.
  3. Use localhost:2510 as the connection address.

Integrating Phantasm's Python SDK

Now that the setup is complete, the final step is to integrate our agent with Phantasm via its Python SDK. This requires installing the SDK on our local machine.

pip install phantasmpy
Enter fullscreen mode Exit fullscreen mode

With the SDK installed, our next step is to initialize it and integrate the human approval workflow logic into our tool-caller function. This involves setting up a checkpoint, so the tool can wait for human approval before invoking the tools.

from langchain_core.messages import AIMessage
from phantasmpy import Phantasm

phantasm = Phantasm()

def call_tools(message: AIMessage):
    tool_map = {tool.name: tool for tool in tools}
    for tool_call in message.tool_calls:
        name, args = tool_call["name"], tool_call["args"]

        # Added the human approval logic below.
        response = phantasm.get_approval(name=name, parameters=args)
        if response.approved:
            tool_map[name].invoke(response.parameters)
        else:
            print(f"Rejected when calling tool: {name}")
Enter fullscreen mode Exit fullscreen mode

When we run the updated code, we should see an approval request from our agent appear in the dashboard. Phantasm will relay the parameters from the AI agent, allowing us to review and modify them as needed. Once approved, our agent will invoke the tool with the parameters specified by the approvers, significantly enhancing the accuracy and effectiveness of each tool call.

Dashboard Preview

Conclusion

Equipping our AI agent with tools enables it to deliver greater impact and value to our users. However, some tools carry inherent risks, which is why human oversight is essential. I developed Phantasm to provide teams with the ability to monitor and manage their AI agents in real time to ensure high performance in production environments.

If you found this article valuable, consider supporting Phantasm to help enhance safe and effective AI deployment. Your support means a lot to an open-source developer like me 😁

GitHub logo phantasmlabs / phantasm

Toolkits to create a human-in-the-loop approval layer to monitor and guide AI agents workflow in real-time.

GitHub Discord License

Phantasm

Phantasm offers open-source toolkits that allows you to create human-in-the-loop (HITL) workflows for modern AI agents. Phantasm comes with 3 main components that work together to create a seamless HITL experience:

  • Server: Coordinating the HITL workflows between humans and AI agents.
  • Dashboard: For the human team to monitor and manage the workflows.
  • Client: A library to integrate the workflows into your AI agents.

Features

  • ✅ Fully open-source and free to use
  • ✅ Works out of the box with any AI framework or model
  • ✅ Load balancer to distribute the requests to multiple approvers (Beta)
  • ✅ Web-based dashboard to manage the approval workflows
  • ✅ Easy-to-use client libraries for popular programming languages

How It Works

Phantasm allows you to have an approval layer on top of your AI agents. This means, you're free to use any AI framework or model you see fit. By using Phantasm, you can delay…

Top comments (0)