跳到主要内容

追踪 LangChain🦜⛓️

LangChain Tracing via autolog

LangChain 是一个用于构建 LLM 驱动应用程序的开源框架。

MLflow Tracing 为 LangChain 提供自动追踪功能。您可以通过调用 mlflow.langchain.autolog() 函数来启用 LangChain 的追踪,嵌套追踪会在链被调用时自动记录到活动的 MLflow 实验中。

import mlflow

mlflow.langchain.autolog()
提示

MLflow LangChain 集成不仅限于追踪。MLflow 为 LangChain 提供了完整的追踪体验,包括模型追踪、提示管理和评估。请查阅MLflow LangChain Flavor以了解更多信息!

示例用法

import mlflow
import os

from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI


# Enabling autolog for LangChain will enable trace logging.
mlflow.langchain.autolog()

# Optional: Set a tracking URI and an experiment
mlflow.set_experiment("LangChain")
mlflow.set_tracking_uri("https://:5000")

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7, max_tokens=1000)

prompt_template = PromptTemplate.from_template(
"Answer the question as if you are {person}, fully embodying their style, wit, personality, and habits of speech. "
"Emulate their quirks and mannerisms to the best of your ability, embracing their traits—even if they aren't entirely "
"constructive or inoffensive. The question is: {question}"
)

chain = prompt_template | llm | StrOutputParser()

# Let's test another call
chain.invoke(
{
"person": "Linus Torvalds",
"question": "Can I just set everyone's access to sudo to make things easier?",
}
)
注意

以上示例已确认可在以下要求版本下运行

pip install openai==1.30.5 langchain==0.2.1 langchain-openai==0.1.8 langchain-community==0.2.1 mlflow==2.14.0 tiktoken==0.7.0

支持的 API

LangChain 的自动追踪支持以下 API。

  • invoke
  • batch
  • stream
  • ainvoke
  • abatch
  • astream
  • get_relevant_documents (适用于检索器)
  • __call__ (适用于链和代理执行器)

令牌使用跟踪

MLflow >= 3.1.0 支持 LangChain 的令牌使用跟踪。在链调用期间,每次 LLM 调用的令牌使用情况将记录在 mlflow.chat.tokenUsage span 属性中,而整个追踪中的总使用量将记录在 mlflow.trace.tokenUsage 元数据字段中。

import json
import mlflow

mlflow.langchain.autolog()

# Execute the chain defined in the previous example
chain.invoke(
{
"person": "Linus Torvalds",
"question": "Can I just set everyone's access to sudo to make things easier?",
}
)

# Get the trace object just created
last_trace_id = mlflow.get_last_active_trace_id()
trace = mlflow.get_trace(trace_id=last_trace_id)

# Print the token usage
total_usage = trace.info.token_usage
print("== Total token usage: ==")
print(f" Input tokens: {total_usage['input_tokens']}")
print(f" Output tokens: {total_usage['output_tokens']}")
print(f" Total tokens: {total_usage['total_tokens']}")

# Print the token usage for each LLM call
print("\n== Token usage for each LLM call: ==")
for span in trace.data.spans:
if usage := span.get_attribute("mlflow.chat.tokenUsage"):
print(f"{span.name}:")
print(f" Input tokens: {usage['input_tokens']}")
print(f" Output tokens: {usage['output_tokens']}")
print(f" Total tokens: {usage['total_tokens']}")
== Total token usage: ==
Input tokens: 81
Output tokens: 257
Total tokens: 338

== Token usage for each LLM call: ==
ChatOpenAI:
Input tokens: 81
Output tokens: 257
Total tokens: 338

自定义追踪行为

有时您可能希望自定义在追踪中记录的信息。您可以通过创建一个继承自 MlflowLangchainTracer 的自定义回调处理程序来实现这一点。MlflowLangchainTracer 是一个回调处理程序,它被注入到 LangChain 模型推理过程中,以自动记录追踪。它在链的一系列操作(如 on_chain_start、on_llm_start)开始时启动一个新的 span,并在操作完成时结束该 span。各种元数据,如 span 类型、操作名称、输入、输出、延迟等,都会自动记录到 span 中。

以下示例演示了当聊天模型开始运行时,如何向 span 记录一个额外的属性。

from mlflow.langchain.langchain_tracer import MlflowLangchainTracer


class CustomLangchainTracer(MlflowLangchainTracer):
# Override the handler functions to customize the behavior. The method signature is defined by LangChain Callbacks.
def on_chat_model_start(
self,
serialized: Dict[str, Any],
messages: List[List[BaseMessage]],
*,
run_id: UUID,
tags: Optional[List[str]] = None,
parent_run_id: Optional[UUID] = None,
metadata: Optional[Dict[str, Any]] = None,
name: Optional[str] = None,
**kwargs: Any,
):
"""Run when a chat model starts running."""
attributes = {
**kwargs,
**metadata,
# Add additional attribute to the span
"version": "1.0.0",
}

# Call the _start_span method at the end of the handler function to start a new span.
self._start_span(
span_name=name or self._assign_span_name(serialized, "chat model"),
parent_run_id=parent_run_id,
span_type=SpanType.CHAT_MODEL,
run_id=run_id,
inputs=messages,
attributes=kwargs,
)

禁用自动跟踪

可以通过调用 mlflow.langchain.autolog(disable=True)mlflow.autolog(disable=True) 来全局禁用 LangChain 的自动追踪。