Skip to content

Commit 6a50ac4

Browse files
authored
feat (ai/provider): add Google Vertex provider (#1592)
1 parent c82da5f commit 6a50ac4

38 files changed

+1432
-32
lines changed

‎.changeset/plenty-ears-behave.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/provider': patch
3+
---
4+
5+
feat (provider): add additional error types

‎.changeset/smooth-peaches-add.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/google-vertex': patch
3+
---
4+
5+
feat (provider/google-vertex): add Google Vertex provider (text generation and streaming only)

‎.changeset/wild-ears-deny.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/provider-utils': patch
3+
---
4+
5+
feat (provider-utils): add loadSetting and convertAsyncGeneratorToReadableStream helpers

‎content/docs/03-ai-sdk-core/02-providers-and-models.mdx‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ Here is an overview of the AI SDK Provider Architecture:
2020
Vercel AI SDK comes with several providers that you can use to interact with different language models:
2121

2222
- [OpenAI Provider](/providers/ai-sdk-providers/openai) (`@ai-sdk/openai`)
23-
- [Mistral Provider](/providers/ai-sdk-providers/mistral) (`@ai-sdk/mistral`)
24-
- [Google Provider](/providers/ai-sdk-providers/google-generative-ai) (`@ai-sdk/google`)
2523
- [Anthropic Provider](/providers/ai-sdk-providers/anthropic) (`@ai-sdk/anthropic`)
24+
- [Google Generative AI Provider](/providers/ai-sdk-providers/google-generative-ai) (`@ai-sdk/google`)
25+
- [Google Vertex Provider](/providers/ai-sdk-providers/google-vertex) (`@ai-sdk/google-vertex`)
26+
- [Mistral Provider](/providers/ai-sdk-providers/mistral) (`@ai-sdk/mistral`)
2627

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

@@ -42,15 +43,16 @@ The open-source community has created the following providers:
4243
The AI providers support different language models with various capabilities.
4344
Here are the capabilities of popular models:
4445

45-
| Provider | Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
46-
| ---------------------------------------------------------- | ------------------------------ | ------------------- | ------------------- | ------------------- | ------------------- |
47-
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4o` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
48-
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4-turbo` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
49-
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
50-
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-3.5-turbo` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
51-
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-opus-20240229` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
52-
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-sonnet-20240229` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
53-
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-haiku-20240307` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
54-
| [Mistral](/providers/ai-sdk-providers/mistral) | `mistral-large-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
55-
| [Mistral](/providers/ai-sdk-providers/mistral) | `mistral-small-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
56-
| [Google](/providers/ai-sdk-providers/google-generative-ai) | `models/gemini-1.5-pro-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
46+
| Provider | Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
47+
| ------------------------------------------------------------------------ | ------------------------------ | ------------------- | ------------------- | ------------------- | ------------------- |
48+
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4o` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
49+
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4-turbo` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
50+
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
51+
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-3.5-turbo` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
52+
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-opus-20240229` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
53+
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-sonnet-20240229` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
54+
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-haiku-20240307` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
55+
| [Mistral](/providers/ai-sdk-providers/mistral) | `mistral-large-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
56+
| [Mistral](/providers/ai-sdk-providers/mistral) | `mistral-small-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
57+
| [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai) | `models/gemini-1.5-pro-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
58+
| [Google Vertex](/providers/ai-sdk-providers/google-vertex) | `gemini-1.0-pro-vision` | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |

‎content/providers/01-ai-sdk-providers/03-google-generative-ai.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Google Generative AI
3-
description: Learn how to use Google Generative AI.
3+
description: Learn how to use Google Generative AI Provider.
44
---
55

66
# Google Generative AI Provider
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: Google Vertex AI
3+
description: Learn how to use the Google Vertex AI provider.
4+
---
5+
6+
# Google Vertex Provider
7+
8+
The Google provider for the [Vercel AI SDK](https://sdk.vercel.ai/docs) contains language model support for the [Google Vertex AI](https://cloud.google.com/vertex-ai) APIs.
9+
It creates language model objects that can be used with the `generateText` and `streamText` AI functions.
10+
Tool calls are currently not supported.
11+
12+
## Setup
13+
14+
The Google provider is available in the `@ai-sdk/google-vertex` module. You can install it with
15+
16+
<Tabs items={['pnpm', 'npm', 'yarn']}>
17+
<Tab>
18+
<Snippet text="pnpm install @ai-sdk/google-vertex" dark />
19+
</Tab>
20+
<Tab>
21+
<Snippet text="npm install @ai-sdk/google-vertex" dark />
22+
</Tab>
23+
<Tab>
24+
<Snippet text="yarn add @ai-sdk/google-vertex" dark />
25+
</Tab>
26+
</Tabs>
27+
28+
## Provider Instance
29+
30+
You can import the default provider instance `vertex` from `@ai-sdk/google-vertex`:
31+
32+
```ts
33+
import { vertex } from '@ai-sdk/google-vertex';
34+
```
35+
36+
If you need a customized setup, you can import `createVertex` from `@ai-sdk/google-vertex` and create a provider instance with your settings:
37+
38+
```ts
39+
import { createVertex } from '@ai-sdk/google-vertex';
40+
41+
const vertex = createVertex({
42+
// custom settings
43+
});
44+
```
45+
46+
## Language Models
47+
48+
You can create models that call the Vertex API using the provider instance.
49+
The first argument is the model id, e.g. `gemini-1.0-pro`.
50+
51+
```ts
52+
const model = vertex('gemini-1.0-pro');
53+
```
54+
55+
Google Vertex models support also some model specific settings that are not part of the [standard call settings](/docs/ai-sdk-core/settings).
56+
You can pass them as an options argument:
57+
58+
```ts
59+
const model = vertex('gemini-1.0-pro', {
60+
topK: 0.2,
61+
});
62+
```
63+
64+
The following optional settings are available for Google Vertex models:
65+
66+
- **topK** _number_
67+
68+
Optional. The maximum number of tokens to consider when sampling.
69+
70+
Models use nucleus sampling or combined Top-k and nucleus sampling.
71+
Top-k sampling considers the set of topK most probable tokens.
72+
Models running with nucleus sampling don't allow topK setting.
73+
74+
### Model Capabilities
75+
76+
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
77+
| ----------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
78+
| `gemini-1.0-pro` | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
79+
| `gemini-1.0-pro-vision` | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |

‎content/providers/01-ai-sdk-providers/index.mdx‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ There are also [community providers](./community-providers) that have been creat
1717

1818
Not all providers support all AI SDK features. Here's a quick comparison of the capabilities of officially supported providers.
1919

20-
| Provider | Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
21-
| ---------------------------------------------------------- | ------------------------------ | ------------------- | ------------------- | ------------------- | ------------------- |
22-
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4o` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
23-
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4-turbo` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
24-
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
25-
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-3.5-turbo` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
26-
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-opus-20240229` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
27-
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-sonnet-20240229` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
28-
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-haiku-20240307` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
29-
| [Mistral](/providers/ai-sdk-providers/mistral) | `mistral-large-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
30-
| [Mistral](/providers/ai-sdk-providers/mistral) | `mistral-small-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
31-
| [Google](/providers/ai-sdk-providers/google-generative-ai) | `models/gemini-1.5-pro-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
20+
| Provider | Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
21+
| ------------------------------------------------------------------------ | ------------------------------ | ------------------- | ------------------- | ------------------- | ------------------- |
22+
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4o` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
23+
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4-turbo` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
24+
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-4` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
25+
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-3.5-turbo` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
26+
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-opus-20240229` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
27+
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-sonnet-20240229` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
28+
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-3-haiku-20240307` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
29+
| [Mistral](/providers/ai-sdk-providers/mistral) | `mistral-large-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
30+
| [Mistral](/providers/ai-sdk-providers/mistral) | `mistral-small-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
31+
| [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai) | `models/gemini-1.5-pro-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
32+
| [Google Vertex](/providers/ai-sdk-providers/google-vertex) | `gemini-1.0-pro-vision` | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |

‎examples/ai-core/.env.example‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ ANTHROPIC_API_KEY=""
22
OPENAI_API_KEY=""
33
MISTRAL_API_KEY=""
44
GOOGLE_GENERATIVE_AI_API_KEY=""
5+
GOOGLE_VERTEX_PROJECT=""
6+
GOOGLE_VERTEX_LOCATION=""
57
FIREWORKS_API_KEY=""
68
GROQ_API_KEY=""
79
PERPLEXITY_API_KEY=""

‎examples/ai-core/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"@ai-sdk/anthropic": "latest",
77
"@ai-sdk/google": "latest",
8+
"@ai-sdk/google-vertex": "latest",
89
"@ai-sdk/mistral": "latest",
910
"@ai-sdk/openai": "latest",
1011
"ai": "latest",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { vertex } from '@ai-sdk/google-vertex';
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: vertex('gemini-1.0-pro-vision'),
11+
maxTokens: 512,
12+
messages: [
13+
{
14+
role: 'user',
15+
content: [
16+
{ type: 'text', text: 'Describe the image in detail.' },
17+
{
18+
type: 'image',
19+
image: fs.readFileSync('./data/comic-cat.png').toString('base64'),
20+
},
21+
],
22+
},
23+
],
24+
});
25+
26+
console.log(result.text);
27+
}
28+
29+
main().catch(console.error);

0 commit comments

Comments
 (0)