Overview

Our OpenAI integration allows you to easily perform AI-powered tasks, such as summarizing text, answering questions, generating images, fine tuning and much more.

Installing the OpenAI packages

npm install @trigger.dev/openai@latest

Authentication

To use the OpenAI API with Trigger.dev, you’ll need an API Key from OpenAI. If you don’t have one yet, you can obtain it from the OpenAI dashboard.

import { OpenAI } from "@trigger.dev/openai";

const openai = new OpenAI({
  id: "openai",
  apiKey: process.env.OPENAI_API_KEY!,
});

Tasks

Once you have set up a OpenAI client, you can add it to your job and start using the provided tasks:

client.defineJob({
  id: "openai-job",
  name: "OpenAI Job",
  version: "1.0.0",
  trigger: invokeTrigger(),
  integrations: {
    openai, // Add the OpenAI client as an integration
  },
  run: async (payload, io, ctx) => {
    // Now you can access it through the io object
    const completion = await io.openai.chat.completions.create("completion", {
      model: "gpt-3.5-turbo",
      messages: [
        {
          role: "user",
          content: "Create a good programming joke about background jobs",
        },
      ],
    });
  },
});

As you can see above, we’ve replicated the API of the OpenAI TypeScript SDK, with a crucial difference of adding the Task Cache Key as the first parameter.

We’ve also added a few convenience methods to make it easier to work with the OpenAI API, especially in a serverless environment. For example, you can run a Chat Completion task in the background with backgroundCreate():

const completion = await io.openai.chat.completions.backgroundCreate("completion", {
  model: "gpt-3.5-turbo",
  messages: [
    {
      role: "user",
      content: "Create a good programming joke about background jobs",
    },
  ],
});

See our full task reference below:

Usage as Universal Client

You can use our OpenAI integration as a universal client to interact with other OpenAI-compatible APIs, such as Perplexity.ai

import { OpenAI } from "@trigger.dev/openai";

const perplexity = new OpenAI({
  id: "perplexity",
  apiKey: process.env["PERPLEXITY_API_KEY"]!,
  baseURL: "https://api.perplexity.ai",
  icon: "brand-open-source",
});

Since Perplexity.ai is compatible with OpenAI, you can use the same tasks as with OpenAI.

client.defineJob({
  id: "perplexity-tasks",
  name: "Perplexity Tasks",
  version: "0.0.1",
  trigger: eventTrigger({
    name: "perplexity.tasks",
  }),
  integrations: {
    perplexity,
  },
  run: async (payload, io, ctx) => {
    await io.perplexity.chat.completions.create("chat-completion", {
      model: "mistral-7b-instruct",
      messages: [
        {
          role: "user",
          content: "Create a good programming joke about background jobs",
        },
      ],
    });

    // Run this in the background
    await io.perplexity.chat.completions.backgroundCreate("background-chat-completion", {
      model: "mistral-7b-instruct",
      messages: [
        {
          role: "user",
          content: "If you were a programming language, what would you be and why?",
        },
      ],
    });
  },
});

And you’ll get the same experience in the Run Dashboard when viewing the logs:

Perplexity.ai logs

Example jobs

Code examples

Check out pre-built jobs using OpenAI in our API section.