共用方式為


啟用流程部署的追蹤和收集意見反應 (預覽)

附註

此功能目前處於公開預覽。 此預覽版本沒有服務等級協定,不建議將其用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款

在生產中部署生成式 AI 應用程式之後,應用程式開發人員會嘗試增強其知識並最佳化效能。 追蹤每個要求、匯總計量和用戶意見反應的資料扮演重要角色。

在本文中,您將了解如何啟用追蹤、收集累積的指標,以及在流程部署的推斷階段接收使用者意見反饋。

必要條件

  • Azure CLI 和適用於 Azure CLI 的 Azure Machine Learning 延伸模組。 如需詳細資訊,請參閱安裝、設定和使用 CLI (v2)
  • Azure Machine Learning 工作區。 如果您沒有工作區資源,請依快速入門:建立工作區資源一文中的步驟來建立工作區資源。
  • Application Insights。 通常,Machine Learning 工作區會有預設連結的 Application Insights。 如果您想要使用新的資源,您可以建立 Application Insights 資源
  • 了解如何在提示流程中建置和測試流程
  • 對受控線上端點有基本的了解。 受控線上端點會以可調整、完全受控的方式,使用 Azure 中強大的 CPU 和 GPU 機器,讓您省去設定和管理基礎的部署基礎結構的額外負荷。 如需受控線上端點的詳細資訊,請參閱進行即時推斷所需的線上端點和部署
  • Azure 角色型存取控制 (Azure RBAC) 可用來授與 Azure Machine Learning 作業的存取權。 若要執行本文中的步驟,您必須為使用者帳戶指派 Azure Machine Learning 工作區的擁有者或參與者角色,或允許「Microsoft.MachineLearningServices/workspaces/onlineEndpoints/」的自訂角色。 如果您使用工作室來建立/管理線上端點/部署,則需要向資源群組擁有者要求其他權限「Microsoft.Resources/deployments/write」。 如需詳細資訊,請參閱管理對 Azure Machine Learning 工作區的存取

部署流程以進行即時推斷

測試您的流程後,不論是彈性流程或 DAG 流程,您可以將流程部署到生產環境。 在本文中,我們用部署流程至 Azure Machine Learning 受控線上端點作為範例。 若為彈性流程,您需要 準備 flow.flex.yaml 檔案而非 flow.dag.yaml

您也可以部署其他平台,例如 Docker 容器、Kubernetes 叢集等

附註

由於您需要使用最新提示流程部署流量,因此這會支援追蹤和反應意見集合 API。

啟用追蹤和收集部署系統計量

若您正在使用 Studio UI 進行部署,則可在部署精靈中開啟進階設定>部署步驟中的 [Application Insights 診斷],方法是將追蹤資料和系統計量收集至連結到 Application Insights 的工作區。

若您正在使用 SDK 和 CLI,您可在部署 yaml 檔案中新增將資料收集至連結到 Application Insights 工作區的屬性 app_insights_enabled: true。 您也可透過部署 yaml 檔案中的環境變數 APPLICATIONINSIGHTS_CONNECTION_STRING 指定其他 Application Insights,方式如下, 您可以在 Azure 入口網站中的概觀頁面尋找 Application Insights 的連接字串。

# below is the property in deployment yaml
# app_insights_enabled: true

# you can also use the environment variable
environment_variables:
  APPLICATIONINSIGHTS_CONNECTION_STRING: <connection_string>

附註

如果您僅設定 app_insights_enabled: true,但您的工作區沒有連結的 Application Insights,您的部署將不會失敗,但是不會收集資料。

如果您同時指定 app_insights_enabled: true 和上述環境變數,則追蹤資料和計量將會傳送至工作區連結的 Application Insights。 因此,若您想要指定不同 Application Insights,則僅需要保留環境變數。

若您部署至其他平台,則也可使用環境變數 APPLICATIONINSIGHTS_CONNECTION_STRING: <connection_string> 收集指定 Application Insights 追蹤資料和計量。

在 Application Insights 中檢視追蹤資料

執行期間的軌跡記錄特定事件或應用程式的狀態。 這可包含函式呼叫、變數值、系統事件等更多相關資料。 追蹤協助將應用程式元件細分為不同輸入和輸出,對除錯和了解應用程式相當重要。 若要深入了解,請參閱追蹤上的 OpenTelemetry 追蹤。 追蹤資料遵守 OpenTelemetry 規格

您可在指定 Application Insights 中檢視詳細追蹤。 下列螢幕擷取畫面顯示包含多個節點部署流程事件的範例。 Application Insights - > 調查 -> 交易搜尋,且您可選擇每個節點來檢視其詳細追蹤。

相依性類型事件記錄部署的呼叫。 該事件名稱是流程資料夾的名稱。 深入瞭解在 Application Insights 中的交易搜尋與診斷。

