Use it as @workflow(name="my_workflow") or @task(name="my_task").The name argument is optional. If you don’t provide it, we will use the
function name as the workflow or task name.
You can version your workflows and tasks. Just provide the version argument
to the decorator: @workflow(name="my_workflow", version=2)
from openai import OpenAI
from traceloop.sdk.decorators import workflow, task
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
@task(name="joke_creation")
def create_joke():
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Tell me a joke about opentelemetry"}],
)
return completion.choices[0].message.content
@task(name="signature_generation")
def generate_signature(joke: str):
completion = openai.Completion.create(
model="davinci-002",[]
prompt="add a signature to the joke:\n\n" + joke,
)
return completion.choices[0].text
@workflow(name="pirate_joke_generator")
def joke_workflow():
eng_joke = create_joke()
pirate_joke = translate_joke_to_pirate(eng_joke)
signature = generate_signature(pirate_joke)
print(pirate_joke + "\n\n" + signature)
This feature is only available in Typescript. Unless you’re on Nest.js, you’ll need to update your tsconfig.json to enable decorators.
Update tsconfig.json to enable decorators:{
"compilerOptions": {
"experimentalDecorators": true
}
}
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.
import * as traceloop from "@traceloop/node-server-sdk";
class JokeCreation {
@traceloop.task({ name: "joke_creation" })
async create_joke() {
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;
}
@traceloop.task({ name: "signature_generation" })
async generate_signature(joke: string) {
completion = await openai.completions.create({
model: "davinci-002",
prompt: "add a signature to the joke:\n\n" + joke,
});
return completion.choices[0].text;
}
@traceloop.workflow({ name: "pirate_joke_generator" })
async joke_workflow() {
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);
}
}