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
- Python
typescript
// Omit initializationawaitopenai .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",},});
python
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",pipeline_slug="example-completion",# 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
- Python
typescript
import {Pipeline } from "@gentrace/core";import {initPlugin } from "@gentrace/openai";constplugin = awaitinitPlugin ({apiKey :process .env .OPENAI_KEY ,});constpipeline = newPipeline ({slug : "advanced-pipeline",plugins : {openai :plugin ,},});// Context is specified directly as an object. This context is associated with all// steps in the generative pipeline.construnner =pipeline .start ({userId : "TWFuIGlzIGRpc3Rpbmd1aXNoZW",});constopenai =runner .openai ;constchatCompletionResponse = awaitopenai .chat .completions .create ({messages : [{role : "user",content : "Hello!" }],model : "gpt-3.5-turbo",stream : true,});
python
import gentracePIPELINE_SLUG = "compose"pipeline = gentrace.Pipeline(PIPELINE_SLUG,openai_config={"api_key": process.env.OPENAI_KEY,},)runner = pipeline.start({"userId": "TWFuIGlzIGRpc3Rpbmd1aXNoZW",})openai = runner.get_openai()result = openai.chat.completions.create(messages=[{ "role": "user", "content": "Hello!" }],model="gpt-3.5-turbo",stream=True)
Limitations
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
- Python
typescript
import {Pipeline } from "@gentrace/core";import {initPlugin } from "@gentrace/openai";constplugin = awaitinitPlugin ({apiKey :process .env .OPENAI_KEY ,});constpipeline = newPipeline ({slug : "advanced-pipeline",plugins : {openai :plugin ,},});construnner =pipeline .start ();constopenai =runner .openai ;constchatCompletionResponse = awaitopenai .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 stepgentrace : {userId : "TWFuIGlzIGRpc3Rpbmd1aXNoZW",}});constchatCompletionResponse2 = awaitopenai .chat .completions .create ({messages : [{role : "user",content : "Hello! My name is Doug." }],model : "gpt-3.5-turbo",});awaitrunner .submit ()
python
import gentracePIPELINE_SLUG = "compose"pipeline = gentrace.Pipeline(PIPELINE_SLUG,openai_config={"api_key": process.env.OPENAI_KEY,},)runner = pipeline.start()openai = runner.get_openai()result = openai.chat.completions.create(messages=[{ "role": "user", "content": "Hello! My name is Vivek." }],model="gpt-3.5-turbo",stream=True,# 🛑 This is not correctgentrace={"userId": "TWFuIGlzIGRpc3Rpbmd1aXNoZW",})result2 = openai.chat.completions.create(messages=[{ "role": "user", "content": "Hello! My name is Doug." }],model="gpt-3.5-turbo",stream=True)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](/img/user-id-filtering.png)