Skip to content

Docs/task examples #1293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions docs/examples/generate-image-with-dall-e3.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: "Generate an image using DALL·E 3"
sidebarTitle: "Generate image using DALL·E"
description: "This example will show you how to generate an image using DALL·E 3 and text using GPT-4o with Trigger.dev."
---

## Overview

This example demonstrates how to use Trigger.dev to make reliable calls to AI APIs, specifically OpenAI's GPT-4o and DALL-E 3. It showcases automatic retrying with a maximum of 3 attempts, built-in error handling to avoid timeouts, and the ability to trace and monitor API calls.

## Task code

```ts trigger/generateContent.ts
import { task } from "@trigger.dev/sdk/v3";
import OpenAI from "openai";

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

type Payload = {
theme: string;
description: string;
};

export const generateContent = task({
id: "generate-content",
retry: {
maxAttempts: 3, // Retry up to 3 times
},
run: async ({ theme, description }: Payload) => {

// Generate text
const textResult = await openai.chat.completions.create({
model: "gpt-4o",
messages: generateTextPrompt(theme, description),
});

if (!textResult.choices[0]) {
throw new Error("No content, retrying…");
}

// Generate image
const imageResult = await openai.images.generate({
model: "dall-e-3",
prompt: generateImagePrompt(theme, description),
});

if (!imageResult.data[0]) {
throw new Error("No image, retrying…");
}

return {
text: textResult.choices[0],
image: imageResult.data[0].url,
};
},
});

function generateTextPrompt(theme: string, description: string): any {
return `Theme: ${theme}\n\nDescription: ${description}`;
}

function generateImagePrompt(theme: string, description: string): any {
return `Theme: ${theme}\n\nDescription: ${description}`;
}
```

## GitHub repo

View this example task [on GitHub](https://github.com/triggerdotdev/v3-test-projects/blob/main/website-examples/trigger/generateContent.ts).
13 changes: 11 additions & 2 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"topbarLinks": [
{
"name": "v2 docs",
"name": "Switch to v2 docs",
"url": "https://v2docs.trigger.dev/documentation/introduction"
},
{
Expand Down Expand Up @@ -79,6 +79,11 @@
"name": "Guides",
"icon": "book",
"url": "guides"
},
{
"name": "Examples",
"icon": "code",
"url": "examples"
}
],
"navigation": [
Expand Down Expand Up @@ -229,8 +234,12 @@
"pages": ["guides/dashboard/creating-a-project"]
},
{
"group": "Use cases",
"group": "Migrations",
"pages": ["guides/use-cases/upgrading-from-v2"]
},
{
"group": "Examples",
"pages": ["examples/generate-image-with-dall-e3"]
}
],
"footerSocials": {
Expand Down
Loading