用上下文管理器捕获自动埋点跨度

介绍 OpenInference 自动埋点场景下用 capture_span_context 上下文管理器捕获代码块内全部跨度的方法,说明跨度层级结构,并给出获取首个与末个跨度的用法。

假设您正在使用 `openinference-instrumentation>=0.1.34` 当使用自动通过仪器的跨度时 [OpenInference](<https://github.com/Arize-ai/openinference>) 在您的 LLM 应用程序中,您常常需要抓取跨度上下文来应用反馈或说明。 `capture_span_context` 上下文管理器提供了一种方便的方法,可以捕捉其范围内的所有OpenInference跨度,从而更容易对下游操作中的特定跨度应用反馈. `capture_span_context` 上下文管理器允许您 : - 捕获在特定代码块内创建的所有跨度 - 检索背景,供日后在反馈系统中使用 - 保持间距创建和注释逻辑之间的干净分隔 - 将反馈应用到跨度, 而不需要手动跟踪跨度 ID ## 了解拓扑地形学 当您的 LLM 应用程序执行时, 它会创建代表不同操作的跨度等级。 例如, 当使用框架时, 您可能拥有 : ``` framework (root span) ├── span 1 (query processing) │ └── span 2 (LLM call) └── span 3 (response formatting) ``` 那个... `capture_span_context` 上下文管理器帮助您轻松访问 : - **第一个跨度**: 您操作的根距( 用于高级反馈和评价) - **最后一个跨度**: 最近的跨度创建( 通常为 LLM 最终调用, 对 LLM 特定反馈有用) - **所有间距**: 在上下文中创建的所有跨度的完整清单(可用于全面分析) ## 使用量 ### 访问第一和最后的垃圾邮件 ``` from openinference.instrumentation import capture_span_context from opentelemetry.trace.span import format_span_id from phoenix.client import Client client = Client() def process_llm_request_with_feedback(prompt: str): with capture_span_context() as capture: # This creates multiple spans in a hierarchy when using a framework response = llm.invoke("Generate a summary") # Get user feedback (simulated) user_feedback = get_user_feedback(response) # Method 1: Get first span ID (root span - good for user feedback) first_span_id = capture.get_first_span_id() if first_span_id: # Apply user feedback to the first span client.spans.add_span_annotation( annotation_name="user_feedback", annotator_kind="HUMAN", span_id=first_span_id, label=user_feedback.label, score=user_feedback.score, explanation=user_feedback.explanation ) # Method 2: Get last span ID (most recent span - often the LLM call) last_span_id = capture.get_last_span_id() if last_span_id: # Apply feedback to the most recent span client.spans.add_span_annotation( annotation_name="llm_response_quality", annotator_kind="HUMAN", span_id=last_span_id, label="helpful", score=4, explanation="The LLM provided a helpful and relevant response" ) ``` ### 何时使用 First vs Last Spans **使用第一个跨度( N)`get_first_span_id()`当:** - 增加用户对总体经验的反馈 - 记录整个请求/答复周期的评价分数 **使用最后一个跨度( N)`get_last_span_id()`当:** - 最后一个跨度代表 LLM 引用 - 您想要注释最终输出或生成步骤 - 将反馈具体应用到模型的响应质量 - 记录特定模型的衡量标准或评价 ### 与所有抓获的西班牙人合作 您也可以访问所有跨度的更复杂的注释方案 : ``` with capture_span_context() as capture: # Make LLM call (auto-instrumented) response = llm.invoke("Generate a summary") # Get all captured span contexts span_contexts = capture.get_span_contexts() # Apply different feedback logic to different spans for i, span_context in enumerate(span_contexts): span_id = format_span_id(span_context.span_id) client.spans.add_span_annotation( annotation_name="span_order", annotator_kind="CODE", span_id=span_id, label=f"span_{i}", metadata={"position": i, "total_spans": len(span_contexts)} ) ``` #### 使用多块类型工作 您可以根据其属性过滤跨度 : ``` with capture_span_context() as capture: # Make LLM call (auto-instrumented) response = llm.invoke("Generate a summary") span_contexts = capture.get_span_contexts() # Filter for specific span types llm_spans = [ ctx for ctx in span_contexts if hasattr(ctx, 'attributes') ] # Apply different feedback logic to different span types for span_context in llm_spans: apply_llm_feedback(span_context) ``` ### 资源 - [OpenInference](<https://github.com/Arize-ai/openinference>) - [凤凰网客户端文档](<https://arize-phoenix.readthedocs.io/projects/client/>) 这页有用吗? [建议编辑](<https://github.com/arize-ai/phoenix/edit/main/docs/phoenix/tracing/how-to-tracing/feedback-and-annotations/annotating-auto-instrumented-spans.mdx>)提出问题

查看原始来源