In this guide, we will show how to send users’ interactions with an OpenAI model to Nebuly’s platform.

Before getting started, you would need two API keys:

  • Your OpenAI key
  • Nebuly authentication key
  • Node.js

import OpenAI from 'openai';

const OPENAI_API_KEY = "<OPENAI_API_KEY>"
const NEBULY_API_KEY = "<NEBULY_API_KEY>"

const openai = new OpenAI({
    apiKey: OPENAI_API_KEY
});

const systemPrompt = "You are a helpful assistant"
const messages = [
    { role: "system", content: systemPrompt },
    { role: "user", content: "Who is the president of the United States of America?" },
]
const timeStart = new Date().toISOString();
const completion = await openai.chat.completions.create({
    messages: messages,
    model: 'gpt-3.5-turbo',
});
const timeEnd = new Date().toISOString();
const response = await fetch("https://backend.nebuly.com/event-ingestion/api/v1/events/interactions", {
    method: "POST",
    body: JSON.stringify({
        interaction: {
            input: messages[messages.length - 1].content,
            output: completion.choices[0].message.content,
            time_start: timeStart,
            time_end: timeEnd,
            history: [],
            end_user: "<YOUR_USER_ID>",
            model: completion.model,
            system_prompt: systemPrompt
        },
        anonymize: true,
    }),
    headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${NEBULY_API_KEY}`
    }
})

console.log(await response.json())

Below you can find a detailed explanation of the parameters:

input
string
required

Input prompt from the user

output
string
required

Output from the LLM model

time_start
string
required

When the end-user request process started

time_end
string
required

When the end-user request process ended

end_user
string
required

An id or username uniquely identifying the end-user. We recommend hashing their username or email address, in order to avoid sending us any identifying information.

model
string
required

The LLM model you are using. Please note that this is needed if you want to visualize the cost of your requests. Now we support costs only for OpenAI models, cost of other providers coming soon.

history
array

History is an array of tuples of input and output, representing the entire conversation. It should be structured as the example below:

history = [
    ["input_1", "output_1"],
    ["input_2", "output_2"]
]

In this format, input_* and output_* stand for the input and output of each interaction. So, input_1 and output_1 are the input and output of the first interaction, input_2 and output_2 are for the second interaction, and so on.

tags
object

Tag user interactions by adding key-value pairs using this parameter. Each key represents the tag name, and the corresponding value is the tag value.

For example, if you want to tag an interaction with the model version used to reply to user input, provide it as an argument for tags, e.g. {version: "v1.0.0"}. You have the flexibility to define custom tags, making them available as potential filters on the Nebuly platform.

You can find all the API details in the API documentation.