【AzureML プロンプトフロー】Bring your own Data QnA

# はじめに
今回はAzureMLのプロンプトフローを試してみます。
サンプルフローはいくつか出ていますが、その中でも「Bring your own Data QnA」を使います。

# Bring Your Own Data QnA
このプロンプトフローでは、GPT3.5やインデックス化したファイルを使用したQ&Aのフローです。インデックス化については、Vector Searchを使って関連性の高い情報を抽出して回答を生成できます。

まず、プロンプトフローを「作成」して、「Bring Your Own Data QnA」を選択して始めます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/8d98aaa9-6d5e-c292-11ec-b8e4c4d0516f.png)

## Azure OpenAIの接続
はじめにプロンプトフローではLLM接続が必要となってくるため、すでにデプロイしたAzure OpenAIのモデルに接続します。

「接続」タブの「作成」から下記の必要な項目を入力してください。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/c4b36b53-42bf-da9e-95c4-3ab23a04f3da.png)


## Runtimeの作成
続いてプロンプトフローを実行するにあたり、Runtimeの作成を行う必要があります。

「ランタイム」タブをクリックし、「作成」をすると「マネージドオンラインエンドポイントデプロイ」と「コンピューティングインスタンス」のどちらかを選択できるようになります。

今回は推奨されている「コンピューティングインスタンス」でRuntimeを作成します。

![無題.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/1a51eb0b-bf72-2018-9b1a-855965c065f5.png)

「AzureMLコンピューティングインスタンスを作成する」をクリックし、

![無題.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/6bb0623f-5384-fc02-24aa-15496258f5a1.png)

「コンピューティング名」や仮想マシンのサイズを選びます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/d1feec1e-78ec-42ed-72f8-f6021f1efebc.png)


数分待てば、コンピューティングインスタンスが作成されるので、完了したら「ランタイム名」を入力し、Runtimeを作成します。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/b7229694-d2b1-056c-3482-70e058b9369a.png)

## 処理の流れ

サンプルを作成すると、次のような画面になり、「入力」と「出力」でユーザーの質問を記載できるようになっています。

デフォルトの「入力」では、「How to use SDK V2?」という質問文がすでにあるので、今回はこれを試して出力結果を見てみます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/4442d1f5-dc2f-59ff-db2b-28890c0045e5.png)

処理の流れはこのようになっています。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/2aba3ad5-be8d-166d-b614-ad5b26eac6af.png)
1つずつ流れや入力・出力を見ていきます。

・**embed_the_question**
質問を埋め込みベクトル化するためのフローです。LLMの接続とデプロイしたモデルを入力します。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/8261502f-6aaa-735f-10eb-21a12c31d2ff.png)

このとき入力は以下のようになります。
```python:入力
{
  "connection":"XXX"
  "deployment_name":"text-embedding-ada-002"
  "model":undefined
  "input":"How to use SDK V2?"
}
```
これで質問文がベクトル化されます。

・**search_question_from_indexed_docs**
Vector Searchを使って先ほどベクトル化された質問から関連のあるドキュメントを高速に検索します。

今回はすでにVector Searchでベクトル化されたドキュメントを使いますが、もし自身のファイルを使いたい場合は、プロンプトフローを作成する前にベクターインデックスの作成を行ってください。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/b8ce4ee3-89cb-ef3d-3590-c359bb3f538f.png)

インデックス化されたドキュメントへのパスと先ほどベクトル化された質問文をクエリとして入力します。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/112da497-16a4-d280-d130-770cae5a7511.png)


出力は下記のようにファイルの名前とタイトルとURL、そして関連のあるテキストが返されます。

