@ai-sdk/google-vertex 4.0.115 → 4.0.117
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 +18 -0
- 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.js +1 -1
- package/dist/maas/edge/index.mjs +1 -1
- package/dist/xai/edge/index.d.mts +92 -0
- package/dist/xai/edge/index.mjs +259 -0
- package/dist/xai/edge/index.mjs.map +1 -0
- package/dist/xai/index.d.mts +76 -0
- package/dist/xai/index.mjs +164 -0
- package/dist/xai/index.mjs.map +1 -0
- package/docs/16-google-vertex.mdx +183 -4
- package/package.json +20 -8
- 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 +60 -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,212 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NoSuchModelError,
|
|
3
|
+
type LanguageModelV3,
|
|
4
|
+
type LanguageModelV3Usage,
|
|
5
|
+
type ProviderV3,
|
|
6
|
+
} from '@ai-sdk/provider';
|
|
7
|
+
import {
|
|
8
|
+
createOpenAICompatible,
|
|
9
|
+
type OpenAICompatibleProvider,
|
|
10
|
+
} from '@ai-sdk/openai-compatible';
|
|
11
|
+
import {
|
|
12
|
+
loadOptionalSetting,
|
|
13
|
+
loadSetting,
|
|
14
|
+
withoutTrailingSlash,
|
|
15
|
+
type FetchFunction,
|
|
16
|
+
type Resolvable,
|
|
17
|
+
} from '@ai-sdk/provider-utils';
|
|
18
|
+
import type { GoogleVertexXaiModelId } from './google-vertex-xai-options';
|
|
19
|
+
|
|
20
|
+
export interface GoogleVertexXaiProvider extends ProviderV3 {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a model for text generation.
|
|
23
|
+
*/
|
|
24
|
+
(modelId: GoogleVertexXaiModelId): LanguageModelV3;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates a model for text generation.
|
|
28
|
+
*/
|
|
29
|
+
languageModel(modelId: GoogleVertexXaiModelId): LanguageModelV3;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Creates a chat model for text generation.
|
|
33
|
+
*/
|
|
34
|
+
chatModel(modelId: GoogleVertexXaiModelId): LanguageModelV3;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @deprecated Use `embeddingModel` instead.
|
|
38
|
+
*/
|
|
39
|
+
textEmbeddingModel(modelId: string): never;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface GoogleVertexXaiProviderSettings {
|
|
43
|
+
/**
|
|
44
|
+
* Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.
|
|
45
|
+
*/
|
|
46
|
+
project?: string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Google Cloud location/region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.
|
|
50
|
+
* Use 'global' for the global endpoint.
|
|
51
|
+
*/
|
|
52
|
+
location?: string;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Base URL for the API calls. If not provided, will be constructed from project and location.
|
|
56
|
+
*/
|
|
57
|
+
baseURL?: string;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Headers to use for requests. Can be:
|
|
61
|
+
* - A headers object
|
|
62
|
+
* - A Promise that resolves to a headers object
|
|
63
|
+
* - A function that returns a headers object
|
|
64
|
+
* - A function that returns a Promise of a headers object
|
|
65
|
+
*/
|
|
66
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
70
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
71
|
+
*/
|
|
72
|
+
fetch?: FetchFunction;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type GoogleVertexXaiUsage =
|
|
76
|
+
| {
|
|
77
|
+
prompt_tokens?: number | null;
|
|
78
|
+
completion_tokens?: number | null;
|
|
79
|
+
prompt_tokens_details?: {
|
|
80
|
+
cached_tokens?: number | null;
|
|
81
|
+
} | null;
|
|
82
|
+
completion_tokens_details?: {
|
|
83
|
+
reasoning_tokens?: number | null;
|
|
84
|
+
} | null;
|
|
85
|
+
}
|
|
86
|
+
| undefined
|
|
87
|
+
| null;
|
|
88
|
+
|
|
89
|
+
function convertGoogleVertexXaiUsage(
|
|
90
|
+
usage: GoogleVertexXaiUsage,
|
|
91
|
+
): LanguageModelV3Usage {
|
|
92
|
+
if (usage == null) {
|
|
93
|
+
return {
|
|
94
|
+
inputTokens: {
|
|
95
|
+
total: undefined,
|
|
96
|
+
noCache: undefined,
|
|
97
|
+
cacheRead: undefined,
|
|
98
|
+
cacheWrite: undefined,
|
|
99
|
+
},
|
|
100
|
+
outputTokens: {
|
|
101
|
+
total: undefined,
|
|
102
|
+
text: undefined,
|
|
103
|
+
reasoning: undefined,
|
|
104
|
+
},
|
|
105
|
+
raw: undefined,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const promptTokens = usage.prompt_tokens ?? 0;
|
|
110
|
+
const completionTokens = usage.completion_tokens ?? 0;
|
|
111
|
+
const cacheReadTokens = usage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
112
|
+
const reasoningTokens =
|
|
113
|
+
usage.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
inputTokens: {
|
|
117
|
+
total: promptTokens,
|
|
118
|
+
noCache: promptTokens - cacheReadTokens,
|
|
119
|
+
cacheRead: cacheReadTokens,
|
|
120
|
+
cacheWrite: undefined,
|
|
121
|
+
},
|
|
122
|
+
outputTokens: {
|
|
123
|
+
total: completionTokens + reasoningTokens,
|
|
124
|
+
text: completionTokens,
|
|
125
|
+
reasoning: reasoningTokens,
|
|
126
|
+
},
|
|
127
|
+
raw: usage,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function transformGoogleVertexXaiRequestBody(args: Record<string, any>) {
|
|
132
|
+
const { reasoning_effort: _reasoningEffort, ...rest } = args;
|
|
133
|
+
return rest;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Create a Google Vertex AI xAI provider instance.
|
|
138
|
+
* Uses the OpenAI-compatible Chat Completions API for Grok partner models.
|
|
139
|
+
*
|
|
140
|
+
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok
|
|
141
|
+
*/
|
|
142
|
+
export function createGoogleVertexXai(
|
|
143
|
+
options: GoogleVertexXaiProviderSettings = {},
|
|
144
|
+
): GoogleVertexXaiProvider {
|
|
145
|
+
const loadLocation = () =>
|
|
146
|
+
loadOptionalSetting({
|
|
147
|
+
settingValue: options.location,
|
|
148
|
+
environmentVariableName: 'GOOGLE_VERTEX_LOCATION',
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const loadProject = () =>
|
|
152
|
+
loadSetting({
|
|
153
|
+
settingValue: options.project,
|
|
154
|
+
settingName: 'project',
|
|
155
|
+
environmentVariableName: 'GOOGLE_VERTEX_PROJECT',
|
|
156
|
+
description: 'Google Vertex project',
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const constructBaseURL = () => {
|
|
160
|
+
const projectId = loadProject();
|
|
161
|
+
const location = loadLocation() ?? 'global';
|
|
162
|
+
|
|
163
|
+
return `https://aiplatform.googleapis.com/v1/projects/${projectId}/locations/${location}/endpoints/openapi`;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const loadBaseURL = () =>
|
|
167
|
+
withoutTrailingSlash(options.baseURL ?? '') || constructBaseURL();
|
|
168
|
+
|
|
169
|
+
let cachedProvider:
|
|
170
|
+
| OpenAICompatibleProvider<GoogleVertexXaiModelId, string, string, string>
|
|
171
|
+
| undefined;
|
|
172
|
+
const getProvider = () =>
|
|
173
|
+
(cachedProvider ??= createOpenAICompatible({
|
|
174
|
+
name: 'googleVertex.xai',
|
|
175
|
+
baseURL: loadBaseURL(),
|
|
176
|
+
fetch: options.fetch,
|
|
177
|
+
includeUsage: true,
|
|
178
|
+
supportsStructuredOutputs: true,
|
|
179
|
+
supportedUrls: () => ({
|
|
180
|
+
'image/*': [/^https?:\/\/.*$/],
|
|
181
|
+
}),
|
|
182
|
+
transformRequestBody: transformGoogleVertexXaiRequestBody,
|
|
183
|
+
convertUsage: convertGoogleVertexXaiUsage,
|
|
184
|
+
}));
|
|
185
|
+
|
|
186
|
+
const createChatModel = (modelId: GoogleVertexXaiModelId) =>
|
|
187
|
+
getProvider().languageModel(modelId);
|
|
188
|
+
|
|
189
|
+
const provider = function (modelId: GoogleVertexXaiModelId) {
|
|
190
|
+
if (new.target) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
'The Google Vertex xAI model function cannot be called with the new keyword.',
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return createChatModel(modelId);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
provider.specificationVersion = 'v3' as const;
|
|
200
|
+
provider.languageModel = createChatModel;
|
|
201
|
+
provider.chatModel = (modelId: GoogleVertexXaiModelId) =>
|
|
202
|
+
getProvider().chatModel(modelId);
|
|
203
|
+
provider.embeddingModel = (modelId: string): never => {
|
|
204
|
+
throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });
|
|
205
|
+
};
|
|
206
|
+
provider.textEmbeddingModel = provider.embeddingModel;
|
|
207
|
+
provider.imageModel = (modelId: string): never => {
|
|
208
|
+
throw new NoSuchModelError({ modelId, modelType: 'imageModel' });
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
return provider;
|
|
212
|
+
}
|
package/src/xai/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createGoogleVertexXai,
|
|
3
|
+
googleVertexXai,
|
|
4
|
+
} from './google-vertex-xai-provider-node';
|
|
5
|
+
export type {
|
|
6
|
+
GoogleVertexXaiProvider,
|
|
7
|
+
GoogleVertexXaiProviderSettings,
|
|
8
|
+
} from './google-vertex-xai-provider-node';
|
|
9
|
+
export type { GoogleVertexXaiModelId } from './google-vertex-xai-options';
|
package/xai/edge.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/xai/edge';
|
package/xai/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/xai/index';
|