這很重要
本文中標示為 (預覽) 的項目目前處於公開預覽狀態。 此預覽版本沒有服務等級協定,不建議將其用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款。
在生產環境中部署產生 AI 應用程式之後,您可能會想要增強瞭解並優化效能。 追蹤每個要求、匯總計量和用戶意見反應的資料扮演重要角色。
在本文中,您將瞭解如何在流程部署的推斷時間啟用追蹤、收集匯總的計量,以及收集使用者意見反應。
備註
如需以更好方式執行已部署應用程式的持續監視(非提示流程),請考慮使用 Azure AI 線上評估。
先決條件
備註
您必須針對這項功能使用 中樞型專案 。 不支援 Foundry 專案 。 請參閱 如何知道我所擁有的項目類型? 和 建立中樞型專案。
- Azure CLI 和適用於 Azure CLI 的 Azure Machine Learning 延伸模組。
- Azure AI Foundry 專案。 如果您還沒有專案,可以 建立一個專案。
- Application Insights 資源。 如果您還沒有 Application Insights 資源,您可以 建立一個。
- Azure 角色型存取控制可用來授予 Azure Machine Learning 作業的存取權。 若要執行本文中的步驟,您必須擁有所選資源群組的擁有者或參與者許可權。 如需詳細資訊,請參閱 Azure AI Foundry 入口網站中的角色型訪問控制。
部署流程以進行即時推斷
正確測試流程之後(彈性流程或 DAG 流程),您就可以在生產環境中部署流程。 在本文中,我們會使用 部署流程進行即時推斷 作為範例。 若為彈性流程,您需要 準備 flow.flex.yaml
檔案而非 flow.dag.yaml
。
您也可以 部署到其他平臺,例如 Docker 容器和 Kubernetes 叢集。
使用最新的提示流程基底映射來部署流程,使其支援追蹤和意見反應收集 API。
啟用追蹤和收集部署系統計量
如果您使用 Azure AI Foundry 入口網站進行部署,請在部署精靈中選取 [部署>Application Insights 診斷>進階設定 ]。 如此一來,追蹤數據和系統計量會被收集到與 Application Insights 相關的專案。
如果您使用 SDK 或 CLI,請在部署的 .yaml 檔案中新增 app_insights_enabled: true
屬性,以將資料收集到連結至 Application Insights 的專案。
app_insights_enabled: true
您也可透過部署 yaml 檔案中的環境變數 APPLICATIONINSIGHTS_CONNECTION_STRING
指定其他 Application Insights。 您可以在 Azure 入口網站的 [概觀 ] 頁面上找到 Application Insights 的連接字串。
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 規格。
您可以在您指定的 Application Insights 中檢視詳細追蹤。 下列螢幕快照顯示已部署流程的事件範例,其中包含多個節點。 選取 [Application Insights>調查>交易搜尋],然後選取每個節點以檢視其詳細追蹤。
相依性類型事件會記錄部署的呼叫。 事件的名稱是流程資料夾的名稱。 若要深入瞭解,請參閱 Application Insights 中的交易搜尋和診斷。
在 Application Insights 中檢視系統計量
計量名稱 | 類型 | 尺寸 | 說明 |
---|---|---|---|
token_consumption |
計數器 | - flow - node - llm_engine - token_type : prompt_tokens :LLM API 輸入令牌; completion_tokens :LLM API 回應令牌; total_tokens = prompt_tokens + completion tokens |
OpenAI 權杖取用計量。 |
flow_latency |
長條圖 |
flow 、response_code 、streaming 、response_type |
要求執行成本 response_type ,表示它是完整或第一個位元組或最後一個位元組。 |
flow_request |
計數器 |
flow 、response_code 、exception 、streaming |
流程要求計數。 |
node_latency |
長條圖 |
flow 、node 、run_status |
節點執行成本。 |
node_request |
計數器 |
flow 、node 、exception 、run_status |
節點執行計數。 |
rpc_latency |
長條圖 |
flow 、node 、api_call |
遠端程序呼叫成本。 |
rpc_request |
計數器 |
flow 、node 、api_call 、exception |
遠端程序呼叫計數。 |
flow_streaming_response_duration |
長條圖 | flow |
串流回應傳送成本,範圍從傳送第一個字節到傳送最後一個字節不等。 |
您可以在 Azure 入口網站的工作區概觀頁面上找到工作區預設 Application Insights 計量。
- 開啟 Application Insights,然後選取左窗格中的 [使用量和估計成本 ]。 選取 [自訂計量 (預覽)]>[維度],然後儲存變更。
- 選取左窗格中的 [ 計量] 索引標籤。 從 [計量命名空間] 中,選取 [promptflow 標準計量]。 您可以使用不同的匯總方法,從 [ 計量 ] 下拉式清單中探索計量。
收集意見反應並傳送至 Application Insights
提示流程服務提供新的 /feedback
API,可協助客戶收集意見反應。 意見反應承載可以是任何 JSON 格式數據。 提示流程服務可協助客戶將意見反應資料儲存至追蹤範圍。 資料會儲存至客戶設定的追蹤匯出工具目標。 提示流程服務也支援 OpenTelemetry 標準追蹤內容傳播。 它會遵守要求標頭中的追蹤內容集,並使用該內容作為要求父範圍內容。 您可以使用分散式追蹤功能,將意見反應追蹤與其聊天要求追蹤相互關聯。
下列範例程式代碼示範如何將已部署至已啟用追蹤之受控端點的流程評分,並將意見反應傳送至評分要求的相同追蹤範圍。 流程具有輸入 question
和 chat_history
。 輸出為 answer
。 在對端點進行評分之後,會收集意見反應,並依照您部署流程時所指定的傳送至 Application Insights。
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 might be updated
# depending on the format your endpoint expects.
data = {
"question": "hello",
"chat_history": []
}
body = str.encode(json.dumps(data))
url = 'https://basic-chat-endpoint-0506.eastus.inference.ml.azure.com/score'
feedback_url = 'https://basic-chat-endpoint-0506.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 中檢視要求的追蹤以及意見反應。
進階使用方式:將追蹤匯出至自訂 OpenTelemetry 收集器服務
在某些情況下,您可能會想要將追蹤數據導出至已部署的 OpenTelemetry 收集器服務。 若要開啟此服務,請設定 OTEL_EXPORTER_OTLP_ENDPOINT
。 當您想要自訂自己的範圍處理邏輯和您自己的追蹤永續性目標時,請使用此匯出工具。