fade22015f
* Adds new features table to top of v4 upgrade guide * Adds wait idempotency to wait-until, wait-for, and wait-for-token pages * Adds new priority docs page and updates the v4 upgrade guide * Adds new task lifecycle hooks * Removes the message about requiring tasks to be exported * Adds new global lifecycle hooks section * Moves sections from upgrade guide into the table * Adds hidden task page * Improves the global lifecycle hooks section * Updates middleware and locals section * Adds new useWaitToken page to the react hooks section * Adds a new ai.tool section * Moves Docker (legacy) page into self-hosting section * Removes known issues from v4 upgrade guide * Replace “toolTask” with “ai.tool” in the Streams page example * Renames guide to “Migrating from v3” and adds redirect * Remove references to v4 * Removes changelog from migration guide * The installation guide now references `@latest update` * Changes all references from `/sdk/v3` to `/sdk` * Updates @v4-beta to @latest * Fixed broken link * Fixes broken link * Adds an upgrade to v4 using AI section * Fixes 2 broken links * Adds an entry for targetting preview branches * Updates the run statuses * Adds boolean helpers section to the runs and realtime pages * Updates the concurrency page * Updates the test page to include the new options * Adds SDK and curl options for the preview branch targeting * Updates new bulk actions page * Remove the releasing concurrency section * Got rid of some more @v4-beta mentions * Improved rate limit docs * Improved migrating docs * Removed commented sections of the docs * useWaitToken hook * Fixed the description * Fix for missing test image --------- Co-authored-by: Matt Aitken <matt@mattaitken.com> Co-authored-by: Dan <8297864+D-K-P@users.noreply.github.com>
57 lines
1.5 KiB
Plaintext
57 lines
1.5 KiB
Plaintext
---
|
|
title: "Call OpenAI with retrying"
|
|
sidebarTitle: "OpenAI with retrying"
|
|
description: "This example will show you how to call OpenAI with retrying using Trigger.dev."
|
|
---
|
|
|
|
## Overview
|
|
|
|
Sometimes OpenAI calls can take a long time to complete, or they can fail. This task will retry if the API call fails completely or if the response is empty.
|
|
|
|
## Task code
|
|
|
|
```ts trigger/openai.ts
|
|
import { task } from "@trigger.dev/sdk";
|
|
import OpenAI from "openai";
|
|
|
|
const openai = new OpenAI({
|
|
apiKey: process.env.OPENAI_API_KEY,
|
|
});
|
|
|
|
export const openaiTask = task({
|
|
id: "openai-task",
|
|
//specifying retry options overrides the defaults defined in your trigger.config file
|
|
retry: {
|
|
maxAttempts: 10,
|
|
factor: 1.8,
|
|
minTimeoutInMs: 500,
|
|
maxTimeoutInMs: 30_000,
|
|
randomize: false,
|
|
},
|
|
run: async (payload: { prompt: string }) => {
|
|
//if this fails, it will throw an error and retry
|
|
const chatCompletion = await openai.chat.completions.create({
|
|
messages: [{ role: "user", content: payload.prompt }],
|
|
model: "gpt-3.5-turbo",
|
|
});
|
|
|
|
if (chatCompletion.choices[0]?.message.content === undefined) {
|
|
//sometimes OpenAI returns an empty response, let's retry by throwing an error
|
|
throw new Error("OpenAI call failed");
|
|
}
|
|
|
|
return chatCompletion.choices[0].message.content;
|
|
},
|
|
});
|
|
```
|
|
|
|
## Testing your task
|
|
|
|
To test this task in the dashboard, you can use the following payload:
|
|
|
|
```json
|
|
{
|
|
"prompt": "What is the meaning of life?"
|
|
}
|
|
```
|