```python:出力
[
  0:{
    "system_metrics":{
      "duration":0.074332
    }
    "output":[
      0:{
      "metadata":{
        "chunk_hash":"d35b41093c8ed75d0658839ede403e4beea441e04cea487bada48fc413976028"
        "extension":".md"
        "markdown_heading":{
          "heading":"[Python SDK](tab/python)"
          "level":1
        }
        "mtime":1683074438
        "source":{
          "filename":"how-to-auto-train-image-models.md"
          "title":"Set up AutoML for computer vision"
          "url":"https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md"
        }
        "source_doc_id":"how-to-auto-train-image-models.md3"
        "stats":{
          "chars":1274
          "lines":36
          "tiktokens":407
        }
      }
      "original_entity":NULL
      "score":0.4166283905506134
      "text":"title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # [Python SDK](#tab/python) [!INCLUDE [sdk v2](../../includes/machine-learning-sdk-v2.md)] * An Azure Machine Learning workspace. To create the workspace, see [Create workspace resources](quickstart-create-resources.md). * The Azure Machine Learning Python SDK v2 installed. To install the SDK you can either, * Create a compute instance, which automatically installs the SDK and is pre-configured for ML workflows. For more information, see [Create and manage an Azure Machine Learning compute instance](how-to-create-manage-compute-instance.md). * Use the following commands to install Azure ML Python SDK v2: * Uninstall previous preview version: ```python pip uninstall azure-ai-ml ``` * Install the Azure ML Python SDK v2: ```python pip install azure-ai-ml ``` --- "
      "vector":NULL
      }
      1:{
        "metadata":{
          "chunk_hash":"e00de731f1c4eaf62d75fa303f2c166320e82f21ede931cc967d6b47ddf13250"
          "extension":".md"
          "markdown_heading":{
            "heading":"Set up AutoML to train computer vision models"
            "level":1
          }
          "mtime":1683074438
          "source":{
            "filename":"how-to-auto-train-image-models.md"
            "title":"Set up AutoML for computer vision"
            "url":"https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md"
          }
          "source_doc_id":"how-to-auto-train-image-models.md0"
          "stats":{
            "chars":1410
            "lines":25
            "tiktokens":376
          }
        }
      "original_entity":NULL
      "score":0.44228315353393555
      "text":"title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # Set up AutoML to train computer vision models [!INCLUDE [dev v2](../../includes/machine-learning-dev-v2.md)] > [!div class="op_single_selector" title1="Select the version of Azure Machine Learning you are using:"] > * [v1](v1/how-to-auto-train-image-models-v1.md) > * [v2 (current version)](how-to-auto-train-image-models.md) In this article, you learn how to train computer vision models on image data with automated ML with the Azure Machine Learning CLI extension v2 or the Azure Machine Learning Python SDK v2. Automated ML supports model training for computer vision tasks like image classification, object detection, and instance segmentation. Authoring AutoML models for computer vision tasks is currently supported via the Azure Machine Learning Python SDK. The resulting experimentation trials, models, and outputs are accessible from the Azure Machine Learning studio UI. [Learn more about automated ml for computer vision tasks on image data](concept-automated-ml.md). "
      "vector":NULL
      }
    ]
  }
  1:{...}
  2:{...}
]
```

・**generate_prompt_context**
出力された検索結果を受け取り、一定の形式で返すようここで変換します。

```python:generate_prompt_context
from typing import List
from promptflow import tool
from embeddingstore.core.contracts import SearchResultEntity


@tool
def generate_prompt_context(search_result: List[dict]) -> str:
    def format_doc(doc: dict):
        return f"Content: {doc['Content']}\nSource: {doc['Source']}"

    SOURCE_KEY = "source"
    URL_KEY = "url"

    retrieved_docs = []
    for item in search_result:

        entity = SearchResultEntity.from_dict(item)
        content = entity.text or ""

        source = ""
        if entity.metadata is not None:
            if SOURCE_KEY in entity.metadata:
                if URL_KEY in entity.metadata[SOURCE_KEY]:
                    source = entity.metadata[SOURCE_KEY][URL_KEY] or ""

        retrieved_docs.append({
            "Content": content,
            "Source": source
        })
    doc_string = "\n\n".join([format_doc(doc) for doc in retrieved_docs])
    return doc_string
```

