> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/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 其他功能的信息以及探索更多教程，请参阅 [MLflow LangChain 文档](https://mlflow.org/docs/latest/llms/langchain/index.html)。如果您是 MLflow 新手，请查看 [MLflow 入门](https://mlflow.org/docs/latest/getting-started/index.html) 指南。

## 设置

要开始使用 LangChain 的 MLflow 追踪，请安装 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

# Set MLflow tracking URI if you have MLflow Tracking Server running
os.environ["MLFLOW_TRACKING_URI"] = ""
os.environ["OPENAI_API_KEY"] = ""
```

## MLflow 追踪

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

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

# Optional: Set an experiment to organize your traces
mlflow.set_experiment("LangChain MLflow Integration")

# Enable tracing
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

# Enable MLflow tracing
mlflow.langchain.autolog()

# Create a simple chain
llm = ChatOpenAI(model_name="gpt-4.1")

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

chain = prompt | llm | StrOutputParser()

# Run the chain
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


# Enable MLflow tracing
mlflow.langchain.autolog()


# Define a tool
@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."


# Create a LangGraph agent
llm = ChatOpenAI(model="gpt-4.1")
tools = [count_words]
graph = create_agent(llm, tools)

# Run the agent
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="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\providers\mlflow_tracking.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
