Skip to main content

LangChain

Opik provides seamless integration with LangChain, allowing you to easily log and trace your LangChain-based applications. By using the OpikTracer callback, you can automatically capture detailed information about your LangChain runs, including inputs, outputs, and metadata for each step in your chain.

You can check out the Colab Notebook if you'd like to jump straight to the code: Open In Colab

Getting Started

To use the OpikTracer with LangChain, you'll need to have both the opik and langchain packages installed. You can install them using pip:

pip install opik langchain langchain_openai

In addition, you can configure Opik using the opik configure command which will prompt you for the correct local server address or if you are using the Cloud platfrom your API key:

opik configure

Using OpikTracer

Here's a basic example of how to use the OpikTracer callback with a LangChain chain:

from langchain.chains import LLMChain
from langchain_openai import OpenAI
from langchain.prompts import PromptTemplate
from opik.integrations.langchain import OpikTracer

# Initialize the tracer
opik_tracer = OpikTracer()

# Create the LLM Chain using LangChain
llm = OpenAI(temperature=0)

prompt_template = PromptTemplate(
input_variables=["input"],
template="Translate the following text to French: {input}"
)

llm_chain = LLMChain(llm=llm, prompt=prompt_template)

# Generate the translations
translation = llm_chain.run("Hello, how are you?", callbacks=[opik_tracer])
print(translation)

# The OpikTracer will automatically log the run and its details to Opik

This example demonstrates how to create a LangChain chain with a OpikTracer callback. When you run the chain with a prompt, the OpikTracer will automatically log the run and its details to Opik, including the input prompt, the output, and metadata for each step in the chain.

Settings tags and metadata

You can also customize the OpikTracer callback to include additional metadata or logging options. For example:

from opik.integrations.langchain import OpikTracer

opik_tracer = OpikTracer(
tags=["langchain"],
metadata={"use-case": "documentation-example"}
)

Accessing logged traces

You can use the created_traces method to access the trace IDs collected by the OpikTracer callback:

from opik.integrations.langchain import OpikTracer

opik_tracer = OpikTracer()

# Calling Langchain object

traces = opik_tracer.created_traces()
print([trace.id for trace in traces])

This can be especially useful if you would like to update or log feedback scores for traces logged using the OpikTracer.

Advanced usage

The OpikTracer object has a flush method that can be used to make sure that all traces are logged to the Opik platform before you exit a script. This method will return once all traces have been logged or if the timeout is reach, whichever comes first.

from opik.integrations.langchain import OpikTracer

opik_tracer = OpikTracer()
opik_tracer.flush()