ここでの出力は以下の通りになります。
```python:出力
[
  0:{
    "system_metrics":{
      "duration":0.000801
    }
    "output":"Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # [Python SDK](#tab/python) [!INCLUDE [sdk v2](../../includes/machine-learning-sdk-v2.md)] * An Azure Machine Learning workspace. To create the workspace, see [Create workspace resources](quickstart-create-resources.md). * The Azure Machine Learning Python SDK v2 installed. To install the SDK you can either, * Create a compute instance, which automatically installs the SDK and is pre-configured for ML workflows. For more information, see [Create and manage an Azure Machine Learning compute instance](how-to-create-manage-compute-instance.md). * Use the following commands to install Azure ML Python SDK v2: * Uninstall previous preview version: ```python pip uninstall azure-ai-ml ``` * Install the Azure ML Python SDK v2: ```python pip install azure-ai-ml ``` --- Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # Set up AutoML to train computer vision models [!INCLUDE [dev v2](../../includes/machine-learning-dev-v2.md)] > [!div class="op_single_selector" title1="Select the version of Azure Machine Learning you are using:"] > * [v1](v1/how-to-auto-train-image-models-v1.md) > * [v2 (current version)](how-to-auto-train-image-models.md) In this article, you learn how to train computer vision models on image data with automated ML with the Azure Machine Learning CLI extension v2 or the Azure Machine Learning Python SDK v2. Automated ML supports model training for computer vision tasks like image classification, object detection, and instance segmentation. Authoring AutoML models for computer vision tasks is currently supported via the Azure Machine Learning Python SDK. The resulting experimentation trials, models, and outputs are accessible from the Azure Machine Learning studio UI. [Learn more about automated ml for computer vision tasks on image data](concept-automated-ml.md). Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md"
  }
  1:{
    "system_metrics":{
      "duration":0.001013
    }
    "output":"Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # [Python SDK](#tab/python) [!INCLUDE [sdk v2](../../includes/machine-learning-sdk-v2.md)] * An Azure Machine Learning workspace. To create the workspace, see [Create workspace resources](quickstart-create-resources.md). * The Azure Machine Learning Python SDK v2 installed. To install the SDK you can either, * Create a compute instance, which automatically installs the SDK and is pre-configured for ML workflows. For more information, see [Create and manage an Azure Machine Learning compute instance](how-to-create-manage-compute-instance.md). * Use the following commands to install Azure ML Python SDK v2: * Uninstall previous preview version: ```python pip uninstall azure-ai-ml ``` * Install the Azure ML Python SDK v2: ```python pip install azure-ai-ml ``` --- Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # Set up AutoML to train computer vision models [!INCLUDE [dev v2](../../includes/machine-learning-dev-v2.md)] > [!div class="op_single_selector" title1="Select the version of Azure Machine Learning you are using:"] > * [v1](v1/how-to-auto-train-image-models-v1.md) > * [v2 (current version)](how-to-auto-train-image-models.md) In this article, you learn how to train computer vision models on image data with automated ML with the Azure Machine Learning CLI extension v2 or the Azure Machine Learning Python SDK v2. Automated ML supports model training for computer vision tasks like image classification, object detection, and instance segmentation. Authoring AutoML models for computer vision tasks is currently supported via the Azure Machine Learning Python SDK. The resulting experimentation trials, models, and outputs are accessible from the Azure Machine Learning studio UI. [Learn more about automated ml for computer vision tasks on image data](concept-automated-ml.md). Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md"
  }
  2:{
    "system_metrics":{
    "duration":0.000895
    }
    "output":"Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # [Python SDK](#tab/python) [!INCLUDE [sdk v2](../../includes/machine-learning-sdk-v2.md)] * An Azure Machine Learning workspace. To create the workspace, see [Create workspace resources](quickstart-create-resources.md). * The Azure Machine Learning Python SDK v2 installed. To install the SDK you can either, * Create a compute instance, which automatically installs the SDK and is pre-configured for ML workflows. For more information, see [Create and manage an Azure Machine Learning compute instance](how-to-create-manage-compute-instance.md). * Use the following commands to install Azure ML Python SDK v2: * Uninstall previous preview version: ```python pip uninstall azure-ai-ml ``` * Install the Azure ML Python SDK v2: ```python pip install azure-ai-ml ``` --- Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # Set up AutoML to train computer vision models [!INCLUDE [dev v2](../../includes/machine-learning-dev-v2.md)] > [!div class="op_single_selector" title1="Select the version of Azure Machine Learning you are using:"] > * [v1](v1/how-to-auto-train-image-models-v1.md) > * [v2 (current version)](how-to-auto-train-image-models.md) In this article, you learn how to train computer vision models on image data with automated ML with the Azure Machine Learning CLI extension v2 or the Azure Machine Learning Python SDK v2. Automated ML supports model training for computer vision tasks like image classification, object detection, and instance segmentation. Authoring AutoML models for computer vision tasks is currently supported via the Azure Machine Learning Python SDK. The resulting experimentation trials, models, and outputs are accessible from the Azure Machine Learning studio UI. [Learn more about automated ml for computer vision tasks on image data](concept-automated-ml.md). Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md"
  }
]
```

・**Prompt_variants**
これまでの出力から得られたテキストに沿ってユーザーからの質問に答えるように、プロンプトを出力します。

```python:プロンプト
system:
You are an AI assistant that helps users answer questions given a specific context. You will be given a context and asked a question based on that context. Your answer should be as precise as possible and should only come from the context.
Please add citation after each sentence when possible in a form "(Source: citation)". 

user:
{{contexts}} 
Human: {{question}} 
AI:
```

