> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# MLflow 集成

> 使用 LangChain Python 与 MLflow 集成。

> [MLflow](https://mlflow.org/) 是一个多功能的开源平台，用于管理机器学习和生成式 AI 生命周期中的工作流和制品。它内置了与许多流行 AI 和 ML 库的集成，但也可以与任何库、算法或部署工具一起使用。

MLflow 的 [LangChain 集成](https://mlflow.org/docs/latest/llms/langchain/autologging.html) 提供以下功能：

* **[追踪](https://mlflow.org/docs/latest/llms/langchain/autologging.html)**：只需一行代码 (`mlflow.langchain.autolog()`) 即可可视化数据在 LangChain 组件中的流动。
* **[实验跟踪](https://mlflow.org/docs/latest/llms/langchain/index.html#experiment-tracking)**：记录来自 LangChain 运行的制品、代码和指标。
* **[模型管理](https://mlflow.org/docs/latest/model-registry.html)**：对 LangChain 应用程序进行版本控制和部署，并跟踪依赖关系。
* **[评估](https://mlflow.org/docs/latest/llms/langchain/index.html#mlflow-evaluate)**：衡量 LangChain 应用程序的性能。

**注意**：MLflow 追踪功能在 MLflow 2.14.0 及更高版本中可用。

本简短指南重点介绍 MLflow 针对 LangChain 和 LangGraph 应用程序的追踪功能。您将了解如何通过一行代码启用追踪，并查看应用程序的执行流程。有关 MLflow 其他功能的信息以及探索更多教程，请参阅 [LangChain 的 MLflow 文档](https://mlflow.org/docs/latest/llms/langchain/index.html)。如果您是 MLflow 新手，请查看 [MLflow 入门指南](https://mlflow.org/docs/latest/getting-started/index.html)。

## 设置

要开始使用 MLflow 追踪 LangChain，请安装 MLflow Python 包。我们还将使用 `langchain-openai` 包。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install mlflow langchain-openai langgraph -qU
```

接下来，设置 MLflow 跟踪 URI 和 OpenAI API 密钥。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os

# 如果您运行了 MLflow 跟踪服务器，请设置 MLflow 跟踪 URI
os.environ["MLFLOW_TRACKING_URI"] = ""
os.environ["OPENAI_API_KEY"] = ""
```

## MLflow 追踪

MLflow 的追踪功能帮助您可视化 LangChain 应用程序的执行流程。以下是启用方法。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import mlflow

# 可选：设置一个实验来组织您的追踪
mlflow.set_experiment("LangChain MLflow Integration")

# 启用追踪
mlflow.langchain.autolog()
```

## 示例：追踪 LangChain 应用程序

这是一个展示 MLflow 与 LangChain 追踪的完整示例：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import mlflow
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

# 启用 MLflow 追踪
mlflow.langchain.autolog()

# 创建一个简单的链
llm = ChatOpenAI(model_name="gpt-5.4")

prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are a helpful assistant that translates {input_language} to {output_language}.",
        ),
        ("human", "{input}"),
    ]
)

chain = prompt | llm | StrOutputParser()

# 运行链
result = chain.invoke(
    {
        "input_language": "English",
        "output_language": "German",
        "input": "I love programming.",
    }
)
```

要查看追踪，请在终端中运行 `mlflow ui` 并导航到 MLflow UI 中的“追踪”选项卡。

## 示例：追踪 LangGraph 应用程序

MLflow 也支持追踪 LangGraph 应用程序：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import mlflow
from langchain.tools import tool
from langchain.agents import create_agent


# 启用 MLflow 追踪
mlflow.langchain.autolog()


# 定义一个工具
@tool
def count_words(text: str) -> str:
    """Counts the number of words in a text."""
    word_count = len(text.split())
    return f"This text contains {word_count} words."


# 创建一个 LangGraph 代理
llm = ChatOpenAI(model="gpt-5.4")
tools = [count_words]
graph = create_agent(llm, tools)

# 运行代理
result = graph.invoke(
    {"messages": [{"role": "user", "content": "Write me a 71-word story about a cat."}]}
)
```

要查看追踪，请在终端中运行 `mlflow ui` 并导航到 MLflow UI 中的“追踪”选项卡。

## 资源

有关将 MLflow 与 LangChain 结合使用的更多信息，请访问：

* [MLflow LangChain 集成文档](https://mlflow.org/docs/latest/llms/langchain/index.html)
* [MLflow 追踪文档](https://mlflow.org/docs/latest/llms/tracing/index.html)
* [记录 LangChain 和 LangGraph 模型](https://mlflow.org/docs/latest/llms/langchain/index.html#logging-models-from-code)
* [评估 LangChain 和 LangGraph 模型](https://mlflow.org/docs/latest/llms/langchain/index.html#how-can-i-evaluate-a-langgraph-agent)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs) 通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/providers/mlflow_tracking.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
