🎃

Microsoft Fabric の組み込みの AI モデルを使ってみる-【感情分析編】

2024/02/06に公開

やってみること

OpenAI Python SDKを使用して、Fabric の Azure OpenAI を使用して感情分析を行う

手順

  1. Microsoft Fabric(https://app.fabric.microsoft.com/home)にアクセス
  2. 「Synapse Data Engineering」をクリック
  3. 「ワークスペース」をクリック
  4. 作業を行うワークスペースをクリック
  5. 「+新規」をクリック
  6. 「ノートブック」をクリック
  7. ノートブックが開くことを確認
  8. 下記のコードを実行し、AIサービスに接続する
# Get workload endpoints and access token

from synapse.ml.mlflow import get_mlflow_env_config
import json

mlflow_env_configs = get_mlflow_env_config()
access_token = access_token = mlflow_env_configs.driver_aad_token
prebuilt_AI_base_host = mlflow_env_configs.workload_endpoint + "cognitive/textanalytics/"
print("Workload endpoint for AI service: \n" + prebuilt_AI_base_host)

service_url = prebuilt_AI_base_host + "language/:analyze-text?api-version=2022-05-01"

# Make a RESful request to AI service

post_headers = {
    "Content-Type" : "application/json",
    "Authorization" : "Bearer {}".format(access_token)
}

def printresponse(response):
    print(f"HTTP {response.status_code}")
    if response.status_code == 200:
        try:
            result = response.json()
            print(json.dumps(result, indent=2, ensure_ascii=False))
        except:
            print(f"pasre error {response.content}")
    else:
        print(response.headers)
        print(f"error message: {response.content}")


9. 下記のコードを実行する

import requests
from pprint import pprint
import uuid

post_body = {
    "kind": "SentimentAnalysis",
    "parameters": {
        "modelVersion": "latest",
        "opinionMining": "True"
    },
    "analysisInput":{
        "documents":[
            {
                "id":"1",
                "language":"ja",
                "text": "ホテルのフロントは最悪だった。だけど、部屋はすごくよかった。"
            }
        ]
    }
} 

post_headers["x-ms-workload-resource-moniker"] = str(uuid.uuid1())
response = requests.post(service_url, json=post_body, headers=post_headers)

# Output all information of the request process
printresponse(response)

  1. 出力結果を確認
HTTP 200
{
  "kind": "SentimentAnalysisResults",
  "results": {
    "documents": [
      {
        "id": "1",
        "sentiment": "mixed",
        "confidenceScores": {
          "positive": 0.45,
          "neutral": 0.05,
          "negative": 0.5
        },
        "sentences": [
          {
            "sentiment": "negative",
            "confidenceScores": {
              "positive": 0.0,
              "neutral": 0.0,
              "negative": 1.0
            },
            "offset": 0,
            "length": 15,
            "text": "ホテルのフロントは最悪だった。",
            "targets": [
              {
                "sentiment": "negative",
                "confidenceScores": {
                  "positive": 0.01,
                  "negative": 0.99
                },
                "offset": 0,
                "length": 3,
                "text": "ホテル",
                "relations": [
                  {
                    "relationType": "assessment",
                    "ref": "#/documents/0/sentences/0/assessments/0"
                  }
                ]
              },
              {
                "sentiment": "negative",
                "confidenceScores": {
                  "positive": 0.01,
                  "negative": 0.99
                },
                "offset": 4,
                "length": 4,
                "text": "フロント",
                "relations": [
                  {
                    "relationType": "assessment",
                    "ref": "#/documents/0/sentences/0/assessments/0"
                  }
                ]
              }
            ],
            "assessments": [
              {
                "sentiment": "negative",
                "confidenceScores": {
                  "positive": 0.01,
                  "negative": 0.99
                },
                "offset": 9,
                "length": 2,
                "text": "最悪",
                "isNegated": false
              }
            ]
          },
          {
            "sentiment": "positive",
            "confidenceScores": {
              "positive": 0.9,
              "neutral": 0.1,
              "negative": 0.01
            },
            "offset": 15,
            "length": 15,
            "text": "だけど、部屋はすごくよかった。",
            "targets": [
              {
                "sentiment": "positive",
                "confidenceScores": {
                  "positive": 0.99,
                  "negative": 0.01
                },
                "offset": 19,
                "length": 2,
                "text": "部屋",
                "relations": [
                  {
                    "relationType": "assessment",
                    "ref": "#/documents/0/sentences/1/assessments/0"
                  }
                ]
              }
            ],
            "assessments": [
              {
                "sentiment": "positive",
                "confidenceScores": {
                  "positive": 0.99,
                  "negative": 0.01
                },
                "offset": 25,
                "length": 4,
                "text": "よかった",
                "isNegated": false
              }
            ]
          }
        ],
        "warnings": []
      }
    ],
    "errors": [],
    "modelVersion": "2022-11-01"
  }
}

ヘッドウォータース

Discussion