| title | OpenAI overview & authentication |
|---|---|
| sidebarTitle | Overview & authentication |
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.
npm install @trigger.dev/openai@latestpnpm add @trigger.dev/openai@latestyarn add @trigger.dev/openai@latestTo 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!,
});Once you have set up a OpenAI client, you can use it to create tasks.
Perform different AI-powered tasks using OpenAI.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:
