opik-langchain
Version:
Opik TypeScript and JavaScript SDK integration with LangChain
114 lines (81 loc) • 3.07 kB
Markdown
//img.shields.io/npm/v/opik-langchain.svg)](https://www.npmjs.com/package/opik-langchain)
[](https://github.com/comet-ml/opik/blob/main/LICENSE)
Seamlessly integrate [Opik](https://www.comet.com/docs/opik/) observability with your [LangChain](https://js.langchain.com/) applications to trace, monitor, and debug your LLM chains, agents, and tools.
- 🔍 **Comprehensive Tracing**: Automatically trace LLM calls, chains, tools, retrievers, and agents
- 📊 **Hierarchical Visualization**: View your LangChain execution as a structured trace with parent-child relationships
- 📝 **Detailed Metadata Capture**: Record model names, prompts, completions, usage statistics, and custom metadata
- 🚨 **Error Handling**: Capture and visualize errors at every step of your LangChain execution
- 🏷️ **Custom Tagging**: Add custom tags to organize and filter your traces
```bash
npm install opik-langchain
yarn add opik-langchain
pnpm add opik-langchain
```
- Node.js ≥ 18
- LangChain (`@langchain/core` ≥ 0.3.42)
- Opik SDK (automatically installed as a dependency)
## Quick Start
```typescript
import { OpikCallbackHandler } from "opik-langchain";
import { ChatOpenAI } from "@langchain/openai";
// Create the Opik callback handler
const opikHandler = new OpikCallbackHandler();
// Create your LangChain components with the handler
const llm = new ChatOpenAI({
callbacks: [opikHandler],
});
// Run LLM
const response = await llm.invoke("Hello, how can you help me today?", {
callbacks: [opikHandler],
});
// Optionally, ensure all traces are sent before your app terminates
await opikHandler.flushAsync();
```
The `OpikCallbackHandler` constructor accepts the following options:
```typescript
interface OpikCallbackHandlerOptions {
// Optional array of tags to apply to all traces
tags?: string[];
// Optional metadata to include with all traces
metadata?: Record<string, unknown>;
// Optional project name for Opik
projectName?: string;
// Optional pre-configured Opik client
client?: Opik;
}
```
You can pass custom metadata when invoking your chains:
```typescript
const response = await chain.invoke(
{ input: "Tell me about AI" },
{
callbacks: [opikHandler],
metadata: {
userId: "user-123",
sessionId: "session-456",
requestId: "req-789",
},
}
);
```
To view your traces:
1. Sign in to your [Comet account](https://www.comet.com/signin)
2. Navigate to the Opik section
3. Select your project to view all traces
4. Click on a specific trace to see the detailed execution flow
- [Opik Documentation](https://www.comet.com/docs/opik/)
- [LangChain Documentation](https://js.langchain.com/)
- [Opik TypeScript SDK](https://github.com/comet-ml/opik/tree/main/sdks/typescript)
Apache 2.0
[![npm version](https: