Skip to content

Latest commit

 

History

History
115 lines (90 loc) · 2.72 KB

File metadata and controls

115 lines (90 loc) · 2.72 KB
title OpenAI overview & authentication
sidebarTitle Overview & authentication

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.

<Card title="Jobs Showcase - OpenAI" icon="rocket" href="https://trigger.dev/showcase?tags=&integrations=openai"

Check out pre-built OpenAI jobs in our showcase.

Installing the OpenAI packages

npm install @trigger.dev/openai@latest
pnpm add @trigger.dev/openai@latest
yarn add @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 use it to create tasks.

Perform different AI-powered tasks using OpenAI.

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