Make a request to a deployment using the Python CL¶
Requirements: 1. UbiOps account 2. You have a UbiOps project 3. You have a UbiOps deployment to which you want to make a request 4. You have an API token: - With deployment-request-user
permission on 'deployment' level - If the data type of one of your deployment input or output fields is a file, you will need the file-writer
permission on 'project' level too, or for the bucket where your file resides. 5. Install the UbiOps Python client library: pip install ubiops
When you have done the 5 steps above, you can define the following variables:
project_name = "my-ubiops-project"
deployment_name = "my-ubiops-deployment"
api_token = "Token abc123def456ghi789jkl..."
We will use these variables in the rest of this article.
Verify access¶
Verify if your UbiOps Python client library is correctly installed and whether your API token is valid:
import ubiops
client = ubiops.ApiClient(ubiops.Configuration(api_key={"Authorization": api_token}))
api = ubiops.CoreApi(client)
print(api.service_status())
Create a request¶
Define input data¶
Define the input data of your request:
data = {
"input_field_1": 1,
"input_field_2": "two",
...
}
The input fields of your deployment should map the dictionary keys of the input data.
Files in input
If you are using files in your input, you can use the file URI to pass the file as input. Have a look at files in input for more information.
Make request¶
After defining the input data of your request, we can make the actual request. This can be either direct
or batch
, read more about the request types on the requests page. How to make a request is defined for both options below.
Direct¶
Create a direct request to the default version of your deployment:
request_response = api.deployment_requests_create(
project_name=project_name, deployment_name=deployment_name, data=data
)
print(request_response)
Create a direct request to a specific version (.e.g. "v1"
) of your deployment:
request_response = api.deployment_version_requests_create(
project_name=project_name, deployment_name=deployment_name, version="v1", data=data
)
print(request_response)
If you want to retrieve the results of this request later, you will need the ID of the request:
request_id = request_response.id
Batch¶
Create a batch request to the default version of your deployment:
request_response = api.batch_deployment_requests_create(
project_name=project_name, deployment_name=deployment_name, data=[data]
)
print(request_response)
Create a batch request to a specific version (.e.g. "v1"
) of your deployment:
request_response = api.batch_deployment_version_requests_create(
project_name=project_name, deployment_name=deployment_name, version="v1", data=[data]
)
print(request_response)
Note: the input data of a batch request is a list of dictionaries.
After creating a batch request, the request will get status 'pending'. We will describe in the section below how you can retrieve the request results when the request finishes. For that step, you will need the ID of the request:
request_id = request_response[0].id
Collect request results¶
After creating a request, you might want to obtain the results later. You can find the ID(s) of the request(s) you created in request_response
, see how we defined the request_id
.
In case you made a direct request to a deployment, you could already find the results of your request in the result
attribute of request_response
. However, after creating a batch request to a deployment, you retrieve a response with status
'pending' and with no result
yet. In both cases, you can retrieve the request results (again) using the ID.
NOTE: The request retention mode of your deployment version must be set to 'full' (default) to be able to retrieve the results of a request.
Use the ID (request_id
) of the request you want to retrieve.
Collect the results for the default version of your deployment:
request_results = api.deployment_requests_get(
project_name=project_name, deployment_name=deployment_name, request_id=request_id
)
print(request_results)
Collect the results for a specific version (.e.g. "v1"
) of your deployment:
request_results = api.deployment_version_requests_get(
project_name=project_name, deployment_name=deployment_name, version="v1", request_id=request_id
)
print(request_results)
Files in output
If you are using files in your output, retrieving the results is a bit different. Have a look at this page for help.