Incorporate improved OpenAI docs from old repo

This commit is contained in:
Eric Allam
2023-08-03 09:51:25 +01:00
parent 87c302bf26
commit 34d77e830c
+163 -20
View File
@@ -2,10 +2,17 @@
title: Introduction
---
Trigger.dev provides seamless integration with OpenAI, enabling developers to harness the power of AI
language models in their serverless applications. With Trigger.dev's background tasks, long-running
OpenAI completions become possible, even within the constraints of serverless timeouts.
<Snippet file="integration-getting-started.mdx" />
## Installation
To get started with the OpenAI integration on Trigger.dev, you need to install the `@trigger.dev/openai` package.
You can do this using npm, pnpm, or yarn:
<CodeGroup>
```bash npm
@@ -13,7 +20,7 @@ npm install @trigger.dev/openai@latest
```
```bash pnpm
pnpm install @trigger.dev/openai@latest
pnpm add @trigger.dev/openai@latest
```
```bash yarn
@@ -24,7 +31,8 @@ yarn add @trigger.dev/openai@latest
## Authentication
OpenAI supports API Keys
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](https://platform.openai.com/account/api-keys).
```ts
import { OpenAI } from "@trigger.dev/openai";
@@ -35,10 +43,12 @@ const openai = new OpenAI({
});
```
## Example
## Usage
Include the OpenAI integration in your Trigger.dev job:
```ts
new Job(client, {
client.defineJob({
id: "openai-tasks",
name: "OpenAI Tasks",
version: "0.0.1",
@@ -50,20 +60,11 @@ new Job(client, {
openai,
},
run: async (payload, io, ctx) => {
const response = await io.openai.backgroundCreateChatCompletion(
"background-chat-completion",
{
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: "Create a good programming joke about background jobs",
},
],
}
);
await io.logger.info("choices", response.choices);
// Now you can access the OpenAI tasks through the io object
await io.openai.createCompletion("completion", {
model: "davinci",
prompt: "Once upon a time",
});
},
});
```
@@ -75,9 +76,9 @@ Tasks that are marked as "long-running" can last longer than your serverless tim
| Function Name | Description | Long-running? |
| -------------------------------- | ------------------------------------------------------------------------- | ------------- |
| `createCompletion` | Generates text completions given a prompt. |
| `backgroundCreateCompletion` | Generates text completions in the background. | ✔ |
| `backgroundCreateCompletion` | Generates text completions in the background. | ✔ |
| `createChatCompletion` | Generates text completions in a conversational context. |
| `backgroundCreateChatCompletion` | Generates text completions in a conversational context in the background. | ✔ |
| `backgroundCreateChatCompletion` | Generates text completions in a conversational context in the background. | ✔ |
| `retrieveModel` | Retrieves a specific model by ID. |
| `listModels` | Lists the available models. |
| `createEdit` | Edits a given text prompt. |
@@ -92,3 +93,145 @@ Tasks that are marked as "long-running" can last longer than your serverless tim
| `cancelFineTune` | Cancels a specific fine-tune by ID. |
| `listFineTuneEvents` | Lists the events for a specific fine-tune by ID. |
| `deleteFineTune` | Deletes a specific fine-tune by ID. |
## Examples
### Generate a joke
Here's an example of how to use the OpenAI integration in a Trigger.dev job.
In this example, we'll create a background task to generate a programming joke.
```ts
client.defineJob({
id: "openai-tasks",
name: "OpenAI Tasks",
version: "0.0.1",
trigger: eventTrigger({
name: "openai.tasks",
schema: z.object({}),
}),
integrations: {
openai,
},
run: async (payload, io, ctx) => {
const response = await io.openai.backgroundCreateChatCompletion("background-chat-completion", {
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: "Create a good programming joke about background jobs",
},
],
});
await io.logger.info("choices", response.choices);
},
});
```
### Generate Code Snippets
In this example, we'll leverage Trigger.dev's background task to generate code snippets for
a given programming task:
```ts
client.defineJob({
id: "openai-tasks",
name: "OpenAI Tasks",
version: "0.0.1",
trigger: eventTrigger({
name: "openai.tasks",
schema: z.object({}),
}),
integrations: {
openai,
},
run: async (payload, io, ctx) => {
const programmingTask = `Create a function that checks if a string is a palindrome.`;
const response = await io.openai.backgroundCreateCompletion("background-completion", {
model: "gpt-3.5-turbo",
prompt: `Coding task: ${programmingTask}\n\n`,
});
await io.logger.info("codeSnippet", response.choices[0]?.text);
},
});
```
### Summarize Text
We'll use Trigger.dev's background task to summarize a lengthy article:
```ts
client.defineJob({
id: "openai-tasks",
name: "OpenAI Tasks",
version: "0.0.1",
trigger: eventTrigger({
name: "openai.tasks",
schema: z.object({}),
}),
integrations: {
openai,
},
run: async (payload, io, ctx) => {
const articleToSummarize = `Lorem ipsum. olor sit amet, consectetur adipiscing elit.
Sed nec aliquet sapien. Pellentesque vitae nisi id purus luctus tincidunt.
Proin condimentum malesuada turpis, eget tincidunt mauris viverra in.`;
const response = await io.openai.backgroundCreateCompletion("background-completion", {
model: "gpt-3.5-turbo",
prompt: `Please summarize the following article:\n\n${articleToSummarize}`,
});
await io.logger.info("summary", response.choices[0]?.text);
},
});
```
### Draft Email Response
we'll use Trigger.dev's background task to draft an email response based on a given email content:
```ts
client.defineJob({
id: "openai-tasks",
name: "OpenAI Tasks",
version: "0.0.1",
trigger: eventTrigger({
name: "openai.tasks",
schema: z.object({}),
}),
integrations: {
openai,
},
run: async (payload, io, ctx) => {
const emailContent = `Dear John,
Thank you for your inquiry. We appreciate your interest in our products.
I have reviewed your request, and I'm pleased to inform you that we can
accommodate your requirements. Please find the attached proposal for your
reference. If you have any further questions, feel free to ask.
Best regards,
Jane Doe`;
const response = await io.openai.backgroundCreateChatCompletion("background-chat-completion", {
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: emailContent,
},
{
role: "assistant",
content: "Draft a suitable response to the email above.",
},
],
});
await io.logger.info("draftedEmailResponse", response.choices[0]?.text);
},
});
```