在 Application Insights 中追蹤資料的螢幕擷取畫面。

在 Application Insights 中檢視系統計量

計量名稱 類型 維度 描述
token_consumption counter - flow
- node
- llm_engine
- token_type:prompt_tokens:LLM API 輸入標記;completion_tokens:LLM API 回應標記;total_tokens = prompt_tokens + completion tokens
OpenAI 代幣使用指標
流量延遲 長條圖 流程, 回應代碼, 流媒體, 回應類型 要求執行成本,response_type 表示其是否為 full/firstbyte/lastbyte
flow_request counter 流程, 響應代碼, 異常, 串流 流量請求次數
節點延遲 長條圖 流程, 節點, 執行狀態 節點執行成本
node_request counter 流程, 節點, 異常, 運行狀態 節點執行計數
rpc_latency 長條圖 流程, 節點, API 呼叫 rpc 成本
rpc_request counter 流程, 節點, API 呼叫, 例外狀況 rpc 計數
flow_streaming_response_duration 長條圖 流程 串流回應傳送成本,從傳送第一個位元組到傳送最後一個位元組

您可以在 Azure 入口網站的工作區概覽頁面中找到工作區預設的應用程式洞察。

開啟 Application Insights,然後從左側導覽列中選取 [使用量和估計成本]。 選取 [自訂計量 (預覽)],然後選取 [使用維度],並儲存變更。

啟用多維度計量的螢幕擷取畫面。

選取左側導覽列中的 [計量] 索引標籤。 從 [計量命名空間] 選取 [promptflow 標準計量],即可從 [計量] 下拉式清單中使用不同的彙總方法來探索計量。

提示流程端點計量的螢幕擷取畫面。

收集意見反應並傳送至 Application Insights

Prompt Flow 服務提供新的/feedback API 來協助客戶收集意見反應,意見反應承載可以是任何 json 格式資料,Prompt Flow 服務只會協助客戶將意見反應資料儲存到追蹤區段。 資料將儲存至已設定的追蹤匯出工具目標客戶。 這也支援 OpenTelemetry 標準追蹤內容傳播,其表示會遵守要求標頭中設定的追蹤內容,並使用該內容做為要求父範圍內容。 您可以利用分散式追蹤功能,將意見反應追蹤與其聊天要求追蹤相互關聯。

下列範例程式碼示範如何評分已啟用追蹤的流程受控端點,並將意見反應傳送至評分要求的相同追蹤範圍。 流程具有輸入 questionchat_hisotry,以及輸出 answer。 為端點評分後,我們會收集意見反應,並傳送至部署流程時指定的 Application Insights。 您必須填入 api_key 值,或根據您的使用案例修改程序碼。

import urllib.request
import json
import os
import ssl
from opentelemetry import trace, context
from opentelemetry.baggage.propagation import W3CBaggagePropagator
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from opentelemetry.sdk.trace import TracerProvider

# Initialize your tracer
tracer = trace.get_tracer("my.genai.tracer")
trace.set_tracer_provider(TracerProvider())

# 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://learn.microsoft.com/azure/machine-learning/how-to-deploy-advanced-entry-script
data = {
    "question": "hello",
    "chat_history": []
}

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

url = 'https://basic-chat-endpoint.eastus.inference.ml.azure.com/score'
feedback_url = 'https://basic-chat-endpoint.eastus.inference.ml.azure.com/feedback'
# Replace this with the primary/secondary key, AMLToken, or Microsoft Entra ID token for the endpoint
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': 'basic-chat-deployment' }

try:
    with tracer.start_as_current_span('genai-request') as span:

        ctx = context.get_current()
        TraceContextTextMapPropagator().inject(headers, ctx)
        print(headers)
        print(ctx)
        req = urllib.request.Request(url, body, headers)
        response = urllib.request.urlopen(req)

        result = response.read()
        print(result)

        # Now you can process the answer and collect feedback
        feedback = "thumbdown"  # Example feedback (modify as needed)

        # Make another request to save the feedback
        feedback_body = str.encode(json.dumps(feedback))
        feedback_req = urllib.request.Request(feedback_url, feedback_body, headers)
        urllib.request.urlopen(feedback_req)


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'))

您可以在 Application Insights 中檢視要求的追蹤以及意見反應。

Application Insights 中要求意見反應和追蹤資料的螢幕擷取畫面。

進階使用方式:將追蹤匯出至自訂 OpenTelemetry 收集器服務

在某些情況下,您可能想將追蹤資料匯出到您已部署的 OTel 收集器服務,這可以透過設定「OTEL_EXPORTER_OTLP_ENDPOINT」來實現。 當您想要自訂我們的範圍處理邏輯和您自己的追蹤永續性目標時,請使用此匯出工具。

下一步