出力は以下のとおりになります。
```python:出力
[
  0:{
    "system_metrics":{
      "duration":0.002254
    }
    "output":"system: You are an AI assistant that helps users answer questions given a specific context. You will be given a context and asked a question based on that context. Your answer should be as precise as possible and should only come from the context. Please add citation after each sentence when possible in a form "(Source: citation)". user: Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # [Python SDK](#tab/python) [!INCLUDE [sdk v2](../../includes/machine-learning-sdk-v2.md)] * An Azure Machine Learning workspace. To create the workspace, see [Create workspace resources](quickstart-create-resources.md). * The Azure Machine Learning Python SDK v2 installed. To install the SDK you can either, * Create a compute instance, which automatically installs the SDK and is pre-configured for ML workflows. For more information, see [Create and manage an Azure Machine Learning compute instance](how-to-create-manage-compute-instance.md). * Use the following commands to install Azure ML Python SDK v2: * Uninstall previous preview version: ```python pip uninstall azure-ai-ml ``` * Install the Azure ML Python SDK v2: ```python pip install azure-ai-ml ``` --- Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # Set up AutoML to train computer vision models [!INCLUDE [dev v2](../../includes/machine-learning-dev-v2.md)] > [!div class="op_single_selector" title1="Select the version of Azure Machine Learning you are using:"] > * [v1](v1/how-to-auto-train-image-models-v1.md) > * [v2 (current version)](how-to-auto-train-image-models.md) In this article, you learn how to train computer vision models on image data with automated ML with the Azure Machine Learning CLI extension v2 or the Azure Machine Learning Python SDK v2. Automated ML supports model training for computer vision tasks like image classification, object detection, and instance segmentation. Authoring AutoML models for computer vision tasks is currently supported via the Azure Machine Learning Python SDK. The resulting experimentation trials, models, and outputs are accessible from the Azure Machine Learning studio UI. [Learn more about automated ml for computer vision tasks on image data](concept-automated-ml.md). Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md Human: How to use SDK V2? AI:"
  }
  1:{
    "system_metrics":{
      "duration":0.002464
    }
    "output":"system: You are an AI assistant that helps users answer questions based on a specific context. You will be given a context and asked a question based on that context. Your answer should be as precise as possible and should only come from the context. Please add citation after each sentence when possible in a form "(Source: citation)". user: Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # [Python SDK](#tab/python) [!INCLUDE [sdk v2](../../includes/machine-learning-sdk-v2.md)] * An Azure Machine Learning workspace. To create the workspace, see [Create workspace resources](quickstart-create-resources.md). * The Azure Machine Learning Python SDK v2 installed. To install the SDK you can either, * Create a compute instance, which automatically installs the SDK and is pre-configured for ML workflows. For more information, see [Create and manage an Azure Machine Learning compute instance](how-to-create-manage-compute-instance.md). * Use the following commands to install Azure ML Python SDK v2: * Uninstall previous preview version: ```python pip uninstall azure-ai-ml ``` * Install the Azure ML Python SDK v2: ```python pip install azure-ai-ml ``` --- Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # Set up AutoML to train computer vision models [!INCLUDE [dev v2](../../includes/machine-learning-dev-v2.md)] > [!div class="op_single_selector" title1="Select the version of Azure Machine Learning you are using:"] > * [v1](v1/how-to-auto-train-image-models-v1.md) > * [v2 (current version)](how-to-auto-train-image-models.md) In this article, you learn how to train computer vision models on image data with automated ML with the Azure Machine Learning CLI extension v2 or the Azure Machine Learning Python SDK v2. Automated ML supports model training for computer vision tasks like image classification, object detection, and instance segmentation. Authoring AutoML models for computer vision tasks is currently supported via the Azure Machine Learning Python SDK. The resulting experimentation trials, models, and outputs are accessible from the Azure Machine Learning studio UI. [Learn more about automated ml for computer vision tasks on image data](concept-automated-ml.md). Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md Human: How to use SDK V2? AI:"
  }
  2:{
    "system_metrics":{
      "duration":0.002212
    }
    "output":"system: You are an AI assistant for helping users answering question given a specific context.You are given a context and you'll be asked a question based on the context.Your answer should be as precise as possible and answer should be only from the context. Please add citation after each sentence when possible in a form "(Source: citation)". user: Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # [Python SDK](#tab/python) [!INCLUDE [sdk v2](../../includes/machine-learning-sdk-v2.md)] * An Azure Machine Learning workspace. To create the workspace, see [Create workspace resources](quickstart-create-resources.md). * The Azure Machine Learning Python SDK v2 installed. To install the SDK you can either, * Create a compute instance, which automatically installs the SDK and is pre-configured for ML workflows. For more information, see [Create and manage an Azure Machine Learning compute instance](how-to-create-manage-compute-instance.md). * Use the following commands to install Azure ML Python SDK v2: * Uninstall previous preview version: ```python pip uninstall azure-ai-ml ``` * Install the Azure ML Python SDK v2: ```python pip install azure-ai-ml ``` --- Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md Content: title: Set up AutoML for computer vision titleSuffix: Azure Machine Learning description: Set up Azure Machine Learning automated ML to train computer vision models with the CLI v2 and Python SDK v2. services: machine-learning author: swatig007 ms.author: swatig ms.reviewer: ssalgado ms.service: machine-learning ms.subservice: automl ms.custom: event-tier1-build-2022, ignite-2022 ms.topic: how-to ms.date: 07/13/2022 # Set up AutoML to train computer vision models [!INCLUDE [dev v2](../../includes/machine-learning-dev-v2.md)] > [!div class="op_single_selector" title1="Select the version of Azure Machine Learning you are using:"] > * [v1](v1/how-to-auto-train-image-models-v1.md) > * [v2 (current version)](how-to-auto-train-image-models.md) In this article, you learn how to train computer vision models on image data with automated ML with the Azure Machine Learning CLI extension v2 or the Azure Machine Learning Python SDK v2. Automated ML supports model training for computer vision tasks like image classification, object detection, and instance segmentation. Authoring AutoML models for computer vision tasks is currently supported via the Azure Machine Learning Python SDK. The resulting experimentation trials, models, and outputs are accessible from the Azure Machine Learning studio UI. [Learn more about automated ml for computer vision tasks on image data](concept-automated-ml.md). Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md Human: How to use SDK V2? AI:"
  }
]
```

