Observation 类型
Litefuse 支持多种 observation 类型,为你的 span 提供更多上下文,并支持高效过滤。
可用类型
-
eventis the basic building block. An event is used to track discrete events in a trace. -
spanrepresents durations of units of work in a trace. -
generationlogs generations of AI models incl. prompts, token usage and costs. -
agentdecides on the application flow and can for example use tools with the guidance of a LLM. -
toolrepresents a tool call, for example to a weather API. -
chainis a link between different application steps, like passing context from a retriever to a LLM call. -
retrieverrepresents data retrieval steps, such as a call to a vector store or a database. -
evaluatorrepresents functions that assess relevance/correctness/helpfulness of a LLM’s outputs. -
embeddingis a call to a LLM to generate embeddings and can include model, token usage and costs -
guardrailis 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_observation 或 start_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)这个页面对你有帮助吗?