ActiveWorkflow Agent Python
Helps you to develop your own ActiveWorkflow agents in Python.
The activeworkflow_agent
library helps you to write your own ActiveWorkflow
agents in Python using ActiveWorkflow's remote agent API.
“Remote” in this context means that agents run in separate processes
from ActiveWorkflow itself. Communication between agents and ActiveWorkflow
takes place via HTTP. Each agent is effectively an HTTP service or microservice
which ActiveWorkflow connects to and interacts with as long as it supports the
remote agent API protocol.
#
InstallationThe library is available on PyPI:
Python >= 3.7 is supported.
#
The Agent API ProtocolActiveWorkflow talks to an agent over HTTP and all the requests and responses are in JSON. All communication happens using a single endpoint (the agent's registered URL) and an agent has to implement and handle the following 3 “methods”:
- register: called to retrieve an agent's metadata (name, description, options).
- receive: called when an agent has to process a message.
- check: called on schedule (as set by ActiveWorkflow users) and has no message to process.
For more details please see the remote agent API protocol.
#
Library FunctionalityTo deal with the protocol in your agent implementation this library offers four helper classes that are described below.
#
RegisterResponseRegisterResponse
is a helper class to construct a response to the
'register' method API call. It has the
following methods:
to_dict()
: returns the response as a dict.to_json()
: returns the response as a JSON string.
#
CheckResponseCheckResponse
is helper class to construct a response to
the 'check' method API call.
add_logs(*str)
: add one or more log messages to the response object.add_errors(*str)
: add one or more error messages to the response object.add_messages(*dict)
: add the list of messages that the agent emits.add_memory(dict)
: add the agent's memory (state) to the response object.to_dict()
: returns the response as a dict.to_json()
: returns the response as a JSON string.
#
ReceiveResponseReceiveResponse
is a helper class to construct a response to the
'receive' method API call.
add_logs(*str)
: add one or more log messages to the response object.add_errors(*str)
: add one or more error messages to the response object.add_messages(*dict)
: add the list of messages that the agent emits.add_memory(dict)
: add the agent's memory (state) to the response object.to_dict()
: returns the response as a dict.to_json()
: returns the response as a JSON string.
#
ParsedRequestParsedRequest
is a helper class to parse the content of a request
from the ActiveWorkflow Agent API. A ParsedRequest object
always has the following attributes:
- method: a string containing the method called on the agent.
- options: a dictionary with all the agent options.
- memory: a dictionary with the state of the agent.
- credentials: a list of dictionaries containing credentials that the agent might need.
- message: a dictionary containing the message the agent received.
#
Example AgentBelow you can find an example of an ActiveWorkflow agent using the library helper classes.
#
Running an Agent Locally for DevelopmentSuppose that for development purposes you have created a simple agent that can
run locally on http://0.0.0.0:5000
and you also run ActiveWorkflow using Docker.
You would typically proceed in the following manner:
- You would first start your agent process at
http://0.0.0.0:5000
. - You would then start ActiveWorkflow locally with Docker, registering your
agent by passing an environment variable. For example:
docker run -e REMOTE_AGENT_URL="http://host.docker.internal:5000/" -p 3000:3000 --rm automaticmode/active_workflow
. - When starting, ActiveWorkflow would make a request to your agent at
http://0.0.0.0:5000
with the 'register' method, in order to retrieve your agent's information. (see the register method for the details). - You would then log in to ActiveWorkflow, go to 'create a new agent' panel, select the agent with the name and description provided as a response to the 'register' method, and you would create an instance of this agent. You or other users could create multiple instances of this agent, possibly with different options (if your agent is configurable).
- If you schedule your agent instance to be invoked periodically, then
ActiveWorkflow would make a request to
http://0.0.0.0:5000
at the scheduled time with the method 'check'. - Every time your agent receives a message from another agent, ActiveWorkflow would call it with the method 'receive' passing the message along.
#
Source CodeYou can find the activeworkflow_agent source code on GitHub.