・**answer_the_question_with_context**
先ほど出力されたプロンプトをChatGPTを通して回答を得ます。

LLM接続が必要なので、すでにデプロイされたAzureOpenAIのモデルを選択します。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2976188/8b20f115-18aa-a936-fcae-71e6f25a5983.png)

## 実行結果
それでは「How to use SDK V2?」という質問に対し、どんな結果が得られるか試してみましょう。
これまでのフローは「実行」をクリックすると自動ですべて実行されます。

下記が実行結果です。次のような回答が返ってきました。

```python:出力
[
  0:{
    "system_metrics":{
      "completion_tokens":141
      "duration":13.574824
      "prompt_tokens":779
      "total_tokens":920
    }
    "output":"To use the Azure Machine Learning Python SDK v2, you firstly need to have it installed. You can either install it by creating a compute instance, which automatically installs the SDK and it's pre-configured for machine learning workflows, or by using specific python commands. If you choose the second method, make sure to uninstall previous versions (pip uninstall azure-ai-ml) before installing the v2 version (pip install azure-ai-ml). Once installed, you can use the SDK to set up Azure Machine Learning automated ML to train computer vision models (Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md)."
  }
  1:{
    "system_metrics":{
      "completion_tokens":126
      "duration":6.81716
      "prompt_tokens":780
      "total_tokens":906
    }
    "output":"To use SDK V2, you first need to have the Azure Machine Learning Python SDK v2 installed. You can either create a compute instance, which automatically installs the SDK and is pre-configured for machine learning workflows. Alternatively, you can manually install the SDK with the following commands: - First, uninstall any previous preview version using "pip uninstall azure-ai-ml" - Then, install the Azure ML Python SDK v2 using "pip install azure-ai-ml". (Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md)"
  }
  2:{
    "system_metrics":{
      "completion_tokens":195
      "duration":16.507695
      "prompt_tokens":781
      "total_tokens":976
    }
    "output":"To use the Azure Machine Learning Python SDK v2, you first need to install it. This can be done in one of two ways: 1. Create a compute instance, which automatically installs the SDK and is pre-configured for machine learning workflows. For more information, you can refer to the guide on how to create and manage an Azure Machine Learning compute instance. 2. You can manually install Azure ML Python SDK v2 by first uninstalling any previous preview version using the command `pip uninstall azure-ai-ml` and then installing the new version using `pip install azure-ai-ml`. After that, you can use this SDK to set up and train automated machine learning models for computer vision tasks. This can be done either with the Azure Machine Learning CLI extension v2 or the Azure Machine Learning Python SDK v2(Source: https://github.com/prakharg-msft/azureml-tutorials/blob/main//how-to-auto-train-image-models.md)."
  }
]
```
Vector Searchを使ってドキュメントから質問内容に沿った回答を出力することができました。