核心可观测性功能Observation 类型

Observation 类型

Litefuse 支持多种 observation 类型,为你的 span 提供更多上下文,并支持高效过滤。

可用类型

  • event is the basic building block. An event is used to track discrete events in a trace.
  • span represents durations of units of work in a trace.
  • generation logs generations of AI models incl. prompts, token usage and costs.
  • agent decides on the application flow and can for example use tools with the guidance of a LLM.
  • tool represents a tool call, for example to a weather API.
  • chain is a link between different application steps, like passing context from a retriever to a LLM call.
  • retriever represents data retrieval steps, such as a call to a vector store or a database.
  • evaluator represents functions that assess relevance/correctness/helpfulness of a LLM’s outputs.
  • embedding is a call to a LLM to generate embeddings and can include model, token usage and costs
  • guardrail is a component that protects against malicious content or jailbreaks.

如何使用 Observation 类型

与 agent 框架的集成 会自动设置 observation 类型。例如,在 langchain 中给方法标记 @tool 会自动把 Litefuse 的 observation 类型设为 tool

你也可以在 Langfuse SDK 中手动为应用设置 observation 类型。在创建 observation 时把 as_type 参数(Python)或 asType 参数(TypeScript)设置为想要的 observation 类型即可。

Observation 类型需要 Python SDK version>=3.3.1

使用 @observe 装饰器:

from langfuse import observe
 
# Agent workflow
@observe(as_type="agent")
def run_agent_workflow(query):
    # Agent reasoning and tool orchestration
    return process_with_tools(query)
 
# Tool calls
@observe(as_type="tool")
def call_weather_api(location):
    # External API call
    return weather_service.get_weather(location)

调用 start_as_current_observationstart_observation 方法:

from langfuse import get_client
 
langfuse = get_client()
 
# Start observation with specific type
with langfuse.start_as_current_observation(
    as_type="embedding",
    name="embedding-generation"
) as obs:
    embeddings = model.encode(["text to embed"])
    obs.update(output=embeddings)
 
# Start observation with specific type
transform_span = langfuse.start_observation(
    as_type="chain",
    name="transform-text"
)
transformed_text = transform_text(["text to transform"])
transform_span.update(output=transformed_text)
这个页面对你有帮助吗?