Skip to main content

OpenInference

还没翻译译自英文(docs.langchain.com)本页 15 段都还没译

Overview

OpenInference instrumentation for LangChain records LangChain and LangGraph operations as OpenTelemetry spans that use OpenInference semantic conventions. Use it to send traces to an OpenTelemetry-compatible collector or observability backend, including Arize AX and Arize Phoenix.

The package hooks into langchain-core, which is shared by LangChain packages. It supports LangChain 1.x, LangChain Classic, and partner packages such as langchain-openai, langchain-anthropic, and langchain-google-vertexai.

Installation

Install the OpenInference instrumentation package with LangChain:

pip install openinference-instrumentation-langchain langchain langchain-openai

For LangChain Classic applications, install langchain-classic instead:

pip install openinference-instrumentation-langchain langchain-classic langchain-openai

Use with an OpenTelemetry collector

Install the OpenTelemetry SDK and OTLP exporter (they are not pulled in by the instrumentation package alone), then configure a tracer provider and instrument LangChain before running your application:

pip install opentelemetry-sdk opentelemetry-exporter-otlp
from openinference.instrumentation.langchain import LangChainInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
trace_api.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))

LangChainInstrumentor().instrument()

The instrumentation records spans for LangChain runs and exports them through the configured tracer provider.

Use with Arize AX

Arize AX can receive traces from OpenInference instrumentation through OpenTelemetry. Install arize-otel with the instrumentation package:

pip install arize-otel openinference-instrumentation-langchain langchain langchain-openai

Register an Arize AX tracer provider, then pass it to the LangChain instrumentor:

import os

from arize.otel import register
from openinference.instrumentation.langchain import LangChainInstrumentor

tracer_provider = register(
    space_id=os.environ["ARIZE_SPACE_ID"],
    api_key=os.environ["ARIZE_API_KEY"],
    project_name=os.environ["ARIZE_PROJECT_NAME"],
)

LangChainInstrumentor().instrument(tracer_provider=tracer_provider)

Resources