├── README.md ├── examples ├── next-ai-rsc │ ├── .env.example │ ├── postcss.config.js │ ├── public │ │ ├── favicon.ico │ │ ├── favicon-16x16.png │ │ └── apple-touch-icon.png │ ├── .gitignore │ ├── next-env.d.ts │ ├── components.json │ ├── components │ │ ├── chat-list.tsx │ │ ├── llm-stocks │ │ │ ├── spinner.tsx │ │ │ ├── stocks-skeleton.tsx │ │ │ └── events-skeleton.tsx │ │ ├── providers.tsx │ │ └── footer.tsx │ ├── app │ │ └── stream-object │ │ │ └── itinerary.tsx │ ├── lib │ │ └── hooks │ │ │ ├── use-at-bottom.tsx │ │ │ └── use-enter-submit.tsx │ └── tsconfig.json ├── next-cohere │ ├── .env.local.example │ ├── app │ │ ├── globals.css │ │ └── favicon.ico │ ├── next.config.js │ ├── postcss.config.js │ ├── .gitignore │ ├── tailwind.config.js │ └── tsconfig.json ├── next-openai │ ├── .env.local.example │ ├── app │ │ ├── globals.css │ │ ├── favicon.ico │ │ ├── stream-assistant-responses │ │ │ ├── layout.tsx │ │ │ └── message.tsx │ │ ├── stream-assistant-responses-with-tools │ │ │ ├── layout.tsx │ │ │ └── message.tsx │ │ ├── completion-rsc │ │ │ └── generate-completion.ts │ │ ├── layout.tsx │ │ └── stream-object │ │ │ └── schema.ts │ ├── next.config.js │ ├── postcss.config.js │ ├── .gitignore │ ├── tailwind.config.js │ └── tsconfig.json ├── sveltekit-openai │ ├── .env.example │ ├── .npmrc │ ├── static │ │ ├── robots.txt │ │ └── favicon.png │ ├── src │ │ ├── routes │ │ │ └── +page.ts │ │ ├── app.d.ts │ │ └── app.html │ ├── vite.config.ts │ ├── .gitignore │ └── tsconfig.json ├── next-langchain │ ├── .env.local.example │ ├── app │ │ ├── globals.css │ │ ├── favicon.ico │ │ ├── layout.tsx │ │ └── api │ │ │ ├── completion │ │ │ └── route.ts │ │ │ └── completion-string-output-parser │ │ │ └── route.ts │ ├── next.config.js │ ├── postcss.config.js │ ├── .gitignore │ ├── tailwind.config.js │ └── tsconfig.json ├── next-replicate │ ├── .env.local.example │ ├── app │ │ ├── globals.css │ │ ├── favicon.ico │ │ └── layout.tsx │ ├── next.config.js │ ├── postcss.config.js │ ├── .gitignore │ ├── tailwind.config.js │ └── tsconfig.json ├── nuxt-openai │ ├── .env.example │ ├── server │ │ └── tsconfig.json │ ├── tsconfig.json │ ├── .gitignore │ ├── nuxt.config.ts │ └── pages │ │ └── index.vue ├── solidstart-openai │ ├── .env.example │ ├── src │ │ ├── root.css │ │ ├── entry-client.tsx │ │ ├── entry-server.tsx │ │ └── routes │ │ │ └── api │ │ │ └── chat │ │ │ └── index.ts │ ├── public │ │ └── favicon.ico │ ├── postcss.config.cjs │ ├── vite.config.ts │ ├── tailwind.config.cjs │ ├── .gitignore │ └── tsconfig.json ├── next-huggingface │ ├── .env.local.example │ ├── app │ │ ├── globals.css │ │ ├── favicon.ico │ │ └── layout.tsx │ ├── next.config.js │ ├── postcss.config.js │ ├── .gitignore │ ├── tailwind.config.js │ └── tsconfig.json ├── next-inkeep │ ├── .env.local.example │ ├── app │ │ ├── globals.css │ │ ├── favicon.ico │ │ └── layout.tsx │ ├── next.config.js │ ├── postcss.config.js │ ├── .gitignore │ ├── tailwind.config.js │ └── tsconfig.json ├── next-openai-pages │ ├── .env.local.example │ ├── styles │ │ └── globals.css │ ├── next.config.js │ ├── postcss.config.js │ ├── pages │ │ ├── _app.tsx │ │ ├── _document.tsx │ │ ├── api │ │ │ ├── chat-edge.ts │ │ │ └── chat-api-route.ts │ │ └── basics │ │ │ └── stream-text │ │ │ └── index.tsx │ ├── app │ │ └── api │ │ │ ├── stream-text │ │ │ └── route.ts │ │ │ ├── generate-text │ │ │ └── route.ts │ │ │ ├── stream-chat │ │ │ └── route.ts │ │ │ └── generate-chat │ │ │ └── route.ts │ ├── .gitignore │ └── tailwind.config.js ├── next-aws-bedrock │ ├── app │ │ ├── globals.css │ │ ├── favicon.ico │ │ └── layout.tsx │ ├── next.config.js │ ├── postcss.config.js │ ├── .env.local.example │ ├── .gitignore │ ├── tailwind.config.js │ └── tsconfig.json ├── next-openai-rate-limits │ ├── app │ │ ├── globals.css │ │ ├── toaster.tsx │ │ ├── favicon.ico │ │ └── layout.tsx │ ├── .env.local.example │ ├── next.config.js │ ├── postcss.config.js │ ├── .gitignore │ ├── tailwind.config.js │ └── kasada │ │ └── kasada-client.tsx ├── ai-core │ ├── data │ │ └── comic-cat.png │ ├── .env.example │ ├── src │ │ ├── registry │ │ │ ├── embed.ts │ │ │ ├── stream-text.ts │ │ │ └── setup-registry.ts │ │ ├── embed │ │ │ ├── azure.ts │ │ │ ├── mistral.ts │ │ │ └── openai.ts │ │ ├── generate-text │ │ │ ├── azure.ts │ │ │ ├── openai-logprobs.ts │ │ │ ├── openai-completion.ts │ │ │ ├── cohere.ts │ │ │ ├── openai.ts │ │ │ ├── openai-system-message-b.ts │ │ │ ├── openai-system-message-a.ts │ │ │ ├── google.ts │ │ │ ├── mistral.ts │ │ │ ├── google-vertex.ts │ │ │ ├── anthropic.ts │ │ │ ├── groq.ts │ │ │ ├── openai-custom-headers.ts │ │ │ ├── openai-multimodal.ts │ │ │ ├── google-multimodal.ts │ │ │ ├── anthropic-multimodal.ts │ │ │ └── google-vertex-safety.ts │ │ ├── tools │ │ │ └── weather-tool.ts │ │ ├── stream-text │ │ │ ├── azure.ts │ │ │ ├── azure-completion.ts │ │ │ ├── cohere.ts │ │ │ ├── google.ts │ │ │ ├── google-vertex.ts │ │ │ ├── anthropic.ts │ │ │ ├── openai-reader.ts │ │ │ ├── openai-response-headers.ts │ │ │ ├── openai-completion.ts │ │ │ ├── openai.ts │ │ │ └── mistral.ts │ │ └── embed-many │ │ │ ├── azure.ts │ │ │ ├── mistral.ts │ │ │ ├── openai.ts │ │ │ └── openai-cosine-similarity.ts │ ├── tsconfig.json │ ├── README.md │ └── package.json ├── next-openai-kasada-bot-protection │ ├── app │ │ ├── globals.css │ │ ├── toaster.tsx │ │ ├── favicon.ico │ │ └── layout.tsx │ ├── .env.local.example │ ├── next.config.js │ ├── postcss.config.js │ ├── middleware.ts │ ├── .gitignore │ ├── tailwind.config.js │ └── kasada │ │ └── kasada-client.tsx └── node-http-server │ ├── .env.example │ ├── tsconfig.json │ ├── package.json │ └── README.md ├── packages ├── core │ ├── core │ │ ├── tool │ │ │ └── index.ts │ │ ├── embed │ │ │ └── index.ts │ │ ├── generate-object │ │ │ └── index.ts │ │ ├── types │ │ │ ├── index.ts │ │ │ ├── embedding-model.ts │ │ │ └── errors.ts │ │ ├── generate-text │ │ │ └── index.ts │ │ ├── util │ │ │ ├── delay.ts │ │ │ ├── is-non-empty-object.ts │ │ │ ├── prepare-response-headers.ts │ │ │ ├── convert-zod-to-json-schema.ts │ │ │ ├── split-array.ts │ │ │ ├── parse-partial-json.ts │ │ │ └── detect-image-mimetype.ts │ │ ├── registry │ │ │ └── index.ts │ │ ├── prompt │ │ │ ├── index.ts │ │ │ └── prompt.ts │ │ └── index.ts │ ├── react │ │ ├── server.ts │ │ ├── index.server.ts │ │ ├── package.json │ │ └── index.ts │ ├── rsc │ │ ├── stream-ui │ │ │ └── index.tsx │ │ ├── constants.ts │ │ ├── rsc-client.ts │ │ ├── rsc-server.ts │ │ ├── rsc-shared.mts │ │ ├── package.json │ │ ├── shared-client │ │ │ └── index.ts │ │ ├── index.ts │ │ └── utils.tsx │ ├── .eslintrc.js │ ├── tests │ │ ├── e2e │ │ │ └── next-server │ │ │ │ ├── app │ │ │ │ ├── page.js │ │ │ │ ├── stream-ui │ │ │ │ │ └── page.js │ │ │ │ ├── layout.js │ │ │ │ └── streamable │ │ │ │ │ ├── page.js │ │ │ │ │ └── client-utils.js │ │ │ │ └── package.json │ │ └── snapshots │ │ │ └── replicate.ts │ ├── prompts │ │ └── index.ts │ ├── svelte │ │ └── index.ts │ ├── turbo.json │ ├── vue │ │ ├── package.json │ │ └── index.ts │ ├── solid │ │ ├── package.json │ │ └── index.ts │ ├── tsconfig.json │ ├── vitest.ui.react.config.js │ ├── vitest.node.config.js │ └── vitest.edge.config.js ├── cohere │ ├── src │ │ ├── index.ts │ │ ├── cohere-chat-settings.ts │ │ ├── cohere-error.ts │ │ └── map-cohere-finish-reason.ts │ ├── turbo.json │ ├── tsup.config.ts │ ├── tsconfig.json │ ├── vitest.node.config.js │ ├── vitest.edge.config.js │ └── CHANGELOG.md ├── ui-utils │ ├── .gitignore │ ├── src │ │ ├── test │ │ │ └── index.ts │ │ ├── duplicated │ │ │ ├── tool-call.ts │ │ │ └── tool-result.ts │ │ └── index.ts │ ├── .eslintrc.js │ ├── turbo.json │ ├── README.md │ ├── tsconfig.json │ ├── vitest.node.config.js │ ├── vitest.edge.config.js │ ├── tsup.config.ts │ └── CHANGELOG.md ├── azure │ ├── src │ │ └── index.ts │ ├── turbo.json │ ├── tsup.config.ts │ ├── tsconfig.json │ ├── vitest.node.config.js │ └── vitest.edge.config.js ├── provider-utils │ ├── .gitignore │ ├── README.md │ ├── src │ │ ├── without-trailing-slash.ts │ │ ├── is-abort-error.ts │ │ ├── test │ │ │ ├── convert-async-iterable-to-array.ts │ │ │ ├── convert-array-to-readable-stream.ts │ │ │ ├── convert-readable-stream-to-array.ts │ │ │ └── index.ts │ │ ├── generate-id.ts │ │ ├── get-error-message.ts │ │ ├── extract-response-headers.ts │ │ ├── index.ts │ │ └── uint8-utils.ts │ ├── turbo.json │ ├── tsconfig.json │ ├── vitest.node.config.js │ ├── vitest.edge.config.js │ └── tsup.config.ts ├── provider │ ├── src │ │ ├── embedding-model │ │ │ ├── index.ts │ │ │ └── v1 │ │ │ │ ├── index.ts │ │ │ │ └── embedding-model-v1-embedding.ts │ │ ├── language-model │ │ │ ├── index.ts │ │ │ └── v1 │ │ │ │ ├── language-model-v1-logprobs.ts │ │ │ │ ├── language-model-v1-function-tool-call.ts │ │ │ │ ├── language-model-v1-tool-choice.ts │ │ │ │ ├── index.ts │ │ │ │ └── language-model-v1-call-warning.ts │ │ ├── index.ts │ │ └── errors │ │ │ ├── get-error-message.ts │ │ │ ├── load-api-key-error.ts │ │ │ ├── load-setting-error.ts │ │ │ ├── empty-response-body-error.ts │ │ │ └── invalid-prompt-error.ts │ ├── README.md │ ├── turbo.json │ ├── tsup.config.ts │ └── tsconfig.json ├── google-vertex │ ├── src │ │ └── index.ts │ ├── turbo.json │ ├── tsup.config.ts │ ├── tsconfig.json │ ├── vitest.node.config.js │ └── vitest.edge.config.js ├── solid │ ├── src │ │ ├── index.ts │ │ └── package.json │ ├── .eslintrc.js │ ├── turbo.json │ ├── tsconfig.json │ ├── tsup.config.ts │ ├── README.md │ ├── CHANGELOG.md │ └── vitest.config.js ├── vue │ ├── src │ │ ├── index.ts │ │ ├── shims-vue.d.ts │ │ ├── package.json │ │ ├── TestCompletionComponent.vue │ │ └── TestCompletionTextStreamComponent.vue │ ├── .eslintrc.js │ ├── turbo.json │ ├── tsconfig.json │ ├── tsup.config.ts │ ├── README.md │ ├── vitest.config.js │ └── CHANGELOG.md ├── google │ ├── src │ │ ├── index.ts │ │ ├── google-error.ts │ │ └── map-google-generative-ai-finish-reason.ts │ ├── turbo.json │ ├── tsup.config.ts │ ├── tsconfig.json │ ├── vitest.node.config.js │ └── vitest.edge.config.js ├── mistral │ ├── src │ │ ├── index.ts │ │ ├── mistral-embedding-settings.ts │ │ ├── map-mistral-finish-reason.ts │ │ ├── mistral-chat-settings.ts │ │ └── mistral-error.ts │ ├── turbo.json │ ├── tsup.config.ts │ ├── tsconfig.json │ ├── vitest.node.config.js │ └── vitest.edge.config.js ├── openai │ ├── src │ │ ├── index.ts │ │ ├── internal │ │ │ └── index.ts │ │ ├── map-openai-finish-reason.ts │ │ └── openai-error.ts │ ├── turbo.json │ ├── tsconfig.json │ ├── vitest.node.config.js │ ├── vitest.edge.config.js │ └── tsup.config.ts ├── react │ ├── .eslintrc.js │ ├── src │ │ └── index.ts │ ├── turbo.json │ ├── tsconfig.json │ ├── tsup.config.ts │ ├── vitest.config.js │ ├── README.md │ └── CHANGELOG.md ├── svelte │ ├── .eslintrc.js │ ├── src │ │ ├── index.ts │ │ └── package.json │ ├── turbo.json │ ├── tsconfig.json │ ├── tsup.config.ts │ ├── README.md │ └── CHANGELOG.md └── anthropic │ ├── src │ ├── index.ts │ ├── map-anthropic-stop-reason.ts │ ├── anthropic-messages-settings.ts │ └── anthropic-error.ts │ ├── turbo.json │ ├── tsup.config.ts │ ├── tsconfig.json │ ├── vitest.node.config.js │ └── vitest.edge.config.js ├── .prettierignore ├── .changeset ├── large-tools-breathe.md ├── dry-wasps-sit.md ├── config.json └── README.md ├── .github ├── dependabot.yml ├── ISSUE_TEMPLATE │ └── config.yml └── SECURITY.md ├── pnpm-workspace.yaml ├── content ├── providers │ ├── index.mdx │ ├── 05-legacy-providers │ │ └── index.mdx │ ├── 04-adapters │ │ └── index.mdx │ └── 03-community-providers │ │ ├── 02-llama-cpp.mdx │ │ └── index.mdx ├── docs │ ├── 08-troubleshooting │ │ ├── 05-ai-sdk-errors │ │ │ ├── index.mdx │ │ │ └── ai-invalid-data-content-error.mdx │ │ ├── 01-migration-guide │ │ │ └── index.mdx │ │ ├── index.mdx │ │ └── 03-common-issues │ │ │ ├── 05-streamable-ui-errors.mdx │ │ │ └── 01-azure-stream-slow.mdx │ ├── 02-getting-started │ │ └── index.mdx │ ├── 07-reference │ │ ├── stream-helpers │ │ │ ├── 06-stream-data.mdx │ │ │ └── 16-langchain-stream.mdx │ │ └── ai-sdk-rsc │ │ │ ├── 06-get-ai-state.mdx │ │ │ ├── 04-create-streamable-value.mdx │ │ │ ├── 07-get-mutable-ai-state.mdx │ │ │ ├── 02-create-ai.mdx │ │ │ ├── 03-create-streamable-ui.mdx │ │ │ ├── 10-use-ui-state.mdx │ │ │ └── 08-use-ai-state.mdx │ └── 06-advanced │ │ └── index.mdx └── examples │ ├── 01-next-app │ ├── 06-assistants │ │ └── index.mdx │ ├── 05-interface │ │ └── index.mdx │ ├── 02-chat │ │ └── index.mdx │ ├── 01-basics │ │ └── index.mdx │ └── 04-state-management │ │ └── index.mdx │ ├── 20-providers │ └── index.mdx │ └── 03-node │ ├── 02-generating-structured-data │ └── index.mdx │ ├── 01-generating-text │ └── 01-generate-text.mdx │ └── 02-streaming-structured-data │ └── index.mdx ├── .npmrc ├── .gitignore ├── tools ├── tsconfig │ ├── package.json │ ├── node14.json │ ├── react-library.json │ ├── nextjs.json │ └── base.json └── eslint-config │ ├── index.js │ └── package.json ├── .eslintrc.js ├── .kodiak.toml └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | packages/core/README.md -------------------------------------------------------------------------------- /examples/next-ai-rsc/.env.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY="xxxxxxxxxxxx" -------------------------------------------------------------------------------- /examples/next-cohere/.env.local.example: -------------------------------------------------------------------------------- 1 | COHERE_API_KEY=xxxxxx -------------------------------------------------------------------------------- /examples/next-openai/.env.local.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=xxxxxxx -------------------------------------------------------------------------------- /examples/sveltekit-openai/.env.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=xxxxxxx -------------------------------------------------------------------------------- /packages/core/core/tool/index.ts: -------------------------------------------------------------------------------- 1 | export * from './tool'; 2 | -------------------------------------------------------------------------------- /packages/core/react/server.ts: -------------------------------------------------------------------------------- 1 | export * from './tokens'; 2 | -------------------------------------------------------------------------------- /examples/next-langchain/.env.local.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=xxxxxxx -------------------------------------------------------------------------------- /examples/next-replicate/.env.local.example: -------------------------------------------------------------------------------- 1 | REPLICATE_API_KEY=xxxxxx -------------------------------------------------------------------------------- /examples/nuxt-openai/.env.example: -------------------------------------------------------------------------------- 1 | NUXT_OPENAI_API_KEY=xxxxxxx 2 | -------------------------------------------------------------------------------- /examples/solidstart-openai/.env.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=xxxxxxx 2 | -------------------------------------------------------------------------------- /packages/cohere/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './cohere-provider'; 2 | -------------------------------------------------------------------------------- /packages/core/react/index.server.ts: -------------------------------------------------------------------------------- 1 | export * from './tokens'; 2 | -------------------------------------------------------------------------------- /packages/ui-utils/.gitignore: -------------------------------------------------------------------------------- 1 | # test distribution folder: 2 | ./test -------------------------------------------------------------------------------- /examples/next-huggingface/.env.local.example: -------------------------------------------------------------------------------- 1 | HUGGINGFACE_API_KEY=xxxxxxx -------------------------------------------------------------------------------- /packages/azure/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './azure-openai-provider'; 2 | -------------------------------------------------------------------------------- /packages/provider-utils/.gitignore: -------------------------------------------------------------------------------- 1 | # test distribution folder: 2 | ./test -------------------------------------------------------------------------------- /packages/ui-utils/src/test/index.ts: -------------------------------------------------------------------------------- 1 | export * from './mock-fetch'; 2 | -------------------------------------------------------------------------------- /packages/provider/src/embedding-model/index.ts: -------------------------------------------------------------------------------- 1 | export * from './v1/index'; 2 | -------------------------------------------------------------------------------- /packages/provider/src/language-model/index.ts: -------------------------------------------------------------------------------- 1 | export * from './v1/index'; 2 | -------------------------------------------------------------------------------- /examples/sveltekit-openai/.npmrc: -------------------------------------------------------------------------------- 1 | engine-strict=true 2 | resolution-mode=highest 3 | -------------------------------------------------------------------------------- /packages/core/rsc/stream-ui/index.tsx: -------------------------------------------------------------------------------- 1 | export { streamUI } from './stream-ui'; 2 | -------------------------------------------------------------------------------- /packages/google-vertex/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './google-vertex-provider'; 2 | -------------------------------------------------------------------------------- /packages/provider/README.md: -------------------------------------------------------------------------------- 1 | # Vercel AI SDK - Provider Language Model Specification 2 | -------------------------------------------------------------------------------- /examples/next-inkeep/.env.local.example: -------------------------------------------------------------------------------- 1 | INKEEP_API_KEY=xxxxxx 2 | INKEEP_INTEGRATION_ID=xxxxxx -------------------------------------------------------------------------------- /examples/next-openai-pages/.env.local.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=xxxxxxx 2 | ASSISTANT_ID=xxxxxxx -------------------------------------------------------------------------------- /packages/provider-utils/README.md: -------------------------------------------------------------------------------- 1 | # Vercel AI SDK - Provider Implementation Utilities 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .next 2 | .nuxt 3 | node_modules 4 | dist 5 | .svelte-kit 6 | .solid 7 | _nuxt 8 | -------------------------------------------------------------------------------- /packages/core/core/embed/index.ts: -------------------------------------------------------------------------------- 1 | export * from './embed'; 2 | export * from './embed-many'; 3 | -------------------------------------------------------------------------------- /packages/solid/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './use-chat'; 2 | export * from './use-completion'; 3 | -------------------------------------------------------------------------------- /packages/vue/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './use-chat'; 2 | export * from './use-completion'; 3 | -------------------------------------------------------------------------------- /examples/nuxt-openai/server/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../.nuxt/tsconfig.server.json" 3 | } 4 | -------------------------------------------------------------------------------- /packages/vue/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ['vercel-ai'], 4 | }; 5 | -------------------------------------------------------------------------------- /.changeset/large-tools-breathe.md: -------------------------------------------------------------------------------- 1 | --- 2 | 'ai': minor 3 | --- 4 | 5 | chore (ai): AI SDK 3.2 release 6 | -------------------------------------------------------------------------------- /examples/next-cohere/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /examples/next-inkeep/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /examples/next-openai/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /packages/core/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ['vercel-ai'], 4 | }; 5 | -------------------------------------------------------------------------------- /packages/google/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './google-facade'; 2 | export * from './google-provider'; 3 | -------------------------------------------------------------------------------- /packages/mistral/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './mistral-facade'; 2 | export * from './mistral-provider'; 3 | -------------------------------------------------------------------------------- /packages/openai/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './openai-facade'; 2 | export * from './openai-provider'; 3 | -------------------------------------------------------------------------------- /packages/react/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ['vercel-ai'], 4 | }; 5 | -------------------------------------------------------------------------------- /packages/solid/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ['vercel-ai'], 4 | }; 5 | -------------------------------------------------------------------------------- /packages/svelte/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ['vercel-ai'], 4 | }; 5 | -------------------------------------------------------------------------------- /packages/ui-utils/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: ['vercel-ai'], 4 | }; 5 | -------------------------------------------------------------------------------- /examples/next-aws-bedrock/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /examples/next-huggingface/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /examples/next-langchain/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /examples/next-replicate/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /examples/solidstart-openai/src/root.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /packages/anthropic/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './anthropic-facade'; 2 | export * from './anthropic-provider'; 3 | -------------------------------------------------------------------------------- /examples/next-openai-pages/styles/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | }, 5 | }; 6 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/app/toaster.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | export { Toaster as default } from 'sonner'; 4 | -------------------------------------------------------------------------------- /examples/sveltekit-openai/static/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /packages/core/core/generate-object/index.ts: -------------------------------------------------------------------------------- 1 | export * from './generate-object'; 2 | export * from './stream-object'; 3 | -------------------------------------------------------------------------------- /packages/core/tests/e2e/next-server/app/page.js: -------------------------------------------------------------------------------- 1 | export default function Page() { 2 | return
Page
; 3 | } 4 | -------------------------------------------------------------------------------- /examples/ai-core/data/comic-cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/ai-core/data/comic-cat.png -------------------------------------------------------------------------------- /examples/next-cohere/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-cohere/app/favicon.ico -------------------------------------------------------------------------------- /examples/next-inkeep/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-inkeep/app/favicon.ico -------------------------------------------------------------------------------- /examples/next-openai/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-openai/app/favicon.ico -------------------------------------------------------------------------------- /packages/core/prompts/index.ts: -------------------------------------------------------------------------------- 1 | export * from './anthropic'; 2 | export * from './huggingface'; 3 | export * from './openai'; 4 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-ai-rsc/public/favicon.ico -------------------------------------------------------------------------------- /examples/next-langchain/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-langchain/app/favicon.ico -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/app/toaster.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | export { Toaster as default } from 'sonner'; 4 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/.env.local.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=xxxxxxx 2 | KV_REST_API_URL=xxxxxxx 3 | KV_REST_API_TOKEN=xxxxxxx -------------------------------------------------------------------------------- /examples/next-replicate/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-replicate/app/favicon.ico -------------------------------------------------------------------------------- /examples/next-aws-bedrock/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-aws-bedrock/app/favicon.ico -------------------------------------------------------------------------------- /examples/next-huggingface/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-huggingface/app/favicon.ico -------------------------------------------------------------------------------- /packages/core/svelte/index.ts: -------------------------------------------------------------------------------- 1 | export * from './use-chat'; 2 | export * from './use-completion'; 3 | export * from './use-assistant'; 4 | -------------------------------------------------------------------------------- /packages/react/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './use-chat'; 2 | export * from './use-completion'; 3 | export * from './use-assistant'; 4 | -------------------------------------------------------------------------------- /packages/svelte/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './use-chat'; 2 | export * from './use-completion'; 3 | export * from './use-assistant'; 4 | -------------------------------------------------------------------------------- /.changeset/dry-wasps-sit.md: -------------------------------------------------------------------------------- 1 | --- 2 | 'ai': patch 3 | --- 4 | 5 | fix (core): pass settings correctly for generateObject and streamObject 6 | -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/.env.local.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=xxxxxxx 2 | KV_REST_API_URL=xxxxxxx 3 | KV_REST_API_TOKEN=xxxxxxx -------------------------------------------------------------------------------- /examples/sveltekit-openai/static/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/sveltekit-openai/static/favicon.png -------------------------------------------------------------------------------- /packages/core/core/types/index.ts: -------------------------------------------------------------------------------- 1 | export * from './embedding-model'; 2 | export * from './errors'; 3 | export * from './language-model'; 4 | -------------------------------------------------------------------------------- /packages/provider/src/embedding-model/v1/index.ts: -------------------------------------------------------------------------------- 1 | export * from './embedding-model-v1'; 2 | export * from './embedding-model-v1-embedding'; 3 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .turbo 4 | *.log 5 | .next 6 | *.local 7 | .env 8 | .cache 9 | .turbo 10 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/public/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-ai-rsc/public/favicon-16x16.png -------------------------------------------------------------------------------- /examples/next-cohere/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-cohere/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /examples/next-inkeep/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-inkeep/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /examples/next-langchain/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-langchain/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /examples/next-openai/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-openai/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /examples/next-replicate/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-replicate/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /examples/nuxt-openai/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | // https://nuxt.com/docs/guide/concepts/typescript 3 | "extends": "./.nuxt/tsconfig.json" 4 | } 5 | -------------------------------------------------------------------------------- /examples/solidstart-openai/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/solidstart-openai/public/favicon.ico -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: 'npm' 4 | directory: '/' 5 | schedule: 6 | interval: 'daily' 7 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/public/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-ai-rsc/public/apple-touch-icon.png -------------------------------------------------------------------------------- /examples/next-aws-bedrock/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-aws-bedrock/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /examples/next-huggingface/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-huggingface/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /examples/next-openai-pages/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-openai-pages/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-openai-rate-limits/app/favicon.ico -------------------------------------------------------------------------------- /examples/solidstart-openai/postcss.config.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {} 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /packages/vue/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /packages/azure/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/cohere/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/core/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/google/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/mistral/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/openai/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/provider/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './embedding-model/index'; 2 | export * from './errors/index'; 3 | export * from './language-model/index'; 4 | -------------------------------------------------------------------------------- /packages/provider/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/react/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/solid/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/svelte/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/ui-utils/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /pnpm-workspace.yaml: -------------------------------------------------------------------------------- 1 | packages: 2 | - 'apps/*' 3 | - 'packages/*' 4 | - 'tools/*' 5 | - 'examples/*' 6 | - 'packages/core/tests/e2e/next-server' 7 | -------------------------------------------------------------------------------- /content/providers/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Providers 3 | description: Learn how to use providers to manage the state of your app. 4 | --- 5 | 6 | # Providers 7 | -------------------------------------------------------------------------------- /examples/solidstart-openai/src/entry-client.tsx: -------------------------------------------------------------------------------- 1 | import { mount, StartClient } from 'solid-start/entry-client'; 2 | 3 | mount(() => , document); 4 | -------------------------------------------------------------------------------- /packages/anthropic/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/google-vertex/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/vue/src/shims-vue.d.ts: -------------------------------------------------------------------------------- 1 | // required for vue testing library 2 | declare module '*.vue' { 3 | import Vue from 'vue'; 4 | export default Vue; 5 | } 6 | -------------------------------------------------------------------------------- /examples/next-aws-bedrock/.env.local.example: -------------------------------------------------------------------------------- 1 | AWS_REGION=YOUR_AWS_REGION 2 | AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID 3 | AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = {}; 3 | 4 | module.exports = nextConfig; 5 | -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /packages/core/core/generate-text/index.ts: -------------------------------------------------------------------------------- 1 | export * from './generate-text'; 2 | export * from './stream-text'; 3 | export type { TokenUsage } from './token-usage'; 4 | -------------------------------------------------------------------------------- /packages/provider-utils/src/without-trailing-slash.ts: -------------------------------------------------------------------------------- 1 | export function withoutTrailingSlash(url: string | undefined) { 2 | return url?.replace(/\/$/, ''); 3 | } 4 | -------------------------------------------------------------------------------- /packages/provider-utils/turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["//"], 3 | "pipeline": { 4 | "build": { 5 | "outputs": ["**/dist/**"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | auto-install-peers = true 2 | # necessary for a PNPM bug: https://github.com/pnpm/pnpm/issues/5152#issuecomment-1223449173 3 | strict-peer-dependencies = false 4 | -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicwebpaige/ai/main/examples/next-openai-kasada-bot-protection/app/favicon.ico -------------------------------------------------------------------------------- /packages/core/rsc/constants.ts: -------------------------------------------------------------------------------- 1 | export const STREAMABLE_VALUE_TYPE = Symbol.for('ui.streamable.value'); 2 | export const DEV_DEFAULT_STREAMABLE_WARNING_TIME = 15 * 1000; 3 | -------------------------------------------------------------------------------- /packages/ui-utils/README.md: -------------------------------------------------------------------------------- 1 | # Vercel AI SDK - UI Framework Integration Implementation Utilities 2 | 3 | Important: this is an internal API. Expect breaking changes. 4 | -------------------------------------------------------------------------------- /packages/core/core/util/delay.ts: -------------------------------------------------------------------------------- 1 | export async function delay(delayInMs: number): Promise { 2 | return new Promise(resolve => setTimeout(resolve, delayInMs)); 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .turbo 4 | *.log 5 | .next 6 | dist 7 | dist-ssr 8 | *.local 9 | .env 10 | .cache 11 | server/dist 12 | public/dist 13 | .turbo 14 | test-results -------------------------------------------------------------------------------- /examples/solidstart-openai/vite.config.ts: -------------------------------------------------------------------------------- 1 | import solid from 'solid-start/vite'; 2 | import { defineConfig } from 'vite'; 3 | 4 | export default defineConfig({ 5 | plugins: [solid()], 6 | }); 7 | -------------------------------------------------------------------------------- /examples/sveltekit-openai/src/routes/+page.ts: -------------------------------------------------------------------------------- 1 | // since there's no dynamic data here, we can prerender 2 | // it so that it gets served as a static asset in production 3 | export const prerender = true; 4 | -------------------------------------------------------------------------------- /packages/core/rsc/rsc-client.ts: -------------------------------------------------------------------------------- 1 | export { 2 | readStreamableValue, 3 | useStreamableValue, 4 | useUIState, 5 | useAIState, 6 | useActions, 7 | useSyncUIState, 8 | } from './rsc-shared.mjs'; 9 | -------------------------------------------------------------------------------- /examples/node-http-server/.env.example: -------------------------------------------------------------------------------- 1 | ANTHROPIC_API_KEY="" 2 | OPENAI_API_KEY="" 3 | MISTRAL_API_KEY="" 4 | GOOGLE_GENERATIVE_AI_API_KEY="" 5 | FIREWORKS_API_KEY="" 6 | GROQ_API_KEY="" 7 | PERPLEXITY_API_KEY="" -------------------------------------------------------------------------------- /examples/sveltekit-openai/vite.config.ts: -------------------------------------------------------------------------------- 1 | import { sveltekit } from '@sveltejs/kit/vite'; 2 | import { defineConfig } from 'vite'; 3 | 4 | export default defineConfig({ 5 | plugins: [sveltekit()], 6 | }); 7 | -------------------------------------------------------------------------------- /packages/core/core/registry/index.ts: -------------------------------------------------------------------------------- 1 | export * from './invalid-model-id-error'; 2 | export * from './no-such-model-error'; 3 | export * from './no-such-provider-error'; 4 | export * from './provider-registry'; 5 | -------------------------------------------------------------------------------- /tools/tsconfig/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@vercel/ai-tsconfig", 3 | "version": "0.0.0", 4 | "private": true, 5 | "license": "MIT", 6 | "publishConfig": { 7 | "access": "public" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /packages/core/tests/e2e/next-server/app/stream-ui/page.js: -------------------------------------------------------------------------------- 1 | import { action } from './action'; 2 | import { Client } from './client'; 3 | 4 | export default function Page() { 5 | return ; 6 | } 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Ask a question 4 | url: https://github.com/vercel/ai/discussions 5 | about: Please ask questions in our discussions forum. 6 | -------------------------------------------------------------------------------- /examples/solidstart-openai/tailwind.config.cjs: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: ['./src/**/*.{html,js,jsx,ts,tsx}'], 4 | theme: { 5 | extend: {} 6 | }, 7 | plugins: [] 8 | } 9 | -------------------------------------------------------------------------------- /packages/core/core/prompt/index.ts: -------------------------------------------------------------------------------- 1 | export * from './content-part'; 2 | export * from './convert-to-core-messages'; 3 | export * from './data-content'; 4 | export * from './invalid-message-role-error'; 5 | export * from './message'; 6 | -------------------------------------------------------------------------------- /examples/next-openai-pages/pages/_app.tsx: -------------------------------------------------------------------------------- 1 | import '@/styles/globals.css'; 2 | import type { AppProps } from 'next/app'; 3 | 4 | export default function App({ Component, pageProps }: AppProps) { 5 | return ; 6 | } 7 | -------------------------------------------------------------------------------- /examples/sveltekit-openai/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /build 4 | /.svelte-kit 5 | /package 6 | .env 7 | .env.* 8 | !.env.example 9 | .vercel 10 | .output 11 | vite.config.js.timestamp-* 12 | vite.config.ts.timestamp-* 13 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/next-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | 4 | // NOTE: This file should not be edited 5 | // see https://nextjs.org/docs/basic-features/typescript for more information. 6 | -------------------------------------------------------------------------------- /examples/next-openai/app/stream-assistant-responses/layout.tsx: -------------------------------------------------------------------------------- 1 | import { ReactNode } from 'react'; 2 | import { AI } from './actions'; 3 | 4 | export default function Layout({ children }: { children: ReactNode }) { 5 | return {children}; 6 | } 7 | -------------------------------------------------------------------------------- /packages/anthropic/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | ]); 11 | -------------------------------------------------------------------------------- /packages/azure/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | ]); 11 | -------------------------------------------------------------------------------- /packages/cohere/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | ]); 11 | -------------------------------------------------------------------------------- /packages/core/core/util/is-non-empty-object.ts: -------------------------------------------------------------------------------- 1 | export function isNonEmptyObject( 2 | object: Record | undefined | null, 3 | ): object is Record { 4 | return object != null && Object.keys(object).length > 0; 5 | } 6 | -------------------------------------------------------------------------------- /packages/google/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | ]); 11 | -------------------------------------------------------------------------------- /packages/mistral/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | ]); 11 | -------------------------------------------------------------------------------- /packages/provider/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | ]); 11 | -------------------------------------------------------------------------------- /examples/ai-core/.env.example: -------------------------------------------------------------------------------- 1 | ANTHROPIC_API_KEY="" 2 | OPENAI_API_KEY="" 3 | MISTRAL_API_KEY="" 4 | GOOGLE_GENERATIVE_AI_API_KEY="" 5 | GOOGLE_VERTEX_PROJECT="" 6 | GOOGLE_VERTEX_LOCATION="" 7 | FIREWORKS_API_KEY="" 8 | GROQ_API_KEY="" 9 | PERPLEXITY_API_KEY="" -------------------------------------------------------------------------------- /packages/google-vertex/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | ]); 11 | -------------------------------------------------------------------------------- /packages/provider-utils/src/is-abort-error.ts: -------------------------------------------------------------------------------- 1 | export function isAbortError(error: unknown): error is DOMException { 2 | return ( 3 | error instanceof DOMException && 4 | (error.name === 'AbortError' || error.name === 'TimeoutError') 5 | ); 6 | } 7 | -------------------------------------------------------------------------------- /examples/next-openai/app/stream-assistant-responses-with-tools/layout.tsx: -------------------------------------------------------------------------------- 1 | import { ReactNode } from 'react'; 2 | import { AI } from './actions'; 3 | 4 | export default function Layout({ children }: { children: ReactNode }) { 5 | return {children}; 6 | } 7 | -------------------------------------------------------------------------------- /examples/solidstart-openai/src/entry-server.tsx: -------------------------------------------------------------------------------- 1 | import { 2 | StartServer, 3 | createHandler, 4 | renderAsync, 5 | } from 'solid-start/entry-server'; 6 | 7 | export default createHandler( 8 | renderAsync(event => ), 9 | ); 10 | -------------------------------------------------------------------------------- /packages/core/vue/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./dist/index.js", 3 | "module": "./dist/index.mjs", 4 | "types": "./dist/index.d.ts", 5 | "exports": "./dist/index.mjs", 6 | "private": true, 7 | "peerDependencies": { 8 | "vue": "*" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/vue/src/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./dist/index.js", 3 | "module": "./dist/index.mjs", 4 | "types": "./dist/index.d.ts", 5 | "exports": "./dist/index.mjs", 6 | "private": true, 7 | "peerDependencies": { 8 | "vue": "*" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | // This tells ESLint to load the config from the package `eslint-config-vercel-ai` 4 | extends: ['vercel-ai'], 5 | settings: { 6 | next: { 7 | rootDir: ['apps/*/'], 8 | }, 9 | }, 10 | }; 11 | -------------------------------------------------------------------------------- /packages/provider/src/embedding-model/v1/embedding-model-v1-embedding.ts: -------------------------------------------------------------------------------- 1 | /** 2 | An embedding is a vector, i.e. an array of numbers. 3 | It is e.g. used to represent a text as a vector of word embeddings. 4 | */ 5 | export type EmbeddingModelV1Embedding = Array; 6 | -------------------------------------------------------------------------------- /packages/svelte/src/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./dist/index.js", 3 | "module": "./dist/index.mjs", 4 | "types": "./dist/index.d.ts", 5 | "exports": "./dist/index.mjs", 6 | "private": true, 7 | "peerDependencies": { 8 | "svelte": "*" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/core/react/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./dist/index.js", 3 | "module": "./dist/index.mjs", 4 | "types": "./dist/index.d.ts", 5 | "exports": "./dist/index.mjs", 6 | "private": true, 7 | "peerDependencies": { 8 | "react": ">=18" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/core/solid/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./dist/index.js", 3 | "module": "./dist/index.mjs", 4 | "types": "./dist/index.d.ts", 5 | "exports": "./dist/index.mjs", 6 | "private": true, 7 | "peerDependencies": { 8 | "solid-js": "*" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/solid/src/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "./dist/index.js", 3 | "module": "./dist/index.mjs", 4 | "types": "./dist/index.d.ts", 5 | "exports": "./dist/index.mjs", 6 | "private": true, 7 | "peerDependencies": { 8 | "solid-js": "*" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tools/tsconfig/node14.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/tsconfig", 3 | "display": "Node 14", 4 | "extends": "./base.json", 5 | "compilerOptions": { 6 | "lib": ["ES2020"], 7 | "module": "commonjs", 8 | "target": "ES2020" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/azure/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/cohere/src/cohere-chat-settings.ts: -------------------------------------------------------------------------------- 1 | // https://docs.cohere.com/docs/models 2 | export type CohereChatModelId = 3 | | 'command-r-plus' 4 | | 'command-r' 5 | | 'command' 6 | | 'command-light' 7 | | (string & {}); 8 | 9 | export interface CohereChatSettings {} 10 | -------------------------------------------------------------------------------- /packages/core/rsc/rsc-server.ts: -------------------------------------------------------------------------------- 1 | export { getAIState, getMutableAIState } from './ai-state'; 2 | export { 3 | createStreamableUI, 4 | createStreamableValue, 5 | render, 6 | } from './streamable'; 7 | export { streamUI } from './stream-ui'; 8 | export { createAI } from './provider'; 9 | -------------------------------------------------------------------------------- /packages/anthropic/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/azure/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/cohere/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/cohere/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/google/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/google/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/mistral/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/mistral/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/openai/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/openai/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/provider/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/ui-utils/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/anthropic/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/core/rsc/rsc-shared.mts: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | export { 4 | readStreamableValue, 5 | useStreamableValue, 6 | useUIState, 7 | useAIState, 8 | useActions, 9 | useSyncUIState, 10 | InternalAIProvider, 11 | InternalStreamableUIClient, 12 | } from './shared-client'; 13 | -------------------------------------------------------------------------------- /packages/google-vertex/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/provider-utils/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/react/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["*/dist", "dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/solid/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["*/dist", "dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/ui-utils/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/vue/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["*/dist", "dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /content/docs/08-troubleshooting/05-ai-sdk-errors/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: AI SDK Errors 3 | description: Troubleshooting information for common AI SDK errors. 4 | --- 5 | 6 | # AI SDK Errors 7 | 8 | - [ AI_InvalidDataContentError ](/docs/troubleshooting/ai-sdk-errors/ai-invalid-data-content-error) 9 | -------------------------------------------------------------------------------- /packages/anthropic/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/azure/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/cohere/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/google-vertex/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/google/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/mistral/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/openai/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/provider-utils/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/svelte/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true 6 | }, 7 | "include": ["."], 8 | "exclude": ["*/dist", "dist", "build", "node_modules"] 9 | } 10 | -------------------------------------------------------------------------------- /packages/ui-utils/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/google-vertex/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /packages/provider-utils/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts', '**/*.test.tsx'], 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /tools/eslint-config/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: ['next', 'turbo', 'prettier'], 3 | rules: { 4 | '@next/next/no-html-link-for-pages': 'off', 5 | }, 6 | parserOptions: { 7 | babelOptions: { 8 | presets: [require.resolve('next/babel')], 9 | }, 10 | }, 11 | }; 12 | -------------------------------------------------------------------------------- /packages/cohere/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @ai-sdk/cohere 2 | 3 | ## 0.0.1 4 | 5 | ### Patch Changes 6 | 7 | - 85712895: feat (@ai-sdk/cohere): add Cohere provider for text generation and streaming 8 | - Updated dependencies [85712895] 9 | - Updated dependencies [85712895] 10 | - @ai-sdk/provider-utils@0.0.15 11 | -------------------------------------------------------------------------------- /content/providers/05-legacy-providers/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Legacy Providers 3 | description: Learn how to use legacy providers. 4 | --- 5 | 6 | # Legacy Providers 7 | 8 | Legacy providers are providers from older versions of the Vercel AI SDK that are still supported but are not recommended for new projects. 9 | -------------------------------------------------------------------------------- /.changeset/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://unpkg.com/@changesets/config@2.2.0/schema.json", 3 | "changelog": "@changesets/cli/changelog", 4 | "commit": false, 5 | "fixed": [], 6 | "linked": [], 7 | "access": "public", 8 | "baseBranch": "main", 9 | "updateInternalDependencies": "patch" 10 | } 11 | -------------------------------------------------------------------------------- /packages/provider-utils/src/test/convert-async-iterable-to-array.ts: -------------------------------------------------------------------------------- 1 | export async function convertAsyncIterableToArray( 2 | iterable: AsyncIterable, 3 | ): Promise { 4 | const result: T[] = []; 5 | for await (const item of iterable) { 6 | result.push(item); 7 | } 8 | return result; 9 | } 10 | -------------------------------------------------------------------------------- /packages/react/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | outDir: 'dist', 7 | banner: {}, 8 | format: ['cjs', 'esm'], 9 | external: ['vue'], 10 | dts: true, 11 | sourcemap: true, 12 | }, 13 | ]); 14 | -------------------------------------------------------------------------------- /packages/solid/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | outDir: 'dist', 7 | banner: {}, 8 | format: ['cjs', 'esm'], 9 | external: ['vue'], 10 | dts: true, 11 | sourcemap: true, 12 | }, 13 | ]); 14 | -------------------------------------------------------------------------------- /packages/vue/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | outDir: 'dist', 7 | banner: {}, 8 | format: ['cjs', 'esm'], 9 | external: ['vue'], 10 | dts: true, 11 | sourcemap: true, 12 | }, 13 | ]); 14 | -------------------------------------------------------------------------------- /packages/core/tests/e2e/next-server/app/layout.js: -------------------------------------------------------------------------------- 1 | export const metadata = { 2 | title: 'Next.js', 3 | description: 'Generated by Next.js', 4 | }; 5 | 6 | export default function RootLayout({ children }) { 7 | return ( 8 | 9 | {children} 10 | 11 | ); 12 | } 13 | -------------------------------------------------------------------------------- /packages/svelte/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | outDir: 'dist', 7 | banner: {}, 8 | format: ['cjs', 'esm'], 9 | external: ['vue'], 10 | dts: true, 11 | sourcemap: true, 12 | }, 13 | ]); 14 | -------------------------------------------------------------------------------- /tools/tsconfig/react-library.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/tsconfig", 3 | "display": "React Library", 4 | "extends": "./base.json", 5 | "compilerOptions": { 6 | "jsx": "react-jsx", 7 | "lib": ["dom", "ES2015"], 8 | "module": "ESNext", 9 | "target": "es6" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /packages/openai/src/internal/index.ts: -------------------------------------------------------------------------------- 1 | export * from '../openai-chat-language-model'; 2 | export * from '../openai-chat-settings'; 3 | export * from '../openai-completion-language-model'; 4 | export * from '../openai-completion-settings'; 5 | export * from '../openai-embedding-model'; 6 | export * from '../openai-embedding-settings'; 7 | -------------------------------------------------------------------------------- /packages/provider-utils/src/generate-id.ts: -------------------------------------------------------------------------------- 1 | import { customAlphabet } from 'nanoid/non-secure'; 2 | 3 | /** 4 | * Generates a 7-character random string to use for IDs. Not secure. 5 | */ 6 | export const generateId = customAlphabet( 7 | '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 8 | 7, 9 | ); 10 | -------------------------------------------------------------------------------- /content/examples/01-next-app/06-assistants/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: OpenAI Assistants 3 | description: Learn to use the OpenAI Assistant API using the Vercel AI SDK in your Next.js App Router application 4 | --- 5 | 6 | # Assistants 7 | 8 | In this section, you will learn to use OpenAI's Assistant API along with `ai/rsc` functions. 9 | -------------------------------------------------------------------------------- /examples/next-openai/app/stream-assistant-responses/message.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { StreamableValue, useStreamableValue } from 'ai/rsc'; 4 | 5 | export function Message({ textStream }: { textStream: StreamableValue }) { 6 | const [text] = useStreamableValue(textStream); 7 | 8 | return
{text}
; 9 | } 10 | -------------------------------------------------------------------------------- /packages/vue/README.md: -------------------------------------------------------------------------------- 1 | # Vercel AI SDK: Vue.js provider 2 | 3 | [Vue.js](https://vuejs.org/) UI components for the [Vercel AI SDK](https://sdk.vercel.ai/docs): 4 | 5 | - [`useChat`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-chat) hook 6 | - [`useCompletion`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-completion) hook 7 | -------------------------------------------------------------------------------- /examples/next-openai-pages/pages/_document.tsx: -------------------------------------------------------------------------------- 1 | import { Html, Head, Main, NextScript } from 'next/document'; 2 | 3 | export default function Document() { 4 | return ( 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | ); 13 | } 14 | -------------------------------------------------------------------------------- /examples/next-openai/app/stream-assistant-responses-with-tools/message.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { StreamableValue, useStreamableValue } from 'ai/rsc'; 4 | 5 | export function Message({ textStream }: { textStream: StreamableValue }) { 6 | const [text] = useStreamableValue(textStream); 7 | 8 | return
{text}
; 9 | } 10 | -------------------------------------------------------------------------------- /examples/sveltekit-openai/src/app.d.ts: -------------------------------------------------------------------------------- 1 | // See https://kit.svelte.dev/docs/types#app 2 | // for information about these interfaces 3 | declare global { 4 | namespace App { 5 | // interface Error {} 6 | // interface Locals {} 7 | // interface PageData {} 8 | // interface Platform {} 9 | } 10 | } 11 | 12 | export {}; 13 | -------------------------------------------------------------------------------- /packages/provider/src/language-model/v1/language-model-v1-logprobs.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Log probabilities for each token and its top log probabilities. 3 | */ 4 | export type LanguageModelV1LogProbs = Array<{ 5 | token: string; 6 | logprob: number; 7 | topLogprobs: Array<{ 8 | token: string; 9 | logprob: number; 10 | }>; 11 | }>; 12 | -------------------------------------------------------------------------------- /packages/solid/README.md: -------------------------------------------------------------------------------- 1 | # Vercel AI SDK: Solid.js provider 2 | 3 | [Solid.js](https://www.solidjs.com/) UI components for the [Vercel AI SDK](https://sdk.vercel.ai/docs): 4 | 5 | - [`useChat`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-chat) hook 6 | - [`useCompletion`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-completion) hook 7 | -------------------------------------------------------------------------------- /packages/core/rsc/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "types": "./dist/index.d.ts", 3 | "exports": { 4 | "types": "./dist/index.d.ts", 5 | "react-server": "./dist/rsc-server.mjs", 6 | "import": "./dist/rsc-client.mjs" 7 | }, 8 | "private": true, 9 | "peerDependencies": { 10 | "react": ">=18", 11 | "zod": ">=3" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /packages/core/rsc/shared-client/index.ts: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | export { readStreamableValue, useStreamableValue } from './streamable'; 4 | export { 5 | useUIState, 6 | useAIState, 7 | useActions, 8 | useSyncUIState, 9 | InternalAIProvider, 10 | } from './context'; 11 | export { InternalStreamableUIClient } from './client-wrapper'; 12 | -------------------------------------------------------------------------------- /content/docs/02-getting-started/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Getting Started 3 | description: Welcome to the Vercel AI SDK documentation! 4 | --- 5 | 6 | # Getting Started 7 | 8 | The following guides are intended to provide you with an introduction to some of the core features provided by the Vercel AI SDK. 9 | 10 | 11 | -------------------------------------------------------------------------------- /examples/nuxt-openai/.gitignore: -------------------------------------------------------------------------------- 1 | # Nuxt dev/build outputs 2 | .output 3 | .nuxt 4 | .vercel 5 | .nitro 6 | .cache 7 | dist 8 | 9 | # Node dependencies 10 | node_modules 11 | 12 | # Logs 13 | logs 14 | *.log 15 | 16 | # Misc 17 | .DS_Store 18 | .fleet 19 | .idea 20 | 21 | # Local env files 22 | .env 23 | .env.* 24 | !.env.example 25 | .dev.vars 26 | .data -------------------------------------------------------------------------------- /packages/core/tests/e2e/next-server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ai-core-e2e-next-server", 3 | "private": true, 4 | "scripts": { 5 | "dev": "next" 6 | }, 7 | "dependencies": { 8 | "next": "canary", 9 | "react": "rc", 10 | "react-dom": "rc", 11 | "zod": "*", 12 | "ai": "workspace:*" 13 | }, 14 | "version": null 15 | } 16 | -------------------------------------------------------------------------------- /packages/vue/vitest.config.js: -------------------------------------------------------------------------------- 1 | import vue from '@vitejs/plugin-vue'; 2 | import { defineConfig } from 'vite'; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [vue()], 7 | test: { 8 | environment: 'jsdom', 9 | globals: true, 10 | include: ['src/**/*.ui.test.ts', 'src/**/*.ui.test.tsx'], 11 | }, 12 | }); 13 | -------------------------------------------------------------------------------- /packages/core/core/types/embedding-model.ts: -------------------------------------------------------------------------------- 1 | import { EmbeddingModelV1, EmbeddingModelV1Embedding } from '@ai-sdk/provider'; 2 | 3 | /** 4 | Embedding model that is used by the AI SDK Core functions. 5 | */ 6 | export type EmbeddingModel = EmbeddingModelV1; 7 | 8 | /** 9 | Embedding. 10 | */ 11 | export type Embedding = EmbeddingModelV1Embedding; 12 | -------------------------------------------------------------------------------- /packages/react/vitest.config.js: -------------------------------------------------------------------------------- 1 | import react from '@vitejs/plugin-react'; 2 | import { defineConfig } from 'vite'; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [react()], 7 | test: { 8 | environment: 'jsdom', 9 | globals: true, 10 | include: ['src/**/*.ui.test.ts', 'src/**/*.ui.test.tsx'], 11 | }, 12 | }); 13 | -------------------------------------------------------------------------------- /examples/nuxt-openai/nuxt.config.ts: -------------------------------------------------------------------------------- 1 | // https://nuxt.com/docs/api/configuration/nuxt-config 2 | export default defineNuxtConfig({ 3 | devtools: { enabled: true }, 4 | modules: ['@nuxtjs/tailwindcss'], 5 | nitro: { 6 | preset: 'vercel-edge', // you can use 'vercel' or other providers here 7 | }, 8 | runtimeConfig: { 9 | openaiApiKey: '', 10 | }, 11 | }); 12 | -------------------------------------------------------------------------------- /examples/solidstart-openai/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | dist 3 | .solid 4 | .output 5 | .vercel 6 | .netlify 7 | netlify 8 | 9 | # dependencies 10 | /node_modules 11 | 12 | # IDEs and editors 13 | /.idea 14 | .project 15 | .classpath 16 | *.launch 17 | .settings/ 18 | 19 | # Temp 20 | gitignore 21 | 22 | # System Files 23 | .DS_Store 24 | Thumbs.db 25 | 26 | # Local env files 27 | .env -------------------------------------------------------------------------------- /packages/core/core/index.ts: -------------------------------------------------------------------------------- 1 | export * from './embed'; 2 | export * from './generate-object'; 3 | export * from './generate-text'; 4 | export * from './prompt'; 5 | export * from './registry'; 6 | export * from './tool'; 7 | export * from './types'; 8 | export type { DeepPartial } from './util/deep-partial'; 9 | export { cosineSimilarity } from './util/cosine-similarity'; 10 | -------------------------------------------------------------------------------- /packages/provider/src/language-model/v1/language-model-v1-function-tool-call.ts: -------------------------------------------------------------------------------- 1 | export type LanguageModelV1FunctionToolCall = { 2 | toolCallType: 'function'; 3 | toolCallId: string; 4 | toolName: string; 5 | 6 | /** 7 | Stringified JSON object with the tool call arguments. Must match the 8 | parameters schema of the tool. 9 | */ 10 | args: string; 11 | }; 12 | -------------------------------------------------------------------------------- /packages/provider-utils/src/test/convert-array-to-readable-stream.ts: -------------------------------------------------------------------------------- 1 | export function convertArrayToReadableStream( 2 | values: T[], 3 | ): ReadableStream { 4 | return new ReadableStream({ 5 | start(controller) { 6 | for (const value of values) { 7 | controller.enqueue(value); 8 | } 9 | controller.close(); 10 | }, 11 | }); 12 | } 13 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Reporting Security Issues 2 | 3 | If you believe you have found a security vulnerability in the AI SDK, we encourage you to let us know right away. 4 | 5 | We will investigate all legitimate reports and do our best to quickly fix the problem. 6 | 7 | Email `security@vercel.com` to disclose any security vulnerabilities. 8 | 9 | https://vercel.com/security 10 | -------------------------------------------------------------------------------- /content/docs/08-troubleshooting/01-migration-guide/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Migration Guide 3 | description: Learn how to migrate from Vercel AI versions. 4 | --- 5 | 6 | # Migration Guide 7 | 8 | - [ Migrate AI SDK 3.0 to 3.1 ](/docs/troubleshooting/migration-guide/migration-guide-3-1) 9 | - [ Migrate AI SDK 3.1 to 3.2 ](/docs/troubleshooting/migration-guide/migration-guide-3-2) 10 | -------------------------------------------------------------------------------- /packages/core/core/util/prepare-response-headers.ts: -------------------------------------------------------------------------------- 1 | export function prepareResponseHeaders( 2 | init: ResponseInit | undefined, 3 | { contentType }: { contentType: string }, 4 | ) { 5 | const headers = new Headers(init?.headers ?? {}); 6 | 7 | if (!headers.has('Content-Type')) { 8 | headers.set('Content-Type', contentType); 9 | } 10 | 11 | return headers; 12 | } 13 | -------------------------------------------------------------------------------- /packages/provider-utils/src/get-error-message.ts: -------------------------------------------------------------------------------- 1 | export function getErrorMessage(error: unknown | undefined) { 2 | if (error == null) { 3 | return 'unknown error'; 4 | } 5 | 6 | if (typeof error === 'string') { 7 | return error; 8 | } 9 | 10 | if (error instanceof Error) { 11 | return error.message; 12 | } 13 | 14 | return JSON.stringify(error); 15 | } 16 | -------------------------------------------------------------------------------- /packages/provider/src/errors/get-error-message.ts: -------------------------------------------------------------------------------- 1 | export function getErrorMessage(error: unknown | undefined) { 2 | if (error == null) { 3 | return 'unknown error'; 4 | } 5 | 6 | if (typeof error === 'string') { 7 | return error; 8 | } 9 | 10 | if (error instanceof Error) { 11 | return error.message; 12 | } 13 | 14 | return JSON.stringify(error); 15 | } 16 | -------------------------------------------------------------------------------- /packages/provider/src/language-model/v1/language-model-v1-tool-choice.ts: -------------------------------------------------------------------------------- 1 | export type LanguageModelV1ToolChoice = 2 | | { type: 'auto' } // the tool selection is automatic (can be no tool) 3 | | { type: 'none' } // no tool must be selected 4 | | { type: 'required' } // one of the available tools must be selected 5 | | { type: 'tool'; toolName: string }; // a specific tool must be selected: 6 | -------------------------------------------------------------------------------- /packages/core/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./node_modules/@vercel/ai-tsconfig/react-library.json", 3 | "compilerOptions": { 4 | "target": "ES2018", 5 | "stripInternal": true, 6 | "lib": ["dom", "dom.iterable", "esnext"], 7 | "types": ["@types/node", "vitest/globals"] 8 | }, 9 | "include": ["."], 10 | "exclude": ["*/dist", "dist", "build", "node_modules"] 11 | } 12 | -------------------------------------------------------------------------------- /packages/core/vitest.ui.react.config.js: -------------------------------------------------------------------------------- 1 | import react from '@vitejs/plugin-react'; 2 | import { defineConfig } from 'vite'; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [react()], 7 | test: { 8 | environment: 'jsdom', 9 | globals: true, 10 | include: ['rsc/**/*.ui.test.ts{,x}'], 11 | exclude: ['**/node_modules/**'], 12 | }, 13 | }); 14 | -------------------------------------------------------------------------------- /examples/ai-core/src/registry/embed.ts: -------------------------------------------------------------------------------- 1 | import { embed } from 'ai'; 2 | import { registry } from './setup-registry'; 3 | 4 | async function main() { 5 | const { embedding } = await embed({ 6 | model: registry.textEmbeddingModel('openai:text-embedding-3-small'), 7 | value: 'sunny day at the beach', 8 | }); 9 | 10 | console.log(embedding); 11 | } 12 | 13 | main().catch(console.error); 14 | -------------------------------------------------------------------------------- /packages/vue/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @ai-sdk/vue 2 | 3 | ## 0.0.3 4 | 5 | ### Patch Changes 6 | 7 | - @ai-sdk/ui-utils@0.0.3 8 | 9 | ## 0.0.2 10 | 11 | ### Patch Changes 12 | 13 | - @ai-sdk/ui-utils@0.0.2 14 | 15 | ## 0.0.1 16 | 17 | ### Patch Changes 18 | 19 | - 85f209a4: chore: extracted ui library support into separate modules 20 | - Updated dependencies [85f209a4] 21 | - @ai-sdk/ui-utils@0.0.1 22 | -------------------------------------------------------------------------------- /packages/solid/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @ai-sdk/solid 2 | 3 | ## 0.0.3 4 | 5 | ### Patch Changes 6 | 7 | - @ai-sdk/ui-utils@0.0.3 8 | 9 | ## 0.0.2 10 | 11 | ### Patch Changes 12 | 13 | - @ai-sdk/ui-utils@0.0.2 14 | 15 | ## 0.0.1 16 | 17 | ### Patch Changes 18 | 19 | - 85f209a4: chore: extracted ui library support into separate modules 20 | - Updated dependencies [85f209a4] 21 | - @ai-sdk/ui-utils@0.0.1 22 | -------------------------------------------------------------------------------- /examples/sveltekit-openai/src/app.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %sveltekit.head% 8 | 9 | 10 |
%sveltekit.body%
11 | 12 | 13 | -------------------------------------------------------------------------------- /packages/mistral/src/mistral-embedding-settings.ts: -------------------------------------------------------------------------------- 1 | export type MistralEmbeddingModelId = 'mistral-embed' | (string & {}); 2 | 3 | export interface MistralEmbeddingSettings { 4 | /** 5 | Override the maximum number of embeddings per call. 6 | */ 7 | maxEmbeddingsPerCall?: number; 8 | 9 | /** 10 | Override the parallelism of embedding calls. 11 | */ 12 | supportsParallelCalls?: boolean; 13 | } 14 | -------------------------------------------------------------------------------- /packages/ui-utils/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | { 11 | entry: ['src/test/index.ts'], 12 | outDir: 'test/dist', 13 | format: ['cjs', 'esm'], 14 | dts: true, 15 | sourcemap: true, 16 | }, 17 | ]); 18 | -------------------------------------------------------------------------------- /content/docs/08-troubleshooting/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Troubleshooting 3 | description: Learn about the legacy providers that are no longer recommended for use in your app. 4 | --- 5 | 6 | # Troubleshooting 7 | 8 | This section is designed to help you quickly identify and resolve common issues encountered with the Vercel AI SDK, ensuring a smoother and more efficient development experience. 9 | 10 | 11 | -------------------------------------------------------------------------------- /content/examples/01-next-app/05-interface/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Generative User Interface 3 | description: Learn to build generative user interfaces using the Vercel AI SDK in your Next.js App Router application 4 | --- 5 | 6 | # Generative User Interface 7 | 8 | In this section you will learn to use the `streamUI` function to stream generative user interfaces to the client based on the response from the language model. 9 | -------------------------------------------------------------------------------- /packages/openai/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | { 11 | entry: ['src/internal/index.ts'], 12 | outDir: 'internal/dist', 13 | format: ['cjs', 'esm'], 14 | dts: true, 15 | sourcemap: true, 16 | }, 17 | ]); 18 | -------------------------------------------------------------------------------- /packages/provider-utils/tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup'; 2 | 3 | export default defineConfig([ 4 | { 5 | entry: ['src/index.ts'], 6 | format: ['cjs', 'esm'], 7 | dts: true, 8 | sourcemap: true, 9 | }, 10 | { 11 | entry: ['src/test/index.ts'], 12 | outDir: 'test/dist', 13 | format: ['cjs', 'esm'], 14 | dts: true, 15 | sourcemap: true, 16 | }, 17 | ]); 18 | -------------------------------------------------------------------------------- /packages/react/README.md: -------------------------------------------------------------------------------- 1 | # Vercel AI SDK: React provider 2 | 3 | [React](https://react.dev/) UI components for the [Vercel AI SDK](https://sdk.vercel.ai/docs): 4 | 5 | - [`useChat`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-chat) hook 6 | - [`useCompletion`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-completion) hook 7 | - [`useAssistant`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-assistant) hook 8 | -------------------------------------------------------------------------------- /packages/core/core/util/convert-zod-to-json-schema.ts: -------------------------------------------------------------------------------- 1 | import { JSONSchema7 } from 'json-schema'; 2 | import * as z from 'zod'; 3 | import zodToJsonSchema from 'zod-to-json-schema'; 4 | 5 | export function convertZodToJSONSchema( 6 | zodSchema: z.Schema, 7 | ): JSONSchema7 { 8 | // we assume that zodToJsonSchema will return a valid JSONSchema7 9 | return zodToJsonSchema(zodSchema) as JSONSchema7; 10 | } 11 | -------------------------------------------------------------------------------- /packages/svelte/README.md: -------------------------------------------------------------------------------- 1 | # Vercel AI SDK: Svelte provider 2 | 3 | [Svelte](https://svelte.dev/) UI components for the [Vercel AI SDK](https://sdk.vercel.ai/docs): 4 | 5 | - [`useChat`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-chat) hook 6 | - [`useCompletion`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-completion) hook 7 | - [`useAssistant`](https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-assistant) hook 8 | -------------------------------------------------------------------------------- /tools/eslint-config/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-config-vercel-ai", 3 | "version": "0.0.0", 4 | "license": "MIT", 5 | "main": "index.js", 6 | "dependencies": { 7 | "eslint-config-next": "^14.2.3", 8 | "eslint-config-prettier": "^8.3.0", 9 | "eslint-config-turbo": "^1.13.3", 10 | "eslint-plugin-react": "7.34.1" 11 | }, 12 | "publishConfig": { 13 | "access": "public" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /content/docs/08-troubleshooting/05-ai-sdk-errors/ai-invalid-data-content-error.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: AI_InvalidDataContentError 3 | description: How to fix AI_InvalidDataContentError 4 | --- 5 | 6 | # AI_InvalidDataContentError 7 | 8 | The data content that you have sent in a multi-modal message part is invalid. 9 | 10 | Check out the [prompt examples for multi-modal messages](/docs/ai-sdk-core/prompts#multi-modal-messages). 11 | -------------------------------------------------------------------------------- /content/providers/04-adapters/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Adapters 3 | description: Learn how to use AI SDK Adapters. 4 | --- 5 | 6 | # Adapters 7 | 8 | Adapters are lightweight integrations that enable you to use 9 | the Vercel AI SDK UI functions (`useChat` and `useCompletion`) 10 | with 3rd party libraries. 11 | 12 | The following adapters are currently available: 13 | 14 | - [LangChain](/providers/adapters/langchain) 15 | -------------------------------------------------------------------------------- /packages/provider-utils/src/test/convert-readable-stream-to-array.ts: -------------------------------------------------------------------------------- 1 | export async function convertReadableStreamToArray( 2 | stream: ReadableStream, 3 | ): Promise { 4 | const reader = stream.getReader(); 5 | const result: T[] = []; 6 | 7 | while (true) { 8 | const { done, value } = await reader.read(); 9 | if (done) break; 10 | result.push(value); 11 | } 12 | 13 | return result; 14 | } 15 | -------------------------------------------------------------------------------- /examples/ai-core/src/embed/azure.ts: -------------------------------------------------------------------------------- 1 | import { azure } from '@ai-sdk/azure'; 2 | import { embed } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const { embedding } = await embed({ 9 | model: azure.embedding('my-embedding-deployment'), 10 | value: 'sunny day at the beach', 11 | }); 12 | console.log(embedding); 13 | } 14 | 15 | main().catch(console.error); 16 | -------------------------------------------------------------------------------- /examples/ai-core/src/embed/mistral.ts: -------------------------------------------------------------------------------- 1 | import { mistral } from '@ai-sdk/mistral'; 2 | import { embed } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const { embedding } = await embed({ 9 | model: mistral.embedding('mistral-embed'), 10 | value: 'sunny day at the beach', 11 | }); 12 | 13 | console.log(embedding); 14 | } 15 | 16 | main().catch(console.error); 17 | -------------------------------------------------------------------------------- /examples/ai-core/src/embed/openai.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { embed } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const { embedding } = await embed({ 9 | model: openai.embedding('text-embedding-3-small'), 10 | value: 'sunny day at the beach', 11 | }); 12 | 13 | console.log(embedding); 14 | } 15 | 16 | main().catch(console.error); 17 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/components.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://ui.shadcn.com/schema.json", 3 | "style": "new-york", 4 | "rsc": true, 5 | "tsx": true, 6 | "tailwind": { 7 | "config": "tailwind.config.ts", 8 | "css": "app/globals.css", 9 | "baseColor": "zinc", 10 | "cssVariables": true, 11 | "prefix": "" 12 | }, 13 | "aliases": { 14 | "components": "@/components", 15 | "utils": "@/lib/utils" 16 | } 17 | } -------------------------------------------------------------------------------- /content/docs/07-reference/stream-helpers/06-stream-data.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: streamData 3 | description: Learn to use streamData helper function in your application. 4 | --- 5 | 6 | # `streamData` 7 | 8 | Allows you to stream additional data to the client. 9 | 10 | ## Import 11 | 12 | ### React 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /examples/next-openai-pages/pages/api/chat-edge.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { streamText } from 'ai'; 3 | 4 | export const runtime = 'edge'; 5 | 6 | export default async function handler(req: Request) { 7 | const { messages } = await req.json(); 8 | 9 | const result = await streamText({ 10 | model: openai('gpt-4-turbo-preview'), 11 | messages, 12 | }); 13 | 14 | return result.toAIStreamResponse(); 15 | } 16 | -------------------------------------------------------------------------------- /examples/next-openai-pages/app/api/stream-text/route.ts: -------------------------------------------------------------------------------- 1 | import { streamText } from 'ai'; 2 | import { openai } from '@ai-sdk/openai'; 3 | 4 | export async function POST(req: Request) { 5 | const { prompt }: { prompt: string } = await req.json(); 6 | 7 | const result = await streamText({ 8 | model: openai('gpt-4'), 9 | system: 'You are a helpful assistant.', 10 | prompt, 11 | }); 12 | 13 | return result.toAIStreamResponse(); 14 | } 15 | -------------------------------------------------------------------------------- /packages/core/rsc/index.ts: -------------------------------------------------------------------------------- 1 | export type { 2 | getAIState, 3 | getMutableAIState, 4 | createStreamableUI, 5 | createStreamableValue, 6 | streamUI, 7 | render, 8 | createAI, 9 | } from './rsc-server'; 10 | 11 | export type { 12 | readStreamableValue, 13 | useStreamableValue, 14 | useUIState, 15 | useAIState, 16 | useActions, 17 | useSyncUIState, 18 | } from './rsc-client'; 19 | 20 | export type { StreamableValue } from './types'; 21 | -------------------------------------------------------------------------------- /examples/next-openai-pages/app/api/generate-text/route.ts: -------------------------------------------------------------------------------- 1 | import { generateText } from 'ai'; 2 | import { openai } from '@ai-sdk/openai'; 3 | 4 | export async function POST(req: Request) { 5 | const { prompt }: { prompt: string } = await req.json(); 6 | 7 | const { text } = await generateText({ 8 | model: openai('gpt-4'), 9 | system: 'You are a helpful assistant.', 10 | prompt, 11 | }); 12 | 13 | return Response.json({ text }); 14 | } 15 | -------------------------------------------------------------------------------- /packages/core/vitest.node.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'node', 7 | globals: true, 8 | include: ['**/*.test.ts{,x}'], 9 | exclude: [ 10 | '**/*.ui.test.ts{,x}', 11 | '**/*.e2e.test.ts{,x}', 12 | '**/node_modules/**', 13 | ], 14 | typecheck: { 15 | enabled: true, 16 | }, 17 | }, 18 | }); 19 | -------------------------------------------------------------------------------- /.kodiak.toml: -------------------------------------------------------------------------------- 1 | # .kodiak.toml 2 | version = 1 3 | 4 | [merge] 5 | automerge_label = "automerge" 6 | require_automerge_label = false 7 | method = "squash" 8 | delete_branch_on_merge = true 9 | optimistic_updates = false 10 | prioritize_ready_to_merge = true 11 | notify_on_conflict = false 12 | 13 | [merge.message] 14 | title = "pull_request_title" 15 | body = "pull_request_body" 16 | include_pr_number = true 17 | body_type = "markdown" 18 | strip_html_comments = true 19 | -------------------------------------------------------------------------------- /examples/solidstart-openai/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowSyntheticDefaultImports": true, 4 | "esModuleInterop": true, 5 | "target": "ESNext", 6 | "module": "ESNext", 7 | "moduleResolution": "node", 8 | "jsxImportSource": "solid-js", 9 | "jsx": "preserve", 10 | "strict": true, 11 | "types": ["solid-start/env"], 12 | "baseUrl": "./", 13 | "paths": { 14 | "~/*": ["./src/*"] 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /packages/core/vitest.edge.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | 3 | // https://vitejs.dev/config/ 4 | export default defineConfig({ 5 | test: { 6 | environment: 'edge-runtime', 7 | globals: true, 8 | include: ['**/*.test.ts{,x}'], 9 | exclude: [ 10 | '**/*.ui.test.ts{,x}', 11 | '**/*.e2e.test.ts{,x}', 12 | '**/node_modules/**', 13 | ], 14 | typecheck: { 15 | enabled: true, 16 | }, 17 | }, 18 | }); 19 | -------------------------------------------------------------------------------- /content/examples/01-next-app/02-chat/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Chat 3 | description: Learn how to generate chat completions using the Vercel AI SDK in your Next.js application 4 | --- 5 | 6 | # Chat 7 | 8 | So far we've learned how to generate text and structured data using single prompts. In this section, we will learn to use `messages` to add a sequence of messages to the language model and generate the response based on the context of the conversation – called chat completion. 9 | -------------------------------------------------------------------------------- /packages/ui-utils/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @ai-sdk/ui-utils 2 | 3 | ## 0.0.3 4 | 5 | ### Patch Changes 6 | 7 | - Updated dependencies [85712895] 8 | - Updated dependencies [85712895] 9 | - @ai-sdk/provider-utils@0.0.15 10 | 11 | ## 0.0.2 12 | 13 | ### Patch Changes 14 | 15 | - Updated dependencies [7910ae84] 16 | - @ai-sdk/provider-utils@0.0.14 17 | 18 | ## 0.0.1 19 | 20 | ### Patch Changes 21 | 22 | - 85f209a4: chore: extracted ui library support into separate modules 23 | -------------------------------------------------------------------------------- /examples/next-openai-pages/app/api/stream-chat/route.ts: -------------------------------------------------------------------------------- 1 | import { CoreMessage, streamText } from 'ai'; 2 | import { openai } from '@ai-sdk/openai'; 3 | 4 | export async function POST(req: Request) { 5 | const { messages }: { messages: CoreMessage[] } = await req.json(); 6 | 7 | const result = await streamText({ 8 | model: openai('gpt-4'), 9 | system: 'You are a helpful assistant.', 10 | messages, 11 | }); 12 | 13 | return result.toAIStreamResponse(); 14 | } 15 | -------------------------------------------------------------------------------- /packages/cohere/src/cohere-error.ts: -------------------------------------------------------------------------------- 1 | import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils'; 2 | import { z } from 'zod'; 3 | 4 | const cohereErrorDataSchema = z.object({ 5 | message: z.string(), 6 | }); 7 | 8 | export type CohereErrorData = z.infer; 9 | 10 | export const cohereFailedResponseHandler = createJsonErrorResponseHandler({ 11 | errorSchema: cohereErrorDataSchema, 12 | errorToMessage: data => data.message, 13 | }); 14 | -------------------------------------------------------------------------------- /packages/core/tests/e2e/next-server/app/streamable/page.js: -------------------------------------------------------------------------------- 1 | import { 2 | streamableUI, 3 | streamableValue, 4 | streamableUIAppend, 5 | streamableUIError, 6 | } from './actions'; 7 | import { Client } from './client'; 8 | 9 | export default function Page() { 10 | return ( 11 | 19 | ); 20 | } 21 | -------------------------------------------------------------------------------- /packages/core/tests/snapshots/replicate.ts: -------------------------------------------------------------------------------- 1 | export const replicateTextChunks = [ 2 | ': hi\n\n', 3 | 'event: output\nid: 1698774369:0\ndata:\n\n', 4 | 'event: output\nid: 1698774369:1\ndata:\n\n', 5 | 'event: output\nid: 1698774370:0\ndata: Hello,\n\n', 6 | 'event: output\nid: 1698774370:1\ndata: world\n\n', 7 | 'event: output\nid: 1698774370:2\ndata: .\n\n', 8 | 'event: output\nid: 1698774371:0\ndata:\n\n', 9 | 'event: done\nid: 1698774371:1\ndata: {}\n\n', 10 | ]; 11 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/azure.ts: -------------------------------------------------------------------------------- 1 | import { azure } from '@ai-sdk/azure'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: azure('v0-gpt-35-turbo'), // use your own deployment 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | console.log(result.text); 14 | } 15 | 16 | main().catch(console.error); 17 | -------------------------------------------------------------------------------- /examples/next-openai/app/completion-rsc/generate-completion.ts: -------------------------------------------------------------------------------- 1 | 'use server'; 2 | 3 | import { openai } from '@ai-sdk/openai'; 4 | import { streamText } from 'ai'; 5 | import { createStreamableValue } from 'ai/rsc'; 6 | 7 | export async function generateCompletion(prompt: string) { 8 | const result = await streamText({ 9 | model: openai('gpt-4-turbo'), 10 | maxTokens: 2000, 11 | prompt, 12 | }); 13 | 14 | return createStreamableValue(result.textStream).value; 15 | } 16 | -------------------------------------------------------------------------------- /content/docs/07-reference/ai-sdk-rsc/06-get-ai-state.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: getAIState 3 | description: Reference for the getAIState function from the AI SDK RSC 4 | --- 5 | 6 | # `getAIState` 7 | 8 | Get the current AI state. 9 | 10 | ## Import 11 | 12 | 13 | 14 | 15 | 16 | ## Examples 17 | 18 | 19 | -------------------------------------------------------------------------------- /content/providers/03-community-providers/02-llama-cpp.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: LLamaCpp 3 | description: Learn how to use Llama CPP. 4 | --- 5 | 6 | # LLamaCpp Provider 7 | 8 | > This is an example page for a community provider. It could contain more 9 | > instructions. 10 | 11 | [nnance/llamacpp-ai-provider](https://github.com/nnance/llamacpp-ai-provider) is a community provider that uses [Llama.cpp](https://github.com/ggerganov/llama.cpp) to provide language model support for the Vercel AI SDK. 12 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/openai-logprobs.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: openai('gpt-3.5-turbo', { logprobs: 2 }), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | console.log(result.logprobs); 14 | } 15 | 16 | main().catch(console.error); 17 | -------------------------------------------------------------------------------- /examples/ai-core/src/registry/stream-text.ts: -------------------------------------------------------------------------------- 1 | import { streamText } from 'ai'; 2 | import { registry } from './setup-registry'; 3 | 4 | async function main() { 5 | const result = await streamText({ 6 | model: registry.languageModel('openai:gpt-4-turbo'), 7 | prompt: 'Invent a new holiday and describe its traditions.', 8 | }); 9 | 10 | for await (const textPart of result.textStream) { 11 | process.stdout.write(textPart); 12 | } 13 | } 14 | 15 | main().catch(console.error); 16 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/components/chat-list.tsx: -------------------------------------------------------------------------------- 1 | import { Separator } from '@/components/ui/separator'; 2 | 3 | export function ChatList({ messages }: { messages: any[] }) { 4 | if (!messages.length) { 5 | return null; 6 | } 7 | 8 | return ( 9 |
10 | {messages.map((message, index) => ( 11 |
12 | {message.display} 13 |
14 | ))} 15 |
16 | ); 17 | } 18 | -------------------------------------------------------------------------------- /examples/ai-core/src/tools/weather-tool.ts: -------------------------------------------------------------------------------- 1 | import { tool } from 'ai'; 2 | import { z } from 'zod'; 3 | 4 | export const weatherTool = tool({ 5 | description: 'Get the weather in a location', 6 | parameters: z.object({ 7 | location: z.string().describe('The location to get the weather for'), 8 | }), 9 | // location below is inferred to be a string: 10 | execute: async ({ location }) => ({ 11 | location, 12 | temperature: 72 + Math.floor(Math.random() * 21) - 10, 13 | }), 14 | }); 15 | -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/middleware.ts: -------------------------------------------------------------------------------- 1 | import type { NextFetchEvent, NextRequest } from 'next/server'; 2 | import { kasadaHandler } from './kasada/kasada-server'; 3 | 4 | export async function middleware(req: NextRequest, ev: NextFetchEvent) { 5 | if (req.method === 'POST') { 6 | if (process.env.NODE_ENV === 'development') { 7 | return undefined; 8 | } 9 | return kasadaHandler(req, ev); 10 | } 11 | } 12 | 13 | export const config = { matcher: ['/api/chat'] }; 14 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/openai-completion.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: openai('gpt-3.5-turbo-instruct'), 10 | maxTokens: 1024, 11 | prompt: 'Invent a new holiday and describe its traditions.', 12 | }); 13 | 14 | console.log(result.text); 15 | } 16 | 17 | main().catch(console.error); 18 | -------------------------------------------------------------------------------- /examples/ai-core/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "strict": true, 4 | "declaration": true, 5 | "sourceMap": true, 6 | "target": "es2022", 7 | "lib": ["es2022", "dom"], 8 | "module": "esnext", 9 | "types": ["node"], 10 | "esModuleInterop": true, 11 | "allowSyntheticDefaultImports": true, 12 | "moduleResolution": "node", 13 | "rootDir": "./src", 14 | "outDir": "./build", 15 | "skipLibCheck": true 16 | }, 17 | "include": ["src/**/*.ts"] 18 | } 19 | -------------------------------------------------------------------------------- /packages/provider/src/language-model/v1/index.ts: -------------------------------------------------------------------------------- 1 | export * from './language-model-v1'; 2 | export * from './language-model-v1-call-options'; 3 | export * from './language-model-v1-call-warning'; 4 | export * from './language-model-v1-finish-reason'; 5 | export * from './language-model-v1-function-tool'; 6 | export * from './language-model-v1-function-tool-call'; 7 | export * from './language-model-v1-logprobs'; 8 | export * from './language-model-v1-prompt'; 9 | export * from './language-model-v1-tool-choice'; 10 | -------------------------------------------------------------------------------- /packages/provider/src/language-model/v1/language-model-v1-call-warning.ts: -------------------------------------------------------------------------------- 1 | import { LanguageModelV1CallSettings } from './language-model-v1-call-settings'; 2 | 3 | /** 4 | Warning from the model provider for this call. The call will proceed, but e.g. 5 | some settings might not be supported, which can lead to suboptimal results. 6 | */ 7 | export type LanguageModelV1CallWarning = 8 | | { type: 'unsupported-setting'; setting: keyof LanguageModelV1CallSettings } 9 | | { type: 'other'; message: string }; 10 | -------------------------------------------------------------------------------- /content/examples/01-next-app/01-basics/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Basics 3 | description: Learn the basics of using the Vercel AI SDK in your Next.js App Router application. 4 | --- 5 | 6 | # Basics 7 | 8 | One of the most basic things you can do with language models is to generate text. In this section, you will learn to generate text, and also stream it to the client. 9 | 10 | Beyond text, you will also learn to generate structured data by providing a schema of your choice, and also stream it to the client. 11 | -------------------------------------------------------------------------------- /examples/next-openai-pages/app/api/generate-chat/route.ts: -------------------------------------------------------------------------------- 1 | import { CoreMessage, generateText } from 'ai'; 2 | import { openai } from '@ai-sdk/openai'; 3 | 4 | export async function POST(req: Request) { 5 | const { messages }: { messages: CoreMessage[] } = await req.json(); 6 | 7 | const { responseMessages } = await generateText({ 8 | model: openai('gpt-4'), 9 | system: 'You are a helpful assistant.', 10 | messages, 11 | }); 12 | 13 | return Response.json({ messages: responseMessages }); 14 | } 15 | -------------------------------------------------------------------------------- /examples/node-http-server/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "strict": true, 4 | "declaration": true, 5 | "sourceMap": true, 6 | "target": "es2022", 7 | "lib": ["es2022", "dom"], 8 | "module": "esnext", 9 | "types": ["node"], 10 | "esModuleInterop": true, 11 | "allowSyntheticDefaultImports": true, 12 | "moduleResolution": "node", 13 | "rootDir": "./src", 14 | "outDir": "./build", 15 | "skipLibCheck": true 16 | }, 17 | "include": ["src/**/*.ts"] 18 | } 19 | -------------------------------------------------------------------------------- /packages/core/core/types/errors.ts: -------------------------------------------------------------------------------- 1 | export { 2 | APICallError, 3 | EmptyResponseBodyError, 4 | InvalidArgumentError, 5 | InvalidDataContentError, 6 | InvalidPromptError, 7 | InvalidResponseDataError, 8 | InvalidToolArgumentsError, 9 | JSONParseError, 10 | LoadAPIKeyError, 11 | NoObjectGeneratedError, 12 | NoSuchToolError, 13 | RetryError, 14 | ToolCallParseError, 15 | TypeValidationError, 16 | UnsupportedFunctionalityError, 17 | UnsupportedJSONSchemaError, 18 | } from '@ai-sdk/provider'; 19 | -------------------------------------------------------------------------------- /packages/mistral/src/map-mistral-finish-reason.ts: -------------------------------------------------------------------------------- 1 | import { LanguageModelV1FinishReason } from '@ai-sdk/provider'; 2 | 3 | export function mapMistralFinishReason( 4 | finishReason: string | null | undefined, 5 | ): LanguageModelV1FinishReason { 6 | switch (finishReason) { 7 | case 'stop': 8 | return 'stop'; 9 | case 'length': 10 | case 'model_length': 11 | return 'length'; 12 | case 'tool_calls': 13 | return 'tool-calls'; 14 | default: 15 | return 'other'; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/cohere.ts: -------------------------------------------------------------------------------- 1 | import { cohere } from '@ai-sdk/cohere'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const { text, usage } = await generateText({ 9 | model: cohere('command-r-plus'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | console.log(text); 14 | console.log(); 15 | console.log('Usage:', usage); 16 | } 17 | 18 | main().catch(console.error); 19 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/openai.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const { text, usage } = await generateText({ 9 | model: openai('gpt-3.5-turbo'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | console.log(text); 14 | console.log(); 15 | console.log('Usage:', usage); 16 | } 17 | 18 | main().catch(console.error); 19 | -------------------------------------------------------------------------------- /packages/provider/src/errors/load-api-key-error.ts: -------------------------------------------------------------------------------- 1 | export class LoadAPIKeyError extends Error { 2 | constructor({ message }: { message: string }) { 3 | super(message); 4 | 5 | this.name = 'AI_LoadAPIKeyError'; 6 | } 7 | 8 | static isLoadAPIKeyError(error: unknown): error is LoadAPIKeyError { 9 | return error instanceof Error && error.name === 'AI_LoadAPIKeyError'; 10 | } 11 | 12 | toJSON() { 13 | return { 14 | name: this.name, 15 | message: this.message, 16 | }; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /content/docs/06-advanced/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Advanced 3 | description: Learn how to use advanced functionality within the AI SDK and RSC API. 4 | --- 5 | 6 | # Advanced 7 | 8 | This section covers advanced topics and concepts for the AI SDK and RSC API. Working with LLMs often requires a different mental model compared to traditional software development. 9 | 10 | After these concepts, you should have a better understanding of the paradigms behind the AI SDK and RSC API, and how to use them to build more AI applications. 11 | -------------------------------------------------------------------------------- /packages/anthropic/src/map-anthropic-stop-reason.ts: -------------------------------------------------------------------------------- 1 | import { LanguageModelV1FinishReason } from '@ai-sdk/provider'; 2 | 3 | export function mapAnthropicStopReason( 4 | finishReason: string | null | undefined, 5 | ): LanguageModelV1FinishReason { 6 | switch (finishReason) { 7 | case 'end_turn': 8 | case 'stop_sequence': 9 | return 'stop'; 10 | case 'tool_use': 11 | return 'tool-calls'; 12 | case 'max_tokens': 13 | return 'length'; 14 | default: 15 | return 'other'; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /packages/mistral/src/mistral-chat-settings.ts: -------------------------------------------------------------------------------- 1 | // https://docs.mistral.ai/platform/endpoints/ 2 | export type MistralChatModelId = 3 | | 'open-mistral-7b' 4 | | 'open-mixtral-8x7b' 5 | | 'open-mixtral-8x22b' 6 | | 'mistral-small-latest' 7 | | 'mistral-medium-latest' 8 | | 'mistral-large-latest' 9 | | (string & {}); 10 | 11 | export interface MistralChatSettings { 12 | /** 13 | Whether to inject a safety prompt before all conversations. 14 | 15 | Defaults to `false`. 16 | */ 17 | safePrompt?: boolean; 18 | } 19 | -------------------------------------------------------------------------------- /packages/provider/src/errors/load-setting-error.ts: -------------------------------------------------------------------------------- 1 | export class LoadSettingError extends Error { 2 | constructor({ message }: { message: string }) { 3 | super(message); 4 | 5 | this.name = 'AI_LoadSettingError'; 6 | } 7 | 8 | static isLoadSettingError(error: unknown): error is LoadSettingError { 9 | return error instanceof Error && error.name === 'AI_LoadSettingError'; 10 | } 11 | 12 | toJSON() { 13 | return { 14 | name: this.name, 15 | message: this.message, 16 | }; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /packages/provider-utils/src/test/index.ts: -------------------------------------------------------------------------------- 1 | export * from './convert-array-to-readable-stream'; 2 | export * from './convert-async-iterable-to-array'; 3 | export * from './convert-readable-stream-to-array'; 4 | export * from './json-test-server'; 5 | export * from './streaming-test-server'; 6 | 7 | import { convertReadableStreamToArray } from './convert-readable-stream-to-array'; 8 | 9 | /** 10 | * @deprecated Use `convertReadableStreamToArray` instead. 11 | */ 12 | export const convertStreamToArray = convertReadableStreamToArray; 13 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/openai-system-message-b.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: openai('gpt-3.5-turbo'), 10 | system: 'You are a helpful assistant.', 11 | messages: [{ role: 'user', content: 'What is the capital of France?' }], 12 | }); 13 | 14 | console.log(result.text); 15 | } 16 | 17 | main().catch(console.error); 18 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/app/stream-object/itinerary.tsx: -------------------------------------------------------------------------------- 1 | import { DeepPartial } from 'ai'; 2 | import { z } from 'zod'; 3 | 4 | export const itinerarySchema = z.object({ 5 | days: z.array( 6 | z.object({ 7 | theme: z.string(), 8 | activities: z.array( 9 | z.object({ 10 | name: z.string(), 11 | description: z.string(), 12 | duration: z.number(), 13 | }), 14 | ), 15 | }), 16 | ), 17 | }); 18 | 19 | export type PartialItinerary = DeepPartial; 20 | -------------------------------------------------------------------------------- /examples/next-inkeep/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css'; 2 | import { Inter } from 'next/font/google'; 3 | 4 | const inter = Inter({ subsets: ['latin'] }); 5 | 6 | export const metadata = { 7 | title: 'Create Next App', 8 | description: 'Generated by create next app', 9 | }; 10 | 11 | export default function RootLayout({ 12 | children, 13 | }: { 14 | children: React.ReactNode; 15 | }) { 16 | return ( 17 | 18 | {children} 19 | 20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /.changeset/README.md: -------------------------------------------------------------------------------- 1 | # Changesets 2 | 3 | Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works 4 | with multi-package repos, or single-package repos to help you version and publish your code. You can 5 | find the full documentation for it [in our repository](https://github.com/changesets/changesets) 6 | 7 | We have a quick list of common questions to get you started engaging with this project in 8 | [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) 9 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/azure.ts: -------------------------------------------------------------------------------- 1 | import { azure } from '@ai-sdk/azure'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: azure('gpt-4o'), // use your own deployment 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | for await (const textPart of result.textStream) { 14 | process.stdout.write(textPart); 15 | } 16 | } 17 | 18 | main().catch(console.error); 19 | -------------------------------------------------------------------------------- /examples/next-aws-bedrock/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css'; 2 | import { Inter } from 'next/font/google'; 3 | 4 | const inter = Inter({ subsets: ['latin'] }); 5 | 6 | export const metadata = { 7 | title: 'Create Next App', 8 | description: 'Generated by create next app', 9 | }; 10 | 11 | export default function RootLayout({ 12 | children, 13 | }: { 14 | children: React.ReactNode; 15 | }) { 16 | return ( 17 | 18 | {children} 19 | 20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /examples/next-huggingface/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css'; 2 | import { Inter } from 'next/font/google'; 3 | 4 | const inter = Inter({ subsets: ['latin'] }); 5 | 6 | export const metadata = { 7 | title: 'Create Next App', 8 | description: 'Generated by create next app', 9 | }; 10 | 11 | export default function RootLayout({ 12 | children, 13 | }: { 14 | children: React.ReactNode; 15 | }) { 16 | return ( 17 | 18 | {children} 19 | 20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /examples/next-langchain/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css'; 2 | import { Inter } from 'next/font/google'; 3 | 4 | const inter = Inter({ subsets: ['latin'] }); 5 | 6 | export const metadata = { 7 | title: 'Create Next App', 8 | description: 'Generated by create next app', 9 | }; 10 | 11 | export default function RootLayout({ 12 | children, 13 | }: { 14 | children: React.ReactNode; 15 | }) { 16 | return ( 17 | 18 | {children} 19 | 20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /examples/next-replicate/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css'; 2 | import { Inter } from 'next/font/google'; 3 | 4 | const inter = Inter({ subsets: ['latin'] }); 5 | 6 | export const metadata = { 7 | title: 'Create Next App', 8 | description: 'Generated by create next app', 9 | }; 10 | 11 | export default function RootLayout({ 12 | children, 13 | }: { 14 | children: React.ReactNode; 15 | }) { 16 | return ( 17 | 18 | {children} 19 | 20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /packages/provider-utils/src/extract-response-headers.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Extracts the headers from a response object and returns them as a key-value object. 3 | 4 | @param response - The response object to extract headers from. 5 | @returns The headers as a key-value object. 6 | */ 7 | export function extractResponseHeaders( 8 | response: Response, 9 | ): Record { 10 | const headers: Record = {}; 11 | response.headers.forEach((value, key) => { 12 | headers[key] = value; 13 | }); 14 | return headers; 15 | } 16 | -------------------------------------------------------------------------------- /content/docs/08-troubleshooting/03-common-issues/05-streamable-ui-errors.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Streamable UI Errors 3 | description: Troubleshooting errors related to streamable UI. 4 | --- 5 | 6 | # Streamable UI Component Error 7 | 8 | ## Issue 9 | 10 | - Variable Not Found 11 | - Cannot find `div` 12 | - `Component` refers to a value, but is being used as a type 13 | 14 | ## Solution 15 | 16 | If you encounter these errors when working with streamable UIs within server actions, it is likely because the file ends in `.ts` instead of `.tsx`. 17 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/components/llm-stocks/spinner.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | export const spinner = ( 4 | 14 | 15 | 16 | ); 17 | -------------------------------------------------------------------------------- /examples/ai-core/src/embed-many/azure.ts: -------------------------------------------------------------------------------- 1 | import { azure } from '@ai-sdk/azure'; 2 | import { embedMany } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const { embeddings } = await embedMany({ 9 | model: azure.embedding('my-embedding-deployment'), 10 | values: [ 11 | 'sunny day at the beach', 12 | 'rainy afternoon in the city', 13 | 'snowy night in the mountains', 14 | ], 15 | }); 16 | 17 | console.log(embeddings); 18 | } 19 | 20 | main().catch(console.error); 21 | -------------------------------------------------------------------------------- /examples/ai-core/src/embed-many/mistral.ts: -------------------------------------------------------------------------------- 1 | import { mistral } from '@ai-sdk/mistral'; 2 | import { embedMany } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const { embeddings } = await embedMany({ 9 | model: mistral.embedding('mistral-embed'), 10 | values: [ 11 | 'sunny day at the beach', 12 | 'rainy afternoon in the city', 13 | 'snowy night in the mountains', 14 | ], 15 | }); 16 | 17 | console.log(embeddings); 18 | } 19 | 20 | main().catch(console.error); 21 | -------------------------------------------------------------------------------- /examples/next-cohere/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /examples/next-inkeep/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /examples/next-langchain/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /examples/next-openai/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /examples/next-replicate/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /content/docs/07-reference/ai-sdk-rsc/04-create-streamable-value.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: createStreamableValue 3 | description: Reference for the createStreamableValue function from the AI SDK RSC 4 | --- 5 | 6 | # `createStreamableValue` 7 | 8 | Create a stream that sends values from the server to the client. The value can be any serializable data. 9 | 10 | ## Import 11 | 12 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /examples/ai-core/src/embed-many/openai.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { embedMany } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const { embeddings } = await embedMany({ 9 | model: openai.embedding('text-embedding-3-small'), 10 | values: [ 11 | 'sunny day at the beach', 12 | 'rainy afternoon in the city', 13 | 'snowy night in the mountains', 14 | ], 15 | }); 16 | 17 | console.log(embeddings); 18 | } 19 | 20 | main().catch(console.error); 21 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/components/providers.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import * as React from 'react'; 4 | import { ThemeProvider as NextThemesProvider } from 'next-themes'; 5 | import { ThemeProviderProps } from 'next-themes/dist/types'; 6 | 7 | import { TooltipProvider } from '@/components/ui/tooltip'; 8 | 9 | export function Providers({ children, ...props }: ThemeProviderProps) { 10 | return ( 11 | 12 | {children} 13 | 14 | ); 15 | } 16 | -------------------------------------------------------------------------------- /examples/next-aws-bedrock/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /examples/next-huggingface/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /examples/next-openai-pages/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /tools/tsconfig/nextjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/tsconfig", 3 | "display": "Next.js", 4 | "extends": "./base.json", 5 | "compilerOptions": { 6 | "allowJs": true, 7 | "declaration": false, 8 | "declarationMap": false, 9 | "incremental": true, 10 | "jsx": "preserve", 11 | "lib": ["dom", "dom.iterable", "esnext"], 12 | "module": "esnext", 13 | "noEmit": true, 14 | "resolveJsonModule": true, 15 | "target": "es5" 16 | }, 17 | "include": ["src", "next-env.d.ts"], 18 | "exclude": ["node_modules"] 19 | } 20 | -------------------------------------------------------------------------------- /content/examples/20-providers/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Providers 3 | description: Examples of using provider functionality in Vercel AI SDK. 4 | --- 5 | 6 | # Provider Examples 7 | 8 | The following section will guide you through the various capabilities of the Vercel AI SDKs using minimal code examples. 9 | 10 | 19 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/openai-system-message-a.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: openai('gpt-3.5-turbo'), 10 | messages: [ 11 | { role: 'system', content: 'You are a helpful assistant.' }, 12 | { role: 'user', content: 'What is the capital of France?' }, 13 | ], 14 | }); 15 | 16 | console.log(result.text); 17 | } 18 | 19 | main().catch(console.error); 20 | -------------------------------------------------------------------------------- /examples/next-cohere/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /examples/next-inkeep/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /examples/next-langchain/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /examples/next-openai/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /examples/next-replicate/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /examples/next-aws-bedrock/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /examples/next-huggingface/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /examples/next-openai-pages/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /packages/google/src/google-error.ts: -------------------------------------------------------------------------------- 1 | import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils'; 2 | import { z } from 'zod'; 3 | 4 | const googleErrorDataSchema = z.object({ 5 | error: z.object({ 6 | code: z.number().nullable(), 7 | message: z.string(), 8 | status: z.string(), 9 | }), 10 | }); 11 | 12 | export type GoogleErrorData = z.infer; 13 | 14 | export const googleFailedResponseHandler = createJsonErrorResponseHandler({ 15 | errorSchema: googleErrorDataSchema, 16 | errorToMessage: data => data.error.message, 17 | }); 18 | -------------------------------------------------------------------------------- /packages/openai/src/map-openai-finish-reason.ts: -------------------------------------------------------------------------------- 1 | import { LanguageModelV1FinishReason } from '@ai-sdk/provider'; 2 | 3 | export function mapOpenAIFinishReason( 4 | finishReason: string | null | undefined, 5 | ): LanguageModelV1FinishReason { 6 | switch (finishReason) { 7 | case 'stop': 8 | return 'stop'; 9 | case 'length': 10 | return 'length'; 11 | case 'content_filter': 12 | return 'content-filter'; 13 | case 'function_call': 14 | case 'tool_calls': 15 | return 'tool-calls'; 16 | default: 17 | return 'unknown'; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /examples/next-openai/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css'; 2 | import { Inter } from 'next/font/google'; 3 | 4 | const inter = Inter({ subsets: ['latin'] }); 5 | 6 | export const metadata = { 7 | title: 'Vercel AI SDK - Next.js OpenAI Examples', 8 | description: 'Examples of using the Vercel AI SDK with Next.js and OpenAI.', 9 | }; 10 | 11 | export default function RootLayout({ 12 | children, 13 | }: { 14 | children: React.ReactNode; 15 | }) { 16 | return ( 17 | 18 | {children} 19 | 20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /packages/core/vue/index.ts: -------------------------------------------------------------------------------- 1 | import { 2 | useChat as useChatVue, 3 | useCompletion as useCompletionVue, 4 | } from '@ai-sdk/vue'; 5 | 6 | /** 7 | * @deprecated Use `useChat` from `@ai-sdk/vue` instead. 8 | */ 9 | export const useChat = useChatVue; 10 | 11 | /** 12 | * @deprecated Use `useCompletion` from `@ai-sdk/vue` instead. 13 | */ 14 | export const useCompletion = useCompletionVue; 15 | 16 | /** 17 | * @deprecated Use `@ai-sdk/vue` instead. 18 | */ 19 | export type { 20 | CreateMessage, 21 | Message, 22 | UseChatOptions, 23 | UseChatHelpers, 24 | } from '@ai-sdk/vue'; 25 | -------------------------------------------------------------------------------- /packages/provider-utils/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './convert-async-generator-to-readable-stream'; 2 | export * from './download'; 3 | export * from './extract-response-headers'; 4 | export * from './generate-id'; 5 | export * from './get-error-message'; 6 | export * from './is-abort-error'; 7 | export * from './load-api-key'; 8 | export * from './load-setting'; 9 | export * from './parse-json'; 10 | export * from './post-to-api'; 11 | export * from './response-handler'; 12 | export * from './uint8-utils'; 13 | export * from './validate-types'; 14 | export * from './without-trailing-slash'; 15 | -------------------------------------------------------------------------------- /examples/ai-core/src/embed-many/openai-cosine-similarity.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { cosineSimilarity, embedMany } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const { embeddings } = await embedMany({ 9 | model: openai.embedding('text-embedding-3-small'), 10 | values: ['sunny day at the beach', 'rainy afternoon in the city'], 11 | }); 12 | 13 | console.log( 14 | `cosine similarity: ${cosineSimilarity(embeddings[0], embeddings[1])}`, 15 | ); 16 | } 17 | 18 | main().catch(console.error); 19 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/google.ts: -------------------------------------------------------------------------------- 1 | import { google } from '@ai-sdk/google'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: google('models/gemini-pro'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | console.log(result.text); 14 | console.log(); 15 | console.log('Token usage:', result.usage); 16 | console.log('Finish reason:', result.finishReason); 17 | } 18 | 19 | main().catch(console.error); 20 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/mistral.ts: -------------------------------------------------------------------------------- 1 | import { mistral } from '@ai-sdk/mistral'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: mistral('open-mistral-7b'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | console.log(result.text); 14 | console.log(); 15 | console.log('Token usage:', result.usage); 16 | console.log('Finish reason:', result.finishReason); 17 | } 18 | 19 | main().catch(console.error); 20 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/azure-completion.ts: -------------------------------------------------------------------------------- 1 | import { azure } from '@ai-sdk/azure'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: azure.completion('my-gpt-35-turbo-instruct-deployment'), // use your own deployment 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | for await (const textPart of result.textStream) { 14 | process.stdout.write(textPart); 15 | } 16 | } 17 | 18 | main().catch(console.error); 19 | -------------------------------------------------------------------------------- /packages/anthropic/src/anthropic-messages-settings.ts: -------------------------------------------------------------------------------- 1 | // https://docs.anthropic.com/claude/docs/models-overview 2 | export type AnthropicMessagesModelId = 3 | | 'claude-3-opus-20240229' 4 | | 'claude-3-sonnet-20240229' 5 | | 'claude-3-haiku-20240307' 6 | | (string & {}); 7 | 8 | export interface AnthropicMessagesSettings { 9 | /** 10 | Only sample from the top K options for each subsequent token. 11 | 12 | Used to remove "long tail" low probability responses. 13 | Recommended for advanced use cases only. You usually only need to use temperature. 14 | */ 15 | topK?: number; 16 | } 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2023 Vercel, Inc. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /content/docs/07-reference/ai-sdk-rsc/07-get-mutable-ai-state.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: getMutableAIState 3 | description: Reference for the getMutableAIState function from the AI SDK RSC 4 | --- 5 | 6 | # `getMutableAIState` 7 | 8 | Get a mutable copy of the AI state. You can use this to update the state in the server. 9 | 10 | ## Import 11 | 12 | 13 | 14 | 15 | 16 | ## Examples 17 | 18 | 19 | -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | -------------------------------------------------------------------------------- /packages/anthropic/src/anthropic-error.ts: -------------------------------------------------------------------------------- 1 | import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils'; 2 | import { z } from 'zod'; 3 | 4 | const anthropicErrorDataSchema = z.object({ 5 | type: z.literal('error'), 6 | error: z.object({ 7 | type: z.string(), 8 | message: z.string(), 9 | }), 10 | }); 11 | 12 | export type AnthropicErrorData = z.infer; 13 | 14 | export const anthropicFailedResponseHandler = createJsonErrorResponseHandler({ 15 | errorSchema: anthropicErrorDataSchema, 16 | errorToMessage: data => data.error.message, 17 | }); 18 | -------------------------------------------------------------------------------- /packages/react/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @ai-sdk/react 2 | 3 | ## 0.0.3 4 | 5 | ### Patch Changes 6 | 7 | - Updated dependencies [85712895] 8 | - Updated dependencies [85712895] 9 | - @ai-sdk/provider-utils@0.0.15 10 | - @ai-sdk/ui-utils@0.0.3 11 | 12 | ## 0.0.2 13 | 14 | ### Patch Changes 15 | 16 | - Updated dependencies [7910ae84] 17 | - @ai-sdk/provider-utils@0.0.14 18 | - @ai-sdk/ui-utils@0.0.2 19 | 20 | ## 0.0.1 21 | 22 | ### Patch Changes 23 | 24 | - 85f209a4: chore: extracted ui library support into separate modules 25 | - Updated dependencies [85f209a4] 26 | - @ai-sdk/ui-utils@0.0.1 27 | -------------------------------------------------------------------------------- /packages/svelte/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @ai-sdk/svelte 2 | 3 | ## 0.0.3 4 | 5 | ### Patch Changes 6 | 7 | - Updated dependencies [85712895] 8 | - Updated dependencies [85712895] 9 | - @ai-sdk/provider-utils@0.0.15 10 | - @ai-sdk/ui-utils@0.0.3 11 | 12 | ## 0.0.2 13 | 14 | ### Patch Changes 15 | 16 | - Updated dependencies [7910ae84] 17 | - @ai-sdk/provider-utils@0.0.14 18 | - @ai-sdk/ui-utils@0.0.2 19 | 20 | ## 0.0.1 21 | 22 | ### Patch Changes 23 | 24 | - 85f209a4: chore: extracted ui library support into separate modules 25 | - Updated dependencies [85f209a4] 26 | - @ai-sdk/ui-utils@0.0.1 27 | -------------------------------------------------------------------------------- /content/docs/07-reference/ai-sdk-rsc/02-create-ai.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: createAI 3 | description: Reference for the createAI function from the AI SDK RSC 4 | --- 5 | 6 | # `createAI` 7 | 8 | Creates a client-server context provider that can be used to wrap parts of your application tree to easily manage both UI and AI states of your application. 9 | 10 | ## Import 11 | 12 | 13 | 14 | 15 | 16 | ## Examples 17 | 18 | 19 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/google-vertex.ts: -------------------------------------------------------------------------------- 1 | import { vertex } from '@ai-sdk/google-vertex'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: vertex('gemini-1.0-pro'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | console.log(result.text); 14 | console.log(); 15 | console.log('Token usage:', result.usage); 16 | console.log('Finish reason:', result.finishReason); 17 | } 18 | 19 | main().catch(console.error); 20 | -------------------------------------------------------------------------------- /examples/next-langchain/app/api/completion/route.ts: -------------------------------------------------------------------------------- 1 | import { ChatOpenAI } from '@langchain/openai'; 2 | import { LangChainAdapter, StreamingTextResponse } from 'ai'; 3 | 4 | // Allow streaming responses up to 30 seconds 5 | export const maxDuration = 30; 6 | 7 | export async function POST(req: Request) { 8 | const { prompt } = await req.json(); 9 | 10 | const model = new ChatOpenAI({ 11 | model: 'gpt-3.5-turbo-0125', 12 | temperature: 0, 13 | }); 14 | 15 | const stream = await model.stream(prompt); 16 | 17 | return new StreamingTextResponse(LangChainAdapter.toAIStream(stream)); 18 | } 19 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/anthropic.ts: -------------------------------------------------------------------------------- 1 | import { anthropic } from '@ai-sdk/anthropic'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: anthropic('claude-3-haiku-20240307'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | console.log(result.text); 14 | console.log(); 15 | console.log('Token usage:', result.usage); 16 | console.log('Finish reason:', result.finishReason); 17 | } 18 | 19 | main().catch(console.error); 20 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/groq.ts: -------------------------------------------------------------------------------- 1 | import { createOpenAI } from '@ai-sdk/openai'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | const groq = createOpenAI({ 8 | apiKey: process.env.GROQ_API_KEY ?? '', 9 | baseURL: 'https://api.groq.com/openai/v1', 10 | }); 11 | 12 | async function main() { 13 | const result = await generateText({ 14 | model: groq.chat('llama3-70b-8192'), 15 | prompt: 'Invent a new holiday and describe its traditions.', 16 | }); 17 | 18 | console.log(result.text); 19 | } 20 | 21 | main().catch(console.error); 22 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css'; 2 | import { Inter } from 'next/font/google'; 3 | import Toaster from './toaster'; 4 | 5 | const inter = Inter({ subsets: ['latin'] }); 6 | 7 | export const metadata = { 8 | title: 'Create Next App', 9 | description: 'Generated by create next app', 10 | }; 11 | 12 | export default function RootLayout({ 13 | children, 14 | }: { 15 | children: React.ReactNode; 16 | }) { 17 | return ( 18 | 19 | 20 | {children} 21 | 22 | ); 23 | } 24 | -------------------------------------------------------------------------------- /packages/core/solid/index.ts: -------------------------------------------------------------------------------- 1 | import { 2 | useChat as useChatSolid, 3 | useCompletion as useCompletionSolid, 4 | } from '@ai-sdk/solid'; 5 | 6 | /** 7 | * @deprecated Use `useChat` from `@ai-sdk/solid` instead. 8 | */ 9 | export const useChat = useChatSolid; 10 | 11 | /** 12 | * @deprecated Use `useCompletion` from `@ai-sdk/solid` instead. 13 | */ 14 | export const useCompletion = useCompletionSolid; 15 | 16 | /** 17 | * @deprecated Use `@ai-sdk/solid` instead. 18 | */ 19 | export type { 20 | CreateMessage, 21 | Message, 22 | UseChatOptions, 23 | UseChatHelpers, 24 | } from '@ai-sdk/solid'; 25 | -------------------------------------------------------------------------------- /content/providers/03-community-providers/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Community Providers 3 | description: Learn how to use Language Model Specification. 4 | --- 5 | 6 | # Community Providers 7 | 8 | The Vercel AI SDK provides a [Language Model Specification](https://github.com/vercel/ai/tree/main/packages/provider/src/language-model/v1). 9 | You can [write your own provider](./community-providers/custom-providers) that adheres to the specification and it will be compatible with the AI SDK. 10 | 11 | Here are the community providers that implement the Language Model Specification: 12 | 13 | 14 | -------------------------------------------------------------------------------- /packages/mistral/src/mistral-error.ts: -------------------------------------------------------------------------------- 1 | import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils'; 2 | import { z } from 'zod'; 3 | 4 | const mistralErrorDataSchema = z.object({ 5 | object: z.literal('error'), 6 | message: z.string(), 7 | type: z.string(), 8 | param: z.string().nullable(), 9 | code: z.string().nullable(), 10 | }); 11 | 12 | export type MistralErrorData = z.infer; 13 | 14 | export const mistralFailedResponseHandler = createJsonErrorResponseHandler({ 15 | errorSchema: mistralErrorDataSchema, 16 | errorToMessage: data => data.message, 17 | }); 18 | -------------------------------------------------------------------------------- /examples/solidstart-openai/src/routes/api/chat/index.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { StreamingTextResponse, streamText } from 'ai'; 3 | import { APIEvent } from 'solid-start/api'; 4 | 5 | export const POST = async (event: APIEvent) => { 6 | try { 7 | const { messages } = await event.request.json(); 8 | 9 | const result = await streamText({ 10 | model: openai('gpt-4-turbo-preview'), 11 | messages, 12 | }); 13 | 14 | return new StreamingTextResponse(result.toAIStream()); 15 | } catch (error) { 16 | console.error(error); 17 | throw error; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /examples/ai-core/README.md: -------------------------------------------------------------------------------- 1 | # Basic Examples 2 | 3 | Basic examples for the `ai/core` functions (script usage). 4 | 5 | ## Usage 6 | 7 | 1. Create .env file with the following content (and more settings, depending on the providers you want to use): 8 | 9 | ```sh 10 | OPENAI_API_KEY="YOUR_OPENAI_API_KEY" 11 | ... 12 | ``` 13 | 14 | 2. Run the following commands from the root directory of the AI SDK repo: 15 | 16 | ```sh 17 | pnpm install 18 | pnpm build 19 | ``` 20 | 21 | 3. Run any example (from the `examples/ai-core` directory) with the following command: 22 | 23 | ```sh 24 | pnpm tsx src/path/to/example.ts 25 | ``` 26 | -------------------------------------------------------------------------------- /examples/next-openai-pages/pages/basics/stream-text/index.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useCompletion } from '@ai-sdk/react'; 4 | 5 | export default function Page() { 6 | const { completion, complete } = useCompletion({ 7 | api: '/api/stream-text', 8 | }); 9 | 10 | return ( 11 |
12 |
{ 15 | await complete('Why is the sky blue?'); 16 | }} 17 | > 18 | Generate 19 |
20 | 21 | {completion} 22 |
23 | ); 24 | } 25 | -------------------------------------------------------------------------------- /examples/node-http-server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ai-sdk-express-example", 3 | "version": "0.0.0", 4 | "private": true, 5 | "dependencies": { 6 | "@ai-sdk/anthropic": "latest", 7 | "@ai-sdk/google": "latest", 8 | "@ai-sdk/mistral": "latest", 9 | "@ai-sdk/openai": "latest", 10 | "ai": "latest", 11 | "dotenv": "16.4.5", 12 | "zod": "3.23.8", 13 | "zod-to-json-schema": "3.22.4" 14 | }, 15 | "scripts": { 16 | "type-check": "tsc --noEmit" 17 | }, 18 | "devDependencies": { 19 | "@types/node": "20.11.20", 20 | "tsx": "4.7.1", 21 | "typescript": "5.1.3" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /packages/core/tests/e2e/next-server/app/streamable/client-utils.js: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useEffect, useState } from 'react'; 4 | 5 | export function ClientInfo({ children }) { 6 | const [renders, setRenders] = useState(0); 7 | 8 | // Increment the render count whenever the children change. 9 | // This can be used to verify that the component is re-rendered with the 10 | // state kept. 11 | useEffect(() => { 12 | setRenders(r => r + 1); 13 | }, [children]); 14 | 15 | return ( 16 |
17 |

