liveBook · Manning

Custom method

As explained in AI Agents in Action

A custom action is an assistant-configured capability that invokes an external service through a defined interface.

A custom action is an assistant-configured capability that invokes an external service through a defined interface. It connects an assistant to APIs, local code, or hosted services so the assistant can retrieve data or perform tasks. section 3.3.1 section 6.2.2

Build and connect a custom action by following these steps:

  • Define the action, endpoint, HTTP method, inputs, and response structure. section 3.3.1
  • Generate and run a FastAPI service, then inspect its Swagger documentation and OpenAPI specification. section 3.3.1
  • Expose a local service through an ngrok tunnel when the assistant requires external access. section 3.3.1
  • Paste the OpenAPI specification and server URL into the assistant’s custom-action configuration, test it, and update it whenever the API changes. section 3.3.2

Daily task organizer

A FastAPI GET endpoint at /tasks returns daily tasks as JSON. A task-organizing assistant calls the endpoint, prioritizes the results, and produces a schedule plot. Public assistants can activate exposed actions, so services handling private information or paid resources require care. section 3.3.1 section 3.3.2

Listing 6.4 playground/assistant_actions/file_actions.py
import os

from playground.actions_manager import agent_action

OUTPUT_FOLDER = "assistant_outputs"


@agent_action     #1
def save_file(filename, content):      #2
    """
    Save content to a file.      #3

    :param filename: The name of the file including extension.
    :param content: The content to save in the file.
    """
    file_path = os.path.join(OUTPUT_FOLDER, filename)
    with open(file_path, "w", encoding="utf-8") as file:
        file.write(content)
    print(f"File '{filename}' saved successfully.")      #4

Shows a local Playground action defined with an agent_action decorator, function parameters, documentation, and file-writing behavior. section 6.2.2

How AI Agents in Action covers it

How deep it goes
Explains endpoint design, FastAPI implementation, OpenAPI configuration, testing, tunneling, and maintenance without detailing action internals.
Examples
Uses a daily-tasks FastAPI endpoint, a task-organizing assistant, a file-saving Playground action, and a mock news API exercise.
What it assumes
Assumes basic API familiarity for alternative methods while teaching endpoint design, FastAPI setup, OpenAPI use, and action registration.
What you can do after
Build it from scratch by defining, running, documenting, exposing, configuring, and testing an assistant-connected service.
How it connects
Connects APIs, FastAPI, OpenAPI, ngrok, assistants, agents, tools, and function documentation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action. The section links point to the parts of the book it draws on.