Single inference REST requests
The single inference syntax is suited for use cases where you want predictions about one customer or event per API call.
Example syntax for calling each of your deployments can be found in the API Endpoints tab in Modelbit.
Making requests
Suppose our deployment's main function is example_doubler
:
def example_doubler(foo: int):
return 2 * foo
In this example we'll double the number 10
with example_doubler
:
- modelbit.get_inference
- requests
- curl
Calling example_doubler
with modelbit.get_inference
:
import modelbit
modelbit.get_inference(
deployment="example_doubler",
workspace="<YOUR_WORKSPACE>",
region="<YOUR_REGION>",
data=10)
# return value
{"data": 20 }
Calling example_doubler
with the requests
package:
import json, requests
requests.post("https://<your-workspace-url>/v1/example_doubler/latest",
headers={"Content-Type":"application/json"},
data=json.dumps({"data": 10})).json()
# return value
{"data": 20 }
Calling example_doubler
with curl
:
curl -s -XPOST "https://<your-workspace-url>/v1/example_doubler/latest" -d '{"data": 10}'
# return value
'{"data": 20}'
The response is the return value of the inference function.
Request format
For functions with a single argument like in the example above, the request format is just that argument. If your function has multiple arguments, send them in a list. Here are some examples calling functions expecting different kinds of arguments:
Dictionary arguments
This deployment expects a dictionary:
def my_deployment(d: Dict[str, float]):
...
To send {"my_key": 25}
as the input, send the dictionary as the data
parameter:
- modelbit.get_inference
- requests
- curl
import modelbit
modelbit.get_inference(..., data={"my_key": 25})
import json, requests
requests.post("https://...", data=json.dumps({"data": {"my_key": 25}})).json()
curl -s -XPOST "https://..." -d '{"data": {"my_key": 25}}'
Multiple arguments
This deployment expects several arguments:
def my_deployment(a: int, b: str, c: float):
...
To send 1, "two", 3.0
as the input, send them as a list in the data
parameter:
- modelbit.get_inference
- requests
- curl
import modelbit
modelbit.get_inference(..., data=[1, "two", 3.0])
import json, requests
requests.post("https://...", data=json.dumps({"data": [1, "two", 3.0]})).json()
curl -s -XPOST "https://..." -d '{"data": [1, "two", 3.0]}'
List arguments
If your deployment expects a list of values as a single input, you'll need to use the batch syntax to differentiate this request from the earlier example. This syntax is a bit more complicated.
To simplify your function's API format, change your function to accept a dictionary instead of a list. Then send a dictionary where one key is the list of values to use with your model.
This deployment expects a list of numbers:
def my_deployment(my_list: List[int]):
...
To send [1, 2, 3]
using batch syntax we'll next this single-inference in a list-of-lists, with an ID. We'll use 0
as the ID for this input (it can be any value).
- modelbit.get_inference
- requests
- curl
import modelbit
my_list = [1, 2, 3]
modelbit.get_inference(..., data=[[0, my_list]])
import json, requests
my_list = [1, 2, 3]
requests.post("https://...", data=json.dumps({"data": [[0, my_list]]})).json()
curl -s -XPOST "https://..." -d '{"data": [[0, [1, 2, 3]]]}'