Python API
Everything here is importable from aaax, as in from aaax import Strategy. The guide pages explain how the pieces fit; come here for exact parameters and defaults.
Strategies
Strategy
Strategy(name: str, *, description: str = '', resources: list[StrategyResource] | tuple[StrategyResource, ...] = (), runner: Handler | None = None, metadata: Mapping[str, Any] | None = None)
Encapsulate package resources and expose one application-level boundary.
store
property
writable
store: StrategyStore | None
The channel store behind this strategy's channel resources, if any.
A :class:StrategyStore: it opens the underlying store on first use.
Assigning to it is the same as calling :meth:attach_store.
runner
runner(fn: Handler) -> Handler
Decorator or setter for the strategy's main run handler.
add_resource
add_resource(resource: StrategyResource, *, handler: Handler | None = None) -> 'Strategy'
Add a resource, as it is; handler makes it callable.
A channel resource without a handler is backed by :attr:store, see
:meth:attach_store.
add_tactic
add_tactic(tactic: Tactic[Any, Any], *, name: str | None = None, ref: str | None = None, description: str | None = None, metadata: Mapping[str, Any] | None = None) -> 'Strategy'
Mount an lllm tactic; it is served at /tactics/<name>/.
A tactic mounted under another name, or with another ref or
description, is served as a copy that has them: replies and
errors name the route, /info gives the description and the ref
as package_ref, and requests carry the ref as tactic_ref.
tactic itself is left as it is. A name LLLM cannot hold (one
with ;) keeps the tactic's own name where it is served, and a
ref that is not a tactic ref (lllm://org/package/tactics/name)
labels only the record.
attach_store
attach_store(store: Any) -> 'Strategy'
Back channel resources with a store; an sssn one is served at /network/.
A strategy has one store. Every channel resource without a handler is
backed by the store channel of the same name, created in the store the
first time the store is used. Packages that declare channels share the
strategy's store (see :func:aaax.add_package), so attach your
store before mounting them. A store object for the folder of a package
store is used as the strategy's store if the strategy has not opened
that folder yet (it does when it is served or a channel is used).
Attaching the store the strategy already has, or None to a strategy
without a store, does nothing. Attaching another store, another store
object for a folder the strategy has opened, or None to a strategy
that has a store raises ValueError: a store is never replaced or
dropped silently.
tactics
tactics() -> dict[str, Tactic[Any, Any]]
Tactic resources as lllm tactics, by local name.
Each is named after its resource (unless LLLM cannot hold the name,
one with ;). A tactic object is returned as-is, or as a copy
when it was mounted under another name, description, or ref (a
tactic ref); the copy has them. A package tactic is loaded on first
use and named after its resource; it keeps its code's description,
examples, and package_ref, taking the manifest's only when the
code has none. A plain handler function is wrapped with
:func:lllm.as_tactic, with the resource's description and ref. A
handler that takes no input is called without it, as the strategy
calls its other handlers.
resource
resource(kind: ResourceKind, name: str, *, ref: str | None = None, entrypoint: str | None = None, description: str = '', metadata: Mapping[str, Any] | None = None, handler: Handler | None = None) -> 'Strategy'
Add a resource of kind; handler makes it callable.
A tactic resource added with a handler and no description is
described as the tactic it serves: by a tactic's own description, or
by a function's docstring. A package tactic, loaded on first use, is
not imported to tell.
use_package
use_package(path: str | Path, *, prefix: str | None = None, store: str | Path | None = None, bind: bool = True) -> 'Strategy'
Import resources from an LLLM hub package manifest.
invoke_resource
async
invoke_resource(name: str, input_value: Any = None, *, context: Mapping[str, Any] | CallContext | None = None) -> Any
Call the resource name; what context means depends on it.
- A tactic resource with a handler runs through its tactic exactly as
POST /tactics/<name>/runruns it:contexthas the shape of a :class:~lllm.protocol.CallContext({"metadata": {...}, "tags": {...}, "request_id": ...}), or is one, and the tactic receives aCallContext. A plain-function handler is wrapped with :func:lllm.as_tactic, so it gets aCallContexton both routes if it takescontext; one that takes no input is called without. - Any other resource with a handler receives
contextas a plain dict (aTactichandler gets that dict asCallContext.metadata). - A channel resource without a handler is served by :attr:
store:input_valueholds anop, one ofappend,query(the default), orinfo, and that op's fields. - Any other resource without a handler echoes the resource record, the input, and the context.
Raises :class:ResourceNotFoundError, a KeyError, for unknown names.
StrategyResource
Bases: BaseModel
A module or resource encapsulated by a strategy.
StrategyInfo
Bases: BaseModel
Public description of a strategy.
ResourceNotFoundError
Bases: KeyError
Raised when a strategy has no resource with the requested name.
Packages
add_package
add_package(strategy: Strategy, path: str | Path, *, prefix: str | None = None, store: str | Path | None = None, bind: bool = True) -> Strategy
Add package resources and local handlers to an existing strategy.
Resource names are <prefix>.<name> with a prefix. A package whose
names clash with resources already in the strategy is rejected before
anything is added; mount it with a prefix to keep the names apart. Each
record takes its description from the manifest only, since package code
is not imported to describe it. A served tactic keeps its code's
description, examples, and package_ref, and takes the manifest's
only when the code has none.
With bind (the default), tactics get handlers that import the package
on first use, and channels are backed by the strategy's one store under
their resource names, so prefixed packages never share a channel. If the
strategy has no store yet, the package opens one at store (default
<package>/.lllm/network); packages mounted later share it, and a
store other than the strategy's raises ValueError. The store is
opened, and the channels created, on first use, never while building or
inspecting. bind=False adds metadata only.
A relative path is relative to the working directory, except while
:func:aaax.load_strategy loads a strategy file (its top level and its
build_strategy()): then every relative package path, given by the
file or by helper code it calls, is relative to the file's folder, as if
built from Path(__file__).parent. AAAX 0.3 looked in the working
directory, so a path that leads to a package (a manifest file, or a
folder with an lllm.toml) only from there, or to a different package
from each place, still uses the working directory's, with a
FutureWarning: a future minor release looks in the file's folder
only.
strategy_from_package
strategy_from_package(path: str | Path, *, name: str | None = None, prefix: str | None = None, store: str | Path | None = None, bind: bool = True) -> Strategy
Create a strategy from one LLLM hub package folder.
A relative path is relative to the working directory, or to a
strategy file's folder while :func:aaax.load_strategy loads the file,
as :func:add_package explains.
load_strategy
load_strategy(target: str | Path) -> Strategy
Load a Strategy from module:attr, a .py file, or a directory.
While a strategy file runs, its build_strategy() included, every
relative package path given to use_package, add_package, or
strategy_from_package, by the file or by helper code it calls, is
resolved against the file's folder; see :func:aaax.add_package.
Serving
create_strategy_app
create_strategy_app(strategy: Strategy, *, allow_cross_site_requests: bool = False) -> FastAPI
Expose a strategy as a FastAPI application.
Building the app serves the strategy: it opens the strategy's store, which
creates the declared channels, and mounts an sssn store (a package store
included) at /network/. That needs sssn 0.2 or newer: without it, a
strategy with an sssn store raises ImportError. A store that is not an
sssn store is not mounted, and needs no sssn.
A tactic named with { or }, which can mark parameters in
routes, cannot be served: building the app raises ValueError.
The app refuses the cross-site requests a page on another site can make
a visitor's browser send without asking first: a POST whose body is not
JSON, and a GET or HEAD to a mounted tactic's custom endpoints, which
run its code. allow_cross_site_requests=True serves them, for an app
that browsers on other origins must reach; it is passed on to LLLM's
tactic services when they take it.
StrategyRunRequest
Bases: BaseModel
Request envelope for strategy calls.
The strategy's routes read a body by the rules of LLLM's tactic routes:
an object with input or task (used when input is missing or
null), and at most context besides, is this envelope; any other body
is the input itself.
StrategyRunResponse
Bases: BaseModel
Response envelope for strategy calls.