Skip to main content
Version: 4.7.14

User IDs

You can associate a user ID to an AI generation by supplying the userId key in the Gentrace context object.

Types

userId

User ID is a UUID string

Usage

Simple SDK

Specify the userId in the gentrace context object when using the simple SDK.

typescript
// Omit initialization
 
await openai.chat.completions.create({
messages: [
{
role: "user",
contentTemplate: "Hello! My name is {{ name }}. Write a brief essay about Maine.",
contentInputs: { name: "Vivek" },
},
],
model: "gpt-3.5-turbo",
pipelineSlug: "simple-pipeline",
stream: true,
 
// Context is specified with the `gentrace` key. In this case, the user ID will be
// associated with this generation.
gentrace: {
userId: "TWFuIGlzIGRpc3Rpbmd1aXNoZW",
},
});

Advanced SDK

In the advanced SDK, the context is specified in the pipeline.start() function.

typescript
import { Pipeline } from "@gentrace/core";
import { initPlugin } from "@gentrace/openai";
 
const plugin = await initPlugin({
apiKey: process.env.OPENAI_KEY,
});
 
const pipeline = new Pipeline({
slug: "advanced-pipeline",
plugins: {
openai: plugin,
},
});
 
// Context is specified directly as an object. This context is associated with all
// steps in the generative pipeline.
const runner = pipeline.start({
userId: "TWFuIGlzIGRpc3Rpbmd1aXNoZW",
});
 
const openai = runner.openai;
 
const chatCompletionResponse = await openai.chat.completions.create({
messages: [{ role: "user", content: "Hello!" }],
model: "gpt-3.5-turbo",
stream: true,
});
 

Limitations

caution

The userId parameter cannot be specified on a single step within a multi-step pipeline.

Let's consider the below incorrect code, which makes two calls to OpenAI in a single generation.

typescript
import { Pipeline } from "@gentrace/core";
import { initPlugin } from "@gentrace/openai";
 
const plugin = await initPlugin({
apiKey: process.env.OPENAI_KEY,
});
 
const pipeline = new Pipeline({
slug: "advanced-pipeline",
plugins: {
openai: plugin,
},
});
 
const runner = pipeline.start();
 
const openai = runner.openai;
 
const chatCompletionResponse = await openai.chat.completions.create({
messages: [{ role: "user", content: "Hello! My name is Vivek." }],
model: "gpt-3.5-turbo",
// 🛑 This is not correct since it's assigned to a single step
gentrace: {
userId: "TWFuIGlzIGRpc3Rpbmd1aXNoZW",
}
});
 
const chatCompletionResponse2 = await openai.chat.completions.create({
messages: [{ role: "user", content: "Hello! My name is Doug." }],
model: "gpt-3.5-turbo",
});
 
await runner.submit()
 

In Gentrace, a user ID is always associated with a single Gentrace run. In the case of our advanced SDK, multiple steps within a runner are considered part of a single run.

Rendering

By associating a user ID with a generation, you can filter generations by user ID in the Gentrace runs view.

Filtering by user ID