Skip to content

Open AI

To get started collecting events in your application using OpenAI, our SDKs have methods to automate AI event collection with a single line of code.

Install SDK
npm i @trubrics/trubrics
pip install trubrics-beta

Firstly, ensure that Trubrics is imported & initialised in your app:

import { Trubrics } from "@trubrics/trubrics";

export const trubrics = new Trubrics({apiKey: TRUBRICS_API_KEY});
from trubrics_beta import Trubrics

trubrics = Trubrics(api_key="TRUBRICS_API_KEY")

API key

Your trubrics API key is unique to your project. Login to Trubrics, and copy your project API key from the settings page. This is a write only API key that allows you to push events.

Now start tacking prompts and generations from OpenAI by adding a single line to your OpenAI initializer:

import OpenAI from "openai";

const openai = new OpenAI({
    apiKey: OPENAI_API_KEY,
    fetch: trubrics.openaiFetch, // (1)!
});
  1. Just add this single line!
# Coming soon

Advanced tracking

While this is all you need to start tracking Open AI messages with Trubrics, enhanced Trubrics features such as Threads will require a tool to be set in the openai.chat.completions.create() request. This tool will only serve the purpose of transmitting metadata to Trubrics, and will be removed from the request to Open AI.

This tool must be named "Trubrics parameters" and be structured as below. It can be added to an existing list of tools, or as a single tool.

We recommend including a thread ID and a user ID if a user has not already been identified.

const completion = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages=[
        {"role": "user", "content": "Who won the world series in 2020?"},
    ],
    tools: [
        {
            "type": "function",
            "function": {
                "name": "Trubrics parameters",
                "parameters": {
                    "thread_id": "your-thread-id",
                    "user_id": "your-user-id"
                }
            }
        }
    ]
});