Elastic Algorithm Service (EAS) allows you to call a service over a virtual private cloud (VPC) endpoint by using the official SDK for Python, official SDK for Java, or custom call logic. This topic describes these call methods in detail.
Benefits
- The network performance overhead of service calls over the Internet is prevented, and the call speed is improved.
- Traffic costs of service calls are reduced.
Limits
- The Elastic Compute Service (ECS) instances that are used to call a service must be in the same region as the service. For example, if the service is in the China (Shanghai) region, you can send call requests only from ECS instances in the China (Shanghai) region.
- Data Science Workshop (DSW) does not allow you to call MaxCompute services over VPC endpoints.
Call methods
$ curl http://166408185518****.vpc.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_online -H 'Authorization: ZGI1YWYxNmQwYjMzMDM1YTNlMmFmNmEzYjIzZTVlZGQ0NDJhYTRm****' -d '[{"sex":0,"cp":0,"fbs":0,"restecg":0,"exang":0,"slop":0,"thal":0,"age":0,"trestbps":0,"chol":0,"thalach":0,"oldpeak":0,"ca":0}]'
- Use the official SDK for Python
EAS encapsulates the call logic and provides the official SDK for Python.
- Use the official SDK for Java
EAS encapsulates the call logic and provides the official SDK for Java.
- Use custom call logic
We recommend that you use the preceding two official SDKs to call services. This reduces the amount of time consumed to define call logic. If you need to use SDKs in other languages or use custom call logic, you can follow the demo in the "Use custom call logic" section of this topic. To implement custom call logic, you must construct service requests based on specific frameworks. For more information, see Construct a request for a TensorFlow service.
Use the official SDK for Python
- Install the SDK.
For more information about how to use the SDK for Python, visit GitHub.pip install -U eas-prediction --user
- Compile a call program.
In this example, a program that uses strings as input and output is used. For information about sample programs with other input and output formats, such as TensorFlow and PyTorch programs, visit GitHub.
The input of the#!/usr/bin/env python from eas_prediction import PredictClient from eas_prediction import StringRequest if __name__ == '__main__': # Set the input of the client = PredictClient() function to the VPC endpoint that is used to call the service. client = PredictClient('http://166408185518****.vpc.cn-hangzhou.pai-eas.aliyuncs.com','heart_predict_online') # Use the token that is obtained on the VPC Invoke tab. For more information, see the "Call methods" section of this topic. client.set_token('ZGI1YWYxNmQwYjMzMDM1YTNlMmFmNmEzYjIzZTVlZGQ0NDJhYTRm****') client.init() # Construct the request based on the model that you want to use. In this example, the input and output are of the STRING type. request = StringRequest('[{"sex":0,"cp":0,"fbs":0,"restecg":0,"exang":0,"slop":0,"thal":0,"age":0,"trestbps":0,"chol":0,"thalach":0,"oldpeak":0,"ca":0}]') for x in range(0, 1): resp = client.predict(request) print(resp)
client = PredictClient()
function is the VPC endpoint that is used to call the service. For example, if the VPC endpoint is http://166408185518****.vpc.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_online, the format that is used to call thePredictClient()
function isclient = PredictClient('http://166408185518****.vpc.cn-hangzhou.pai-eas.aliyuncs.com','heart_predict_online')
. - Run the call program.
The$ python heart_predict.py
heart_predict.py
parameter specifies the name of the Python program. You can replace it with the actual program name. The following prediction result is returned after the call program is run:IT-C02YJ0V8JHD2:Desktop wowei$ python heart_predict.py [{"p_0":0.9941226679708888,"p_1":0.005877332029111252}]
Use the official SDK for Java
- Add the required dependency.
You must use Maven to manage projects when you define code that runs on a Java client. Therefore, you must add the client dependency eas-sdk to the pom.xml file. The latest version of the dependency is 2.0.1. The following code provides an example on how to add the dependency:
For more information about how to use the SDK for Java, visit GitHub.<dependency> <groupId>com.aliyun.openservices.eas</groupId> <artifactId>eas-sdk</artifactId> <version>2.0.1</version> </dependency>
- Compile a call program.
In this example, a program that uses strings as input and output is used. For information about sample programs with other input and output formats, such as TensorFlow and PyTorch programs, visit GitHub.
The input of theimport com.aliyun.openservices.eas.predict.http.PredictClient; import com.aliyun.openservices.eas.predict.http.HttpConfig; public class Test_String { public static void main(String[] args) throws Exception{ // Start and initialize the client. PredictClient client = new PredictClient(new HttpConfig()); // Use the token that is obtained on the VPC Invoke tab. For more information, see the "Call methods" section of this topic. client.setToken("ZGI1YWYxNmQwYjMzMDM1YTNlMmFmNmEzYjIzZTVlZGQ0NDJhYTRm****"); // Set the endpoint to the VPC endpoint that is used to call the service. client.setEndpoint("http://166408185518****.vpc.cn-hangzhou.pai-eas.aliyuncs.com"); // Set the service name. client.setModelName("heart_predict_online"); // Define the input string. Construct the request based on the model that you want to use. In this example, the input and output are of the STRING type. String request = "[{\"sex\":0,\"cp\":0,\"fbs\":0,\"restecg\":0,\"exang\":0,\"slop\":0,\"thal\":0,\"age\":0,\"trestbps\":0,\"chol\":0,\"thalach\":0,\"oldpeak\":0,\"ca\":0}]"; System.out.println(request); // Return a string by using EAS. try { String response = client.predict(request); System.out.println(response); } catch(Exception e) { e.printStackTrace(); } // Shut down the client. client.shutdown(); return; } }
client.setEndpoint()
andclient.setModelName()
functions is the VPC endpoint that is used to call the service. For example, if the VPC endpoint is http://166408185518****.vpc.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_online, the format that is used to call theclient.setEndpoint()
function isclient.setEndpoint("http://166408185518****.vpc.cn-hangzhou.pai-eas.aliyuncs.com")
, and the format that is used to call theclient.setModelName()
function isclient.setModelName("heart_predict_online")
. - Run the call program.
The following result is returned after the call program is run:
[{"p_0":0.9941226679708888,"p_1":0.005877332029111252}]
Use custom call logic
import requests
# Use the VPC endpoint that is obtained on the VPC Invoke tab as the URL. For more information, see the "Call methods" section of this topic.
url = 'http://166408185518****.vpc.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_online'
# Add the token that is obtained on the VPC Invoke tab to the header. For more information, see the "Call methods" section of this topic.
headers = {"Authorization": 'ZGI1YWYxNmQwYjMzMDM1YTNlMmFmNmEzYjIzZTVlZGQ0NDJhYTRm****'}
# Construct the service request based on the data format required by the model that you want to use. In this example, the input and output are of the STRING type.
data = '[{"sex":0,"cp":0,"fbs":0,"restecg":0,"exang":0,"slop":0,"thal":0,"age":0,"trestbps":0,"chol":0,"thalach":0,"oldpeak":0,"ca":0}]'
resp = requests.post(url, data=data, headers=headers)
print resp
print resp.content