Enrich your traces by annotating chains and workflows in your app
Traceloop SDK supports several ways to annotate workflows, tasks, agents and tools in your code to get a more complete picture of your app structure.
If you’re using a framework like Langchain, Haystack or LlamaIndex - no need
to do anything! OpenLLMetry will automatically detect the framework and
annotate your traces.
Use it in your code @traceloop.workflow({ name: "my_workflow" }).
You can provide the parameters to the decorator directly or by providing a function that resolves to the parameters.
The function will be called with the this parameter and the arguments of the decorated function
(see example).
The name is optional. If you don’t provide it, we will use the function
qualified name as the workflow or task name.
Use it as withWorkflow("my_workflow", {}, () => ...) or withTask(name="my_task", () => ...).
The function passed to withWorkflow or withTask witll be part of the workflow or task and can be async or sync.
import * as traceloop from "@traceloop/node-server-sdk";async function create_joke() { return await traceloop.withTask({ name: "joke_creation" }, async () => { completion = await openai.chat.completions({ model: "gpt-3.5-turbo", messages: [ { role: "user", content: "Tell me a joke about opentelemetry" }, ], }); return completion.choices[0].message.content; });}async function generate_signature(joke: string) { return await traceloop.withTask( { name: "signature_generation" }, async () => { completion = await openai.completions.create({ model: "davinci-002", prompt: "add a signature to the joke:\n\n" + joke, }); return completion.choices[0].text; } );}async function joke_workflow() { return await traceloop.withWorkflow( { name: "pirate_joke_generator" }, async () => { eng_joke = create_joke(); pirate_joke = await translate_joke_to_pirate(eng_joke); signature = await generate_signature(pirate_joke); console.log(pirate_joke + "\n\n" + signature); } );}
In Typescript, you can use the same syntax for async methods.
In python, the decorators work seamlessly with both synchronous and asynchronous functions.
Use @workflow, @task, @agent, and so forth for both sync and async methods.
The async-specific decorators (@aworkflow, @atask, etc.) are deprecated and will be removed in a future version.
While the examples above shows how to decorate functions, you can also decorate classes.
In this case, you will also need to provide the name of the method that runs the workflow, task, agent or tool.
Python
from openai import OpenAIfrom traceloop.sdk.decorators import agentclient = OpenAI(api_key=os.environ["OPENAI_API_KEY"])@agent(name="base_joke_generator", method_name="generate_joke")class JokeAgent: def generate_joke(self): completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Tell me a joke about Traceloop"}], ) return completion.choices[0].message.content