@ai-sdk/google-vertex 5.0.0-beta.63 → 5.0.0-beta.66
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 +25 -0
- package/dist/anthropic/edge/index.js +1 -1
- package/dist/anthropic/index.js +13 -19
- package/dist/anthropic/index.js.map +1 -1
- package/dist/edge/index.js +1 -1
- package/dist/index.js +14 -21
- package/dist/index.js.map +1 -1
- package/dist/maas/edge/index.js +1 -1
- package/dist/maas/index.js +13 -18
- package/dist/maas/index.js.map +1 -1
- package/dist/xai/edge/index.d.ts +92 -0
- package/dist/xai/edge/index.js +259 -0
- package/dist/xai/edge/index.js.map +1 -0
- package/dist/xai/index.d.ts +76 -0
- package/dist/xai/index.js +159 -0
- package/dist/xai/index.js.map +1 -0
- package/docs/16-google-vertex.mdx +182 -3
- package/package.json +15 -3
- package/src/anthropic/google-vertex-anthropic-provider-node.ts +3 -2
- package/src/google-vertex-auth-google-auth-library.ts +10 -22
- package/src/google-vertex-provider.ts +4 -4
- package/src/maas/google-vertex-maas-provider-node.ts +4 -2
- package/src/xai/edge/google-vertex-xai-provider-edge.ts +61 -0
- package/src/xai/edge/index.ts +9 -0
- package/src/xai/google-vertex-xai-options.ts +7 -0
- package/src/xai/google-vertex-xai-provider-node.ts +62 -0
- package/src/xai/google-vertex-xai-provider.ts +212 -0
- package/src/xai/index.ts +9 -0
- package/xai/edge.d.ts +1 -0
- package/xai/index.d.ts +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/xai/google-vertex-xai-provider-node.ts","../../src/google-vertex-auth-google-auth-library.ts","../../src/xai/google-vertex-xai-provider.ts"],"sourcesContent":["import { resolve, type FetchFunction } from '@ai-sdk/provider-utils';\nimport type { GoogleAuthOptions } from 'google-auth-library';\nimport { createAuthTokenGenerator } from '../google-vertex-auth-google-auth-library';\nimport {\n createGoogleVertexXai as createGoogleVertexXaiOriginal,\n type GoogleVertexXaiProvider,\n type GoogleVertexXaiProviderSettings as GoogleVertexXaiProviderSettingsOriginal,\n} from './google-vertex-xai-provider';\nexport type { GoogleVertexXaiProvider };\n\nexport interface GoogleVertexXaiProviderSettings extends GoogleVertexXaiProviderSettingsOriginal {\n /**\n * Optional. The Authentication options provided by google-auth-library.\n * Complete list of authentication options is documented in the\n * GoogleAuthOptions interface:\n * https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.\n */\n googleAuthOptions?: GoogleAuthOptions;\n}\n\n/**\n * Create a Google Vertex AI xAI provider instance for Node.js.\n * Uses the OpenAI-compatible Chat Completions API for Grok partner models.\n * Automatically handles Google Cloud authentication.\n *\n * @see https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok\n */\nexport function createGoogleVertexXai(\n options: GoogleVertexXaiProviderSettings = {},\n): GoogleVertexXaiProvider {\n const generateAuthToken = createAuthTokenGenerator(options.googleAuthOptions);\n\n const customFetch: FetchFunction = async (url, init) => {\n const token = await generateAuthToken();\n const resolvedHeaders = await resolve(options.headers);\n const authHeaders = {\n ...resolvedHeaders,\n Authorization: `Bearer ${token}`,\n };\n\n const fetchInit = {\n ...init,\n headers: {\n ...init?.headers,\n ...authHeaders,\n },\n };\n\n return (options.fetch ?? fetch)(url, fetchInit);\n };\n\n return createGoogleVertexXaiOriginal({\n ...options,\n fetch: customFetch,\n headers: undefined,\n });\n}\n\n/**\n * Default Google Vertex AI xAI provider instance for Node.js.\n */\nexport const googleVertexXai = createGoogleVertexXai();\n","import { GoogleAuth, type GoogleAuthOptions } from 'google-auth-library';\n\nexport function createAuthTokenGenerator(options?: GoogleAuthOptions) {\n const auth = new GoogleAuth({\n scopes: ['https://www.googleapis.com/auth/cloud-platform'],\n ...options,\n });\n\n return async function generateAuthToken() {\n const client = await auth.getClient();\n const token = await client.getAccessToken();\n return token?.token ?? null;\n };\n}\n","import {\n NoSuchModelError,\n type LanguageModelV4,\n type LanguageModelV4Usage,\n type ProviderV4,\n} from '@ai-sdk/provider';\nimport {\n createOpenAICompatible,\n type OpenAICompatibleProvider,\n} from '@ai-sdk/openai-compatible';\nimport {\n loadOptionalSetting,\n loadSetting,\n withoutTrailingSlash,\n type FetchFunction,\n type Resolvable,\n} from '@ai-sdk/provider-utils';\nimport type { GoogleVertexXaiModelId } from './google-vertex-xai-options';\n\nexport interface GoogleVertexXaiProvider extends ProviderV4 {\n /**\n * Creates a model for text generation.\n */\n (modelId: GoogleVertexXaiModelId): LanguageModelV4;\n\n /**\n * Creates a model for text generation.\n */\n languageModel(modelId: GoogleVertexXaiModelId): LanguageModelV4;\n\n /**\n * Creates a chat model for text generation.\n */\n chatModel(modelId: GoogleVertexXaiModelId): LanguageModelV4;\n\n /**\n * @deprecated Use `embeddingModel` instead.\n */\n textEmbeddingModel(modelId: string): never;\n}\n\nexport interface GoogleVertexXaiProviderSettings {\n /**\n * Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.\n */\n project?: string;\n\n /**\n * Google Cloud location/region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.\n * Use 'global' for the global endpoint.\n */\n location?: string;\n\n /**\n * Base URL for the API calls. If not provided, will be constructed from project and location.\n */\n baseURL?: string;\n\n /**\n * Headers to use for requests. Can be:\n * - A headers object\n * - A Promise that resolves to a headers object\n * - A function that returns a headers object\n * - A function that returns a Promise of a headers object\n */\n headers?: Resolvable<Record<string, string | undefined>>;\n\n /**\n * Custom fetch implementation. You can use it as a middleware to intercept requests,\n * or to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\ntype GoogleVertexXaiUsage =\n | {\n prompt_tokens?: number | null;\n completion_tokens?: number | null;\n prompt_tokens_details?: {\n cached_tokens?: number | null;\n } | null;\n completion_tokens_details?: {\n reasoning_tokens?: number | null;\n } | null;\n }\n | undefined\n | null;\n\nfunction convertGoogleVertexXaiUsage(\n usage: GoogleVertexXaiUsage,\n): LanguageModelV4Usage {\n if (usage == null) {\n return {\n inputTokens: {\n total: undefined,\n noCache: undefined,\n cacheRead: undefined,\n cacheWrite: undefined,\n },\n outputTokens: {\n total: undefined,\n text: undefined,\n reasoning: undefined,\n },\n raw: undefined,\n };\n }\n\n const promptTokens = usage.prompt_tokens ?? 0;\n const completionTokens = usage.completion_tokens ?? 0;\n const cacheReadTokens = usage.prompt_tokens_details?.cached_tokens ?? 0;\n const reasoningTokens =\n usage.completion_tokens_details?.reasoning_tokens ?? 0;\n\n return {\n inputTokens: {\n total: promptTokens,\n noCache: promptTokens - cacheReadTokens,\n cacheRead: cacheReadTokens,\n cacheWrite: undefined,\n },\n outputTokens: {\n total: completionTokens + reasoningTokens,\n text: completionTokens,\n reasoning: reasoningTokens,\n },\n raw: usage,\n };\n}\n\nfunction transformGoogleVertexXaiRequestBody(args: Record<string, any>) {\n const { reasoning_effort: _reasoningEffort, ...rest } = args;\n return rest;\n}\n\n/**\n * Create a Google Vertex AI xAI provider instance.\n * Uses the OpenAI-compatible Chat Completions API for Grok partner models.\n *\n * @see https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok\n */\nexport function createGoogleVertexXai(\n options: GoogleVertexXaiProviderSettings = {},\n): GoogleVertexXaiProvider {\n const loadLocation = () =>\n loadOptionalSetting({\n settingValue: options.location,\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n });\n\n const loadProject = () =>\n loadSetting({\n settingValue: options.project,\n settingName: 'project',\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n description: 'Google Vertex project',\n });\n\n const constructBaseURL = () => {\n const projectId = loadProject();\n const location = loadLocation() ?? 'global';\n\n return `https://aiplatform.googleapis.com/v1/projects/${projectId}/locations/${location}/endpoints/openapi`;\n };\n\n const loadBaseURL = () =>\n withoutTrailingSlash(options.baseURL ?? '') || constructBaseURL();\n\n let cachedProvider:\n | OpenAICompatibleProvider<GoogleVertexXaiModelId, string, string, string>\n | undefined;\n const getProvider = () =>\n (cachedProvider ??= createOpenAICompatible({\n name: 'googleVertex.xai',\n baseURL: loadBaseURL(),\n fetch: options.fetch,\n includeUsage: true,\n supportsStructuredOutputs: true,\n supportedUrls: () => ({\n 'image/*': [/^https?:\\/\\/.*$/],\n }),\n transformRequestBody: transformGoogleVertexXaiRequestBody,\n convertUsage: convertGoogleVertexXaiUsage,\n }));\n\n const createChatModel = (modelId: GoogleVertexXaiModelId) =>\n getProvider().languageModel(modelId);\n\n const provider = function (modelId: GoogleVertexXaiModelId) {\n if (new.target) {\n throw new Error(\n 'The Google Vertex xAI model function cannot be called with the new keyword.',\n );\n }\n\n return createChatModel(modelId);\n };\n\n provider.specificationVersion = 'v4' as const;\n provider.languageModel = createChatModel;\n provider.chatModel = (modelId: GoogleVertexXaiModelId) =>\n getProvider().chatModel(modelId);\n provider.embeddingModel = (modelId: string): never => {\n throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });\n };\n provider.textEmbeddingModel = provider.embeddingModel;\n provider.imageModel = (modelId: string): never => {\n throw new NoSuchModelError({ modelId, modelType: 'imageModel' });\n };\n\n return provider;\n}\n"],"mappings":";AAAA,SAAS,eAAmC;;;ACA5C,SAAS,kBAA0C;AAE5C,SAAS,yBAAyB,SAA6B;AACpE,QAAM,OAAO,IAAI,WAAW;AAAA,IAC1B,QAAQ,CAAC,gDAAgD;AAAA,IACzD,GAAG;AAAA,EACL,CAAC;AAED,SAAO,eAAe,oBAAoB;AAR5C;AASI,UAAM,SAAS,MAAM,KAAK,UAAU;AACpC,UAAM,QAAQ,MAAM,OAAO,eAAe;AAC1C,YAAO,oCAAO,UAAP,YAAgB;AAAA,EACzB;AACF;;;ACbA;AAAA,EACE;AAAA,OAIK;AACP;AAAA,EACE;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AAwEP,SAAS,4BACP,OACsB;AA1FxB;AA2FE,MAAI,SAAS,MAAM;AACjB,WAAO;AAAA,MACL,aAAa;AAAA,QACX,OAAO;AAAA,QACP,SAAS;AAAA,QACT,W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SAAS;AAAA,EAChD;AAEA,SAAO,sBAA8B;AAAA,IACnC,GAAG;AAAA,IACH,OAAO;AAAA,IACP,SAAS;AAAA,EACX,CAAC;AACH;AAKO,IAAM,kBAAkBA,uBAAsB;","names":["createGoogleVertexXai"]}
|
|
@@ -5,7 +5,7 @@ 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), [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).
|
|
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), [xAI's Grok partner models](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok), 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.
|
|
@@ -18,7 +18,7 @@ The Google Vertex provider for the [AI SDK](/docs) contains language model suppo
|
|
|
18
18
|
|
|
19
19
|
## Setup
|
|
20
20
|
|
|
21
|
-
The Google Vertex and Google Vertex
|
|
21
|
+
The Google Vertex, Google Vertex Anthropic, Google Vertex xAI, and Google Vertex MaaS providers are available in the `@ai-sdk/google-vertex` module. You can install it with
|
|
22
22
|
|
|
23
23
|
<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}>
|
|
24
24
|
<Tab>
|
|
@@ -1579,7 +1579,7 @@ console.log(
|
|
|
1579
1579
|
|
|
1580
1580
|
You can also use cache control on system messages by providing multiple system messages at the head of your messages array:
|
|
1581
1581
|
|
|
1582
|
-
```ts highlight="3,9
|
|
1582
|
+
```ts highlight="3,7-9"
|
|
1583
1583
|
const result = await generateText({
|
|
1584
1584
|
model: vertexAnthropic('claude-3-5-sonnet-20240620'),
|
|
1585
1585
|
messages: [
|
|
@@ -1782,6 +1782,185 @@ See also [Anthropic Model Comparison](https://docs.anthropic.com/en/docs/about-c
|
|
|
1782
1782
|
model ID as a string if needed.
|
|
1783
1783
|
</Note>
|
|
1784
1784
|
|
|
1785
|
+
## Google Vertex xAI Provider Usage
|
|
1786
|
+
|
|
1787
|
+
The Google Vertex xAI provider offers support for xAI's Grok partner models through the Google Vertex AI OpenAI-compatible Chat Completions API.
|
|
1788
|
+
|
|
1789
|
+
For more information, see the [Vertex AI Grok documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok).
|
|
1790
|
+
|
|
1791
|
+
### Provider Instance
|
|
1792
|
+
|
|
1793
|
+
You can import the default provider instance `googleVertexXai` from `@ai-sdk/google-vertex/xai`:
|
|
1794
|
+
|
|
1795
|
+
```typescript
|
|
1796
|
+
import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
|
|
1797
|
+
```
|
|
1798
|
+
|
|
1799
|
+
If you need a customized setup, you can import `createGoogleVertexXai` from `@ai-sdk/google-vertex/xai` and create a provider instance with your settings:
|
|
1800
|
+
|
|
1801
|
+
```typescript
|
|
1802
|
+
import { createGoogleVertexXai } from '@ai-sdk/google-vertex/xai';
|
|
1803
|
+
|
|
1804
|
+
const googleVertexXai = createGoogleVertexXai({
|
|
1805
|
+
project: 'my-project', // optional
|
|
1806
|
+
location: 'global', // optional, defaults to 'global'
|
|
1807
|
+
});
|
|
1808
|
+
```
|
|
1809
|
+
|
|
1810
|
+
#### Node.js Runtime
|
|
1811
|
+
|
|
1812
|
+
For Node.js environments, the Google Vertex xAI provider supports all standard Google Cloud authentication options through the `google-auth-library`:
|
|
1813
|
+
|
|
1814
|
+
```typescript
|
|
1815
|
+
import { createGoogleVertexXai } from '@ai-sdk/google-vertex/xai';
|
|
1816
|
+
|
|
1817
|
+
const googleVertexXai = createGoogleVertexXai({
|
|
1818
|
+
googleAuthOptions: {
|
|
1819
|
+
credentials: {
|
|
1820
|
+
client_email: 'my-email',
|
|
1821
|
+
private_key: 'my-private-key',
|
|
1822
|
+
},
|
|
1823
|
+
},
|
|
1824
|
+
});
|
|
1825
|
+
```
|
|
1826
|
+
|
|
1827
|
+
##### Optional Provider Settings
|
|
1828
|
+
|
|
1829
|
+
- **project** _string_
|
|
1830
|
+
|
|
1831
|
+
The Google Cloud project ID. Defaults to the `GOOGLE_VERTEX_PROJECT` environment variable.
|
|
1832
|
+
|
|
1833
|
+
- **location** _string_
|
|
1834
|
+
|
|
1835
|
+
The Google Cloud location. Grok models are available on the global endpoint. Defaults to the `GOOGLE_VERTEX_LOCATION` environment variable. If not set, defaults to `global`.
|
|
1836
|
+
|
|
1837
|
+
- **googleAuthOptions** _object_
|
|
1838
|
+
|
|
1839
|
+
Optional. The Authentication options used by the [Google Auth Library](https://github.com/googleapis/google-auth-library-nodejs/).
|
|
1840
|
+
|
|
1841
|
+
- **headers** _Resolvable<Record<string, string | undefined>>_
|
|
1842
|
+
|
|
1843
|
+
Headers to include in requests.
|
|
1844
|
+
|
|
1845
|
+
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise<Response>_
|
|
1846
|
+
|
|
1847
|
+
Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
|
|
1848
|
+
|
|
1849
|
+
<a id="google-vertex-xai-edge-runtime"></a>
|
|
1850
|
+
|
|
1851
|
+
#### Edge Runtime
|
|
1852
|
+
|
|
1853
|
+
For Edge runtimes, import from `@ai-sdk/google-vertex/xai/edge`:
|
|
1854
|
+
|
|
1855
|
+
```typescript
|
|
1856
|
+
import { googleVertexXai } from '@ai-sdk/google-vertex/xai/edge';
|
|
1857
|
+
```
|
|
1858
|
+
|
|
1859
|
+
```typescript
|
|
1860
|
+
import { createGoogleVertexXai } from '@ai-sdk/google-vertex/xai/edge';
|
|
1861
|
+
|
|
1862
|
+
const googleVertexXai = createGoogleVertexXai({
|
|
1863
|
+
project: 'my-project',
|
|
1864
|
+
location: 'global',
|
|
1865
|
+
});
|
|
1866
|
+
```
|
|
1867
|
+
|
|
1868
|
+
For Edge runtime authentication, set these environment variables:
|
|
1869
|
+
|
|
1870
|
+
- `GOOGLE_CLIENT_EMAIL`
|
|
1871
|
+
- `GOOGLE_PRIVATE_KEY`
|
|
1872
|
+
- `GOOGLE_PRIVATE_KEY_ID` (optional)
|
|
1873
|
+
|
|
1874
|
+
### Language Models
|
|
1875
|
+
|
|
1876
|
+
You can create models using the provider instance. The first argument is the model ID:
|
|
1877
|
+
|
|
1878
|
+
```ts
|
|
1879
|
+
import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
|
|
1880
|
+
import { generateText } from 'ai';
|
|
1881
|
+
|
|
1882
|
+
const { text } = await generateText({
|
|
1883
|
+
model: googleVertexXai('xai/grok-4.1-fast-reasoning'),
|
|
1884
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
1885
|
+
});
|
|
1886
|
+
```
|
|
1887
|
+
|
|
1888
|
+
Streaming is also supported:
|
|
1889
|
+
|
|
1890
|
+
```ts
|
|
1891
|
+
import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
|
|
1892
|
+
import { streamText } from 'ai';
|
|
1893
|
+
|
|
1894
|
+
const result = streamText({
|
|
1895
|
+
model: googleVertexXai('xai/grok-4.1-fast-reasoning'),
|
|
1896
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
1897
|
+
});
|
|
1898
|
+
|
|
1899
|
+
for await (const textPart of result.textStream) {
|
|
1900
|
+
process.stdout.write(textPart);
|
|
1901
|
+
}
|
|
1902
|
+
```
|
|
1903
|
+
|
|
1904
|
+
### Function Calling
|
|
1905
|
+
|
|
1906
|
+
Grok models on Vertex support OpenAI-compatible function calling. You can use AI SDK tools as usual:
|
|
1907
|
+
|
|
1908
|
+
```ts
|
|
1909
|
+
import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
|
|
1910
|
+
import { generateText, tool } from 'ai';
|
|
1911
|
+
import { z } from 'zod';
|
|
1912
|
+
|
|
1913
|
+
const result = await generateText({
|
|
1914
|
+
model: googleVertexXai('xai/grok-4.1-fast-reasoning'),
|
|
1915
|
+
tools: {
|
|
1916
|
+
weather: tool({
|
|
1917
|
+
description: 'Get the weather in a city',
|
|
1918
|
+
inputSchema: z.object({ city: z.string() }),
|
|
1919
|
+
execute: async ({ city }) => `The weather in ${city} is sunny.`,
|
|
1920
|
+
}),
|
|
1921
|
+
},
|
|
1922
|
+
prompt: 'What is the weather in San Francisco?',
|
|
1923
|
+
});
|
|
1924
|
+
```
|
|
1925
|
+
|
|
1926
|
+
### Structured Outputs
|
|
1927
|
+
|
|
1928
|
+
Grok models on Vertex support JSON mode and schema-backed structured outputs:
|
|
1929
|
+
|
|
1930
|
+
```ts
|
|
1931
|
+
import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
|
|
1932
|
+
import { generateText, Output } from 'ai';
|
|
1933
|
+
import { z } from 'zod';
|
|
1934
|
+
|
|
1935
|
+
const result = await generateText({
|
|
1936
|
+
model: googleVertexXai('xai/grok-4.1-fast-reasoning'),
|
|
1937
|
+
output: Output.object({
|
|
1938
|
+
schema: z.object({
|
|
1939
|
+
name: z.string(),
|
|
1940
|
+
date: z.string(),
|
|
1941
|
+
participants: z.array(z.string()),
|
|
1942
|
+
}),
|
|
1943
|
+
}),
|
|
1944
|
+
prompt: 'Alice and Bob are going to a science fair on Friday.',
|
|
1945
|
+
});
|
|
1946
|
+
```
|
|
1947
|
+
|
|
1948
|
+
### Available Models
|
|
1949
|
+
|
|
1950
|
+
The following models are available through the Google Vertex xAI provider. You can also pass any valid model ID as a string.
|
|
1951
|
+
|
|
1952
|
+
| Model ID | Reasoning |
|
|
1953
|
+
| ----------------------------------- | --------- |
|
|
1954
|
+
| `xai/grok-4.20-reasoning` | Yes |
|
|
1955
|
+
| `xai/grok-4.20-non-reasoning` | No |
|
|
1956
|
+
| `xai/grok-4.1-fast-reasoning` | Yes |
|
|
1957
|
+
| `xai/grok-4.1-fast-non-reasoning` | No |
|
|
1958
|
+
|
|
1959
|
+
<Note>
|
|
1960
|
+
Grok reasoning models on Vertex report reasoning token counts in usage
|
|
1961
|
+
metadata. They do not support the `reasoning_effort` request parameter.
|
|
1962
|
+
</Note>
|
|
1963
|
+
|
|
1785
1964
|
## Google Vertex MaaS Provider Usage
|
|
1786
1965
|
|
|
1787
1966
|
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.
|
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.66",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"anthropic/edge.d.ts",
|
|
21
21
|
"anthropic/index.d.ts",
|
|
22
22
|
"maas/edge.d.ts",
|
|
23
|
-
"maas/index.d.ts"
|
|
23
|
+
"maas/index.d.ts",
|
|
24
|
+
"xai/edge.d.ts",
|
|
25
|
+
"xai/index.d.ts"
|
|
24
26
|
],
|
|
25
27
|
"directories": {
|
|
26
28
|
"doc": "./docs"
|
|
@@ -56,13 +58,23 @@
|
|
|
56
58
|
"types": "./dist/maas/edge/index.d.ts",
|
|
57
59
|
"import": "./dist/maas/edge/index.js",
|
|
58
60
|
"default": "./dist/maas/edge/index.js"
|
|
61
|
+
},
|
|
62
|
+
"./xai": {
|
|
63
|
+
"types": "./dist/xai/index.d.ts",
|
|
64
|
+
"import": "./dist/xai/index.js",
|
|
65
|
+
"default": "./dist/xai/index.js"
|
|
66
|
+
},
|
|
67
|
+
"./xai/edge": {
|
|
68
|
+
"types": "./dist/xai/edge/index.d.ts",
|
|
69
|
+
"import": "./dist/xai/edge/index.js",
|
|
70
|
+
"default": "./dist/xai/edge/index.js"
|
|
59
71
|
}
|
|
60
72
|
},
|
|
61
73
|
"dependencies": {
|
|
62
74
|
"google-auth-library": "^10.5.0",
|
|
63
75
|
"@ai-sdk/anthropic": "4.0.0-beta.42",
|
|
64
76
|
"@ai-sdk/google": "4.0.0-beta.49",
|
|
65
|
-
"@ai-sdk/openai-compatible": "3.0.0-beta.
|
|
77
|
+
"@ai-sdk/openai-compatible": "3.0.0-beta.36",
|
|
66
78
|
"@ai-sdk/provider": "4.0.0-beta.14",
|
|
67
79
|
"@ai-sdk/provider-utils": "5.0.0-beta.30"
|
|
68
80
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { resolve } from '@ai-sdk/provider-utils';
|
|
2
2
|
import type { GoogleAuthOptions } from 'google-auth-library';
|
|
3
|
-
import {
|
|
3
|
+
import { createAuthTokenGenerator } from '../google-vertex-auth-google-auth-library';
|
|
4
4
|
import {
|
|
5
5
|
createGoogleVertexAnthropic as createVertexAnthropicOriginal,
|
|
6
6
|
type GoogleVertexAnthropicProvider,
|
|
@@ -28,7 +28,8 @@ export function createGoogleVertexAnthropic(
|
|
|
28
28
|
): GoogleVertexAnthropicProvider {
|
|
29
29
|
const generateAuthToken =
|
|
30
30
|
options.generateAuthToken ??
|
|
31
|
-
(
|
|
31
|
+
createAuthTokenGenerator(options.googleAuthOptions);
|
|
32
|
+
|
|
32
33
|
return createVertexAnthropicOriginal({
|
|
33
34
|
...options,
|
|
34
35
|
headers: async () => ({
|
|
@@ -1,26 +1,14 @@
|
|
|
1
1
|
import { GoogleAuth, type GoogleAuthOptions } from 'google-auth-library';
|
|
2
|
-
let authInstance: GoogleAuth | null = null;
|
|
3
|
-
let authOptions: GoogleAuthOptions | null = null;
|
|
4
2
|
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
});
|
|
11
|
-
authOptions = options;
|
|
12
|
-
}
|
|
13
|
-
return authInstance;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export async function generateAuthToken(options?: GoogleAuthOptions) {
|
|
17
|
-
const auth = getAuth(options || {});
|
|
18
|
-
const client = await auth.getClient();
|
|
19
|
-
const token = await client.getAccessToken();
|
|
20
|
-
return token?.token || null;
|
|
21
|
-
}
|
|
3
|
+
export function createAuthTokenGenerator(options?: GoogleAuthOptions) {
|
|
4
|
+
const auth = new GoogleAuth({
|
|
5
|
+
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
|
|
6
|
+
...options,
|
|
7
|
+
});
|
|
22
8
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
9
|
+
return async function generateAuthToken() {
|
|
10
|
+
const client = await auth.getClient();
|
|
11
|
+
const token = await client.getAccessToken();
|
|
12
|
+
return token?.token ?? null;
|
|
13
|
+
};
|
|
26
14
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { loadOptionalSetting, resolve } from '@ai-sdk/provider-utils';
|
|
2
2
|
import type { GoogleAuthOptions } from 'google-auth-library';
|
|
3
|
-
import {
|
|
3
|
+
import { createAuthTokenGenerator } from './google-vertex-auth-google-auth-library';
|
|
4
4
|
import {
|
|
5
5
|
createGoogleVertex as createGoogleVertexOriginal,
|
|
6
6
|
type GoogleVertexProvider,
|
|
@@ -30,12 +30,12 @@ export function createGoogleVertex(
|
|
|
30
30
|
return createGoogleVertexOriginal(options);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
const generateAuthToken = createAuthTokenGenerator(options.googleAuthOptions);
|
|
34
|
+
|
|
33
35
|
return createGoogleVertexOriginal({
|
|
34
36
|
...options,
|
|
35
37
|
headers: async () => ({
|
|
36
|
-
Authorization: `Bearer ${await generateAuthToken(
|
|
37
|
-
options.googleAuthOptions,
|
|
38
|
-
)}`,
|
|
38
|
+
Authorization: `Bearer ${await generateAuthToken()}`,
|
|
39
39
|
...(await resolve(options.headers)),
|
|
40
40
|
}),
|
|
41
41
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { resolve, type FetchFunction } from '@ai-sdk/provider-utils';
|
|
2
2
|
import type { GoogleAuthOptions } from 'google-auth-library';
|
|
3
|
-
import {
|
|
3
|
+
import { createAuthTokenGenerator } from '../google-vertex-auth-google-auth-library';
|
|
4
4
|
import {
|
|
5
5
|
createGoogleVertexMaas as createVertexMaasOriginal,
|
|
6
6
|
type GoogleVertexMaasProvider,
|
|
@@ -28,9 +28,11 @@ export interface GoogleVertexMaasProviderSettings extends GoogleVertexMaasProvid
|
|
|
28
28
|
export function createGoogleVertexMaas(
|
|
29
29
|
options: GoogleVertexMaasProviderSettings = {},
|
|
30
30
|
): GoogleVertexMaasProvider {
|
|
31
|
+
const generateAuthToken = createAuthTokenGenerator(options.googleAuthOptions);
|
|
32
|
+
|
|
31
33
|
// Create a custom fetch wrapper that adds auth headers
|
|
32
34
|
const customFetch: FetchFunction = async (url, init) => {
|
|
33
|
-
const token = await generateAuthToken(
|
|
35
|
+
const token = await generateAuthToken();
|
|
34
36
|
const resolvedHeaders = await resolve(options.headers);
|
|
35
37
|
const authHeaders = {
|
|
36
38
|
...resolvedHeaders,
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { resolve, type FetchFunction } from '@ai-sdk/provider-utils';
|
|
2
|
+
import {
|
|
3
|
+
generateAuthToken,
|
|
4
|
+
type GoogleCredentials,
|
|
5
|
+
} from '../../edge/google-vertex-auth-edge';
|
|
6
|
+
import {
|
|
7
|
+
createGoogleVertexXai as createGoogleVertexXaiOriginal,
|
|
8
|
+
type GoogleVertexXaiProvider,
|
|
9
|
+
type GoogleVertexXaiProviderSettings as GoogleVertexXaiProviderSettingsOriginal,
|
|
10
|
+
} from '../google-vertex-xai-provider';
|
|
11
|
+
export type { GoogleVertexXaiProvider };
|
|
12
|
+
|
|
13
|
+
export interface GoogleVertexXaiProviderSettings extends GoogleVertexXaiProviderSettingsOriginal {
|
|
14
|
+
/**
|
|
15
|
+
* Optional. The Google credentials for the Google Cloud service account. If
|
|
16
|
+
* not provided, the Google Vertex provider will use environment variables to
|
|
17
|
+
* load the credentials.
|
|
18
|
+
*/
|
|
19
|
+
googleCredentials?: GoogleCredentials;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Create a Google Vertex AI xAI provider instance for Edge runtimes.
|
|
24
|
+
* Uses the OpenAI-compatible Chat Completions API for Grok partner models.
|
|
25
|
+
* Automatically handles Google Cloud authentication.
|
|
26
|
+
*
|
|
27
|
+
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok
|
|
28
|
+
*/
|
|
29
|
+
export function createGoogleVertexXai(
|
|
30
|
+
options: GoogleVertexXaiProviderSettings = {},
|
|
31
|
+
): GoogleVertexXaiProvider {
|
|
32
|
+
const customFetch: FetchFunction = async (url, init) => {
|
|
33
|
+
const token = await generateAuthToken(options.googleCredentials);
|
|
34
|
+
const resolvedHeaders = await resolve(options.headers);
|
|
35
|
+
const authHeaders = {
|
|
36
|
+
...resolvedHeaders,
|
|
37
|
+
Authorization: `Bearer ${token}`,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const fetchInit = {
|
|
41
|
+
...init,
|
|
42
|
+
headers: {
|
|
43
|
+
...init?.headers,
|
|
44
|
+
...authHeaders,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return (options.fetch ?? fetch)(url, fetchInit);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return createGoogleVertexXaiOriginal({
|
|
52
|
+
...options,
|
|
53
|
+
fetch: customFetch,
|
|
54
|
+
headers: undefined,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Default Google Vertex AI xAI provider instance for Edge runtimes.
|
|
60
|
+
*/
|
|
61
|
+
export const googleVertexXai = createGoogleVertexXai();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createGoogleVertexXai,
|
|
3
|
+
googleVertexXai,
|
|
4
|
+
} from './google-vertex-xai-provider-edge';
|
|
5
|
+
export type {
|
|
6
|
+
GoogleVertexXaiProvider,
|
|
7
|
+
GoogleVertexXaiProviderSettings,
|
|
8
|
+
} from './google-vertex-xai-provider-edge';
|
|
9
|
+
export type { GoogleVertexXaiModelId } from '../google-vertex-xai-options';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok
|
|
2
|
+
export type GoogleVertexXaiModelId =
|
|
3
|
+
| 'xai/grok-4.20-reasoning'
|
|
4
|
+
| 'xai/grok-4.20-non-reasoning'
|
|
5
|
+
| 'xai/grok-4.1-fast-reasoning'
|
|
6
|
+
| 'xai/grok-4.1-fast-non-reasoning'
|
|
7
|
+
| (string & {});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { resolve, type FetchFunction } from '@ai-sdk/provider-utils';
|
|
2
|
+
import type { GoogleAuthOptions } from 'google-auth-library';
|
|
3
|
+
import { createAuthTokenGenerator } from '../google-vertex-auth-google-auth-library';
|
|
4
|
+
import {
|
|
5
|
+
createGoogleVertexXai as createGoogleVertexXaiOriginal,
|
|
6
|
+
type GoogleVertexXaiProvider,
|
|
7
|
+
type GoogleVertexXaiProviderSettings as GoogleVertexXaiProviderSettingsOriginal,
|
|
8
|
+
} from './google-vertex-xai-provider';
|
|
9
|
+
export type { GoogleVertexXaiProvider };
|
|
10
|
+
|
|
11
|
+
export interface GoogleVertexXaiProviderSettings extends GoogleVertexXaiProviderSettingsOriginal {
|
|
12
|
+
/**
|
|
13
|
+
* Optional. The Authentication options provided by google-auth-library.
|
|
14
|
+
* Complete list of authentication options is documented in the
|
|
15
|
+
* GoogleAuthOptions interface:
|
|
16
|
+
* https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.
|
|
17
|
+
*/
|
|
18
|
+
googleAuthOptions?: GoogleAuthOptions;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Create a Google Vertex AI xAI provider instance for Node.js.
|
|
23
|
+
* Uses the OpenAI-compatible Chat Completions API for Grok partner models.
|
|
24
|
+
* Automatically handles Google Cloud authentication.
|
|
25
|
+
*
|
|
26
|
+
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok
|
|
27
|
+
*/
|
|
28
|
+
export function createGoogleVertexXai(
|
|
29
|
+
options: GoogleVertexXaiProviderSettings = {},
|
|
30
|
+
): GoogleVertexXaiProvider {
|
|
31
|
+
const generateAuthToken = createAuthTokenGenerator(options.googleAuthOptions);
|
|
32
|
+
|
|
33
|
+
const customFetch: FetchFunction = async (url, init) => {
|
|
34
|
+
const token = await generateAuthToken();
|
|
35
|
+
const resolvedHeaders = await resolve(options.headers);
|
|
36
|
+
const authHeaders = {
|
|
37
|
+
...resolvedHeaders,
|
|
38
|
+
Authorization: `Bearer ${token}`,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const fetchInit = {
|
|
42
|
+
...init,
|
|
43
|
+
headers: {
|
|
44
|
+
...init?.headers,
|
|
45
|
+
...authHeaders,
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return (options.fetch ?? fetch)(url, fetchInit);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return createGoogleVertexXaiOriginal({
|
|
53
|
+
...options,
|
|
54
|
+
fetch: customFetch,
|
|
55
|
+
headers: undefined,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Default Google Vertex AI xAI provider instance for Node.js.
|
|
61
|
+
*/
|
|
62
|
+
export const googleVertexXai = createGoogleVertexXai();
|