Skip to content

Commit

Permalink
Merge branch 'vercel:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatsandeepsen authored Dec 23, 2024
2 parents 1391eb6 + 930342e commit bd00852
Show file tree
Hide file tree
Showing 136 changed files with 2,527 additions and 515 deletions.
5 changes: 0 additions & 5 deletions .changeset/silver-items-double.md

This file was deleted.

6 changes: 4 additions & 2 deletions content/docs/02-foundations/02-providers-and-models.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ The AI SDK comes with several providers that you can use to interact with differ
- [xAI Grok Provider](/providers/ai-sdk-providers/xai) (`@ai-sdk/xai`)
- [Together.ai Provider](/providers/ai-sdk-providers/togetherai) (`@ai-sdk/togetherai`)
- [Cohere Provider](/providers/ai-sdk-providers/cohere) (`@ai-sdk/cohere`)
- [Groq](/providers/ai-sdk-providers/groq) (`@ai-sdk/groq`)
- [Fireworks Provider](/providers/ai-sdk-providers/fireworks) (`@ai-sdk/fireworks`)
- [DeepInfra Provider](/providers/ai-sdk-providers/deepinfra) (`@ai-sdk/deepinfra`)
- [Groq Provider](/providers/ai-sdk-providers/groq) (`@ai-sdk/groq`)

You can also use the OpenAI provider with OpenAI-compatible APIs:

- [Perplexity](/providers/ai-sdk-providers/perplexity)
- [Fireworks](/providers/ai-sdk-providers/fireworks)
- [LM Studio](/providers/openai-compatible-providers/lmstudio)
- [Baseten](/providers/openai-compatible-providers/baseten)

Expand All @@ -56,6 +57,7 @@ The open-source community has created the following providers:
- [Crosshatch Provider](/providers/community-providers/crosshatch) (`@crosshatch/ai-provider`)
- [Mixedbread Provider](/providers/community-providers/mixedbread) (`mixedbread-ai-provider`)
- [Voyage AI Provider](/providers/community-providers/voyage-ai) (`voyage-ai-provider`)
- [Mem0 Provider](/providers/community-providers/mem0)(`@mem0/vercel-ai-provider`)
- [LLamaCpp Provider](/providers/community-providers/llama-cpp) (`llamacpp-ai-provider`)

