Metadata
You can attach custom context (we call this metadata) to your pipeline runs. If you want to associate a URL to an external service or a prompt variant to the run, you can use metadata to tag your generations.
Types
The value object for each metadata value is a union type. We only support two value types at this time. We will add more upon request.
"type": "url"
If specified as a URL, you must specify url
and text
key/value pairs.
json
{"type": "url","url": "https://external-service.example.com","text": "External service"}
"type": "string"
If specified as a string, you must specify a value
key/value pair.
json
{"type": "string","value": "MzY0MzQ0"}
If your metadata is not properly formatted with the specified type, our API will reject your request.
Usage
Advanced example for generation
You can associate metadata with an entire generation by specifying the metadata
key in .start()
function.
- TypeScript
- Python
typescript
import {Pipeline } from "@gentrace/core";import {initPlugin } from "@gentrace/openai";constGIT_SHA = "63ce36deceeb28da94f7f8176166f9f1f399134d";constplugin = awaitinitPlugin ({apiKey :process .env .OPENAI_KEY ,});constpipeline = newPipeline ({slug : "advanced-pipeline",plugins : {openai :plugin ,},});construnner =pipeline .start ({metadata : {// We support multiple key/value pairs within the metadata objectexternalServiceUrl : {// Every metadata object must have a "type" keytype : "url",url : "https://external-service.example.com",text : "External service"},gitSha : {type : "string",value :GIT_SHA }}});constopenai =runner .openai ;constchatCompletionResponse = awaitopenai .chat .completions .create ({messages : [{role : "user",content : "Hello! What's the capital of Maine?" }],model : "gpt-3.5-turbo",stream : true});awaitrunner .submit ()
python
import gentraceGIT_SHA = "63ce36deceeb28da94f7f8176166f9f1f399134d"PIPELINE_SLUG = "compose"pipeline = gentrace.Pipeline(PIPELINE_SLUG,openai_config={"api_key": process.env.OPENAI_KEY,},)runner = pipeline.start({"metadata": {# We support multiple metadata keys within the metadata object"externalServiceUrl": {# Every metadata object must have a "type" key"type": "url","url": "https://external-service.example.com","text": "External service"},"gitSha": {"type": "string","value": GIT_SHA}}})openai = runner.get_openai()result = openai.ChatCompletion.create(messages=[{ "role": "user", "content": "Hello! What's the capital of Maine?" }],model="gpt-3.5-turbo",stream=True,)
Advanced example on step
You can also add metadata to the run by using the context object for a particular step.
- TypeScript
- Python
typescript
import {Pipeline } from "@gentrace/core";import {initPlugin } from "@gentrace/openai";constGIT_SHA = "63ce36deceeb28da94f7f8176166f9f1f399134d";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! What's the capital of Maine?" }],model : "gpt-3.5-turbo",stream : true,gentrace : {metadata : {// We support multiple metadata keys within the metadata objectexternalServiceUrl : {// Every metadata object must have a "type" keytype : "url",url : "https://external-service.example.com",text : "External service"},gitSha : {type : "string",value :GIT_SHA }}}});awaitrunner .submit ();
python
import gentraceGIT_SHA = "63ce36deceeb28da94f7f8176166f9f1f399134d"PIPELINE_SLUG = "compose"pipeline = gentrace.Pipeline(PIPELINE_SLUG,openai_config={"api_key": process.env.OPENAI_KEY,},)runner = pipeline.start()openai = runner.get_openai()result = openai.ChatCompletion.create(messages=[{ "role": "user", "content": "Hello! What's the capital of Maine?" }],model="gpt-3.5-turbo",stream=True,gentrace={"metadata": {# We support multiple metadata keys within the metadata object"externalServiceUrl": {# Every metadata object must have a "type" key"type": "url","url": "https://external-service.example.com","text": "External service"},"gitSha": {"type": "string","value": GIT_SHA}}},)runner.submit()
Behavior
All metadata is scoped to a pipeline run. If you're using the advanced SDK and have multiple interim steps in your pipeline, any metadata that you associate with a step is associated directly with the pipeline run, not the step.
Let's say you have the following pipeline.
- TypeScript
- Python
typescript
import {Pipeline } from "@gentrace/core";import {initPlugin } from "@gentrace/openai";constGIT_SHA = "63ce36deceeb28da94f7f8176166f9f1f399134d";constplugin = awaitinitPlugin ({apiKey :process .env .OPENAI_KEY ,});constpipeline = newPipeline ({slug : "advanced-pipeline",plugins : {openai :plugin ,},});construnner =pipeline .start ({// Assign metadata upon pipeline instantiationmetadata : {gitSha : {type : "string",value :GIT_SHA }}});constopenai =runner .openai ;constchatCompletionResponse = awaitopenai .chat .completions .create ({messages : [{role : "user",content : "Hello! What's the capital of Maine?" }],model : "gpt-3.5-turbo",stream : true,gentrace : {// Create additional metadata when you're adding steps to your pipelinemetadata : {// We support multiple metadata keys within the metadata objectexternalServiceUrl : {// Every metadata object must have a "type" keytype : "url",url : "https://external-service.example.com",text : "External service"}}}});
python
# Omit SDK initializationGIT_SHA = "63ce36deceeb28da94f7f8176166f9f1f399134d"runner = pipeline.start({# Assign metadata upon pipeline instantiation"metadata": {"gitSha": {"type": "string","value": GIT_SHA}}})openai = runner.get_openai()result = openai.ChatCompletion.create(messages=[{ "role": "user", "content": "Hello! What's the capital of Maine?" }],model="gpt-3.5-turbo",stream=True,gentrace={# Create additional metadata when you're adding steps to your pipeline"metadata": {# We support multiple metadata keys within the metadata object"externalServiceUrl": {# Every metadata object must have a "type" key"type": "url","url": "https://external-service.example.com","text": "External service"}}},)
Both items will render in the runs page in the Gentrace UI similar to below.
When defining a metadata key with the same identifier in a pipeline run, please note that only the latest write will be applied to the metadata for the entire run.
- TypeScript
- Python
typescript
// Omit SDK and pipeline run initializationconstfirst = awaitopenai .chat .completions .create ({messages : [{role : "user",content : "Hello! What's the capital of Maine?" }],model : "gpt-3.5-turbo",stream : true,gentrace : {metadata : {promptId : {type : "string",value : "MzY0MzQ0"}}}});// ...constsecond = awaitopenai .chat .completions .create ({messages : [{role : "user",content : "Hello! What's the capital of Georgia" }],model : "gpt-3.5-turbo",stream : true,gentrace : {// 🚧 This overwrites the first instantiationmetadata : {promptId : {type : "string",value : "SGVsbG8g"}}}});
python
# Omit SDK and pipeline run initializationfirst = openai.ChatCompletion.create(messages=[{ "role": "user", "content": "Hello! What's the capital of Maine?" }],model="gpt-3.5-turbo",stream=True,gentrace={"metadata": {"promptId": {"type": "string","value": "MzY0MzQ0"}}},)# ...second = openai.ChatCompletion.create(messages=[{ "role": "user", "content": "Hello! What's the capital of Georgia?" }],model="gpt-3.5-turbo",stream=True,gentrace={# 🚧 This overwrites the first instantiation"metadata": {"promptId": {"type": "string","value": "SGVsbG8g"}}},)