{renders > 1 ? '(Rerendered) ' : ''}

18 | {children} 19 |
20 | ); 21 | } 22 | -------------------------------------------------------------------------------- /packages/openai/src/openai-error.ts: -------------------------------------------------------------------------------- 1 | import { z } from 'zod'; 2 | import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils'; 3 | 4 | export const openAIErrorDataSchema = z.object({ 5 | error: z.object({ 6 | message: z.string(), 7 | type: z.string(), 8 | param: z.any().nullable(), 9 | code: z.string().nullable(), 10 | }), 11 | }); 12 | 13 | export type OpenAIErrorData = z.infer; 14 | 15 | export const openaiFailedResponseHandler = createJsonErrorResponseHandler({ 16 | errorSchema: openAIErrorDataSchema, 17 | errorToMessage: data => data.error.message, 18 | }); 19 | -------------------------------------------------------------------------------- /packages/ui-utils/src/duplicated/tool-call.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Typed tool call that is returned by generateText and streamText. 3 | It contains the tool call ID, the tool name, and the tool arguments. 4 | */ 5 | export interface ToolCall { 6 | /** 7 | ID of the tool call. This ID is used to match the tool call with the tool result. 8 | */ 9 | toolCallId: string; 10 | 11 | /** 12 | Name of the tool that is being called. 13 | */ 14 | toolName: NAME; 15 | 16 | /** 17 | Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema. 18 | */ 19 | args: ARGS; 20 | } 21 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/openai-custom-headers.ts: -------------------------------------------------------------------------------- 1 | import { createOpenAI } from '@ai-sdk/openai'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | const openai = createOpenAI({ 8 | apiKey: process.env.OPENAI_API_KEY!, 9 | headers: { 10 | 'X-My-Header': 'something', 11 | }, 12 | }); 13 | 14 | async function main() { 15 | const result = await generateText({ 16 | model: openai('gpt-3.5-turbo'), 17 | prompt: 'Invent a new holiday and describe its traditions.', 18 | }); 19 | 20 | console.log(result.text); 21 | } 22 | 23 | main().catch(console.error); 24 | -------------------------------------------------------------------------------- /examples/ai-core/src/registry/setup-registry.ts: -------------------------------------------------------------------------------- 1 | import { anthropic } from '@ai-sdk/anthropic'; 2 | import { mistral } from '@ai-sdk/mistral'; 3 | import { createOpenAI } from '@ai-sdk/openai'; 4 | import { experimental_createProviderRegistry as createProviderRegistry } from 'ai'; 5 | import dotenv from 'dotenv'; 6 | 7 | dotenv.config(); 8 | 9 | export const registry = createProviderRegistry({ 10 | // register provider with prefix and default setup: 11 | anthropic, 12 | mistral, 13 | 14 | // register provider with prefix and custom setup: 15 | openai: createOpenAI({ 16 | apiKey: process.env.OPENAI_API_KEY, 17 | }), 18 | }); 19 | -------------------------------------------------------------------------------- /packages/provider/src/errors/empty-response-body-error.ts: -------------------------------------------------------------------------------- 1 | export class EmptyResponseBodyError extends Error { 2 | constructor({ message = 'Empty response body' }: { message?: string } = {}) { 3 | super(message); 4 | 5 | this.name = 'AI_EmptyResponseBodyError'; 6 | } 7 | 8 | static isEmptyResponseBodyError( 9 | error: unknown, 10 | ): error is EmptyResponseBodyError { 11 | return error instanceof Error && error.name === 'AI_EmptyResponseBodyError'; 12 | } 13 | 14 | toJSON() { 15 | return { 16 | name: this.name, 17 | message: this.message, 18 | stack: this.stack, 19 | }; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /content/docs/07-reference/ai-sdk-rsc/03-create-streamable-ui.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: createStreamableUI 3 | description: Reference for the createStreamableUI function from the AI SDK RSC 4 | --- 5 | 6 | # `createStreamableUI` 7 | 8 | Create a stream that sends UI from the server to the client. On the client side, it can be rendered as a normal React node. 9 | 10 | ## Import 11 | 12 | 13 | 14 | 15 | 16 | ## Examples 17 | 18 | 19 | -------------------------------------------------------------------------------- /content/docs/08-troubleshooting/03-common-issues/01-azure-stream-slow.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Azure OpenAI Slow to Stream 3 | description: Learn to troubleshoot Azure OpenAI slow to stream issues. 4 | --- 5 | 6 | # Azure OpenAI Slow To Stream 7 | 8 | ## Issue 9 | 10 | When using OpenAI hosted on Azure, streaming is slow and in big chunks. 11 | 12 | ## Solution 13 | 14 | This is a Microsoft issue. Some users have reported the following solutions: 15 | 16 | - **Update Content Filtering Settings**: 17 | OpenAI Azure portal, within Content Filtering (Preview), there's an option to set Streaming mode from Default to Asynchronous Modified Filter. 18 | -------------------------------------------------------------------------------- /examples/next-openai-pages/pages/api/chat-api-route.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { streamText } from 'ai'; 3 | import { NextApiRequest, NextApiResponse } from 'next'; 4 | 5 | export default async function handler( 6 | request: NextApiRequest, 7 | response: NextApiResponse, 8 | ) { 9 | const { messages } = await request.body; 10 | 11 | const result = await streamText({ 12 | model: openai('gpt-4-turbo-preview'), 13 | messages, 14 | }); 15 | 16 | // write the AI stream to the response 17 | // Note: this is sent as a single response, not a stream 18 | result.pipeAIStreamToResponse(response); 19 | } 20 | -------------------------------------------------------------------------------- /examples/next-openai/app/stream-object/schema.ts: -------------------------------------------------------------------------------- 1 | import { DeepPartial } from 'ai'; 2 | import { z } from 'zod'; 3 | 4 | // define a schema for the notifications 5 | export const notificationSchema = z.object({ 6 | notifications: z.array( 7 | z.object({ 8 | name: z.string().describe('Name of a fictional person.'), 9 | message: z.string().describe('Message. Do not use emojis or links.'), 10 | minutesAgo: z.number(), 11 | }), 12 | ), 13 | }); 14 | 15 | // define a type for the partial notifications during generation 16 | export type PartialNotification = DeepPartial< 17 | z.infer 18 | >; 19 | -------------------------------------------------------------------------------- /content/examples/01-next-app/04-state-management/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: State Management 3 | description: Learn to manage AI and UI states using the Vercel AI SDK in your Next.js App Router application 4 | --- 5 | 6 | # State Management 7 | 8 | In this section you will learn how to manage states between the language model on the server, and the user interface on the client. You will learn how to use `createAI` to initiate a context provider that can be used to manage these two states. 9 | 10 | You will also learn about the function callbacks that `createAI` supports, which can be used to save and restore the AI and UI state of your application. 11 | -------------------------------------------------------------------------------- /packages/core/core/prompt/prompt.ts: -------------------------------------------------------------------------------- 1 | import { CoreMessage } from './message'; 2 | 3 | /** 4 | Prompt part of the AI function options. It contains a system message, a simple text prompt, or a list of messages. 5 | */ 6 | export type Prompt = { 7 | /** 8 | System message to include in the prompt. Can be used with `prompt` or `messages`. 9 | */ 10 | system?: string; 11 | 12 | /** 13 | A simple text prompt. You can either use `prompt` or `messages` but not both. 14 | */ 15 | prompt?: string; 16 | 17 | /** 18 | A list of messsages. You can either use `prompt` or `messages` but not both. 19 | */ 20 | messages?: Array; 21 | }; 22 | -------------------------------------------------------------------------------- /packages/vue/src/TestCompletionComponent.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 18 | -------------------------------------------------------------------------------- /content/docs/07-reference/ai-sdk-rsc/10-use-ui-state.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: useUIState 3 | description: Reference for the useUIState function from the AI SDK RSC 4 | --- 5 | 6 | # `useUIState` 7 | 8 | It is a hook that enables you to read and update the UI State. The state is client-side and can contain functions, React nodes, and other data. UIState is the visual representation of the AI state. 9 | 10 | ## Import 11 | 12 | 13 | 14 | 15 | 16 | ## Examples 17 | 18 | 19 | -------------------------------------------------------------------------------- /examples/sveltekit-openai/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./.svelte-kit/tsconfig.json", 3 | "compilerOptions": { 4 | "allowJs": true, 5 | "checkJs": true, 6 | "esModuleInterop": true, 7 | "forceConsistentCasingInFileNames": true, 8 | "resolveJsonModule": true, 9 | "skipLibCheck": true, 10 | "sourceMap": true, 11 | "strict": true 12 | } 13 | // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias 14 | // 15 | // If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes 16 | // from the referenced tsconfig.json - TypeScript does not merge them in 17 | } 18 | -------------------------------------------------------------------------------- /packages/solid/vitest.config.js: -------------------------------------------------------------------------------- 1 | import solidPlugin from 'vite-plugin-solid'; 2 | import { defineConfig } from 'vite'; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [solidPlugin()], 7 | server: { port: 3000 }, 8 | build: { target: 'esnext' }, 9 | test: { 10 | environment: 'jsdom', 11 | globals: true, 12 | include: ['src/**/*.ui.test.ts', 'src/**/*.ui.test.tsx'], 13 | deps: { 14 | registerNodeLoader: true, 15 | inline: [/solid-js/], 16 | }, 17 | transformMode: { web: [/\.[jt]sx?$/] }, 18 | }, 19 | resolve: { 20 | conditions: ['development', 'browser'], 21 | }, 22 | }); 23 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/cohere.ts: -------------------------------------------------------------------------------- 1 | import { cohere } from '@ai-sdk/cohere'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: cohere('command-r-plus'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | for await (const textPart of result.textStream) { 14 | process.stdout.write(textPart); 15 | } 16 | 17 | console.log(); 18 | console.log('Token usage:', await result.usage); 19 | console.log('Finish reason:', await result.finishReason); 20 | } 21 | 22 | main().catch(console.error); 23 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/google.ts: -------------------------------------------------------------------------------- 1 | import { google } from '@ai-sdk/google'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: google('models/gemini-pro'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | for await (const textPart of result.textStream) { 14 | process.stdout.write(textPart); 15 | } 16 | 17 | console.log(); 18 | console.log('Token usage:', await result.usage); 19 | console.log('Finish reason:', await result.finishReason); 20 | } 21 | 22 | main().catch(console.error); 23 | -------------------------------------------------------------------------------- /tools/tsconfig/base.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/tsconfig", 3 | "display": "Default", 4 | "compilerOptions": { 5 | "composite": false, 6 | "declaration": true, 7 | "declarationMap": true, 8 | "esModuleInterop": true, 9 | "forceConsistentCasingInFileNames": true, 10 | "inlineSources": false, 11 | "isolatedModules": true, 12 | "moduleResolution": "node", 13 | "noUnusedLocals": false, 14 | "noUnusedParameters": false, 15 | "preserveWatchOutput": true, 16 | "skipLibCheck": true, 17 | "strict": true, 18 | "types": ["@types/node", "vitest/globals"] 19 | }, 20 | "exclude": ["node_modules"] 21 | } 22 | -------------------------------------------------------------------------------- /packages/core/rsc/utils.tsx: -------------------------------------------------------------------------------- 1 | export function createResolvablePromise() { 2 | let resolve: (value: T) => void, reject: (error: unknown) => void; 3 | const promise = new Promise((res, rej) => { 4 | resolve = res; 5 | reject = rej; 6 | }); 7 | return { 8 | promise, 9 | resolve: resolve!, 10 | reject: reject!, 11 | }; 12 | } 13 | 14 | export const isFunction = (x: unknown): x is Function => 15 | typeof x === 'function'; 16 | 17 | export const consumeStream = async (stream: ReadableStream) => { 18 | const reader = stream.getReader(); 19 | while (true) { 20 | const { done } = await reader.read(); 21 | if (done) break; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/google-vertex.ts: -------------------------------------------------------------------------------- 1 | import { vertex } from '@ai-sdk/google-vertex'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: vertex('gemini-1.0-pro'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | for await (const textPart of result.textStream) { 14 | process.stdout.write(textPart); 15 | } 16 | 17 | console.log(); 18 | console.log('Token usage:', await result.usage); 19 | console.log('Finish reason:', await result.finishReason); 20 | } 21 | 22 | main().catch(console.error); 23 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/components/llm-stocks/stocks-skeleton.tsx: -------------------------------------------------------------------------------- 1 | export const StocksSkeleton = () => { 2 | return ( 3 |
4 |
5 |
6 |
7 |
8 | ); 9 | }; 10 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/lib/hooks/use-at-bottom.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | 3 | export function useAtBottom(offset = 0) { 4 | const [isAtBottom, setIsAtBottom] = React.useState(false); 5 | 6 | React.useEffect(() => { 7 | const handleScroll = () => { 8 | setIsAtBottom( 9 | window.innerHeight + window.scrollY >= 10 | document.body.offsetHeight - offset, 11 | ); 12 | }; 13 | 14 | window.addEventListener('scroll', handleScroll, { passive: true }); 15 | handleScroll(); 16 | 17 | return () => { 18 | window.removeEventListener('scroll', handleScroll); 19 | }; 20 | }, [offset]); 21 | 22 | return isAtBottom; 23 | } 24 | -------------------------------------------------------------------------------- /examples/node-http-server/README.md: -------------------------------------------------------------------------------- 1 | # Node.js Http Server + Vercel AI SDK Example 2 | 3 | ## Usage 4 | 5 | 1. Create .env file with the following content (and more settings, depending on the providers you want to use): 6 | 7 | ```sh 8 | OPENAI_API_KEY="YOUR_OPENAI_API_KEY" 9 | ... 10 | ``` 11 | 12 | 2. Run the following commands from the root directory of the AI SDK repo: 13 | 14 | ```sh 15 | pnpm install 16 | pnpm build 17 | ``` 18 | 19 | 3. Run any example (from the `examples/ai-core` directory) with the following command: 20 | 21 | ```sh 22 | pnpm tsx src/server.ts 23 | ``` 24 | 25 | 4. Test the endpoint with Curl: 26 | 27 | ```sh 28 | curl -X POST http://localhost:8080 29 | ``` 30 | -------------------------------------------------------------------------------- /packages/core/core/util/split-array.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Splits an array into chunks of a specified size. 3 | * 4 | * @template T - The type of elements in the array. 5 | * @param {T[]} array - The array to split. 6 | * @param {number} chunkSize - The size of each chunk. 7 | * @returns {T[][]} - A new array containing the chunks. 8 | */ 9 | export function splitArray(array: T[], chunkSize: number): T[][] { 10 | if (chunkSize <= 0) { 11 | throw new Error('chunkSize must be greater than 0'); 12 | } 13 | 14 | const result = []; 15 | for (let i = 0; i < array.length; i += chunkSize) { 16 | result.push(array.slice(i, i + chunkSize)); 17 | } 18 | 19 | return result; 20 | } 21 | -------------------------------------------------------------------------------- /content/docs/07-reference/ai-sdk-rsc/08-use-ai-state.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: useAIState 3 | description: Reference for the useAIState function from the AI SDK RSC 4 | --- 5 | 6 | # `useAIState` 7 | 8 | It is a hook that enables you to read and update the AI state. The AI state is shared globally between all `useAIState` hooks under the same `` provider. 9 | 10 | The AI state is intended to contain context and information shared with the AI model, such as system messages, function responses, and other relevant data. 11 | 12 | ## Import 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/anthropic.ts: -------------------------------------------------------------------------------- 1 | import { anthropic } from '@ai-sdk/anthropic'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: anthropic('claude-3-haiku-20240307'), 10 | prompt: 'Invent a new holiday and describe its traditions.', 11 | }); 12 | 13 | for await (const textPart of result.textStream) { 14 | process.stdout.write(textPart); 15 | } 16 | 17 | console.log(); 18 | console.log('Token usage:', await result.usage); 19 | console.log('Finish reason:', await result.finishReason); 20 | } 21 | 22 | main().catch(console.error); 23 | -------------------------------------------------------------------------------- /packages/cohere/src/map-cohere-finish-reason.ts: -------------------------------------------------------------------------------- 1 | import { LanguageModelV1FinishReason } from '@ai-sdk/provider'; 2 | 3 | export function mapCohereFinishReason( 4 | finishReason: string | null | undefined, 5 | ): LanguageModelV1FinishReason { 6 | switch (finishReason) { 7 | case 'COMPLETE': 8 | case 'STOP_SEQUENCE': 9 | return 'stop'; 10 | 11 | case 'MAX_TOKENS': 12 | return 'length'; 13 | 14 | case 'ERROR': 15 | case 'ERROR_LIMIT': 16 | return 'error'; 17 | 18 | case 'ERROR_TOXIC': 19 | return 'content-filter'; 20 | 21 | case 'USER_CANCEL': 22 | return 'other'; 23 | 24 | default: 25 | return 'unknown'; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /packages/vue/src/TestCompletionTextStreamComponent.vue: -------------------------------------------------------------------------------- 1 | 8 | 9 | 20 | -------------------------------------------------------------------------------- /content/examples/03-node/02-generating-structured-data/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Generating Structured Data 3 | description: Learn how to generate structured data in a Node.js application. 4 | --- 5 | 6 | # Generating Structured Data 7 | 8 | The following sections will guide you through generating structured data with Node.js and the Vercel AI SDK. 9 | 10 | 22 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/components/footer.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | import { cn } from '@/lib/utils'; 4 | import { ExternalLink } from '@/components/external-link'; 5 | 6 | export function FooterText({ className, ...props }: React.ComponentProps<'p'>) { 7 | return ( 8 |