## Model Capabilities
Expand Down
136 changes: 109 additions & 27 deletions content/docs/02-guides/04-o1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ With the [release of OpenAI's o1 series models](https://openai.com/index/introdu

The [AI SDK](/) is a powerful TypeScript toolkit for building AI applications with large language models (LLMs) like OpenAI o1 alongside popular frameworks like React, Next.js, Vue, Svelte, Node.js, and more.

<Note>
OpenAI o1 models are currently [in beta with limited
features](https://platform.openai.com/docs/guides/reasoning/beta-limitations).
Access is restricted to developers in tier 4 and tier 5, with low rate limits
(20 RPM). OpenAI is working on adding more features, increasing rate limits,
and expanding access to more developers in the coming weeks.
</Note>

## OpenAI o1

OpenAI released a series of AI models designed to spend more time thinking before responding. They can reason through complex tasks and solve harder problems than previous models in science, coding, and math. These models, named the o1 series, are trained with reinforcement learning and can "think before they answer". As a result, they are able to produce a long internal chain of thought before responding to a prompt.

There are two reasoning models available in the API:
There are three reasoning models available in the API:

1. [**o1-preview**](https://platform.openai.com/docs/models/o1): An early preview of the o1 model, designed to reason about hard problems using broad general knowledge about the world.
2. [**o1-mini**](https://platform.openai.com/docs/models/ohttps://platform.openai.com/docs/models/o1): A faster and cheaper version of o1, particularly adept at coding, math, and science tasks where extensive general knowledge isn't required.
1. [**o1**](https://platform.openai.com/docs/models#o1): Designed to reason about hard problems using broad general knowledge about the world.
1. [**o1-preview**](https://platform.openai.com/docs/models#o1): The original preview version of o1 - slower than o1 but supports streaming.
1. [**o1-mini**](https://platform.openai.com/docs/models#o1): A faster and cheaper version of o1, particularly adept at coding, math, and science tasks where extensive general knowledge isn't required. o1-mini supports streaming.

| Model | Streaming | Tools | Object Generation | Reasoning Effort |
| ---------- | ------------------- | ------------------- | ------------------- | ------------------- |
| o1 | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
| o1-preview | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
| o1-mini | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |

### Benchmarks

Expand Down Expand Up @@ -75,17 +74,103 @@ import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

const { text } = await generateText({
model: openai('o1-preview'),
model: openai('o1'),
prompt: 'Explain the concept of quantum entanglement.',
});
```

<Note>o1 only supports streaming in a simulated capacity.</Note>

### Refining Reasoning Effort

You can control the amount of reasoning effort expended by o1 through the `reasoningEffort` parameter. This parameter can be set to `'low'`, `'medium'`, or `'high'` to adjust how much time and computation the model spends on internal reasoning before producing a response.

```tsx
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

// Reduce reasoning effort for faster responses
const { text } = await generateText({
model: openai('o1'),
prompt: 'Explain quantum entanglement briefly.',
experimental_providerMetadata: {
openai: {
reasoningEffort: 'low',
},
},
});
```

<Note>
During the beta phase, access to most chat completions parameters is not
supported for o1 models. Features like function calling and image inputs are
currently unavailable, and streaming is simulated.
The `reasoningEffort` parameter is only supported by o1 and has no effect on
other models.
</Note>

### Generating Structured Data

While text generation can be useful, you might want to generate structured JSON data. For example, you might want to extract information from text, classify data, or generate synthetic data. AI SDK Core provides two functions ([`generateObject`](/docs/reference/ai-sdk-core/generate-object) and [`streamObject`](/docs/reference/ai-sdk-core/stream-object)) to generate structured data, allowing you to constrain model outputs to a specific schema.

```tsx
import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

const { object } = await generateObject({
model: openai('o1'),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
steps: z.array(z.string()),
}),
}),
prompt: 'Generate a lasagna recipe.',
});
```

This code snippet will generate a type-safe recipe that conforms to the specified zod schema.

<Note>
Structured object generation is only supported with o1, not o1-preview or
o1-mini.
</Note>

### Tools

While LLMs have incredible generation capabilities, they struggle with discrete tasks (e.g. mathematics) and interacting with the outside world (e.g. getting the weather). The solution: [tools](/docs/foundations/tools), which are like programs that you provide to the model, which it can choose to call as necessary.

### Using Tools with the AI SDK

The AI SDK supports tool usage across several of its functions, like [`generateText`](/docs/reference/ai-sdk-core/generate-text) and [`streamText`](/docs/reference/ai-sdk-core/stream-text). By passing one or more tools to the `tools` parameter, you can extend the capabilities of LLMs, allowing them to perform discrete tasks and interact with external systems.

Here's an example of how you can use a tool with the AI SDK and o1:

```tsx
import { generateText, tool } from 'ai';
import { openai } from '@ai-sdk/openai';

const { text } = await generateText({
model: openai('o1'),
prompt: 'What is the weather like today?',
tools: {
getWeather: tool({
description: 'Get the weather in a location',
parameters: z.object({
location: z.string().describe('The location to get the weather for'),
}),
execute: async ({ location }) => ({
location,
temperature: 72 + Math.floor(Math.random() * 21) - 10,
}),
}),
},
});
```

In this example, the `getWeather` tool allows the model to fetch real-time weather data (simulated for simplicity), enhancing its ability to provide accurate and up-to-date information.

<Note>Tools are only compatible with o1, not o1-preview or o1-mini.</Note>

### Building Interactive Interfaces

AI SDK Core can be paired with [AI SDK UI](/docs/ai-sdk-ui/overview), another powerful component of the AI SDK, to streamline the process of building chat, completion, and assistant interfaces with popular frameworks like Next.js, Nuxt, SvelteKit, and SolidStart.
Expand All @@ -97,21 +182,21 @@ With four main hooks — [`useChat`](/docs/reference/ai-sdk-ui/use-chat), [`useC
Let's explore building a chatbot with [Next.js](https://nextjs.org), the AI SDK, and OpenAI o1:

```tsx filename="app/api/chat/route.ts"
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';

// Allow responses up to 5 minutes
export const maxDuration = 300;

export async function POST(req: Request) {
const { messages } = await req.json();

const { text } = await generateText({
model: openai('o1-preview'),
const result = streamText({
model: openai('o1-mini'),
messages,
});

return new Response(text);
return result.toDataStreamResponse();
}
```

Expand All @@ -121,9 +206,7 @@ export async function POST(req: Request) {
import { useChat } from 'ai/react';

export default function Page() {
const { messages, input, handleInputChange, handleSubmit, error } = useChat({
streamProtocol: 'text',
});
const { messages, input, handleInputChange, handleSubmit, error } = useChat();

return (
<>
Expand All @@ -145,9 +228,10 @@ export default function Page() {
The useChat hook on your root page (`app/page.tsx`) will make a request to your AI provider endpoint (`app/api/chat/route.ts`) whenever the user submits a message. The messages are then displayed in the chat UI.

<Note>
Due to the current limitations of o1 models during the beta phase, real-time
streaming is not supported. The response will be sent once the model completes
its reasoning and generates the full output.
o1 does not support streaming. To use o1 with `streamText`, enable the
[`simulateStreaming`](/providers/ai-sdk-providers/openai#chat-models) option,
which will 'stream' the response once the model completes its reasoning and
generates the full output. The latest version of o1-mini supports streaming.
</Note>

## Get Started
Expand All @@ -159,5 +243,3 @@ Ready to get started? Here's how you can dive in:
1. Check out practical examples at [sdk.vercel.ai/examples](/examples) to see the SDK in action and get inspired for your own projects.
1. Dive deeper with advanced guides on topics like Retrieval-Augmented Generation (RAG) and multi-modal chat at [sdk.vercel.ai/docs/guides](/docs/guides).
1. Check out ready-to-deploy AI templates at [vercel.com/templates?type=ai](https://vercel.com/templates?type=ai).

Remember that OpenAI o1 models are currently in beta with limited features and access. Stay tuned for updates as OpenAI expands access and adds more features to these powerful reasoning models.
10 changes: 6 additions & 4 deletions content/docs/03-ai-sdk-core/35-image-generation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ const { image } = await generateImage({

## Image Models

| Provider | Model | Supported Sizes |
| --------------------------------------------------------- | ---------- | ------------------------------- |
| [OpenAI](/providers/ai-sdk-providers/openai#image-models) | `dall-e-3` | 1024x1024, 1792x1024, 1024x1792 |
| [OpenAI](/providers/ai-sdk-providers/openai#image-models) | `dall-e-2` | 256x256, 512x512, 1024x1024 |
| Provider | Model | Supported Sizes |
| ----------------------------------------------------------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------- |
| [Google Vertex](/providers/ai-sdk-providers/google-vertex#image-models) | `imagen-3.0-generate-001` | See [aspect ratios](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images#aspect-ratio) |
| [Google Vertex](/providers/ai-sdk-providers/google-vertex#image-models) | `imagen-3.0-fast-generate-001` | See [aspect ratios](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images#aspect-ratio) |
| [OpenAI](/providers/ai-sdk-providers/openai#image-models) | `dall-e-3` | 1024x1024, 1792x1024, 1024x1792 |
| [OpenAI](/providers/ai-sdk-providers/openai#image-models) | `dall-e-2` | 256x256, 512x512, 1024x1024 |
10 changes: 5 additions & 5 deletions content/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export default function Page() {
<div>
{messages.map(message => (
<div key={message.id}>
{message.role === 'user' ? 'User: ' : 'AI: '}
{message.content}
<div>{message.role === 'user' ? 'User: ' : 'AI: '}</div>
<div>{message.content}</div>
</div>
))}

Expand Down Expand Up @@ -93,7 +93,7 @@ import { z } from 'zod';
export const weatherTool = createTool({
description: 'Display the weather for a location',
parameters: z.object({
location: z.string(),
location: z.string().describe('The location to get the weather for'),
}),
execute: async function ({ location }) {
await new Promise(resolve => setTimeout(resolve, 2000));
Expand Down Expand Up @@ -244,7 +244,7 @@ To add more tools, simply define them in your `ai/tools.ts` file:
export const stockTool = createTool({
description: 'Get price for a stock',
parameters: z.object({
symbol: z.string(),
symbol: z.string().describe('The stock symbol to get the price for'),
}),
execute: async function ({ symbol }) {
// Simulated API call
Expand Down Expand Up @@ -312,7 +312,7 @@ export default function Page() {
);
} else if (toolName === 'getStockPrice') {
const { result } = toolInvocation;
return <Stock {...result} />;
return <Stock key={toolCallId} {...result} />;
}
} else {
return (
Expand Down
17 changes: 12 additions & 5 deletions content/providers/01-ai-sdk-providers/01-openai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,17 @@ console.log('Usage:', {
```

<Note type="warning">
Reasoning models like `o1`, `o1-preview`, and `o1-mini` require additional
runtime inference to complete their reasoning phase before generating a
response. You can use the `reasoningEffort` model setting to influence the
inference time.
Reasoning models like `o1-mini` and `o1-preview` require additional runtime
inference to complete their reasoning phase before generating a response. This
introduces longer latency compared to other models, with `o1-preview`
exhibiting significantly more inference time than `o1-mini`.
</Note>

<Note>
OpenAI has introduced a new `developer` message type for reasoning models.
However, `system` messages are automatically converted to `developer` messages
by OpenAI. You can pass `system` messages to reasoning models and set the
`system` instruction as usual.
</Note>

#### Prompt Caching
Expand Down Expand Up @@ -551,7 +558,7 @@ using the `.embedding()` factory method.
const model = openai.embedding('text-embedding-3-large');
```

OpenAI embedding models support several aditional settings.
OpenAI embedding models support several additional settings.
You can pass them as an options argument:

```ts
Expand Down
27 changes: 27 additions & 0 deletions content/providers/01-ai-sdk-providers/11-google-vertex.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,33 @@ The following optional settings are available for Google Vertex AI embedding mod
model ID as a string if needed.
</Note>

### Image Models

You can create [Imagen](https://cloud.google.com/vertex-ai/generative-ai/docs/image/overview) models that call the [Imagen on Vertex AI API](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images)
using the `.image()` factory method. For more on image generation with the AI SDK see [generateImage()](/docs/reference/ai-sdk-core/generate-image).

Note that Imagen does not support an explicit size parameter. Instead, size is driven by the [aspect ratio](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images#aspect-ratio) of the input image.

```ts
import { vertex } from '@ai-sdk/google-vertex';
import { experimental_generateImage as generateImage } from 'ai';

const { image } = await generateImage({
model: vertex.image('imagen-3.0-generate-001'),
prompt: 'A futuristic cityscape at sunset',
providerOptions: {
vertex: { aspectRatio: '16:9' },
},
});
```

#### Model Capabilities

| Model | Supported Sizes |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------- |
| `imagen-3.0-generate-001` | See [aspect ratios](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images#aspect-ratio) |
| `imagen-3.0-fast-generate-001` | See [aspect ratios](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images#aspect-ratio) |

## Google Vertex Anthropic Provider Usage

The Google Vertex Anthropic provider for the [AI SDK](https://sdk.vercel.ai/docs) offers support for Anthropic's Claude models through the Google Vertex AI APIs. This section provides details on how to set up and use the Google Vertex Anthropic provider.
Expand Down
4 changes: 2 additions & 2 deletions content/providers/01-ai-sdk-providers/24-togetherai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ The Together.ai provider also supports [completion models](https://docs.together
<Note>
The table above lists popular models. Please see the [Together.ai
docs](https://docs.together.ai/docs/serverless-models) for a full list of
available models. The table above lists popular models. You can also pass any
available provider model ID as a string if needed.
available models. You can also pass any available provider model ID as a
string if needed.
</Note>
Loading

0 comments on commit bd00852

Please sign in to comment.