LangChainJS

LangChainJS is the Javascript package for LangChain which allows you to develop applications powered by Large Language Models.

Opik provides an integration that allows you to log all your chain executions to the Opik dashboard, making it easier to debug your chains and monitor your applicaation in production.

If you are using the Python LangChain package, refer to this integration guide

Getting Started

To use Opik with LangChainJS, make sure you have the following installed:

$npm install langchain @langchain/core opik-langchain

In addition you will need to set the following environment variables to make sure the data is logged to Opik if you are using the Cloud platform:

$export OPIK_URL_OVERRIDE="https://www.comet.com/opik/api"
>export OPIK_API_KEY=<your-api-key>
>export OPIK_WORKSPACE_NAME=<your-workspace-name>

Logging a chain

In order to log a chain, you will need to create the Opik callback:

1import { OpikCallbackHandler } from "opik-langchain";
2
3const opikCallback = new OpikCallbackHandler();

You can then use this callback in your LangChain chain:

1import { OpenAI } from "@langchain/openai";
2import { PromptTemplate } from "@langchain/core/prompts";
3import { OpikCallbackHandler } from "opik/langchain";
4
5// This is just a demo that doesn't require API key
6const model = new OpenAI({
7 temperature: 0.9,
8});
9
10const prompt = PromptTemplate.fromTemplate("What is a good name for a company that makes {product}?");
11
12const chain = prompt.pipe(model);
13const opikHandler = new OpikCallbackHandler();
14
15const result = await chain.invoke({ product: "colorful socks" }, { callbacks: [opikHandler] });

Settings tags and metadata

You can also customize the OpikCallbackHandler to include additional metadata, tags. For example:

{pytest_codeblocks_skip=true}
1const opikHandler = new OpikCallbackHandler({
2 tags: ["langchain"],
3 metadata: { "use-case": "documentation-example" },
4});

Logging to a specific project

You can also log to a specific project by setting the project parameter:

{pytest_codeblocks_skip=true}
1const opikHandler = new OpikCallbackHandler({
2 projectName: "my-project",
3});

Or alternatively, you can use the environment variable:

$export OPIK_PROJECT_NAME="my-project"