OpenAI Universal SDK

Eric AllamEric Allam

Image forOpenAI Universal SDK

We've made a small tweak to our OpenAI integration that allows it to be used with any OpenAI compatible API, such as Perplexity.ai:


_10
import { OpenAI } from "@trigger.dev/openai";
_10
_10
const perplexity = new OpenAI({
_10
id: "perplexity",
_10
apiKey: process.env["PERPLEXITY_API_KEY"]!,
_10
baseURL: "https://api.perplexity.ai", // specify the base URL for Perplexity.ai
_10
icon: "brand-open-source", // change the task icon to a generic open source logo
_10
});

Since Perplexity.ai is compatible with OpenAI, you can use the same tasks as with OpenAI but using Open Source models, like minstral-7b-instruct:


_37
client.defineJob({
_37
id: "perplexity-tasks",
_37
name: "Perplexity Tasks",
_37
version: "0.0.1",
_37
trigger: eventTrigger({
_37
name: "perplexity.tasks",
_37
}),
_37
integrations: {
_37
perplexity,
_37
},
_37
run: async (payload, io, ctx) => {
_37
await io.perplexity.chat.completions.create("chat-completion", {
_37
model: "mistral-7b-instruct",
_37
messages: [
_37
{
_37
role: "user",
_37
content: "Create a good programming joke about background jobs",
_37
},
_37
],
_37
});
_37
_37
// Run this in the background
_37
await io.perplexity.chat.completions.backgroundCreate(
_37
"background-chat-completion",
_37
{
_37
model: "mistral-7b-instruct",
_37
messages: [
_37
{
_37
role: "user",
_37
content:
_37
"If you were a programming language, what would you be and why?",
_37
},
_37
],
_37
}
_37
);
_37
},
_37
});

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

Perplexity.ai logs

We also support the Azure OpenAI Service through the defaultHeaders and defaultQuery options:


_11
import { OpenAI } from "@trigger.dev/openai";
_11
_11
const azureOpenAI = new OpenAI({
_11
id: "azure-openai",
_11
apiKey: process.env["AZURE_API_KEY"]!,
_11
icon: "brand-azure",
_11
baseURL:
_11
"https://my-resource.openai.azure.com/openai/deployments/my-gpt35-16k-deployment",
_11
defaultQuery: { "api-version": "2023-06-01-preview" },
_11
defaultHeaders: { "api-key": process.env["AZURE_API_KEY"] },
_11
});

,