Skip to content

Commit

Permalink
feat (docs): document timeouts with abort signals (#3240)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Oct 14, 2024
1 parent ba451aa commit 5219b41
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
15 changes: 14 additions & 1 deletion content/docs/03-ai-sdk-core/25-settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ Maximum number of retries. Set to 0 to disable retries. Default: `2`.

An optional abort signal that can be used to cancel the call.

The abort signal can e.g. be forwarded from a user interface to cancel the call,
or to define a timeout.

#### Example: Timeout

```ts
const result = await generateText({
model: openai('gpt-4o'),
prompt: 'Invent a new holiday and describe its traditions.',
abortSignal: AbortSignal.timeout(5000), // 5 seconds
});
```

### `headers`

Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
Expand All @@ -103,7 +116,7 @@ import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

const result = await generateText({
model: openai('gpt-3.5-turbo'),
model: openai('gpt-4o'),
prompt: 'Invent a new holiday and describe its traditions.',
headers: {
'Prompt-Id': 'my-prompt-id',
Expand Down
17 changes: 17 additions & 0 deletions examples/ai-core/src/generate-text/openai-timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import 'dotenv/config';

async function main() {
const { text, usage } = await generateText({
model: openai('gpt-3.5-turbo'),
prompt: 'Invent a new holiday and describe its traditions.',
abortSignal: AbortSignal.timeout(1000),
});

console.log(text);
console.log();
console.log('Usage:', usage);
}

main().catch(console.error);

0 comments on commit 5219b41

Please sign in to comment.