你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

为流部署启用跟踪并收集反馈

重要

本文介绍的某些功能可能仅在预览版中提供。 此预览版未提供服务级别协议,不建议将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款

在生产环境中部署生成式 AI 应用程序后,你可能希望增强自己的理解并优化性能。 每个请求的跟踪数据、聚合指标和用户反馈发挥着关键作用。

本文介绍如何在流部署的推理时间内启用跟踪、收集聚合指标以及收集用户反馈。

先决条件

  • Azure CLI 和安装到 Azure CLI 的 Azure 机器学习扩展。
  • AI Studio 项目。 如果你没有项目,可在此处创建一个项目
  • Application Insights。 如果你没有 Application Insights 资源,可在此处创建一个资源
  • Azure 基于角色的访问控制用于授予对 Azure 机器学习中的操作的访问权限。 若要执行本文中的步骤,你必须对所选资源组具有“所有者”或“参与者”权限。 有关详细信息,请参阅 Azure AI Studio 中基于角色的访问控制

部署实时推理流

正确测试流(无论是 Flex 流还是 DAG 流)后,可以在生产环境中部署该流。 在本文中,我们将使用“部署实时推理流”作为示例。 对于 flex 流,需要准备 flow.flex.yaml 文件而不是 flow.dag.yaml

还可以部署到其他平台,例如 Docker 容器、Kubernetes 群集等

注意

需要使用最新的提示流基础映像来部署流,以支持跟踪和反馈收集 API。

为部署启用跟踪并收集系统指标

如果使用 Studio UI 进行部署,则可以在部署向导的“高级设置”>“部署”步骤中启用“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 counter - 流
- node
- llm_engine
- token_type:prompt_tokens:LLM API 输入令牌;completion_tokens:LLM API 响应令牌;total_tokens = prompt_tokens + completion tokens
OpenAI 令牌消耗指标
flow_latency histogram flow、response_code、streaming、response_type 请求执行成本,response_type 表示是否为 full/firstbyte/lastbyte
flow_request counter flow、response_code、exception、streaming 流请求计数
node_latency histogram flow、node、run_status 节点执行成本
node_request counter 流、节点、异常、run_status 节点执行计数
rpc_latency histogram 流、节点、api_call rpc 成本
rpc_request counter 流、节点、api_call、异常 rpc 计数
flow_streaming_response_duration histogram flow 流式处理响应发送成本,从发送第一个字节到发送最后一个字节

可以在 Azure 门户的工作区概述页中找到工作区默认 Application Insights。

  1. 打开 Application Insights,然后从左侧导航中选择“使用情况和预估成本”。 选择“自定义指标(预览版)”,然后选择“使用维度”并保存更改。
  2. 在左侧导航中选择“指标”选项卡。 从“指标命名空间”中选择“promptflow 标准指标”,然后可以使用不同的聚合方法从“指标”下拉列表中浏览指标。

收集反馈并发送到 Application Insights

提示流服务提供了新的 /feedback API 来帮助客户收集反馈,反馈有效负载可以是任何 json 格式的数据,PF 服务只是帮助客户将反馈数据保存到跟踪范围。 数据将保存到目标客户配置的跟踪导出程序。 它还支持 OpenTelemetry 标准跟踪上下文传播,表示它将遵循请求头中设置的跟踪上下文并将其用作请求父范围上下文。 可以利用分布式跟踪功能将反馈跟踪与其聊天请求跟踪相关联。

以下示例代码演示如何对已部署的托管终结点启用跟踪的流进行评分,并将反馈发送到评分请求的同一跟踪范围。 该流具有输入 questionchat_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 may 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 收集器服务

在某些情况下,你可能希望将跟踪数据导出到已部署的 OTel 收集器服务(通过设置“OTEL_EXPORTER_OTLP_ENDPOINT”启用)。 当你想要自定义自己的范围处理逻辑和自己的跟踪持久目标时,请使用此导出程序。