ai 6.0.30 → 6.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/00-introduction/index.mdx +76 -0
- package/docs/02-foundations/01-overview.mdx +43 -0
- package/docs/02-foundations/02-providers-and-models.mdx +163 -0
- package/docs/02-foundations/03-prompts.mdx +620 -0
- package/docs/02-foundations/04-tools.mdx +160 -0
- package/docs/02-foundations/05-streaming.mdx +62 -0
- package/docs/02-foundations/index.mdx +43 -0
- package/docs/02-getting-started/00-choosing-a-provider.mdx +110 -0
- package/docs/02-getting-started/01-navigating-the-library.mdx +85 -0
- package/docs/02-getting-started/02-nextjs-app-router.mdx +556 -0
- package/docs/02-getting-started/03-nextjs-pages-router.mdx +542 -0
- package/docs/02-getting-started/04-svelte.mdx +627 -0
- package/docs/02-getting-started/05-nuxt.mdx +566 -0
- package/docs/02-getting-started/06-nodejs.mdx +512 -0
- package/docs/02-getting-started/07-expo.mdx +766 -0
- package/docs/02-getting-started/08-tanstack-start.mdx +583 -0
- package/docs/02-getting-started/index.mdx +44 -0
- package/docs/03-agents/01-overview.mdx +96 -0
- package/docs/03-agents/02-building-agents.mdx +367 -0
- package/docs/03-agents/03-workflows.mdx +370 -0
- package/docs/03-agents/04-loop-control.mdx +350 -0
- package/docs/03-agents/05-configuring-call-options.mdx +286 -0
- package/docs/03-agents/index.mdx +40 -0
- package/docs/03-ai-sdk-core/01-overview.mdx +33 -0
- package/docs/03-ai-sdk-core/05-generating-text.mdx +600 -0
- package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +662 -0
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1102 -0
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +375 -0
- package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +144 -0
- package/docs/03-ai-sdk-core/25-settings.mdx +198 -0
- package/docs/03-ai-sdk-core/30-embeddings.mdx +247 -0
- package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
- package/docs/03-ai-sdk-core/35-image-generation.mdx +341 -0
- package/docs/03-ai-sdk-core/36-transcription.mdx +173 -0
- package/docs/03-ai-sdk-core/37-speech.mdx +167 -0
- package/docs/03-ai-sdk-core/40-middleware.mdx +480 -0
- package/docs/03-ai-sdk-core/45-provider-management.mdx +349 -0
- package/docs/03-ai-sdk-core/50-error-handling.mdx +149 -0
- package/docs/03-ai-sdk-core/55-testing.mdx +218 -0
- package/docs/03-ai-sdk-core/60-telemetry.mdx +313 -0
- package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
- package/docs/03-ai-sdk-core/index.mdx +88 -0
- package/docs/04-ai-sdk-ui/01-overview.mdx +44 -0
- package/docs/04-ai-sdk-ui/02-chatbot.mdx +1313 -0
- package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +535 -0
- package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +263 -0
- package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +682 -0
- package/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx +389 -0
- package/docs/04-ai-sdk-ui/05-completion.mdx +186 -0
- package/docs/04-ai-sdk-ui/08-object-generation.mdx +344 -0
- package/docs/04-ai-sdk-ui/20-streaming-data.mdx +397 -0
- package/docs/04-ai-sdk-ui/21-error-handling.mdx +190 -0
- package/docs/04-ai-sdk-ui/21-transport.mdx +174 -0
- package/docs/04-ai-sdk-ui/24-reading-ui-message-streams.mdx +104 -0
- package/docs/04-ai-sdk-ui/25-message-metadata.mdx +152 -0
- package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +477 -0
- package/docs/04-ai-sdk-ui/index.mdx +64 -0
- package/docs/05-ai-sdk-rsc/01-overview.mdx +45 -0
- package/docs/05-ai-sdk-rsc/02-streaming-react-components.mdx +209 -0
- package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +279 -0
- package/docs/05-ai-sdk-rsc/03-saving-and-restoring-states.mdx +105 -0
- package/docs/05-ai-sdk-rsc/04-multistep-interfaces.mdx +282 -0
- package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +158 -0
- package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
- package/docs/05-ai-sdk-rsc/08-error-handling.mdx +96 -0
- package/docs/05-ai-sdk-rsc/09-authentication.mdx +42 -0
- package/docs/05-ai-sdk-rsc/10-migrating-to-ui.mdx +722 -0
- package/docs/05-ai-sdk-rsc/index.mdx +58 -0
- package/docs/06-advanced/01-prompt-engineering.mdx +96 -0
- package/docs/06-advanced/02-stopping-streams.mdx +184 -0
- package/docs/06-advanced/03-backpressure.mdx +173 -0
- package/docs/06-advanced/04-caching.mdx +169 -0
- package/docs/06-advanced/05-multiple-streamables.mdx +68 -0
- package/docs/06-advanced/06-rate-limiting.mdx +60 -0
- package/docs/06-advanced/07-rendering-ui-with-language-models.mdx +213 -0
- package/docs/06-advanced/08-model-as-router.mdx +120 -0
- package/docs/06-advanced/09-multistep-interfaces.mdx +115 -0
- package/docs/06-advanced/09-sequential-generations.mdx +55 -0
- package/docs/06-advanced/10-vercel-deployment-guide.mdx +117 -0
- package/docs/06-advanced/index.mdx +11 -0
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +2142 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3215 -0
- package/docs/07-reference/01-ai-sdk-core/03-generate-object.mdx +780 -0
- package/docs/07-reference/01-ai-sdk-core/04-stream-object.mdx +1140 -0
- package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +190 -0
- package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +171 -0
- package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +309 -0
- package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +227 -0
- package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +138 -0
- package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +214 -0
- package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +203 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +449 -0
- package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +148 -0
- package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +168 -0
- package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +144 -0
- package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +196 -0
- package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +175 -0
- package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +410 -0
- package/docs/07-reference/01-ai-sdk-core/24-mcp-stdio-transport.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/25-json-schema.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx +109 -0
- package/docs/07-reference/01-ai-sdk-core/27-valibot-schema.mdx +55 -0
- package/docs/07-reference/01-ai-sdk-core/28-output.mdx +342 -0
- package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +415 -0
- package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +246 -0
- package/docs/07-reference/01-ai-sdk-core/32-validate-ui-messages.mdx +101 -0
- package/docs/07-reference/01-ai-sdk-core/33-safe-validate-ui-messages.mdx +113 -0
- package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +182 -0
- package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +121 -0
- package/docs/07-reference/01-ai-sdk-core/50-cosine-similarity.mdx +52 -0
- package/docs/07-reference/01-ai-sdk-core/60-wrap-language-model.mdx +59 -0
- package/docs/07-reference/01-ai-sdk-core/61-wrap-image-model.mdx +64 -0
- package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +46 -0
- package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/67-simulate-streaming-middleware.mdx +71 -0
- package/docs/07-reference/01-ai-sdk-core/68-default-settings-middleware.mdx +80 -0
- package/docs/07-reference/01-ai-sdk-core/69-add-tool-input-examples-middleware.mdx +155 -0
- package/docs/07-reference/01-ai-sdk-core/70-extract-json-middleware.mdx +147 -0
- package/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx +84 -0
- package/docs/07-reference/01-ai-sdk-core/71-has-tool-call.mdx +120 -0
- package/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/80-smooth-stream.mdx +145 -0
- package/docs/07-reference/01-ai-sdk-core/90-generate-id.mdx +43 -0
- package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
- package/docs/07-reference/01-ai-sdk-core/index.mdx +159 -0
- package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +446 -0
- package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +179 -0
- package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +178 -0
- package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +230 -0
- package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +108 -0
- package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +151 -0
- package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +113 -0
- package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +73 -0
- package/docs/07-reference/02-ai-sdk-ui/43-read-ui-message-stream.mdx +57 -0
- package/docs/07-reference/02-ai-sdk-ui/46-infer-ui-tools.mdx +99 -0
- package/docs/07-reference/02-ai-sdk-ui/47-infer-ui-tool.mdx +75 -0
- package/docs/07-reference/02-ai-sdk-ui/50-direct-chat-transport.mdx +333 -0
- package/docs/07-reference/02-ai-sdk-ui/index.mdx +89 -0
- package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +767 -0
- package/docs/07-reference/03-ai-sdk-rsc/02-create-ai.mdx +90 -0
- package/docs/07-reference/03-ai-sdk-rsc/03-create-streamable-ui.mdx +91 -0
- package/docs/07-reference/03-ai-sdk-rsc/04-create-streamable-value.mdx +48 -0
- package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +78 -0
- package/docs/07-reference/03-ai-sdk-rsc/06-get-ai-state.mdx +50 -0
- package/docs/07-reference/03-ai-sdk-rsc/07-get-mutable-ai-state.mdx +70 -0
- package/docs/07-reference/03-ai-sdk-rsc/08-use-ai-state.mdx +26 -0
- package/docs/07-reference/03-ai-sdk-rsc/09-use-actions.mdx +42 -0
- package/docs/07-reference/03-ai-sdk-rsc/10-use-ui-state.mdx +35 -0
- package/docs/07-reference/03-ai-sdk-rsc/11-use-streamable-value.mdx +46 -0
- package/docs/07-reference/03-ai-sdk-rsc/20-render.mdx +262 -0
- package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
- package/docs/07-reference/04-stream-helpers/01-ai-stream.mdx +89 -0
- package/docs/07-reference/04-stream-helpers/02-streaming-text-response.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/05-stream-to-response.mdx +108 -0
- package/docs/07-reference/04-stream-helpers/07-openai-stream.mdx +77 -0
- package/docs/07-reference/04-stream-helpers/08-anthropic-stream.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/09-aws-bedrock-stream.mdx +91 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-anthropic-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-messages-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/11-aws-bedrock-cohere-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/12-aws-bedrock-llama-2-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/13-cohere-stream.mdx +78 -0
- package/docs/07-reference/04-stream-helpers/14-google-generative-ai-stream.mdx +85 -0
- package/docs/07-reference/04-stream-helpers/15-hugging-face-stream.mdx +84 -0
- package/docs/07-reference/04-stream-helpers/16-langchain-adapter.mdx +98 -0
- package/docs/07-reference/04-stream-helpers/16-llamaindex-adapter.mdx +70 -0
- package/docs/07-reference/04-stream-helpers/17-mistral-stream.mdx +81 -0
- package/docs/07-reference/04-stream-helpers/18-replicate-stream.mdx +83 -0
- package/docs/07-reference/04-stream-helpers/19-inkeep-stream.mdx +80 -0
- package/docs/07-reference/04-stream-helpers/index.mdx +103 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +30 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-empty-response-body-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-argument-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-message-role-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +47 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-response-data-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-input-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-json-parse-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-api-key-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-setting-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-message-conversion-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-content-generated-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +36 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-object-generated-error.mdx +43 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-speech-generated-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-tool-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-retry-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-too-many-embedding-values-for-call-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-not-found-for-approval-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-repair-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-type-validation-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-unsupported-functionality-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/index.mdx +38 -0
- package/docs/07-reference/index.mdx +34 -0
- package/docs/08-migration-guides/00-versioning.mdx +46 -0
- package/docs/08-migration-guides/24-migration-guide-6-0.mdx +823 -0
- package/docs/08-migration-guides/25-migration-guide-5-0-data.mdx +882 -0
- package/docs/08-migration-guides/26-migration-guide-5-0.mdx +3427 -0
- package/docs/08-migration-guides/27-migration-guide-4-2.mdx +99 -0
- package/docs/08-migration-guides/28-migration-guide-4-1.mdx +14 -0
- package/docs/08-migration-guides/29-migration-guide-4-0.mdx +1157 -0
- package/docs/08-migration-guides/36-migration-guide-3-4.mdx +14 -0
- package/docs/08-migration-guides/37-migration-guide-3-3.mdx +64 -0
- package/docs/08-migration-guides/38-migration-guide-3-2.mdx +46 -0
- package/docs/08-migration-guides/39-migration-guide-3-1.mdx +168 -0
- package/docs/08-migration-guides/index.mdx +22 -0
- package/docs/09-troubleshooting/01-azure-stream-slow.mdx +33 -0
- package/docs/09-troubleshooting/02-client-side-function-calls-not-invoked.mdx +22 -0
- package/docs/09-troubleshooting/03-server-actions-in-client-components.mdx +40 -0
- package/docs/09-troubleshooting/04-strange-stream-output.mdx +36 -0
- package/docs/09-troubleshooting/05-streamable-ui-errors.mdx +16 -0
- package/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx +106 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-deployed.mdx +31 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-proxied.mdx +31 -0
- package/docs/09-troubleshooting/06-timeout-on-vercel.mdx +60 -0
- package/docs/09-troubleshooting/07-unclosed-streams.mdx +34 -0
- package/docs/09-troubleshooting/08-use-chat-failed-to-parse-stream.mdx +26 -0
- package/docs/09-troubleshooting/09-client-stream-error.mdx +25 -0
- package/docs/09-troubleshooting/10-use-chat-tools-no-response.mdx +32 -0
- package/docs/09-troubleshooting/11-use-chat-custom-request-options.mdx +149 -0
- package/docs/09-troubleshooting/12-typescript-performance-zod.mdx +46 -0
- package/docs/09-troubleshooting/12-use-chat-an-error-occurred.mdx +59 -0
- package/docs/09-troubleshooting/13-repeated-assistant-messages.mdx +73 -0
- package/docs/09-troubleshooting/14-stream-abort-handling.mdx +73 -0
- package/docs/09-troubleshooting/14-tool-calling-with-structured-outputs.mdx +48 -0
- package/docs/09-troubleshooting/15-abort-breaks-resumable-streams.mdx +55 -0
- package/docs/09-troubleshooting/15-stream-text-not-working.mdx +33 -0
- package/docs/09-troubleshooting/16-streaming-status-delay.mdx +63 -0
- package/docs/09-troubleshooting/17-use-chat-stale-body-data.mdx +141 -0
- package/docs/09-troubleshooting/18-ontoolcall-type-narrowing.mdx +66 -0
- package/docs/09-troubleshooting/19-unsupported-model-version.mdx +50 -0
- package/docs/09-troubleshooting/20-no-object-generated-content-filter.mdx +72 -0
- package/docs/09-troubleshooting/30-model-is-not-assignable-to-type.mdx +21 -0
- package/docs/09-troubleshooting/40-typescript-cannot-find-namespace-jsx.mdx +24 -0
- package/docs/09-troubleshooting/50-react-maximum-update-depth-exceeded.mdx +39 -0
- package/docs/09-troubleshooting/60-jest-cannot-find-module-ai-rsc.mdx +22 -0
- package/docs/09-troubleshooting/index.mdx +11 -0
- package/package.json +7 -3
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Settings
|
|
3
|
+
description: Learn how to configure the AI SDK.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Settings
|
|
7
|
+
|
|
8
|
+
Large language models (LLMs) typically provide settings to augment their output.
|
|
9
|
+
|
|
10
|
+
All AI SDK functions support the following common settings in addition to the model, the [prompt](./prompts), and additional provider-specific settings:
|
|
11
|
+
|
|
12
|
+
```ts highlight="3-5"
|
|
13
|
+
const result = await generateText({
|
|
14
|
+
model: __MODEL__,
|
|
15
|
+
maxOutputTokens: 512,
|
|
16
|
+
temperature: 0.3,
|
|
17
|
+
maxRetries: 5,
|
|
18
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
<Note>
|
|
23
|
+
Some providers do not support all common settings. If you use a setting with a
|
|
24
|
+
provider that does not support it, a warning will be generated. You can check
|
|
25
|
+
the `warnings` property in the result object to see if any warnings were
|
|
26
|
+
generated.
|
|
27
|
+
</Note>
|
|
28
|
+
|
|
29
|
+
### `maxOutputTokens`
|
|
30
|
+
|
|
31
|
+
Maximum number of tokens to generate.
|
|
32
|
+
|
|
33
|
+
### `temperature`
|
|
34
|
+
|
|
35
|
+
Temperature setting.
|
|
36
|
+
|
|
37
|
+
The value is passed through to the provider. The range depends on the provider and model.
|
|
38
|
+
For most providers, `0` means almost deterministic results, and higher values mean more randomness.
|
|
39
|
+
|
|
40
|
+
It is recommended to set either `temperature` or `topP`, but not both.
|
|
41
|
+
|
|
42
|
+
<Note>In AI SDK 5.0, temperature is no longer set to `0` by default.</Note>
|
|
43
|
+
|
|
44
|
+
### `topP`
|
|
45
|
+
|
|
46
|
+
Nucleus sampling.
|
|
47
|
+
|
|
48
|
+
The value is passed through to the provider. The range depends on the provider and model.
|
|
49
|
+
For most providers, nucleus sampling is a number between 0 and 1.
|
|
50
|
+
E.g. 0.1 would mean that only tokens with the top 10% probability mass are considered.
|
|
51
|
+
|
|
52
|
+
It is recommended to set either `temperature` or `topP`, but not both.
|
|
53
|
+
|
|
54
|
+
### `topK`
|
|
55
|
+
|
|
56
|
+
Only sample from the top K options for each subsequent token.
|
|
57
|
+
|
|
58
|
+
Used to remove "long tail" low probability responses.
|
|
59
|
+
Recommended for advanced use cases only. You usually only need to use `temperature`.
|
|
60
|
+
|
|
61
|
+
### `presencePenalty`
|
|
62
|
+
|
|
63
|
+
The presence penalty affects the likelihood of the model to repeat information that is already in the prompt.
|
|
64
|
+
|
|
65
|
+
The value is passed through to the provider. The range depends on the provider and model.
|
|
66
|
+
For most providers, `0` means no penalty.
|
|
67
|
+
|
|
68
|
+
### `frequencyPenalty`
|
|
69
|
+
|
|
70
|
+
The frequency penalty affects the likelihood of the model to repeatedly use the same words or phrases.
|
|
71
|
+
|
|
72
|
+
The value is passed through to the provider. The range depends on the provider and model.
|
|
73
|
+
For most providers, `0` means no penalty.
|
|
74
|
+
|
|
75
|
+
### `stopSequences`
|
|
76
|
+
|
|
77
|
+
The stop sequences to use for stopping the text generation.
|
|
78
|
+
|
|
79
|
+
If set, the model will stop generating text when one of the stop sequences is generated.
|
|
80
|
+
Providers may have limits on the number of stop sequences.
|
|
81
|
+
|
|
82
|
+
### `seed`
|
|
83
|
+
|
|
84
|
+
It is the seed (integer) to use for random sampling.
|
|
85
|
+
If set and supported by the model, calls will generate deterministic results.
|
|
86
|
+
|
|
87
|
+
### `maxRetries`
|
|
88
|
+
|
|
89
|
+
Maximum number of retries. Set to 0 to disable retries. Default: `2`.
|
|
90
|
+
|
|
91
|
+
### `abortSignal`
|
|
92
|
+
|
|
93
|
+
An optional abort signal that can be used to cancel the call.
|
|
94
|
+
|
|
95
|
+
The abort signal can e.g. be forwarded from a user interface to cancel the call,
|
|
96
|
+
or to define a timeout using `AbortSignal.timeout`.
|
|
97
|
+
|
|
98
|
+
#### Example: AbortSignal.timeout
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const result = await generateText({
|
|
102
|
+
model: __MODEL__,
|
|
103
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
104
|
+
abortSignal: AbortSignal.timeout(5000), // 5 seconds
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### `timeout`
|
|
109
|
+
|
|
110
|
+
An optional timeout in milliseconds. The call will be aborted if it takes longer than the specified duration.
|
|
111
|
+
|
|
112
|
+
This is a convenience parameter that creates an abort signal internally. It can be used alongside `abortSignal` - if both are provided, the call will abort when either condition is met.
|
|
113
|
+
|
|
114
|
+
You can specify the timeout either as a number (milliseconds) or as an object with `totalMs`, `stepMs`, and/or `chunkMs` properties:
|
|
115
|
+
|
|
116
|
+
- `totalMs`: The total timeout for the entire call including all steps.
|
|
117
|
+
- `stepMs`: The timeout for each individual step (LLM call). This is useful for multi-step generations where you want to limit the time spent on each step independently.
|
|
118
|
+
- `chunkMs`: The timeout between stream chunks (streaming only). The call will abort if no new chunk is received within this duration. This is useful for detecting stalled streams.
|
|
119
|
+
|
|
120
|
+
#### Example: 5 second timeout (number format)
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
const result = await generateText({
|
|
124
|
+
model: __MODEL__,
|
|
125
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
126
|
+
timeout: 5000, // 5 seconds
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### Example: 5 second total timeout (object format)
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
const result = await generateText({
|
|
134
|
+
model: __MODEL__,
|
|
135
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
136
|
+
timeout: { totalMs: 5000 }, // 5 seconds
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
#### Example: 10 second step timeout
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
const result = await generateText({
|
|
144
|
+
model: __MODEL__,
|
|
145
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
146
|
+
timeout: { stepMs: 10000 }, // 10 seconds per step
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### Example: Combined total and step timeout
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
const result = await generateText({
|
|
154
|
+
model: __MODEL__,
|
|
155
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
156
|
+
timeout: {
|
|
157
|
+
totalMs: 60000, // 60 seconds total
|
|
158
|
+
stepMs: 10000, // 10 seconds per step
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
#### Example: Per-chunk timeout for streaming (streamText only)
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
const result = streamText({
|
|
167
|
+
model: __MODEL__,
|
|
168
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
169
|
+
timeout: { chunkMs: 5000 }, // abort if no chunk received for 5 seconds
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### `headers`
|
|
174
|
+
|
|
175
|
+
Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
|
|
176
|
+
|
|
177
|
+
You can use the request headers to provide additional information to the provider,
|
|
178
|
+
depending on what the provider supports. For example, some observability providers support
|
|
179
|
+
headers such as `Prompt-Id`.
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
import { generateText } from 'ai';
|
|
183
|
+
__PROVIDER_IMPORT__;
|
|
184
|
+
|
|
185
|
+
const result = await generateText({
|
|
186
|
+
model: __MODEL__,
|
|
187
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
188
|
+
headers: {
|
|
189
|
+
'Prompt-Id': 'my-prompt-id',
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
<Note>
|
|
195
|
+
The `headers` setting is for request-specific headers. You can also set
|
|
196
|
+
`headers` in the provider configuration. These headers will be sent with every
|
|
197
|
+
request made by the provider.
|
|
198
|
+
</Note>
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Embeddings
|
|
3
|
+
description: Learn how to embed values with the AI SDK.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Embeddings
|
|
7
|
+
|
|
8
|
+
Embeddings are a way to represent words, phrases, or images as vectors in a high-dimensional space.
|
|
9
|
+
In this space, similar words are close to each other, and the distance between words can be used to measure their similarity.
|
|
10
|
+
|
|
11
|
+
## Embedding a Single Value
|
|
12
|
+
|
|
13
|
+
The AI SDK provides the [`embed`](/docs/reference/ai-sdk-core/embed) function to embed single values, which is useful for tasks such as finding similar words
|
|
14
|
+
or phrases or clustering text.
|
|
15
|
+
You can use it with embeddings models, e.g. `openai.embeddingModel('text-embedding-3-large')` or `mistral.embeddingModel('mistral-embed')`.
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { embed } from 'ai';
|
|
19
|
+
import { openai } from '@ai-sdk/openai';
|
|
20
|
+
|
|
21
|
+
// 'embedding' is a single embedding object (number[])
|
|
22
|
+
const { embedding } = await embed({
|
|
23
|
+
model: 'openai/text-embedding-3-small',
|
|
24
|
+
value: 'sunny day at the beach',
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Embedding Many Values
|
|
29
|
+
|
|
30
|
+
When loading data, e.g. when preparing a data store for retrieval-augmented generation (RAG),
|
|
31
|
+
it is often useful to embed many values at once (batch embedding).
|
|
32
|
+
|
|
33
|
+
The AI SDK provides the [`embedMany`](/docs/reference/ai-sdk-core/embed-many) function for this purpose.
|
|
34
|
+
Similar to `embed`, you can use it with embeddings models,
|
|
35
|
+
e.g. `openai.embeddingModel('text-embedding-3-large')` or `mistral.embeddingModel('mistral-embed')`.
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { openai } from '@ai-sdk/openai';
|
|
39
|
+
import { embedMany } from 'ai';
|
|
40
|
+
|
|
41
|
+
// 'embeddings' is an array of embedding objects (number[][]).
|
|
42
|
+
// It is sorted in the same order as the input values.
|
|
43
|
+
const { embeddings } = await embedMany({
|
|
44
|
+
model: 'openai/text-embedding-3-small',
|
|
45
|
+
values: [
|
|
46
|
+
'sunny day at the beach',
|
|
47
|
+
'rainy afternoon in the city',
|
|
48
|
+
'snowy night in the mountains',
|
|
49
|
+
],
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Embedding Similarity
|
|
54
|
+
|
|
55
|
+
After embedding values, you can calculate the similarity between them using the [`cosineSimilarity`](/docs/reference/ai-sdk-core/cosine-similarity) function.
|
|
56
|
+
This is useful to e.g. find similar words or phrases in a dataset.
|
|
57
|
+
You can also rank and filter related items based on their similarity.
|
|
58
|
+
|
|
59
|
+
```ts highlight={"2,10"}
|
|
60
|
+
import { openai } from '@ai-sdk/openai';
|
|
61
|
+
import { cosineSimilarity, embedMany } from 'ai';
|
|
62
|
+
|
|
63
|
+
const { embeddings } = await embedMany({
|
|
64
|
+
model: 'openai/text-embedding-3-small',
|
|
65
|
+
values: ['sunny day at the beach', 'rainy afternoon in the city'],
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
console.log(
|
|
69
|
+
`cosine similarity: ${cosineSimilarity(embeddings[0], embeddings[1])}`,
|
|
70
|
+
);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Token Usage
|
|
74
|
+
|
|
75
|
+
Many providers charge based on the number of tokens used to generate embeddings.
|
|
76
|
+
Both `embed` and `embedMany` provide token usage information in the `usage` property of the result object:
|
|
77
|
+
|
|
78
|
+
```ts highlight={"4,9"}
|
|
79
|
+
import { openai } from '@ai-sdk/openai';
|
|
80
|
+
import { embed } from 'ai';
|
|
81
|
+
|
|
82
|
+
const { embedding, usage } = await embed({
|
|
83
|
+
model: 'openai/text-embedding-3-small',
|
|
84
|
+
value: 'sunny day at the beach',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
console.log(usage); // { tokens: 10 }
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Settings
|
|
91
|
+
|
|
92
|
+
### Provider Options
|
|
93
|
+
|
|
94
|
+
Embedding model settings can be configured using `providerOptions` for provider-specific parameters:
|
|
95
|
+
|
|
96
|
+
```ts highlight={"5-9"}
|
|
97
|
+
import { openai } from '@ai-sdk/openai';
|
|
98
|
+
import { embed } from 'ai';
|
|
99
|
+
|
|
100
|
+
const { embedding } = await embed({
|
|
101
|
+
model: 'openai/text-embedding-3-small',
|
|
102
|
+
value: 'sunny day at the beach',
|
|
103
|
+
providerOptions: {
|
|
104
|
+
openai: {
|
|
105
|
+
dimensions: 512, // Reduce embedding dimensions
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Parallel Requests
|
|
112
|
+
|
|
113
|
+
The `embedMany` function now supports parallel processing with configurable `maxParallelCalls` to optimize performance:
|
|
114
|
+
|
|
115
|
+
```ts highlight={"4"}
|
|
116
|
+
import { openai } from '@ai-sdk/openai';
|
|
117
|
+
import { embedMany } from 'ai';
|
|
118
|
+
|
|
119
|
+
const { embeddings, usage } = await embedMany({
|
|
120
|
+
maxParallelCalls: 2, // Limit parallel requests
|
|
121
|
+
model: 'openai/text-embedding-3-small',
|
|
122
|
+
values: [
|
|
123
|
+
'sunny day at the beach',
|
|
124
|
+
'rainy afternoon in the city',
|
|
125
|
+
'snowy night in the mountains',
|
|
126
|
+
],
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Retries
|
|
131
|
+
|
|
132
|
+
Both `embed` and `embedMany` accept an optional `maxRetries` parameter of type `number`
|
|
133
|
+
that you can use to set the maximum number of retries for the embedding process.
|
|
134
|
+
It defaults to `2` retries (3 attempts in total). You can set it to `0` to disable retries.
|
|
135
|
+
|
|
136
|
+
```ts highlight={"7"}
|
|
137
|
+
import { openai } from '@ai-sdk/openai';
|
|
138
|
+
import { embed } from 'ai';
|
|
139
|
+
|
|
140
|
+
const { embedding } = await embed({
|
|
141
|
+
model: 'openai/text-embedding-3-small',
|
|
142
|
+
value: 'sunny day at the beach',
|
|
143
|
+
maxRetries: 0, // Disable retries
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Abort Signals and Timeouts
|
|
148
|
+
|
|
149
|
+
Both `embed` and `embedMany` accept an optional `abortSignal` parameter of
|
|
150
|
+
type [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
|
|
151
|
+
that you can use to abort the embedding process or set a timeout.
|
|
152
|
+
|
|
153
|
+
```ts highlight={"7"}
|
|
154
|
+
import { openai } from '@ai-sdk/openai';
|
|
155
|
+
import { embed } from 'ai';
|
|
156
|
+
|
|
157
|
+
const { embedding } = await embed({
|
|
158
|
+
model: 'openai/text-embedding-3-small',
|
|
159
|
+
value: 'sunny day at the beach',
|
|
160
|
+
abortSignal: AbortSignal.timeout(1000), // Abort after 1 second
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Custom Headers
|
|
165
|
+
|
|
166
|
+
Both `embed` and `embedMany` accept an optional `headers` parameter of type `Record<string, string>`
|
|
167
|
+
that you can use to add custom headers to the embedding request.
|
|
168
|
+
|
|
169
|
+
```ts highlight={"7"}
|
|
170
|
+
import { openai } from '@ai-sdk/openai';
|
|
171
|
+
import { embed } from 'ai';
|
|
172
|
+
|
|
173
|
+
const { embedding } = await embed({
|
|
174
|
+
model: 'openai/text-embedding-3-small',
|
|
175
|
+
value: 'sunny day at the beach',
|
|
176
|
+
headers: { 'X-Custom-Header': 'custom-value' },
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Response Information
|
|
181
|
+
|
|
182
|
+
Both `embed` and `embedMany` return response information that includes the raw provider response:
|
|
183
|
+
|
|
184
|
+
```ts highlight={"4,9"}
|
|
185
|
+
import { openai } from '@ai-sdk/openai';
|
|
186
|
+
import { embed } from 'ai';
|
|
187
|
+
|
|
188
|
+
const { embedding, response } = await embed({
|
|
189
|
+
model: 'openai/text-embedding-3-small',
|
|
190
|
+
value: 'sunny day at the beach',
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
console.log(response); // Raw provider response
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Embedding Middleware
|
|
197
|
+
|
|
198
|
+
You can enhance embedding models, e.g. to set default values, using
|
|
199
|
+
`wrapEmbeddingModel` and `EmbeddingModelV3Middleware`.
|
|
200
|
+
|
|
201
|
+
Here is an example that uses the built-in `defaultEmbeddingSettingsMiddleware`:
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
import {
|
|
205
|
+
customProvider,
|
|
206
|
+
defaultEmbeddingSettingsMiddleware,
|
|
207
|
+
embed,
|
|
208
|
+
wrapEmbeddingModel,
|
|
209
|
+
gateway,
|
|
210
|
+
} from 'ai';
|
|
211
|
+
|
|
212
|
+
const embeddingModelWithDefaults = wrapEmbeddingModel({
|
|
213
|
+
model: gateway.embeddingModel('google/gemini-embedding-001'),
|
|
214
|
+
middleware: defaultEmbeddingSettingsMiddleware({
|
|
215
|
+
settings: {
|
|
216
|
+
providerOptions: {
|
|
217
|
+
google: {
|
|
218
|
+
outputDimensionality: 256,
|
|
219
|
+
taskType: 'CLASSIFICATION',
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
}),
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Embedding Providers & Models
|
|
228
|
+
|
|
229
|
+
Several providers offer embedding models:
|
|
230
|
+
|
|
231
|
+
| Provider | Model | Embedding Dimensions |
|
|
232
|
+
| ----------------------------------------------------------------------------------------- | ------------------------------- | -------------------- |
|
|
233
|
+
| [OpenAI](/providers/ai-sdk-providers/openai#embedding-models) | `text-embedding-3-large` | 3072 |
|
|
234
|
+
| [OpenAI](/providers/ai-sdk-providers/openai#embedding-models) | `text-embedding-3-small` | 1536 |
|
|
235
|
+
| [OpenAI](/providers/ai-sdk-providers/openai#embedding-models) | `text-embedding-ada-002` | 1536 |
|
|
236
|
+
| [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai#embedding-models) | `gemini-embedding-001` | 3072 |
|
|
237
|
+
| [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai#embedding-models) | `text-embedding-004` | 768 |
|
|
238
|
+
| [Mistral](/providers/ai-sdk-providers/mistral#embedding-models) | `mistral-embed` | 1024 |
|
|
239
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#embedding-models) | `embed-english-v3.0` | 1024 |
|
|
240
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#embedding-models) | `embed-multilingual-v3.0` | 1024 |
|
|
241
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#embedding-models) | `embed-english-light-v3.0` | 384 |
|
|
242
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#embedding-models) | `embed-multilingual-light-v3.0` | 384 |
|
|
243
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#embedding-models) | `embed-english-v2.0` | 4096 |
|
|
244
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#embedding-models) | `embed-english-light-v2.0` | 1024 |
|
|
245
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#embedding-models) | `embed-multilingual-v2.0` | 768 |
|
|
246
|
+
| [Amazon Bedrock](/providers/ai-sdk-providers/amazon-bedrock#embedding-models) | `amazon.titan-embed-text-v1` | 1536 |
|
|
247
|
+
| [Amazon Bedrock](/providers/ai-sdk-providers/amazon-bedrock#embedding-models) | `amazon.titan-embed-text-v2:0` | 1024 |
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Reranking
|
|
3
|
+
description: Learn how to rerank documents with the AI SDK.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Reranking
|
|
7
|
+
|
|
8
|
+
Reranking is a technique used to improve search relevance by reordering a set of documents based on their relevance to a query.
|
|
9
|
+
Unlike embedding-based similarity search, reranking models are specifically trained to understand the relationship between queries and documents,
|
|
10
|
+
often producing more accurate relevance scores.
|
|
11
|
+
|
|
12
|
+
## Reranking Documents
|
|
13
|
+
|
|
14
|
+
The AI SDK provides the [`rerank`](/docs/reference/ai-sdk-core/rerank) function to rerank documents based on their relevance to a query.
|
|
15
|
+
You can use it with reranking models, e.g. `cohere.reranking('rerank-v3.5')` or `bedrock.reranking('cohere.rerank-v3-5:0')`.
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { rerank } from 'ai';
|
|
19
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
20
|
+
|
|
21
|
+
const documents = [
|
|
22
|
+
'sunny day at the beach',
|
|
23
|
+
'rainy afternoon in the city',
|
|
24
|
+
'snowy night in the mountains',
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
const { ranking } = await rerank({
|
|
28
|
+
model: cohere.reranking('rerank-v3.5'),
|
|
29
|
+
documents,
|
|
30
|
+
query: 'talk about rain',
|
|
31
|
+
topN: 2, // Return top 2 most relevant documents
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
console.log(ranking);
|
|
35
|
+
// [
|
|
36
|
+
// { originalIndex: 1, score: 0.9, document: 'rainy afternoon in the city' },
|
|
37
|
+
// { originalIndex: 0, score: 0.3, document: 'sunny day at the beach' }
|
|
38
|
+
// ]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Working with Object Documents
|
|
42
|
+
|
|
43
|
+
Reranking also supports structured documents (JSON objects), making it ideal for searching through databases, emails, or other structured content:
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
import { rerank } from 'ai';
|
|
47
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
48
|
+
|
|
49
|
+
const documents = [
|
|
50
|
+
{
|
|
51
|
+
from: 'Paul Doe',
|
|
52
|
+
subject: 'Follow-up',
|
|
53
|
+
text: 'We are happy to give you a discount of 20% on your next order.',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
from: 'John McGill',
|
|
57
|
+
subject: 'Missing Info',
|
|
58
|
+
text: 'Sorry, but here is the pricing information from Oracle: $5000/month',
|
|
59
|
+
},
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
const { ranking, rerankedDocuments } = await rerank({
|
|
63
|
+
model: cohere.reranking('rerank-v3.5'),
|
|
64
|
+
documents,
|
|
65
|
+
query: 'Which pricing did we get from Oracle?',
|
|
66
|
+
topN: 1,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
console.log(rerankedDocuments[0]);
|
|
70
|
+
// { from: 'John McGill', subject: 'Missing Info', text: '...' }
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Understanding the Results
|
|
74
|
+
|
|
75
|
+
The `rerank` function returns a comprehensive result object:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
79
|
+
import { rerank } from 'ai';
|
|
80
|
+
|
|
81
|
+
const { ranking, rerankedDocuments, originalDocuments } = await rerank({
|
|
82
|
+
model: cohere.reranking('rerank-v3.5'),
|
|
83
|
+
documents: ['sunny day at the beach', 'rainy afternoon in the city'],
|
|
84
|
+
query: 'talk about rain',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// ranking: sorted array of { originalIndex, score, document }
|
|
88
|
+
// rerankedDocuments: documents sorted by relevance (convenience property)
|
|
89
|
+
// originalDocuments: original documents array
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Each item in the `ranking` array contains:
|
|
93
|
+
|
|
94
|
+
- `originalIndex`: Position in the original documents array
|
|
95
|
+
- `score`: Relevance score (typically 0-1, where higher is more relevant)
|
|
96
|
+
- `document`: The original document
|
|
97
|
+
|
|
98
|
+
## Settings
|
|
99
|
+
|
|
100
|
+
### Top-N Results
|
|
101
|
+
|
|
102
|
+
Use `topN` to limit the number of results returned. This is useful for retrieving only the most relevant documents:
|
|
103
|
+
|
|
104
|
+
```ts highlight={"7"}
|
|
105
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
106
|
+
import { rerank } from 'ai';
|
|
107
|
+
|
|
108
|
+
const { ranking } = await rerank({
|
|
109
|
+
model: cohere.reranking('rerank-v3.5'),
|
|
110
|
+
documents: ['doc1', 'doc2', 'doc3', 'doc4', 'doc5'],
|
|
111
|
+
query: 'relevant information',
|
|
112
|
+
topN: 3, // Return only top 3 most relevant documents
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Provider Options
|
|
117
|
+
|
|
118
|
+
Reranking model settings can be configured using `providerOptions` for provider-specific parameters:
|
|
119
|
+
|
|
120
|
+
```ts highlight={"8-12"}
|
|
121
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
122
|
+
import { rerank } from 'ai';
|
|
123
|
+
|
|
124
|
+
const { ranking } = await rerank({
|
|
125
|
+
model: cohere.reranking('rerank-v3.5'),
|
|
126
|
+
documents: ['sunny day at the beach', 'rainy afternoon in the city'],
|
|
127
|
+
query: 'talk about rain',
|
|
128
|
+
providerOptions: {
|
|
129
|
+
cohere: {
|
|
130
|
+
maxTokensPerDoc: 1000, // Limit tokens per document
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Retries
|
|
137
|
+
|
|
138
|
+
The `rerank` function accepts an optional `maxRetries` parameter of type `number`
|
|
139
|
+
that you can use to set the maximum number of retries for the reranking process.
|
|
140
|
+
It defaults to `2` retries (3 attempts in total). You can set it to `0` to disable retries.
|
|
141
|
+
|
|
142
|
+
```ts highlight={"7"}
|
|
143
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
144
|
+
import { rerank } from 'ai';
|
|
145
|
+
|
|
146
|
+
const { ranking } = await rerank({
|
|
147
|
+
model: cohere.reranking('rerank-v3.5'),
|
|
148
|
+
documents: ['sunny day at the beach', 'rainy afternoon in the city'],
|
|
149
|
+
query: 'talk about rain',
|
|
150
|
+
maxRetries: 0, // Disable retries
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Abort Signals and Timeouts
|
|
155
|
+
|
|
156
|
+
The `rerank` function accepts an optional `abortSignal` parameter of
|
|
157
|
+
type [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
|
|
158
|
+
that you can use to abort the reranking process or set a timeout.
|
|
159
|
+
|
|
160
|
+
```ts highlight={"7"}
|
|
161
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
162
|
+
import { rerank } from 'ai';
|
|
163
|
+
|
|
164
|
+
const { ranking } = await rerank({
|
|
165
|
+
model: cohere.reranking('rerank-v3.5'),
|
|
166
|
+
documents: ['sunny day at the beach', 'rainy afternoon in the city'],
|
|
167
|
+
query: 'talk about rain',
|
|
168
|
+
abortSignal: AbortSignal.timeout(5000), // Abort after 5 seconds
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Custom Headers
|
|
173
|
+
|
|
174
|
+
The `rerank` function accepts an optional `headers` parameter of type `Record<string, string>`
|
|
175
|
+
that you can use to add custom headers to the reranking request.
|
|
176
|
+
|
|
177
|
+
```ts highlight={"7"}
|
|
178
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
179
|
+
import { rerank } from 'ai';
|
|
180
|
+
|
|
181
|
+
const { ranking } = await rerank({
|
|
182
|
+
model: cohere.reranking('rerank-v3.5'),
|
|
183
|
+
documents: ['sunny day at the beach', 'rainy afternoon in the city'],
|
|
184
|
+
query: 'talk about rain',
|
|
185
|
+
headers: { 'X-Custom-Header': 'custom-value' },
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Response Information
|
|
190
|
+
|
|
191
|
+
The `rerank` function returns response information that includes the raw provider response:
|
|
192
|
+
|
|
193
|
+
```ts highlight={"4,10"}
|
|
194
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
195
|
+
import { rerank } from 'ai';
|
|
196
|
+
|
|
197
|
+
const { ranking, response } = await rerank({
|
|
198
|
+
model: cohere.reranking('rerank-v3.5'),
|
|
199
|
+
documents: ['sunny day at the beach', 'rainy afternoon in the city'],
|
|
200
|
+
query: 'talk about rain',
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
console.log(response); // { id, timestamp, modelId, headers, body }
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Reranking Providers & Models
|
|
207
|
+
|
|
208
|
+
Several providers offer reranking models:
|
|
209
|
+
|
|
210
|
+
| Provider | Model |
|
|
211
|
+
| ----------------------------------------------------------------------------- | ------------------------------------- |
|
|
212
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#reranking-models) | `rerank-v3.5` |
|
|
213
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#reranking-models) | `rerank-english-v3.0` |
|
|
214
|
+
| [Cohere](/providers/ai-sdk-providers/cohere#reranking-models) | `rerank-multilingual-v3.0` |
|
|
215
|
+
| [Amazon Bedrock](/providers/ai-sdk-providers/amazon-bedrock#reranking-models) | `amazon.rerank-v1:0` |
|
|
216
|
+
| [Amazon Bedrock](/providers/ai-sdk-providers/amazon-bedrock#reranking-models) | `cohere.rerank-v3-5:0` |
|
|
217
|
+
| [Together.ai](/providers/ai-sdk-providers/togetherai#reranking-models) | `Salesforce/Llama-Rank-v1` |
|
|
218
|
+
| [Together.ai](/providers/ai-sdk-providers/togetherai#reranking-models) | `mixedbread-ai/Mxbai-Rerank-Large-V2` |
|