@ai-sdk/google-vertex 5.0.0-beta.24 → 5.0.0-beta.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +63 -1
- package/dist/anthropic/edge/index.js +1 -1
- package/dist/anthropic/edge/index.mjs +1 -1
- package/dist/edge/index.js +1 -1
- package/dist/edge/index.mjs +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/maas/edge/index.d.mts +76 -0
- package/dist/maas/edge/index.d.ts +76 -0
- package/dist/maas/edge/index.js +215 -0
- package/dist/maas/edge/index.js.map +1 -0
- package/dist/maas/edge/index.mjs +196 -0
- package/dist/maas/edge/index.mjs.map +1 -0
- package/dist/maas/index.d.mts +60 -0
- package/dist/maas/index.d.ts +60 -0
- package/dist/maas/index.js +125 -0
- package/dist/maas/index.js.map +1 -0
- package/dist/maas/index.mjs +101 -0
- package/dist/maas/index.mjs.map +1 -0
- package/docs/16-google-vertex.mdx +148 -3
- package/maas/edge.d.ts +1 -0
- package/maas/index.d.ts +1 -0
- package/package.json +19 -6
- package/src/maas/edge/google-vertex-maas-provider-edge.ts +65 -0
- package/src/maas/edge/index.ts +9 -0
- package/src/maas/google-vertex-maas-options.ts +15 -0
- package/src/maas/google-vertex-maas-provider-node.ts +64 -0
- package/src/maas/google-vertex-maas-provider.ts +111 -0
- package/src/maas/index.ts +9 -0
|
@@ -5,14 +5,15 @@ description: Learn how to use the Google Vertex AI provider.
|
|
|
5
5
|
|
|
6
6
|
# Google Vertex Provider
|
|
7
7
|
|
|
8
|
-
The Google Vertex provider for the [AI SDK](/docs) contains language model support for the [Google Vertex AI](https://cloud.google.com/vertex-ai) APIs. This includes support for [Google's Gemini models](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models)
|
|
8
|
+
The Google Vertex provider for the [AI SDK](/docs) contains language model support for the [Google Vertex AI](https://cloud.google.com/vertex-ai) APIs. This includes support for [Google's Gemini models](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models), [Anthropic's Claude partner models](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude), and [MaaS (Model as a Service) open models](https://cloud.google.com/vertex-ai/generative-ai/docs/maas/use-open-models).
|
|
9
9
|
|
|
10
10
|
<Note>
|
|
11
11
|
The Google Vertex provider is compatible with both Node.js and Edge runtimes.
|
|
12
12
|
The Edge runtime is supported through the `@ai-sdk/google-vertex/edge`
|
|
13
13
|
sub-module. More details can be found in the [Google Vertex Edge
|
|
14
|
-
Runtime](#google-vertex-edge-runtime)
|
|
15
|
-
Runtime](#google-vertex-anthropic-edge-runtime)
|
|
14
|
+
Runtime](#google-vertex-edge-runtime), [Google Vertex Anthropic Edge
|
|
15
|
+
Runtime](#google-vertex-anthropic-edge-runtime), and [Google Vertex MaaS Edge
|
|
16
|
+
Runtime](#google-vertex-maas-edge-runtime) sections below.
|
|
16
17
|
</Note>
|
|
17
18
|
|
|
18
19
|
## Setup
|
|
@@ -1716,3 +1717,147 @@ See also [Anthropic Model Comparison](https://docs.anthropic.com/en/docs/about-c
|
|
|
1716
1717
|
The table above lists popular models. You can also pass any available provider
|
|
1717
1718
|
model ID as a string if needed.
|
|
1718
1719
|
</Note>
|
|
1720
|
+
|
|
1721
|
+
## Google Vertex MaaS Provider Usage
|
|
1722
|
+
|
|
1723
|
+
The Google Vertex MaaS (Model as a Service) provider offers access to partner and open models hosted on Vertex AI through an OpenAI-compatible Chat Completions API. This includes models from DeepSeek, Qwen, Meta, MiniMax, Moonshot, and OpenAI.
|
|
1724
|
+
|
|
1725
|
+
For more information, see the [Vertex AI MaaS documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/maas/use-open-models).
|
|
1726
|
+
|
|
1727
|
+
### Provider Instance
|
|
1728
|
+
|
|
1729
|
+
You can import the default provider instance `vertexMaas` from `@ai-sdk/google-vertex/maas`:
|
|
1730
|
+
|
|
1731
|
+
```typescript
|
|
1732
|
+
import { vertexMaas } from '@ai-sdk/google-vertex/maas';
|
|
1733
|
+
```
|
|
1734
|
+
|
|
1735
|
+
If you need a customized setup, you can import `createVertexMaas` from `@ai-sdk/google-vertex/maas` and create a provider instance with your settings:
|
|
1736
|
+
|
|
1737
|
+
```typescript
|
|
1738
|
+
import { createVertexMaas } from '@ai-sdk/google-vertex/maas';
|
|
1739
|
+
|
|
1740
|
+
const vertexMaas = createVertexMaas({
|
|
1741
|
+
project: 'my-project', // optional
|
|
1742
|
+
location: 'us-east5', // optional, defaults to 'global'
|
|
1743
|
+
});
|
|
1744
|
+
```
|
|
1745
|
+
|
|
1746
|
+
#### Node.js Runtime
|
|
1747
|
+
|
|
1748
|
+
For Node.js environments, the Google Vertex MaaS provider supports all standard Google Cloud authentication options through the `google-auth-library`:
|
|
1749
|
+
|
|
1750
|
+
```typescript
|
|
1751
|
+
import { createVertexMaas } from '@ai-sdk/google-vertex/maas';
|
|
1752
|
+
|
|
1753
|
+
const vertexMaas = createVertexMaas({
|
|
1754
|
+
googleAuthOptions: {
|
|
1755
|
+
credentials: {
|
|
1756
|
+
client_email: 'my-email',
|
|
1757
|
+
private_key: 'my-private-key',
|
|
1758
|
+
},
|
|
1759
|
+
},
|
|
1760
|
+
});
|
|
1761
|
+
```
|
|
1762
|
+
|
|
1763
|
+
##### Optional Provider Settings
|
|
1764
|
+
|
|
1765
|
+
- **project** _string_
|
|
1766
|
+
|
|
1767
|
+
The Google Cloud project ID. Defaults to the `GOOGLE_VERTEX_PROJECT` environment variable.
|
|
1768
|
+
|
|
1769
|
+
- **location** _string_
|
|
1770
|
+
|
|
1771
|
+
The Google Cloud location, e.g. `us-east5` or `global`. Defaults to the `GOOGLE_VERTEX_LOCATION` environment variable. If not set, defaults to `global`.
|
|
1772
|
+
|
|
1773
|
+
- **googleAuthOptions** _object_
|
|
1774
|
+
|
|
1775
|
+
Optional. The Authentication options used by the [Google Auth Library](https://github.com/googleapis/google-auth-library-nodejs/).
|
|
1776
|
+
|
|
1777
|
+
- **headers** _Resolvable<Record<string, string | undefined>>_
|
|
1778
|
+
|
|
1779
|
+
Headers to include in requests.
|
|
1780
|
+
|
|
1781
|
+
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise<Response>_
|
|
1782
|
+
|
|
1783
|
+
Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
|
|
1784
|
+
|
|
1785
|
+
<a id="google-vertex-maas-edge-runtime"></a>
|
|
1786
|
+
|
|
1787
|
+
#### Edge Runtime
|
|
1788
|
+
|
|
1789
|
+
For Edge runtimes, import from `@ai-sdk/google-vertex/maas/edge`:
|
|
1790
|
+
|
|
1791
|
+
```typescript
|
|
1792
|
+
import { vertexMaas } from '@ai-sdk/google-vertex/maas/edge';
|
|
1793
|
+
```
|
|
1794
|
+
|
|
1795
|
+
```typescript
|
|
1796
|
+
import { createVertexMaas } from '@ai-sdk/google-vertex/maas/edge';
|
|
1797
|
+
|
|
1798
|
+
const vertexMaas = createVertexMaas({
|
|
1799
|
+
project: 'my-project',
|
|
1800
|
+
location: 'us-east5',
|
|
1801
|
+
});
|
|
1802
|
+
```
|
|
1803
|
+
|
|
1804
|
+
For Edge runtime authentication, set these environment variables:
|
|
1805
|
+
|
|
1806
|
+
- `GOOGLE_CLIENT_EMAIL`
|
|
1807
|
+
- `GOOGLE_PRIVATE_KEY`
|
|
1808
|
+
- `GOOGLE_PRIVATE_KEY_ID` (optional)
|
|
1809
|
+
|
|
1810
|
+
### Language Models
|
|
1811
|
+
|
|
1812
|
+
You can create models using the provider instance. The first argument is the model ID:
|
|
1813
|
+
|
|
1814
|
+
```ts
|
|
1815
|
+
import { vertexMaas } from '@ai-sdk/google-vertex/maas';
|
|
1816
|
+
import { generateText } from 'ai';
|
|
1817
|
+
|
|
1818
|
+
const { text } = await generateText({
|
|
1819
|
+
model: vertexMaas('deepseek-ai/deepseek-v3.2-maas'),
|
|
1820
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
1821
|
+
});
|
|
1822
|
+
```
|
|
1823
|
+
|
|
1824
|
+
Streaming is also supported:
|
|
1825
|
+
|
|
1826
|
+
```ts
|
|
1827
|
+
import { vertexMaas } from '@ai-sdk/google-vertex/maas';
|
|
1828
|
+
import { streamText } from 'ai';
|
|
1829
|
+
|
|
1830
|
+
const result = streamText({
|
|
1831
|
+
model: vertexMaas('deepseek-ai/deepseek-v3.2-maas'),
|
|
1832
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
1833
|
+
});
|
|
1834
|
+
|
|
1835
|
+
for await (const textPart of result.textStream) {
|
|
1836
|
+
process.stdout.write(textPart);
|
|
1837
|
+
}
|
|
1838
|
+
```
|
|
1839
|
+
|
|
1840
|
+
### Available Models
|
|
1841
|
+
|
|
1842
|
+
The following models are available through the MaaS provider. You can also pass any valid model ID as a string.
|
|
1843
|
+
|
|
1844
|
+
| Model ID | Provider |
|
|
1845
|
+
| ---------------------------------------------- | -------- |
|
|
1846
|
+
| `deepseek-ai/deepseek-r1-0528-maas` | DeepSeek |
|
|
1847
|
+
| `deepseek-ai/deepseek-v3.1-maas` | DeepSeek |
|
|
1848
|
+
| `deepseek-ai/deepseek-v3.2-maas` | DeepSeek |
|
|
1849
|
+
| `openai/gpt-oss-120b-maas` | OpenAI |
|
|
1850
|
+
| `openai/gpt-oss-20b-maas` | OpenAI |
|
|
1851
|
+
| `meta/llama-4-maverick-17b-128e-instruct-maas` | Meta |
|
|
1852
|
+
| `meta/llama-4-scout-17b-16e-instruct-maas` | Meta |
|
|
1853
|
+
| `minimax/minimax-m2-maas` | MiniMax |
|
|
1854
|
+
| `qwen/qwen3-coder-480b-a35b-instruct-maas` | Qwen |
|
|
1855
|
+
| `qwen/qwen3-next-80b-a3b-instruct-maas` | Qwen |
|
|
1856
|
+
| `qwen/qwen3-next-80b-a3b-thinking-maas` | Qwen |
|
|
1857
|
+
| `moonshotai/kimi-k2-thinking-maas` | Moonshot |
|
|
1858
|
+
|
|
1859
|
+
<Note>
|
|
1860
|
+
Model availability depends on your Google Cloud project and region. Check the
|
|
1861
|
+
[Vertex AI Model Garden](https://console.cloud.google.com/vertex-ai/model-garden)
|
|
1862
|
+
for the latest available models.
|
|
1863
|
+
</Note>
|
package/maas/edge.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/maas/edge/index';
|
package/maas/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/maas/index';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/google-vertex",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.26",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -18,7 +18,9 @@
|
|
|
18
18
|
"README.md",
|
|
19
19
|
"edge.d.ts",
|
|
20
20
|
"anthropic/edge.d.ts",
|
|
21
|
-
"anthropic/index.d.ts"
|
|
21
|
+
"anthropic/index.d.ts",
|
|
22
|
+
"maas/edge.d.ts",
|
|
23
|
+
"maas/index.d.ts"
|
|
22
24
|
],
|
|
23
25
|
"directories": {
|
|
24
26
|
"doc": "./docs"
|
|
@@ -44,13 +46,24 @@
|
|
|
44
46
|
"types": "./dist/anthropic/edge/index.d.ts",
|
|
45
47
|
"import": "./dist/anthropic/edge/index.mjs",
|
|
46
48
|
"require": "./dist/anthropic/edge/index.js"
|
|
49
|
+
},
|
|
50
|
+
"./maas": {
|
|
51
|
+
"types": "./dist/maas/index.d.ts",
|
|
52
|
+
"import": "./dist/maas/index.mjs",
|
|
53
|
+
"require": "./dist/maas/index.js"
|
|
54
|
+
},
|
|
55
|
+
"./maas/edge": {
|
|
56
|
+
"types": "./dist/maas/edge/index.d.ts",
|
|
57
|
+
"import": "./dist/maas/edge/index.mjs",
|
|
58
|
+
"require": "./dist/maas/edge/index.js"
|
|
47
59
|
}
|
|
48
60
|
},
|
|
49
61
|
"dependencies": {
|
|
50
62
|
"google-auth-library": "^10.5.0",
|
|
51
|
-
"@ai-sdk/google": "4.0.0-beta.18",
|
|
52
|
-
"@ai-sdk/provider": "4.0.0-beta.5",
|
|
53
63
|
"@ai-sdk/anthropic": "4.0.0-beta.12",
|
|
64
|
+
"@ai-sdk/google": "4.0.0-beta.19",
|
|
65
|
+
"@ai-sdk/provider": "4.0.0-beta.5",
|
|
66
|
+
"@ai-sdk/openai-compatible": "3.0.0-beta.10",
|
|
54
67
|
"@ai-sdk/provider-utils": "5.0.0-beta.7"
|
|
55
68
|
},
|
|
56
69
|
"devDependencies": {
|
|
@@ -58,8 +71,8 @@
|
|
|
58
71
|
"tsup": "^8",
|
|
59
72
|
"typescript": "5.8.3",
|
|
60
73
|
"zod": "3.25.76",
|
|
61
|
-
"@ai-
|
|
62
|
-
"@
|
|
74
|
+
"@vercel/ai-tsconfig": "0.0.0",
|
|
75
|
+
"@ai-sdk/test-server": "2.0.0-beta.0"
|
|
63
76
|
},
|
|
64
77
|
"peerDependencies": {
|
|
65
78
|
"zod": "^3.25.76 || ^4.1.8"
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { FetchFunction, resolve } from '@ai-sdk/provider-utils';
|
|
2
|
+
import {
|
|
3
|
+
generateAuthToken,
|
|
4
|
+
GoogleCredentials,
|
|
5
|
+
} from '../../edge/google-vertex-auth-edge';
|
|
6
|
+
import {
|
|
7
|
+
createVertexMaas as createVertexMaasOriginal,
|
|
8
|
+
GoogleVertexMaasProvider,
|
|
9
|
+
GoogleVertexMaasProviderSettings as GoogleVertexMaasProviderSettingsOriginal,
|
|
10
|
+
} from '../google-vertex-maas-provider';
|
|
11
|
+
|
|
12
|
+
export type { GoogleVertexMaasProvider };
|
|
13
|
+
|
|
14
|
+
export interface GoogleVertexMaasProviderSettings extends GoogleVertexMaasProviderSettingsOriginal {
|
|
15
|
+
/**
|
|
16
|
+
* Optional. The Google credentials for the Google Cloud service account. If
|
|
17
|
+
* not provided, the Google Vertex provider will use environment variables to
|
|
18
|
+
* load the credentials.
|
|
19
|
+
*/
|
|
20
|
+
googleCredentials?: GoogleCredentials;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Create a Google Vertex AI MaaS (Model as a Service) provider instance for Edge runtimes.
|
|
25
|
+
* Uses the OpenAI-compatible Chat Completions API for partner and open models.
|
|
26
|
+
* Automatically handles Google Cloud authentication.
|
|
27
|
+
*
|
|
28
|
+
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/maas/use-open-models
|
|
29
|
+
*/
|
|
30
|
+
export function createVertexMaas(
|
|
31
|
+
options: GoogleVertexMaasProviderSettings = {},
|
|
32
|
+
): GoogleVertexMaasProvider {
|
|
33
|
+
// Create a custom fetch wrapper that adds auth headers
|
|
34
|
+
const customFetch: FetchFunction = async (url, init) => {
|
|
35
|
+
const token = await generateAuthToken(options.googleCredentials);
|
|
36
|
+
const resolvedHeaders = await resolve(options.headers);
|
|
37
|
+
const authHeaders = {
|
|
38
|
+
...resolvedHeaders,
|
|
39
|
+
Authorization: `Bearer ${token}`,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Merge auth headers with existing headers from init
|
|
43
|
+
const fetchInit = {
|
|
44
|
+
...init,
|
|
45
|
+
headers: {
|
|
46
|
+
...init?.headers,
|
|
47
|
+
...authHeaders,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Call the original fetch or user's custom fetch
|
|
52
|
+
return (options.fetch ?? fetch)(url, fetchInit);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return createVertexMaasOriginal({
|
|
56
|
+
...options,
|
|
57
|
+
fetch: customFetch,
|
|
58
|
+
headers: undefined, // Don't pass headers, we handle them in fetch
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Default Google Vertex AI MaaS provider instance for Edge runtimes.
|
|
64
|
+
*/
|
|
65
|
+
export const vertexMaas = createVertexMaas();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createVertexMaas,
|
|
3
|
+
vertexMaas,
|
|
4
|
+
} from './google-vertex-maas-provider-edge';
|
|
5
|
+
export type {
|
|
6
|
+
GoogleVertexMaasProvider,
|
|
7
|
+
GoogleVertexMaasProviderSettings,
|
|
8
|
+
} from './google-vertex-maas-provider-edge';
|
|
9
|
+
export type { GoogleVertexMaasModelId } from '../google-vertex-maas-options';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// https://cloud.google.com/vertex-ai/generative-ai/docs/maas/use-open-models
|
|
2
|
+
export type GoogleVertexMaasModelId =
|
|
3
|
+
| 'deepseek-ai/deepseek-r1-0528-maas'
|
|
4
|
+
| 'deepseek-ai/deepseek-v3.1-maas'
|
|
5
|
+
| 'deepseek-ai/deepseek-v3.2-maas'
|
|
6
|
+
| 'openai/gpt-oss-120b-maas'
|
|
7
|
+
| 'openai/gpt-oss-20b-maas'
|
|
8
|
+
| 'meta/llama-4-maverick-17b-128e-instruct-maas'
|
|
9
|
+
| 'meta/llama-4-scout-17b-16e-instruct-maas'
|
|
10
|
+
| 'minimax/minimax-m2-maas'
|
|
11
|
+
| 'qwen/qwen3-coder-480b-a35b-instruct-maas'
|
|
12
|
+
| 'qwen/qwen3-next-80b-a3b-instruct-maas'
|
|
13
|
+
| 'qwen/qwen3-next-80b-a3b-thinking-maas'
|
|
14
|
+
| 'moonshotai/kimi-k2-thinking-maas'
|
|
15
|
+
| (string & {});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { FetchFunction, resolve } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { GoogleAuthOptions } from 'google-auth-library';
|
|
3
|
+
import { generateAuthToken } from '../google-vertex-auth-google-auth-library';
|
|
4
|
+
import {
|
|
5
|
+
createVertexMaas as createVertexMaasOriginal,
|
|
6
|
+
GoogleVertexMaasProvider,
|
|
7
|
+
GoogleVertexMaasProviderSettings as GoogleVertexMaasProviderSettingsOriginal,
|
|
8
|
+
} from './google-vertex-maas-provider';
|
|
9
|
+
|
|
10
|
+
export type { GoogleVertexMaasProvider };
|
|
11
|
+
|
|
12
|
+
export interface GoogleVertexMaasProviderSettings extends GoogleVertexMaasProviderSettingsOriginal {
|
|
13
|
+
/**
|
|
14
|
+
* Optional. The Authentication options provided by google-auth-library.
|
|
15
|
+
* Complete list of authentication options is documented in the
|
|
16
|
+
* GoogleAuthOptions interface:
|
|
17
|
+
* https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.
|
|
18
|
+
*/
|
|
19
|
+
googleAuthOptions?: GoogleAuthOptions;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Create a Google Vertex AI MaaS (Model as a Service) provider instance for Node.js.
|
|
24
|
+
* Uses the OpenAI-compatible Chat Completions API for partner and open models.
|
|
25
|
+
* Automatically handles Google Cloud authentication.
|
|
26
|
+
*
|
|
27
|
+
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/maas/use-open-models
|
|
28
|
+
*/
|
|
29
|
+
export function createVertexMaas(
|
|
30
|
+
options: GoogleVertexMaasProviderSettings = {},
|
|
31
|
+
): GoogleVertexMaasProvider {
|
|
32
|
+
// Create a custom fetch wrapper that adds auth headers
|
|
33
|
+
const customFetch: FetchFunction = async (url, init) => {
|
|
34
|
+
const token = await generateAuthToken(options.googleAuthOptions);
|
|
35
|
+
const resolvedHeaders = await resolve(options.headers);
|
|
36
|
+
const authHeaders = {
|
|
37
|
+
...resolvedHeaders,
|
|
38
|
+
Authorization: `Bearer ${token}`,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Merge auth headers with existing headers from init
|
|
42
|
+
const fetchInit = {
|
|
43
|
+
...init,
|
|
44
|
+
headers: {
|
|
45
|
+
...init?.headers,
|
|
46
|
+
...authHeaders,
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Call the original fetch or user's custom fetch
|
|
51
|
+
return (options.fetch ?? fetch)(url, fetchInit);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return createVertexMaasOriginal({
|
|
55
|
+
...options,
|
|
56
|
+
fetch: customFetch,
|
|
57
|
+
headers: undefined, // Don't pass headers, we handle them in fetch
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Default Google Vertex AI MaaS provider instance for Node.js.
|
|
63
|
+
*/
|
|
64
|
+
export const vertexMaas = createVertexMaas();
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
|
2
|
+
import type { OpenAICompatibleProvider } from '@ai-sdk/openai-compatible';
|
|
3
|
+
import {
|
|
4
|
+
FetchFunction,
|
|
5
|
+
loadOptionalSetting,
|
|
6
|
+
loadSetting,
|
|
7
|
+
Resolvable,
|
|
8
|
+
withoutTrailingSlash,
|
|
9
|
+
} from '@ai-sdk/provider-utils';
|
|
10
|
+
import type { GoogleVertexMaasModelId } from './google-vertex-maas-options';
|
|
11
|
+
|
|
12
|
+
export interface GoogleVertexMaasProvider extends OpenAICompatibleProvider<
|
|
13
|
+
GoogleVertexMaasModelId,
|
|
14
|
+
string,
|
|
15
|
+
string,
|
|
16
|
+
string
|
|
17
|
+
> {}
|
|
18
|
+
|
|
19
|
+
export interface GoogleVertexMaasProviderSettings {
|
|
20
|
+
/**
|
|
21
|
+
* Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.
|
|
22
|
+
*/
|
|
23
|
+
project?: string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Google Cloud location/region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.
|
|
27
|
+
* Use 'global' for the global endpoint.
|
|
28
|
+
*/
|
|
29
|
+
location?: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Base URL for the API calls. If not provided, will be constructed from project and location.
|
|
33
|
+
*/
|
|
34
|
+
baseURL?: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Headers to use for requests. Can be:
|
|
38
|
+
* - A headers object
|
|
39
|
+
* - A Promise that resolves to a headers object
|
|
40
|
+
* - A function that returns a headers object
|
|
41
|
+
* - A function that returns a Promise of a headers object
|
|
42
|
+
*/
|
|
43
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
47
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
48
|
+
*/
|
|
49
|
+
fetch?: FetchFunction;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Create a Google Vertex AI MaaS (Model as a Service) provider instance.
|
|
54
|
+
* Uses the OpenAI-compatible Chat Completions API for partner and open models.
|
|
55
|
+
*
|
|
56
|
+
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/maas/use-open-models
|
|
57
|
+
*/
|
|
58
|
+
export function createVertexMaas(
|
|
59
|
+
options: GoogleVertexMaasProviderSettings = {},
|
|
60
|
+
): GoogleVertexMaasProvider {
|
|
61
|
+
// Lazy-load settings to support loading from environment variables at runtime
|
|
62
|
+
const loadLocation = () =>
|
|
63
|
+
loadOptionalSetting({
|
|
64
|
+
settingValue: options.location,
|
|
65
|
+
environmentVariableName: 'GOOGLE_VERTEX_LOCATION',
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const loadProject = () =>
|
|
69
|
+
loadSetting({
|
|
70
|
+
settingValue: options.project,
|
|
71
|
+
settingName: 'project',
|
|
72
|
+
environmentVariableName: 'GOOGLE_VERTEX_PROJECT',
|
|
73
|
+
description: 'Google Vertex project',
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Construct base URL: https://aiplatform.googleapis.com/v1/projects/{project}/locations/{location}/endpoints/openapi
|
|
77
|
+
const constructBaseURL = () => {
|
|
78
|
+
const projectId = loadProject();
|
|
79
|
+
const location = loadLocation() ?? 'global';
|
|
80
|
+
|
|
81
|
+
return `https://aiplatform.googleapis.com/v1/projects/${projectId}/locations/${location}/endpoints/openapi`;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const loadBaseURL = () =>
|
|
85
|
+
withoutTrailingSlash(options.baseURL ?? '') || constructBaseURL();
|
|
86
|
+
|
|
87
|
+
let cachedProvider: GoogleVertexMaasProvider | undefined;
|
|
88
|
+
const getProvider = () =>
|
|
89
|
+
(cachedProvider ??= createOpenAICompatible({
|
|
90
|
+
name: 'vertex.maas',
|
|
91
|
+
baseURL: loadBaseURL(),
|
|
92
|
+
fetch: options.fetch,
|
|
93
|
+
}));
|
|
94
|
+
|
|
95
|
+
const provider = (modelId: GoogleVertexMaasModelId) => getProvider()(modelId);
|
|
96
|
+
|
|
97
|
+
provider.specificationVersion = 'v4' as const;
|
|
98
|
+
provider.languageModel = (modelId: GoogleVertexMaasModelId) =>
|
|
99
|
+
getProvider().languageModel(modelId);
|
|
100
|
+
provider.chatModel = (modelId: GoogleVertexMaasModelId) =>
|
|
101
|
+
getProvider().chatModel(modelId);
|
|
102
|
+
provider.completionModel = (modelId: string) =>
|
|
103
|
+
getProvider().completionModel(modelId);
|
|
104
|
+
provider.embeddingModel = (modelId: string) =>
|
|
105
|
+
getProvider().embeddingModel(modelId);
|
|
106
|
+
provider.textEmbeddingModel = (modelId: string) =>
|
|
107
|
+
getProvider().textEmbeddingModel(modelId);
|
|
108
|
+
provider.imageModel = (modelId: string) => getProvider().imageModel(modelId);
|
|
109
|
+
|
|
110
|
+
return provider as GoogleVertexMaasProvider;
|
|
111
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createVertexMaas,
|
|
3
|
+
vertexMaas,
|
|
4
|
+
} from './google-vertex-maas-provider-node';
|
|
5
|
+
export type {
|
|
6
|
+
GoogleVertexMaasProvider,
|
|
7
|
+
GoogleVertexMaasProviderSettings,
|
|
8
|
+
} from './google-vertex-maas-provider-node';
|
|
9
|
+
export type { GoogleVertexMaasModelId } from './google-vertex-maas-options';
|