文档

通过阿里云Python LLM SDK上报LLM Trace数据

更新时间:

阿里云自研Python LLM SDK为LLM应用提供高质量的自动埋点能力,并自动上报链路数据至可观测链路 OpenTelemetry 版。数据上报成功后,可观测链路 OpenTelemetry 版即可开始监控应用,您可以查看LLM领域的新版TraceView,更直观地分析不同操作类型的输入输出、Token消耗等信息。

重要

LLM调用链分析功能正在内测中,如需体验该功能,请加入钉钉群(群号:67690017432)获取帮助。

前提条件

获取接入点信息

  1. 登录可观测链路 OpenTelemetry 版控制台,在左侧导航栏单击接入中心

  2. 开源框架区域单击OpenTelemetry卡片。

  3. 在弹出的OpenTelemetry面板中选择数据需要上报的地域。

    说明

    初次接入的地域将会自动进行资源初始化。

  4. 选择连接方式上报方式,然后复制接入点信息。

    • 连接方式:若您的服务部署在阿里云上,且所属地域与选择的接入地域一致,推荐使用阿里云内网方式,否则选择公网方式。

    • 上报方式:根据客户端支持的协议类型选择HTTP或gRPC协议上报数据。

    image.png

背景信息

Python LLM SDK是阿里云可观测产品自研的Python语言instrumentor SDK,其基于OpenTelemetry标准实现了自动化埋点能力,支持追踪LLM应用程序。

LlamaIndex插件

通过依赖库产生的追踪信息完全兼容OpenTelemetry,您可以将追踪信息发送至可观测链路 OpenTelemetry 版然后查看相关数据。

兼容性

llama-indexaliyun-instrumentation-llama-index版本要求如下:

  • llama-index版本:≥0.10.0

  • aliyun-instrumentation-llama-index版本:≥0.0.7.dev

上报LLM数据

  1. 安装阿里云Python SDK:请加入钉钉群67690017432咨询。

  2. 为LLM应用埋点。

    重要
    • 埋点程序需要在初始化中进行,即在业务代码运行之前添加。具体上报操作,请参见官方文档

    • 请将endpointAuthentication替换为前提条件中获取的接入点和鉴权Token。

    以下Python代码用于设置AliyunLlamaIndexInstrumentor追踪llama-index,并将追踪信息上报至可观测链路 OpenTelemetry 版控制台。

    #######################################
    from aliyun.instrumentation.llama_index import AliyunLlamaIndexInstrumentor
    # 这个包内包含了透传自定义 attributes 的能力
    from aliyun.instrumentation.context.context import set_custom_attributes
    #######################################
    
    from opentelemetry import trace
    from opentelemetry.sdk.resources import Resource, SERVICE_NAME, SERVICE_VERSION,HOST_NAME
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
    from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter as OTLPSpanGrpcExporter
    
    resource = Resource(attributes={
        SERVICE_NAME: "<service-name>",
        HOST_NAME: "<host-name>"
    })
    # 使用GRPC协议上报,从接入中心获取endpoint以及header
    span_processor = BatchSpanProcessor(OTLPSpanGrpcExporter(
        endpoint="<endpoint>",
        headers=("Authentication=<token>")
    ))
    provider = TracerProvider(resource=resource)
    provider.add_span_processor(span_processor)  
    trace.set_tracer_provider(provider)
    
    # aliyun llama-index instrumentor
    AliyunLlamaIndexInstrumentor().instrument()

DashScope插件

通过依赖库产生的追踪信息完全兼容OpenTelemetry,您可以将追踪信息发送至可观测链路 OpenTelemetry 版然后查看相关数据。

