Skip to content

AI Core (experimental)

💡

AI Core is an experimental API. The API is not yet stable and may change in the future without a major version bump.

The Vercel AI SDK offers a unified way of calling large language models (LLMs) that can be used with any AI Core-compatible provider. It provides the following AI functions:

  • generateText [ API ] - Generate text and call tools. This function is ideal for non-interactive use cases such as automation tasks where you need to write text (e.g. drafting email or summarizing web pages) and for agents that use tools.
  • streamText [ API ] - Stream text and call tools. You can use the streamText function for interactive use cases such as chat bots (with and without tool usage), and text and code diff streaming in UIs. You can also generate UI components with tools (see Generative UI).
  • generateObject [ API ] - Generate a typed, structured object that matches a Zod (opens in a new tab) schema. You can use this function to force the language model to return structured data, e.g. for information extraction, synthetic data generation, or classification tasks.
  • streamObject [ API ] - Stream a structured object that matches a Zod schema. You can use this function to stream generated UIs in combination with React Server Components (see Generative UI).

The AI functions share the same prompt structure and the same common settings. The model is created using a language model provider, e.g. the OpenAI provider . Here is a simple example for generateText:

import { experimental_generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
 
const { text } = await experimental_generateText({
  model: openai.chat('gpt-3.5-turbo'),
  prompt: 'Invent a new holiday and describe its traditions.',
});

Schema Specification and Validation with Zod

Tool usage and structured object generation require the specification of schemas. The AI SDK uses Zod (opens in a new tab), the most popular JavaScript schema validation library, for schema specification and validation.

You can install Zod with

npm install zod

You can then easily specify schemas, for example:

const recipeSchema = z.object({
  recipe: z.object({
    name: z.string(),
    ingredients: z.array(
      z.object({
        name: z.string(),
        amount: z.string(),
      }),
    ),
    steps: z.array(z.string()),
  }),
});

Such schemas can be used to define parameters for tool calls and to generated structured objects with generateObject and streamObject.

Language Model Interface

Providers need to provide an implementation of the language model interface to be compatible with the AI SDK. The AI SDK contains the following providers:

The AI SDK also provides a language model specification (opens in a new tab) that you can use to implement custom providers.

AI SDK Diagram


© 2023 Vercel Inc.