opik-openai
Version:
Opik TypeScript and JavaScript SDK integration with OpenAI
89 lines (63 loc) • 2.53 kB
Markdown
//img.shields.io/npm/v/opik-openai.svg)](https://www.npmjs.com/package/opik-openai)
[](https://github.com/comet-ml/opik/blob/main/LICENSE)
Seamlessly integrate [Opik](https://www.comet.com/docs/opik/) observability with your [OpenAI](https://platform.openai.com/docs) applications to trace, monitor, and debug your LLM API calls.
- 🔍 **Comprehensive Tracing**: Automatically trace OpenAI API calls and completions
- 📊 **Hierarchical Visualization**: View your OpenAI execution as a structured trace with parent-child relationships
- 📝 **Detailed Metadata Capture**: Record model names, prompts, completions, token usage, and custom metadata
- 🚨 **Error Handling**: Capture and visualize errors in your OpenAI API interactions
- 🏷️ **Custom Tagging**: Add custom tags to organize and filter your traces
- 🔄 **Streaming Support**: Full support for streamed completions and chat responses
```bash
npm install opik-openai
yarn add opik-openai
pnpm add opik-openai
```
- Node.js ≥ 18
- OpenAI SDK (`openai` ≥ 4.0.0)
- Opik SDK (automatically installed as a dependency)
## Usage
```typescript
import OpenAI from "openai";
import { trackOpenAI } from "opik-openai";
// Initialize the OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// Wrap the client with Opik tracking
const trackedOpenAI = trackOpenAI(openai, {
// Optional configuration
traceMetadata: {
tags: ["production", "my-app"],
},
});
// Use the tracked client just like the original
async function main() {
const completion = await trackedOpenAI.chat.completions.create({
model: "gpt-4",
messages: [{ role: "user", content: "Hello world" }],
});
console.log(completion.choices[0].message);
// Flush traces at the end of your application
await trackedOpenAI.flush();
}
main().catch(console.error);
```
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/)
- [OpenAI Documentation](https://platform.openai.com/docs)
- [Opik TypeScript SDK](https://github.com/comet-ml/opik/tree/main/sdks/typescript)
Apache 2.0
[![npm version](https: