MLflow는 머신러닝 및 생성형 AI 라이프사이클 전반에 걸쳐 워크플로우와 아티팩트를 관리하기 위한 다목적 오픈소스 플랫폼입니다. 많은 인기 있는 AI 및 ML 라이브러리와의 내장 통합을 제공하지만, 모든 라이브러리, 알고리즘 또는 배포 도구와 함께 사용할 수 있습니다.
MLflow의 LangChain 통합은 다음과 같은 기능을 제공합니다:
  • Tracing: 한 줄의 코드(mlflow.langchain.autolog())로 LangChain 컴포넌트를 통한 데이터 흐름을 시각화합니다
  • Experiment Tracking: LangChain 실행에서 아티팩트, 코드 및 메트릭을 로깅합니다
  • Model Management: 의존성 추적과 함께 LangChain 애플리케이션을 버전 관리하고 배포합니다
  • Evaluation: LangChain 애플리케이션의 성능을 측정합니다
참고: MLflow tracing은 MLflow 버전 2.14.0 이상에서 사용할 수 있습니다. 이 짧은 가이드는 LangChain 및 LangGraph 애플리케이션을 위한 MLflow의 tracing 기능에 중점을 둡니다. 한 줄의 코드로 tracing을 활성화하고 애플리케이션의 실행 흐름을 확인하는 방법을 살펴봅니다. MLflow의 다른 기능에 대한 정보와 추가 튜토리얼을 살펴보려면 MLflow documentation for LangChain을 참조하세요. MLflow를 처음 사용하는 경우 Getting Started with MLflow 가이드를 확인하세요.

Setup

LangChain을 위한 MLflow tracing을 시작하려면 MLflow Python 패키지를 설치하세요. langchain-openai 패키지도 사용할 것입니다.
pip install mlflow langchain-openai langgraph -qU
다음으로 MLflow tracking URI와 OpenAI API key를 설정합니다.
import os

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

MLflow Tracing

MLflow의 tracing 기능은 LangChain 애플리케이션의 실행 흐름을 시각화하는 데 도움을 줍니다. 활성화하는 방법은 다음과 같습니다.
import mlflow

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

# Enable tracing
mlflow.langchain.autolog()

예제: LangChain 애플리케이션 Tracing

다음은 LangChain과 함께 MLflow tracing을 보여주는 완전한 예제입니다:
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-4o")

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.",
    }
)
trace를 보려면 터미널에서 mlflow ui를 실행하고 MLflow UI의 Traces 탭으로 이동하세요.

예제: LangGraph 애플리케이션 Tracing

MLflow는 LangGraph 애플리케이션의 tracing도 지원합니다:
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-4o")
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."}]}
)
trace를 보려면 터미널에서 mlflow ui를 실행하고 MLflow UI의 Traces 탭으로 이동하세요.

Resources

LangChain과 함께 MLflow를 사용하는 방법에 대한 자세한 내용은 다음을 참조하세요:
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I