すべてのプロダクト
Search
ドキュメントセンター

Platform For AI:パブリックエンドポイントを介したサービスの呼び出し

最終更新日:Jul 22, 2024

Elastic Algorithm Service (EAS) では、Python用の公式SDK、Java用の公式SDK、またはカスタム呼び出しロジックを使用して、共通のパブリックエンドポイントを介してサービスを呼び出すことができます。 このトピックでは、これらの呼び出し方法について詳しく説明します。

共通のパブリックエンドポイントを介したサービスの呼び出し

モデルがEASにサービスとしてデプロイされると、そのサービスのパブリックエンドポイントが自動的に生成されます。 サービスのパブリックエンドポイントを取得するには、次の手順を実行します。[Elastic Algorithm service (EAS)] ページで、呼び出すサービスを見つけ、[Service Method] 列の [Invoke Intro] をクリックします。 [イントロの呼び出し] ダイアログボックスで、[パブリックネットワークの呼び出し] タブをクリックして、[アクセスアドレス] の値と関連ドキュメントへのリンク、および [トークン] の値を確認します。 通話情報に基づいて、通話テストを実行できます。 次のコードは、呼び出しテストの例を示しています。

$curl http:// 166408185518 **** .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}]'
説明

より安全な暗号化伝送のために、httpをhttpsに置き換えることができます。

呼び出しテストが成功したら、サービスを呼び出すことができます。 サービス呼び出しを容易にするために、EASでは次のいずれかの方法を使用して、共通のパブリックエンドポイントを介してサービスを呼び出すことができます。

  • Pythonの公式SDKの使用

    EASは呼び出しロジックをカプセル化し、Pythonの公式SDKを提供します。

  • 公式SDK for Javaの使用

    EASは呼び出しロジックをカプセル化し、Javaの公式SDKを提供します。

  • カスタム呼び出しロジックの使用

    上記の2つの公式SDKを使用してサービスを呼び出すことを推奨します。 これにより、呼び出しロジックの定義にかかる時間が短縮され、呼び出しの安定性が向上します。 他の言語でSDKを使用するか、カスタム呼び出しロジックを使用する必要がある場合は、このトピックの「カスタム呼び出しロジックを使用する」セクションのデモに従うことができます。 カスタム呼び出しロジックを実装するには、特定のフレームワークに基づいてサービスリクエストを作成する必要があります。 詳細については、「TensorFlowサービスのリクエストの構築」をご参照ください。

Python用の公式SDKを使用する

  1. SDK をインストールします。

    pip install -U eas-prediction -- user

    SDK For Pythonの使用方法の詳細については、『GitHub』をご参照ください。

  2. 呼び出しプログラムをコンパイルします。

    この例では、入力と出力として文字列を使用するプログラムが使用されます。 TensorFlowやPyTorchプログラムなど、他の入出力形式のサンプルプログラムについては、GitHubをご覧ください。

    #!/usr/bin/env python
    from eas_prediction import PredictClient
    from eas_prediction import StringRequest
    if __name__ == '__main__':
        # Set the input of client = PredictClient() to the public endpoint that is used to call the service. 
        client = PredictClient('http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com','heart_predict_online')
        # Use the token that is obtained on the Public Network Invoke tab. For more information, see the "Call a service over a common public endpoint" 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() 関数の入力は、サービスを呼び出すために使用されるパブリックエンドポイントです。 たとえば、パブリックエンドポイントがhttp:// 166408185518 **** .cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_onlineの場合、PredictClient() 関数の呼び出しに使用される形式はclient = PredictClient('http:// 166408185518 **** .cn-hangzhou.pai-eas.aliyuncs.com ','heart_predict_online') です。

  3. 呼び出しプログラムを実行します。

    $ python heart_predict.py

    heart_predict.pyパラメーターは、Pythonプログラムの名前を指定します。 実際のプログラム名に置き換えることができます。 呼び出しプログラムの実行後、次の予測結果が返されます。

    IT-C02YJ0V8JHD2:Desktop wowei$ python heart_predict.py
    [{"p_0":0.9941226679708888,"p_1":0.005877332029111252}] 

Java用の公式SDKを使用する

  1. 必要な依存関係を追加します。

    Javaクライアントで実行するコードを定義するときは、Mavenを使用してプロジェクトを管理する必要があります。 したがって、クライアント依存関係eas-sdkpom.xmlファイルに追加する必要があります。 依存関係の最新バージョンは2.0.1です。 次のコードでは、依存関係を追加する方法の例を示します。

    <dependency>
      <groupId>com.aliyun.openservices.eas</groupId>
      <artifactId>eas-sdk</artifactId>
      <version>2.0.1</version>
    </dependency> 

    SDK For Javaの使用方法の詳細については、『GitHub』をご参照ください。

  2. 呼び出しプログラムをコンパイルします。

    この例では、入力と出力として文字列を使用するプログラムが使用されます。 TensorFlowやPyTorchプログラムなど、他の入出力形式のサンプルプログラムについては、GitHubをご覧ください。

    import 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 Public Network Invoke tab. For more information, see the "Call a service over a common public endpoint" section of this topic.     
            client.setToken("ZGI1YWYxNmQwYjMzMDM1YTNlMmFmNmEzYjIzZTVlZGQ0NDJhYTRm****");
            // Set the endpoint to the public endpoint that is used to call the service. 
            client.setEndpoint("166408185518****.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() およびclient.setModelName() 関数の入力は、サービスを呼び出すために使用されるパブリックエンドポイントです。 たとえば、パブリックエンドポイントがhttp:// 166408185518 **** .cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_onlineの場合、client.setEndpoint() 関数の呼び出しに使用される形式はclient.setEndpoint("166408185518 **** .cn-hangzhou.pai-eas.aliyuncs.com") で、client.setModelName() 関数の呼び出しに使用される形式はclient. modelName ("heart_predict_online") です。

  3. 呼び出しプログラムを実行します。

    呼び出しプログラムの実行後、次の結果が返されます。

    [{"p_0":0.9941226679708888,"p_1":0.005877332029111252}]

カスタム呼び出しロジックの使用

EASは、Python、Java、およびその他の言語でのカスタム呼び出しロジックをサポートします。 特定のフレームワークに基づいてサービスリクエストをカスタマイズする必要があります。 詳細については、「TensorFlowサービスのリクエストの構築」をご参照ください。 次のコードは、呼び出しロジックを定義する方法の例を示しています。

import requests
# Use the public endpoint that is obtained on the Public Network Invoke tab as the URL. For more information, see the "Call a service over a common public endpoint" section of this topic. 
url = 'http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_online'
# Add the token that is obtained on the Public Network Invoke tab to the header. For more information, see the "Call a service over a common public endpoint" 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