Skip to main content

OpenInference LangChain Instrumentation

Project description

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.

pypi

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.

More Info

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

File details

Details for the file openinference_instrumentation_langchain-0.1.29.tar.gz.

File metadata

File hashes

Hashes for openinference_instrumentation_langchain-0.1.29.tar.gz
Algorithm Hash digest
SHA256 f6c10079c91f810cff39ff24c278e41d16df0c3706b230e1859f46cad20f8e0b
MD5 ddff5a7851c680353f45c9e393a430ac
BLAKE2b-256 10d2185e4dd18431f35faba90482f511328d283e71ed12737e050664f7b5efeb

See more details on using hashes here.

File details

Details for the file openinference_instrumentation_langchain-0.1.29-py3-none-any.whl.

File metadata

File hashes

Hashes for openinference_instrumentation_langchain-0.1.29-py3-none-any.whl
Algorithm Hash digest
SHA256 9faaedb62f90ca8099b48c0b9ded97fe109a646b756f4bf7c98479b500a8446d
MD5 5e8951cdebcc68f7d206c1a6e50e07a1
BLAKE2b-256 ebbab0855a5f9bb565e462d85ecfad63d108c4c4bc209a34850623273f7dbcef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page