上报LLM数据

  1. 安装阿里云Python SDK:请加入钉钉群67690017432咨询。

  2. 为LLM应用埋点。

    以下Python代码用于设置AliyunDashScopeInstrumentor追踪dashscope,并将追踪信息上报至可观测链路 OpenTelemetry 版控制台。

    说明

    请将xtrace_endpoint替换为前提条件中获取的接入点信息。

    # coding=utf-8
    # For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html
    import dashscope
    from dashscope import Generation
    from http import HTTPStatus
    import json
    from aliyun.instrumentation.dashscope import AliyunDashScopeInstrumentor
    from opentelemetry.sdk.resources import Resource, SERVICE_NAME, SERVICE_VERSION
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import  ConsoleSpanExporter,BatchSpanProcessor
    from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter as OTLPSpanHttpExporter
    from opentelemetry import trace
    
    resource = Resource(
        attributes={
            SERVICE_NAME: 'aliyun_llm_demo_test',
            SERVICE_VERSION: '0.0.1',
            "source": "python agent",
            # "telemetry.sdk.language": "Python",
        }
    )
    xtrace_endpoint = '<your_endpoint>'
    
    span_exporter = BatchSpanProcessor(OTLPSpanHttpExporter(
        endpoint=xtrace_endpoint,
    ))
    
    provider = TracerProvider(resource=resource)
    provider.add_span_processor(
        span_exporter
    )  # 通过 OTLPSpanExporter 上报Trace
    provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))  # 在控制台输出Trace
    trace.set_tracer_provider(provider)
    AliyunDashScopeInstrumentor().instrument()
    

OpenAI插件

上报LLM数据

  1. 安装阿里云Python SDK:请加入钉钉群67690017432咨询。

  2. 为LLM应用埋点。

    以下Python代码用于设置AliyunOpenAIInstrumentor追踪openai,并将追踪信息上报至可观测链路 OpenTelemetry 版控制台。

    说明

    请将xtrace_endpoint替换为前提条件中获取的接入点信息。

    # coding=utf-8
    # For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html
    from http import HTTPStatus
    import json
    from aliyun.instrumentation.openai import AliyunOpenAIInstrumentor
    from opentelemetry.sdk.resources import Resource, SERVICE_NAME, SERVICE_VERSION
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import  ConsoleSpanExporter,BatchSpanProcessor
    from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter as OTLPSpanHttpExporter
    from opentelemetry import trace
    
    resource = Resource(
        attributes={
            SERVICE_NAME: 'aliyun_llm_demo_test',
            SERVICE_VERSION: '0.0.1',
            "source": "python agent",
            # "telemetry.sdk.language": "Python",
        }
    )
    xtrace_endpoint = '<your_endpoint>'
    
    span_exporter = BatchSpanProcessor(OTLPSpanHttpExporter(
        endpoint=xtrace_endpoint,
    ))
    
    provider = TracerProvider(resource=resource)
    provider.add_span_processor(
        span_exporter
    )  # 通过 OTLPSpanExporter 上报Trace
    provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))  # 在控制台输出Trace
    trace.set_tracer_provider(provider)
    AliyunOpenAIInstrumentor().instrument()
    

LangChain插件

上报LLM数据

  1. 安装阿里云Python SDK:请加入钉钉群67690017432咨询。

  2. 为LLM应用埋点。

    以下Python代码用于设置AliyunLangChainInstrumentor追踪langchain,并将追踪信息上报至可观测链路 OpenTelemetry 版控制台。

    说明

    请将xtrace_endpoint替换为前提条件中获取的接入点信息。

    # coding=utf-8
    # For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html
    from http import HTTPStatus
    import json
    from aliyun.instrumentation.langchain import AliyunLangChainInstrumentor
    from opentelemetry.sdk.resources import Resource, SERVICE_NAME, SERVICE_VERSION
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import  ConsoleSpanExporter,BatchSpanProcessor
    from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter as OTLPSpanHttpExporter
    from opentelemetry import trace
    
    resource = Resource(
        attributes={
            SERVICE_NAME: 'aliyun_llm_demo_test',
            SERVICE_VERSION: '0.0.1',
            "source": "python agent",
            # "telemetry.sdk.language": "Python",
        }
    )
    xtrace_endpoint = '<your_endpoint>'
    
    span_exporter = BatchSpanProcessor(OTLPSpanHttpExporter(
        endpoint=xtrace_endpoint,
    ))
    
    provider = TracerProvider(resource=resource)
    provider.add_span_processor(
        span_exporter
    )  # 通过 OTLPSpanExporter 上报Trace
    provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))  # 在控制台输出Trace
    trace.set_tracer_provider(provider)
    AliyunLangChainInstrumentor().instrument()
    

  • 本页导读 (1)