跳到主要内容

追踪 LangChain🦜⛓️

LangChain Tracing via autolog

LangChain 是一个用于构建由大型语言模型(LLM)驱动的应用程序的开源框架。

MLflow 追踪为 LangChain 提供了自动追踪功能。您可以通过调用 mlflow.langchain.autolog() 函数来为 LangChain 启用追踪,当调用链(chains)时,嵌套的追踪信息会自动记录到活动的 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__(用于 Chains 和 AgentExecutors)

Token 使用量跟踪

MLflow >= 3.1.0 支持 LangChain 的 Token 使用量跟踪。在链调用期间,每个 LLM 调用的 Token 使用量将记录在 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 记录一个额外的属性。

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 的自动追踪。