Published
OpenInference LangChain Instrumentation
pip install openinference-instrumentation-langchain
Package Downloads
Authors
Project URLs
Requires Python
<3.13,>=3.8
Dependencies
- openinference-instrumentation
>=0.1.17
- openinference-semantic-conventions
>=0.1.9
- opentelemetry-api
- opentelemetry-instrumentation
- opentelemetry-semantic-conventions
- wrapt
- langchain-core
>=0.1.0; extra == "instruments"
- langchain-community
==0.0.10; extra == "test"
- langchain-core
==0.1.8; extra == "test"
- langchain-openai
==0.0.2; extra == "test"
- langchain
==0.1.0; extra == "test"
- langsmith
; extra == "test"
- opentelemetry-sdk
; extra == "test"
- portpicker
; extra == "test"
- pytest-recording
; extra == "test"
- respx
; extra == "test"
- starlette
; extra == "test"
- uvicorn
; extra == "test"
- langchain-core
==0.1.5; extra == "type-check"
OpenInference LangChain Instrumentation
Python auto-instrumentation library for LangChain.
These traces are fully OpenTelemetry compatible and can be sent to an OpenTelemetry collector for viewing, such as arize-phoenix
.
Installation
pip install openinference-instrumentation-langchain
Quickstart
Install packages needed for this demonstration.
pip install openinference-instrumentation-langchain langchain arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp
Start the Phoenix app in the background as a collector. By default, it listens on http://localhost:6006
. You can visit the app via a browser at the same address.
The Phoenix app does not send data over the internet. It only operates locally on your machine.
python -m phoenix.server.main serve
The following Python code sets up the LangChainInstrumentor
to trace langchain
and send the traces to Phoenix at the endpoint shown below.
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
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 ConsoleSpanExporter, 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)))
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
LangChainInstrumentor().instrument()
To demonstrate langchain
tracing, we'll make a simple chain to tell a joke. First, configure your OpenAI credentials.
import os
os.environ["OPENAI_API_KEY"] = "<your openai key>"
Now we can create a chain and run it.
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(input_variables=["adjective"], template=prompt_template)
llm = LLMChain(llm=OpenAI(), prompt=prompt, metadata={"category": "jokes"})
completion = llm.predict(adjective="funny", metadata={"variant": "funny"})
print(completion)
Visit the Phoenix app at http://localhost:6006
to see the traces.