15 | Open source AI chatbot built with{' '} 16 | Next.js and{' '} 17 | Vercel AI SDK. 18 |

19 | ); 20 | } 21 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/components/llm-stocks/events-skeleton.tsx: -------------------------------------------------------------------------------- 1 | export const EventsSkeleton = () => { 2 | return ( 3 |
4 |
5 |
6 | {'xxxxx'} 7 |
8 |
9 | {'xxxxxxxxxxx'} 10 |
11 |
12 |
13 |
14 | ); 15 | }; 16 | -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css'; 2 | import { Inter } from 'next/font/google'; 3 | import Toaster from './toaster'; 4 | import { KasadaClient } from '@/kasada/kasada-client'; 5 | 6 | const inter = Inter({ subsets: ['latin'] }); 7 | 8 | export const metadata = { 9 | title: 'Create Next App', 10 | description: 'Generated by create next app', 11 | }; 12 | 13 | export default function RootLayout({ 14 | children, 15 | }: { 16 | children: React.ReactNode; 17 | }) { 18 | return ( 19 | 20 | 21 | 22 | {children} 23 | 24 | ); 25 | } 26 | -------------------------------------------------------------------------------- /content/docs/07-reference/stream-helpers/16-langchain-stream.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: LangChainStream 3 | description: API Reference for LangChainStream. 4 | --- 5 | 6 | # `LangChainStream` 7 | 8 | 9 | LangChainStream is part of the [legacy LangChain 10 | integration](/providers/legacy-providers/langchain). It is recommended to use 11 | the [LangChain Adapter](/providers/adapters/langchain) instead. 12 | 13 | 14 | Helps with the integration of LangChain. It is compatible with useChat and useCompletion. 15 | 16 | ## Import 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /packages/core/core/util/parse-partial-json.ts: -------------------------------------------------------------------------------- 1 | import SecureJSON from 'secure-json-parse'; 2 | import { fixJson } from './fix-json'; 3 | 4 | export function parsePartialJson( 5 | jsonText: string | undefined, 6 | ): unknown | undefined { 7 | if (jsonText == null) { 8 | return undefined; 9 | } 10 | 11 | try { 12 | // first attempt a regular JSON parse: 13 | return SecureJSON.parse(jsonText); 14 | } catch (ignored) { 15 | try { 16 | // then try to fix the partial JSON and parse it: 17 | const fixedJsonText = fixJson(jsonText); 18 | return SecureJSON.parse(fixedJsonText); 19 | } catch (ignored) { 20 | // ignored 21 | } 22 | } 23 | 24 | return undefined; 25 | } 26 | -------------------------------------------------------------------------------- /packages/google/src/map-google-generative-ai-finish-reason.ts: -------------------------------------------------------------------------------- 1 | import { LanguageModelV1FinishReason } from '@ai-sdk/provider'; 2 | 3 | export function mapGoogleGenerativeAIFinishReason({ 4 | finishReason, 5 | hasToolCalls, 6 | }: { 7 | finishReason: string | null | undefined; 8 | hasToolCalls: boolean; 9 | }): LanguageModelV1FinishReason { 10 | switch (finishReason) { 11 | case 'STOP': 12 | return hasToolCalls ? 'tool-calls' : 'stop'; 13 | case 'MAX_TOKENS': 14 | return 'length'; 15 | case 'RECITATION': 16 | case 'SAFETY': 17 | return 'content-filter'; 18 | case 'FINISH_REASON_UNSPECIFIED': 19 | case 'OTHER': 20 | default: 21 | return 'other'; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /packages/provider-utils/src/uint8-utils.ts: -------------------------------------------------------------------------------- 1 | export function convertBase64ToUint8Array(base64String: string) { 2 | const base64Url = base64String.replace(/-/g, '+').replace(/_/g, '/'); 3 | const latin1string = globalThis.atob(base64Url); 4 | return Uint8Array.from(latin1string, byte => byte.codePointAt(0)!); 5 | } 6 | 7 | export function convertUint8ArrayToBase64(array: Uint8Array): string { 8 | let latin1string = ''; 9 | 10 | // Note: regular for loop to support older JavaScript versions that 11 | // do not support for..of on Uint8Array 12 | for (let i = 0; i < array.length; i++) { 13 | latin1string += String.fromCodePoint(array[i]); 14 | } 15 | 16 | return globalThis.btoa(latin1string); 17 | } 18 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/openai-reader.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: openai('gpt-3.5-turbo'), 10 | maxTokens: 512, 11 | temperature: 0.3, 12 | maxRetries: 5, 13 | prompt: 'Invent a new holiday and describe its traditions.', 14 | }); 15 | 16 | const reader = result.textStream.getReader(); 17 | while (true) { 18 | const { done, value } = await reader.read(); 19 | if (done) { 20 | break; 21 | } 22 | process.stdout.write(value); 23 | } 24 | } 25 | 26 | main().catch(console.error); 27 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/openai-response-headers.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: openai('gpt-3.5-turbo'), 10 | maxTokens: 512, 11 | temperature: 0.3, 12 | maxRetries: 5, 13 | prompt: 'Invent a new holiday and describe its traditions.', 14 | }); 15 | 16 | console.log(`Request ID: ${result.rawResponse?.headers?.['x-request-id']}`); 17 | console.log(); 18 | 19 | for await (const textPart of result.textStream) { 20 | process.stdout.write(textPart); 21 | } 22 | } 23 | 24 | main().catch(console.error); 25 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/lib/hooks/use-enter-submit.tsx: -------------------------------------------------------------------------------- 1 | import { useRef, type RefObject } from 'react'; 2 | 3 | export function useEnterSubmit(): { 4 | formRef: RefObject; 5 | onKeyDown: (event: React.KeyboardEvent) => void; 6 | } { 7 | const formRef = useRef(null); 8 | 9 | const handleKeyDown = ( 10 | event: React.KeyboardEvent, 11 | ): void => { 12 | if ( 13 | event.key === 'Enter' && 14 | !event.shiftKey && 15 | !event.nativeEvent.isComposing 16 | ) { 17 | formRef.current?.requestSubmit(); 18 | event.preventDefault(); 19 | } 20 | }; 21 | 22 | return { formRef, onKeyDown: handleKeyDown }; 23 | } 24 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/openai-multimodal.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | import fs from 'node:fs'; 5 | 6 | dotenv.config(); 7 | 8 | async function main() { 9 | const result = await generateText({ 10 | model: openai('gpt-4-turbo'), 11 | maxTokens: 512, 12 | messages: [ 13 | { 14 | role: 'user', 15 | content: [ 16 | { type: 'text', text: 'Describe the image in detail.' }, 17 | { type: 'image', image: fs.readFileSync('./data/comic-cat.png') }, 18 | ], 19 | }, 20 | ], 21 | }); 22 | 23 | console.log(result.text); 24 | } 25 | 26 | main().catch(console.error); 27 | -------------------------------------------------------------------------------- /content/examples/03-node/01-generating-text/01-generate-text.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Generate Text 3 | description: Learn to generate text in a Node.js application. 4 | --- 5 | 6 | # Generate Text 7 | 8 | The most basic LLM use case is generating text based on a prompt. 9 | For example, you may want to generate a response to a question or summarize a body of text. 10 | The `generateText` function can be used to generate text based on the input prompt. 11 | 12 | ```ts file='index.ts' 13 | import { generateText } from 'ai'; 14 | import { openai } from '@ai-sdk/openai'; 15 | 16 | const result = await generateText({ 17 | model: openai('gpt-3.5-turbo'), 18 | prompt: 'Why is the sky blue?', 19 | }); 20 | 21 | console.log(result); 22 | ``` 23 | -------------------------------------------------------------------------------- /examples/nuxt-openai/pages/index.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 23 | -------------------------------------------------------------------------------- /packages/ui-utils/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './types'; 2 | 3 | export { generateId } from '@ai-sdk/provider-utils'; 4 | 5 | // Export stream data utilities for custom stream implementations, 6 | // both on the client and server side. 7 | export { callChatApi } from './call-chat-api'; 8 | export { callCompletionApi } from './call-completion-api'; 9 | export { createChunkDecoder } from './create-chunk-decoder'; 10 | export { parseComplexResponse } from './parse-complex-response'; 11 | export { processChatStream } from './process-chat-stream'; 12 | export { readDataStream } from './read-data-stream'; 13 | export { formatStreamPart, parseStreamPart } from './stream-parts'; 14 | export type { StreamPart, StreamString } from './stream-parts'; 15 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/google-multimodal.ts: -------------------------------------------------------------------------------- 1 | import { google } from '@ai-sdk/google'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | import fs from 'node:fs'; 5 | 6 | dotenv.config(); 7 | 8 | async function main() { 9 | const result = await generateText({ 10 | model: google('models/gemini-pro-vision'), 11 | maxTokens: 512, 12 | messages: [ 13 | { 14 | role: 'user', 15 | content: [ 16 | { type: 'text', text: 'Describe the image in detail.' }, 17 | { type: 'image', image: fs.readFileSync('./data/comic-cat.png') }, 18 | ], 19 | }, 20 | ], 21 | }); 22 | 23 | console.log(result.text); 24 | } 25 | 26 | main().catch(console.error); 27 | -------------------------------------------------------------------------------- /examples/next-ai-rsc/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2015", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "noEmit": true, 9 | "esModuleInterop": true, 10 | "module": "esnext", 11 | "moduleResolution": "bundler", 12 | "resolveJsonModule": true, 13 | "isolatedModules": true, 14 | "jsx": "preserve", 15 | "incremental": true, 16 | "plugins": [ 17 | { 18 | "name": "next" 19 | } 20 | ], 21 | "paths": { 22 | "@/*": ["./*"] 23 | } 24 | }, 25 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 26 | "exclude": ["node_modules"] 27 | } 28 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/openai-completion.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: openai('gpt-3.5-turbo-instruct'), 10 | maxTokens: 1024, 11 | temperature: 0.3, 12 | prompt: 'Invent a new holiday and describe its traditions.', 13 | }); 14 | 15 | for await (const textPart of result.textStream) { 16 | process.stdout.write(textPart); 17 | } 18 | 19 | console.log(); 20 | console.log('Token usage:', await result.usage); 21 | console.log('Finish reason:', await result.finishReason); 22 | } 23 | 24 | main().catch(console.error); 25 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/openai.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: openai('gpt-3.5-turbo'), 10 | maxTokens: 512, 11 | temperature: 0.3, 12 | maxRetries: 5, 13 | prompt: 'Invent a new holiday and describe its traditions.', 14 | }); 15 | 16 | for await (const textPart of result.textStream) { 17 | process.stdout.write(textPart); 18 | } 19 | 20 | console.log(); 21 | console.log('Token usage:', await result.usage); 22 | console.log('Finish reason:', await result.finishReason); 23 | } 24 | 25 | main().catch(console.error); 26 | -------------------------------------------------------------------------------- /examples/ai-core/src/stream-text/mistral.ts: -------------------------------------------------------------------------------- 1 | import { mistral } from '@ai-sdk/mistral'; 2 | import { streamText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await streamText({ 9 | model: mistral('open-mistral-7b'), 10 | maxTokens: 512, 11 | temperature: 0.3, 12 | maxRetries: 5, 13 | prompt: 'Invent a new holiday and describe its traditions.', 14 | }); 15 | 16 | for await (const textPart of result.textStream) { 17 | process.stdout.write(textPart); 18 | } 19 | 20 | console.log(); 21 | console.log('Token usage:', await result.usage); 22 | console.log('Finish reason:', await result.finishReason); 23 | } 24 | 25 | main().catch(console.error); 26 | -------------------------------------------------------------------------------- /examples/next-openai-rate-limits/kasada/kasada-client.tsx: -------------------------------------------------------------------------------- 1 | import Script from 'next/script'; 2 | 3 | export function KasadaClient() { 4 | return ( 5 | <> 6 | 19 | 23 | 24 | ); 25 | } 26 | -------------------------------------------------------------------------------- /examples/ai-core/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ai-core-examples", 3 | "version": "0.0.0", 4 | "private": true, 5 | "dependencies": { 6 | "@ai-sdk/anthropic": "latest", 7 | "@ai-sdk/azure": "latest", 8 | "@ai-sdk/cohere": "latest", 9 | "@ai-sdk/google": "latest", 10 | "@ai-sdk/google-vertex": "latest", 11 | "@ai-sdk/mistral": "latest", 12 | "@ai-sdk/openai": "latest", 13 | "ai": "latest", 14 | "dotenv": "16.4.5", 15 | "mathjs": "12.4.2", 16 | "zod": "3.23.8", 17 | "zod-to-json-schema": "3.22.4" 18 | }, 19 | "scripts": { 20 | "type-check": "tsc --noEmit" 21 | }, 22 | "devDependencies": { 23 | "@types/node": "20.11.20", 24 | "tsx": "4.7.1", 25 | "typescript": "5.1.3" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/anthropic-multimodal.ts: -------------------------------------------------------------------------------- 1 | import { anthropic } from '@ai-sdk/anthropic'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | import fs from 'node:fs'; 5 | 6 | dotenv.config(); 7 | 8 | async function main() { 9 | const result = await generateText({ 10 | model: anthropic('claude-3-haiku-20240307'), 11 | maxTokens: 512, 12 | messages: [ 13 | { 14 | role: 'user', 15 | content: [ 16 | { type: 'text', text: 'Describe the image in detail.' }, 17 | { type: 'image', image: fs.readFileSync('./data/comic-cat.png') }, 18 | ], 19 | }, 20 | ], 21 | }); 22 | 23 | console.log(result.text); 24 | } 25 | 26 | main().catch(console.error); 27 | -------------------------------------------------------------------------------- /examples/next-openai-kasada-bot-protection/kasada/kasada-client.tsx: -------------------------------------------------------------------------------- 1 | import Script from 'next/script'; 2 | 3 | export function KasadaClient() { 4 | return ( 5 | <> 6 | 19 | 23 | 24 | ); 25 | } 26 | -------------------------------------------------------------------------------- /examples/next-langchain/app/api/completion-string-output-parser/route.ts: -------------------------------------------------------------------------------- 1 | import { ChatOpenAI } from '@langchain/openai'; 2 | import { LangChainAdapter, StreamingTextResponse } from 'ai'; 3 | import { StringOutputParser } from '@langchain/core/output_parsers'; 4 | 5 | // Allow streaming responses up to 30 seconds 6 | export const maxDuration = 30; 7 | 8 | export async function POST(req: Request) { 9 | const { prompt } = await req.json(); 10 | 11 | const model = new ChatOpenAI({ 12 | model: 'gpt-3.5-turbo-0125', 13 | temperature: 0, 14 | }); 15 | 16 | const parser = new StringOutputParser(); 17 | 18 | const stream = await model.pipe(parser).stream(prompt); 19 | 20 | return new StreamingTextResponse(LangChainAdapter.toAIStream(stream)); 21 | } 22 | -------------------------------------------------------------------------------- /examples/ai-core/src/generate-text/google-vertex-safety.ts: -------------------------------------------------------------------------------- 1 | import { vertex } from '@ai-sdk/google-vertex'; 2 | import { generateText } from 'ai'; 3 | import dotenv from 'dotenv'; 4 | 5 | dotenv.config(); 6 | 7 | async function main() { 8 | const result = await generateText({ 9 | model: vertex('gemini-1.5-pro', { 10 | safetySettings: [ 11 | { 12 | category: 'HARM_CATEGORY_UNSPECIFIED', 13 | threshold: 'BLOCK_LOW_AND_ABOVE', 14 | }, 15 | ], 16 | }), 17 | prompt: 'tell me a joke about a clown', 18 | }); 19 | 20 | console.log(result.text); 21 | console.log(); 22 | console.log('Token usage:', result.usage); 23 | console.log('Finish reason:', result.finishReason); 24 | } 25 | 26 | main().catch(console.error); 27 | -------------------------------------------------------------------------------- /packages/ui-utils/src/duplicated/tool-result.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Typed tool result that is returned by generateText and streamText. 3 | It contains the tool call ID, the tool name, the tool arguments, and the tool result. 4 | */ 5 | export interface ToolResult { 6 | /** 7 | ID of the tool call. This ID is used to match the tool call with the tool result. 8 | */ 9 | toolCallId: string; 10 | 11 | /** 12 | Name of the tool that was called. 13 | */ 14 | toolName: NAME; 15 | 16 | /** 17 | Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema. 18 | */ 19 | args: ARGS; 20 | 21 | /** 22 | Result of the tool call. This is the result of the tool's execution. 23 | */ 24 | result: RESULT; 25 | } 26 | -------------------------------------------------------------------------------- /examples/next-cohere/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "plugins": [ 18 | { 19 | "name": "next" 20 | } 21 | ], 22 | "paths": { 23 | "@/*": ["./*"] 24 | } 25 | }, 26 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /examples/next-inkeep/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "plugins": [ 18 | { 19 | "name": "next" 20 | } 21 | ], 22 | "paths": { 23 | "@/*": ["./*"] 24 | } 25 | }, 26 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /examples/next-openai/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "plugins": [ 18 | { 19 | "name": "next" 20 | } 21 | ], 22 | "paths": { 23 | "@/*": ["./*"] 24 | } 25 | }, 26 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /packages/core/core/util/detect-image-mimetype.ts: -------------------------------------------------------------------------------- 1 | const mimeTypeSignatures = [ 2 | { mimeType: 'image/gif' as const, bytes: [0x47, 0x49, 0x46] }, 3 | { mimeType: 'image/png' as const, bytes: [0x89, 0x50, 0x4e, 0x47] }, 4 | { mimeType: 'image/jpeg' as const, bytes: [0xff, 0xd8] }, 5 | { mimeType: 'image/webp' as const, bytes: [0x52, 0x49, 0x46, 0x46] }, 6 | ]; 7 | 8 | export function detectImageMimeType( 9 | image: Uint8Array, 10 | ): 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp' | undefined { 11 | for (const { bytes, mimeType } of mimeTypeSignatures) { 12 | if ( 13 | image.length >= bytes.length && 14 | bytes.every((byte, index) => image[index] === byte) 15 | ) { 16 | return mimeType; 17 | } 18 | } 19 | 20 | return undefined; 21 | } 22 | -------------------------------------------------------------------------------- /packages/provider/src/errors/invalid-prompt-error.ts: -------------------------------------------------------------------------------- 1 | export class InvalidPromptError extends Error { 2 | readonly prompt: unknown; 3 | 4 | constructor({ prompt, message }: { prompt: unknown; message: string }) { 5 | super(`Invalid prompt: ${message}`); 6 | 7 | this.name = 'AI_InvalidPromptError'; 8 | 9 | this.prompt = prompt; 10 | } 11 | 12 | static isInvalidPromptError(error: unknown): error is InvalidPromptError { 13 | return ( 14 | error instanceof Error && 15 | error.name === 'AI_InvalidPromptError' && 16 | prompt != null 17 | ); 18 | } 19 | 20 | toJSON() { 21 | return { 22 | name: this.name, 23 | message: this.message, 24 | stack: this.stack, 25 | 26 | prompt: this.prompt, 27 | }; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /content/examples/03-node/02-streaming-structured-data/index.mdx: -------------------------------------------------------------------------------- 1 | --- 2 | title: Streaming Structured Data 3 | description: Examples of how to stream structured data. 4 | --- 5 | 6 | # Streaming Structured Data 7 | 8 | The following sections will guide you through streaming structured data with Node.js and the Vercel AI SDK. 9 | 10 | 26 | -------------------------------------------------------------------------------- /examples/next-aws-bedrock/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "plugins": [ 18 | { 19 | "name": "next" 20 | } 21 | ], 22 | "paths": { 23 | "@/*": ["./*"] 24 | } 25 | }, 26 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /examples/next-huggingface/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "plugins": [ 18 | { 19 | "name": "next" 20 | } 21 | ], 22 | "paths": { 23 | "@/*": ["./*"] 24 | } 25 | }, 26 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /examples/next-langchain/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "plugins": [ 18 | { 19 | "name": "next" 20 | } 21 | ], 22 | "paths": { 23 | "@/*": ["./*"] 24 | } 25 | }, 26 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /examples/next-replicate/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "plugins": [ 18 | { 19 | "name": "next" 20 | } 21 | ], 22 | "paths": { 23 | "@/*": ["./*"] 24 | } 25 | }, 26 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /packages/core/react/index.ts: -------------------------------------------------------------------------------- 1 | import { 2 | useChat as useChatReact, 3 | useCompletion as useCompletionReact, 4 | useAssistant as useAssistantReact, 5 | } from '@ai-sdk/react'; 6 | 7 | /** 8 | * @deprecated Use `useChat` from `@ai-sdk/react` instead. 9 | */ 10 | export const useChat = useChatReact; 11 | 12 | /** 13 | * @deprecated Use `useCompletion` from `@ai-sdk/react` instead. 14 | */ 15 | export const useCompletion = useCompletionReact; 16 | 17 | /** 18 | * @deprecated Use `useAssistant` from `@ai-sdk/react` instead. 19 | */ 20 | export const useAssistant = useAssistantReact; 21 | 22 | /** 23 | * @deprecated Use `@ai-sdk/react` instead. 24 | */ 25 | export type { 26 | CreateMessage, 27 | Message, 28 | UseChatOptions, 29 | UseChatHelpers, 30 | } from '@ai-sdk/react'; 31 | --------------------------------------------------------------------------------