There are three ways to add metadata to logs and traces.
- The first is to add metadata directly to Parea’s completion method.
- The second is to add metadata to the trace decorator.
- Finally, for the Python SDK, if your trace has already been created, and you want to add dynamic metadata, you can use the trace_insert helper function.
Completion method
The completion method supports the following metadata fields:
trace_name
- default is ‘LLM’
end_user_identifier
- This is a string representing any identifier for your end users such as an email address or user id.
metadata
- This is a serializable dictionary of key value pairs that you want added to the trace. ex: {'git-hash': '1234abcd'}
tags
- This is a list of strings that you want added to the trace. ex: ['tag1', 'tag2']
target
- A string representing the ground truth answer you expect from the LLM
completion_with_metadata = Completion(
trace_name="custom_trace_name",
end_user_identifier="user_id_123",
metadata={"git-hash": "1234awbcd"},
tags=["tag1", "tag2"],
target="ground_truth_answer",
)
completion_with_metadata = Completion(
trace_name="custom_trace_name",
end_user_identifier="user_id_123",
metadata={"git-hash": "1234awbcd"},
tags=["tag1", "tag2"],
target="ground_truth_answer",
)
const completion: Completion = {
trace_name: 'custom_trace_name',
end_user_identifier: 'user_id_123',
metadata: { 'git-hash': '1234awbcd' },
tags: ['tag1', 'tag2'],
target: 'ground_truth_answer',
};
Trace decorator
The trace decorator supports the same metadata fields as the completion method.
name
- default is the function’s name
end_user_identifier
metadata
tags
target
deployment_id
session_id
- A string representing a session id for the completion.
@trace(
name="custom_trace_name",
end_user_identifier="user_id_123",
metadata={"git-hash": "1234abcd"},
tags=["tag1", "tag2"],
target="ground_truth_answer",
deployment_id="deployment_id"
session_id="session_id"
)
def my_function():
...
@trace(
name="custom_trace_name",
end_user_identifier="user_id_123",
metadata={"git-hash": "1234abcd"},
tags=["tag1", "tag2"],
target="ground_truth_answer",
deployment_id="deployment_id"
session_id="session_id"
)
def my_function():
...
const tracedFunc = trace('custom_trace_name', myFunction, {
endUserIdentifier: 'user_id_123',
metadata: { 'git-hash': '1234awbcd' },
tags: ['tag1', 'tag2'],
target: 'ground_truth_answer',
deploymentId: 'deployment_id'
sessionId: 'session_id'
});
Trace insert
Sometimes the metadata you want to add to a trace is based on something that happens within the function.
In this case you could use the trace_insert()
helper function to add metadata to the trace.
trace_insert
takes a dictionary of metadata field name to value.
The fields supported by trace_insert are:
trace_name
end_user_identifier
metadata
tags
deployment_id
session_id
- A string representing a session id for the completion.
The new data will be added to the most recent trace.
from parea import trace, trace_insert
@trace
def get_sections(chapter: Dict[str, str], model: str):
# I want the trace name to be dynamic and based on the chapter dictionary passed in,
# so I can use trace_insert helper method to add dynamic data
trace_insert({"trace_name": f"Get {chapter['name']} Sections"})
return client.chat.completions.create(
model=model,
temperature=0.7,
messages=[
{
"role": "user",
"content": f"Generate a section outline on {chapter['name']}, with description {chapter['description']}",
}
],
)
The new data will be added to the most recent trace.
from parea import trace, trace_insert
@trace
def get_sections(chapter: Dict[str, str], model: str):
# I want the trace name to be dynamic and based on the chapter dictionary passed in,
# so I can use trace_insert helper method to add dynamic data
trace_insert({"trace_name": f"Get {chapter['name']} Sections"})
return client.chat.completions.create(
model=model,
temperature=0.7,
messages=[
{
"role": "user",
"content": f"Generate a section outline on {chapter['name']}, with description {chapter['description']}",
}
],
)
You need to use the getCurrentTraceId() helper for the Typescript traceInsert helper method to know where to insert the new data.
import {getCurrentTraceId, trace, traceInsert} from "parea-ai";
const getSections = trace('getSections', async (chapter: { name: string; description: string }, model: string) => {
// I want the trace's metadata to be dynamic and contain the chapter name from the passed in object,
// so I can use traceInsert helper method to add dynamic data
traceInsert({metadata: {'chapterName': `Get ${chapter.name} Sections`}})
return await openai.chat.completions.create({
model, messages: [
{
role: "user",
content: `Generate a section outline on ${chapter.name}, with description ${chapter.description}`,
}
], temperature: 0.7
});
});
What’s Next
Now that we have added metadata to our traces, we add user feedback scores to our traces.