LoginSignup
0
1

【AzureML】モデルカタログからDatabricksのDollyを使ってみる

Last updated at Posted at 2023-05-29

はじめに

Microsoft Build 2023の発表で、AzureMLのアップデートがいくつか入りました。今回はHugging Faceに登録されているモデルをエンドポイントへデプロイできる『モデルカタログ』という機能を触ってみます。

デプロイするモデルはDatabricksのDollyを使ってみます。

image.png

Databricks Dolly

Dollyは、Databricksの大規模な言語モデルです。今回はその中でも「dolly-v2-12b」というモデルを使ってみます。

まずは「デプロイ」ボタンから「リアルタイム エンドポイント」を選択します。

Untitled (3).png

デプロイ時の詳細を設定します。
仮想マシンは「Standard_E16s_v3」を使用しました。ほかの設定も行ったら、「デプロイ」をクリックします。

Untitled.png

約15分後、デプロイが終わるとこのような画面になります。

image.png

「使用」タブに移り、Pythonコードをコピペすれば推論を行えます。

dolly.png

そのコードはこちらです。

import urllib.request
import json
import os
import ssl

def allowSelfSignedHttps(allowed):
    # bypass the server certificate verification on client side
    if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
        ssl._create_default_https_context = ssl._create_unverified_context

allowSelfSignedHttps(True) # this line is needed if you use self-signed certificate in your scoring service.

# Request data goes here
# The example below assumes JSON formatting which may be updated
# depending on the format your endpoint expects.
# More information can be found here:
# https://docs.microsoft.com/azure/machine-learning/how-to-deploy-advanced-entry-script
data =  {
  "input_data": {
      "input_string": ["Tell me what foods cats like."]
      }
}

body = str.encode(json.dumps(data))

url = 'https://<INPUT-YOUR-BASE>.japaneast.inference.ml.azure.com/score'
# Replace this with the primary/secondary key or AMLToken for the endpoint
api_key = '<API key>'
if not api_key:
    raise Exception("A key should be provided to invoke the endpoint")

# The azureml-model-deployment header will force the request to go to a specific deployment.
# Remove this header to have the request observe the endpoint traffic rules
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key), 'azureml-model-deployment': 'databricks-dolly-v2-12b-2' }

req = urllib.request.Request(url, body, headers)

try:
    response = urllib.request.urlopen(req)

    result = response.read()
    print(result)
except urllib.error.HTTPError as error:
    print("The request failed with status code: " + str(error.code))

    # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
    print(error.info())
    print(error.read().decode("utf8", 'ignore'))

APIキーと "input_data"の"input_string" にテキストを入力したら、実行してみます。
結果はこちらです。

b'["Cats love fish and chicken, but they have to have it hidden under the form of a food that cat is accustomed to, such as chicken grain free and frozen, or in this case, chicken and vegetable poke cake. You can switch out the chicken for shrimp, and you have a deliciously cat-friendly dinner."]'

お疲れさまでした。

現状、日本語での出力はうまくいかないようです。
AzureML上で『微調整』(ファインチューニング?)からトレーニングデータを入れるところがあるので、日本語テキストで学習させてみたいですね。

Untitled (4).png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1