withAdditionalAttributes for typescript or inject_additional_attributes function for python. This will add the specified attributes to the trace spans. Make sure to pass the attributes as a key-value pair to the function.
Typescript SDK
import * as Langtrace from "@langtrase/typescript-sdk";
import OpenAI from 'openai'
Langtrace.init({
write_spans_to_console: true,
})
const openai = new OpenAI()
export const run = async ()=>{
const response = await Langtrace.withAdditionalAttributes(async () => {
return await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'system', content: 'Talk like a pirate' },
{ role: 'user', content: 'Tell me a story in 3 sentences or less.' }
],
stream: false
})
}, { user_id: "userid1234", request_id: "randomId" }) // Add additional attributes to the trace spans can be any key value pair
}
run().then(() => console.log('done'))
import * as Langtrace from "@langtrase/typescript-sdk";
import OpenAI from 'openai'
Langtrace.init({
write_spans_to_console: true,
})
await withAdditionalAttributes(async () => {
const resp = await ai.generateText({
model: openai('gpt-4-turbo', { user: 'abc' }),
system: 'You are a friendly assistant!',
temperature: 0.5,
topP: 1,
maxRetries: 3,
maxTokens: 1024,
messages: [{ role: 'system', content: 'hi' }, { role: 'user', content: 'why is the sky blue' }]
})
}, { 'langtrace.span.name': 'my_custom_span' })
Python SDK
from langtrace_python_sdk import inject_additional_attributes
def do_llm_stuff(name=""):
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say this is a test three times"}],
stream=False,
)
return response
def main():
response = inject_additional_attributes(lambda: do_llm_stuff(name="llm"), {'user_id': 'userid1234'})
# if the function do not take arguments then this syntax will work
response = inject_additional_attributes(do_llm_stuff, {'user_id': 'userid1234'})
main()
from langtrace_python_sdk import langtrace, with_additional_attributes
from openai import OpenAI
langtrace.init(write_spans_to_console=True)
openai = new OpenAI()
# NOTE: will be deprecated in the future.
@with_additional_attributes({'user_id': 1234, 'request_id': 'random_id'})
def run():
response = openai.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Talk like a pirate"},
{"role": "user", "content": "Tell me a story in 3 sentences or less."},
],
stream=True,
)
return response
run()
DO NOT USE this the Python decorator -
with_additional_attributes. It will be
deprecated soon. Please use inject_additional_attributes function instead.