Decorator: @ivcap_lambda¶
The ivcap_lambda decorator registers a Python function as a set of HTTP endpoints on the FastAPI application.
ivcap_lambda(path_prefix, *, opts=ToolOptions(), context=None)
¶
Add a few routes to the service for use with an AI tool.
The tool itself is implemented in worker_fn where the first
argument is a pydantic model describing all the tool's "public" parameters.
The function is also expected to return it's result as a single pydantic model.
The tool function can have two optioanl paramters, one with the same type as
context and the second one with fastapi.Request. The context paramter will
be identical to the above context, while the request will be the incoming
request.
This function then sets up three endpoints: - POST {path_prefix}: To request the execution of the tool (the 'job') - GET {path_prefix}/{job_id}: To collect the result of a tool execution - GET {path_prefix}: To obtain a description of the tool suitable for most agent frameworks
The POST request will only wait opts.max_wait_time for the tool to finish. If it
hasn't finished by then, a 204 No Content code will be returned with additional
header fields Location and Retry-later to inform the caller where and approx.
when the result can be collected later.
The opts parameter allows for customization of the endpoints. See ToolOptions
for a more detailed description.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_prefix
|
str
|
The path prefix to use for this set of endpoints |
required |
opts
|
Optional[ToolOptions]
|
Additional behaviour settings. Defaults to ToolOptions(). |
ToolOptions()
|
context
|
Optional[ExecutionContext]
|
An optional context to be provided to every invocation of |
None
|
Source code in ivcap_lambda/decorators.py
Related: add_tool_api_route¶
The lower-level function used by @ivcap_lambda when you need to register a route programmatically (e.g. in a factory pattern):
add_tool_api_route(app, path_prefix, worker_fn, *, opts=ToolOptions(), context=None)
¶
Add a few routes to app for use with an AI tool.
The tool itself is implemented in worker_fn where the first
argument is a pydantic model describing all the tool's "public" parameters.
The function is also expected to return it's result as a single pydantic model.
The tool function can have two optioanl paramters, one with the same type as
context and the second one with fastapi.Request. The context paramter will
be identical to the above context, while the request will be the incoming
request.
This function then sets up three endpoints: - POST {path_prefix}: To request the execution of the tool (the 'job') - GET {path_prefix}/{job_id}: To collect the result of a tool execution - GET {path_prefix}: To obtain a description of the tool suitable for most agent frameworks
The POST request will only wait opts.max_wait_time for the tool to finish. If it
hasn't finished by then, a 204 No Content code will be returned with additional
header fields Location and Retry-later to inform the caller where and approx.
when the result can be collected later.
The opts parameter allows for customization of the endpoints. See ToolOptions
for a more detailed description.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI context |
required |
path_prefix
|
str
|
The path prefix to use for this set of endpoints |
required |
worker_fn
|
Callable[[BaseModel, Optional[ExecutionContext], Optional[Response]], BaseModel]
|
description |
required |
opts
|
Optional[ToolOptions]
|
Additional behaviour settings. Defaults to ToolOptions(). |
ToolOptions()
|
context
|
Optional[ExecutionContext]
|
An optional context to be provided to every invocation of |
None
|