@ai-sdk/google 4.0.0-beta.3 → 4.0.0-beta.30
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 +201 -4
- package/README.md +2 -0
- package/dist/index.d.mts +73 -23
- package/dist/index.d.ts +73 -23
- package/dist/index.js +756 -136
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +767 -130
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +42 -15
- package/dist/internal/index.d.ts +42 -15
- package/dist/internal/index.js +499 -74
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +504 -72
- package/dist/internal/index.mjs.map +1 -1
- package/docs/15-google-generative-ai.mdx +36 -3
- package/package.json +3 -5
- package/src/convert-google-generative-ai-usage.ts +9 -2
- package/src/convert-to-google-generative-ai-messages.ts +330 -50
- package/src/google-generative-ai-embedding-model.ts +42 -13
- package/src/google-generative-ai-embedding-options.ts +24 -0
- package/src/google-generative-ai-files.ts +230 -0
- package/src/google-generative-ai-image-model.ts +14 -14
- package/src/google-generative-ai-language-model.ts +317 -44
- package/src/google-generative-ai-options.ts +5 -1
- package/src/google-generative-ai-prompt.ts +48 -4
- package/src/google-generative-ai-video-model.ts +7 -7
- package/src/google-prepare-tools.ts +64 -9
- package/src/google-provider.ts +31 -18
- package/src/index.ts +1 -0
- package/src/map-google-generative-ai-finish-reason.ts +2 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AISDKError,
|
|
3
|
-
type
|
|
4
|
-
type
|
|
3
|
+
type Experimental_VideoModelV4,
|
|
4
|
+
type SharedV4Warning,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import {
|
|
7
7
|
combineHeaders,
|
|
@@ -50,8 +50,8 @@ interface GoogleGenerativeAIVideoModelConfig {
|
|
|
50
50
|
};
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
export class GoogleGenerativeAIVideoModel implements
|
|
54
|
-
readonly specificationVersion = '
|
|
53
|
+
export class GoogleGenerativeAIVideoModel implements Experimental_VideoModelV4 {
|
|
54
|
+
readonly specificationVersion = 'v4';
|
|
55
55
|
|
|
56
56
|
get provider(): string {
|
|
57
57
|
return this.config.provider;
|
|
@@ -68,10 +68,10 @@ export class GoogleGenerativeAIVideoModel implements Experimental_VideoModelV3 {
|
|
|
68
68
|
) {}
|
|
69
69
|
|
|
70
70
|
async doGenerate(
|
|
71
|
-
options: Parameters<
|
|
72
|
-
): Promise<Awaited<ReturnType<
|
|
71
|
+
options: Parameters<Experimental_VideoModelV4['doGenerate']>[0],
|
|
72
|
+
): Promise<Awaited<ReturnType<Experimental_VideoModelV4['doGenerate']>>> {
|
|
73
73
|
const currentDate = this.config._internal?.currentDate?.() ?? new Date();
|
|
74
|
-
const warnings:
|
|
74
|
+
const warnings: SharedV4Warning[] = [];
|
|
75
75
|
|
|
76
76
|
const googleOptions = (await parseProviderOptions({
|
|
77
77
|
provider: 'google',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
LanguageModelV4CallOptions,
|
|
3
|
+
SharedV4Warning,
|
|
4
4
|
UnsupportedFunctionalityError,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import { convertJSONSchemaToOpenAPISchema } from './convert-json-schema-to-openapi-schema';
|
|
@@ -11,8 +11,8 @@ export function prepareTools({
|
|
|
11
11
|
toolChoice,
|
|
12
12
|
modelId,
|
|
13
13
|
}: {
|
|
14
|
-
tools:
|
|
15
|
-
toolChoice?:
|
|
14
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
15
|
+
toolChoice?: LanguageModelV4CallOptions['toolChoice'];
|
|
16
16
|
modelId: GoogleGenerativeAIModelId;
|
|
17
17
|
}): {
|
|
18
18
|
tools:
|
|
@@ -30,17 +30,18 @@ export function prepareTools({
|
|
|
30
30
|
toolConfig:
|
|
31
31
|
| undefined
|
|
32
32
|
| {
|
|
33
|
-
functionCallingConfig
|
|
33
|
+
functionCallingConfig?: {
|
|
34
34
|
mode: 'AUTO' | 'NONE' | 'ANY' | 'VALIDATED';
|
|
35
35
|
allowedFunctionNames?: string[];
|
|
36
36
|
};
|
|
37
|
+
includeServerSideToolInvocations?: boolean;
|
|
37
38
|
};
|
|
38
|
-
toolWarnings:
|
|
39
|
+
toolWarnings: SharedV4Warning[];
|
|
39
40
|
} {
|
|
40
41
|
// when the tools array is empty, change it to undefined to prevent errors:
|
|
41
42
|
tools = tools?.length ? tools : undefined;
|
|
42
43
|
|
|
43
|
-
const toolWarnings:
|
|
44
|
+
const toolWarnings: SharedV4Warning[] = [];
|
|
44
45
|
|
|
45
46
|
const isLatest = (
|
|
46
47
|
[
|
|
@@ -54,6 +55,7 @@ export function prepareTools({
|
|
|
54
55
|
modelId.includes('gemini-3') ||
|
|
55
56
|
modelId.includes('nano-banana') ||
|
|
56
57
|
isLatest;
|
|
58
|
+
const isGemini3orNewer = modelId.includes('gemini-3');
|
|
57
59
|
const supportsFileSearch =
|
|
58
60
|
modelId.includes('gemini-2.5') || modelId.includes('gemini-3');
|
|
59
61
|
|
|
@@ -65,7 +67,7 @@ export function prepareTools({
|
|
|
65
67
|
const hasFunctionTools = tools.some(tool => tool.type === 'function');
|
|
66
68
|
const hasProviderTools = tools.some(tool => tool.type === 'provider');
|
|
67
69
|
|
|
68
|
-
if (hasFunctionTools && hasProviderTools) {
|
|
70
|
+
if (hasFunctionTools && hasProviderTools && !isGemini3orNewer) {
|
|
69
71
|
toolWarnings.push({
|
|
70
72
|
type: 'unsupported',
|
|
71
73
|
feature: `combination of function and provider-defined tools`,
|
|
@@ -120,7 +122,7 @@ export function prepareTools({
|
|
|
120
122
|
type: 'unsupported',
|
|
121
123
|
feature: `provider-defined tool ${tool.id}`,
|
|
122
124
|
details:
|
|
123
|
-
'The code execution
|
|
125
|
+
'The code execution tool is not supported with other Gemini models than Gemini 2.',
|
|
124
126
|
});
|
|
125
127
|
}
|
|
126
128
|
break;
|
|
@@ -178,6 +180,59 @@ export function prepareTools({
|
|
|
178
180
|
}
|
|
179
181
|
});
|
|
180
182
|
|
|
183
|
+
if (hasFunctionTools && isGemini3orNewer && googleTools.length > 0) {
|
|
184
|
+
const functionDeclarations: Array<{
|
|
185
|
+
name: string;
|
|
186
|
+
description: string;
|
|
187
|
+
parameters: unknown;
|
|
188
|
+
}> = [];
|
|
189
|
+
for (const tool of tools) {
|
|
190
|
+
if (tool.type === 'function') {
|
|
191
|
+
functionDeclarations.push({
|
|
192
|
+
name: tool.name,
|
|
193
|
+
description: tool.description ?? '',
|
|
194
|
+
parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema),
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const combinedToolConfig: {
|
|
200
|
+
functionCallingConfig: {
|
|
201
|
+
mode: 'VALIDATED' | 'ANY' | 'NONE';
|
|
202
|
+
allowedFunctionNames?: string[];
|
|
203
|
+
};
|
|
204
|
+
includeServerSideToolInvocations: true;
|
|
205
|
+
} = {
|
|
206
|
+
functionCallingConfig: { mode: 'VALIDATED' },
|
|
207
|
+
includeServerSideToolInvocations: true,
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
if (toolChoice != null) {
|
|
211
|
+
switch (toolChoice.type) {
|
|
212
|
+
case 'auto':
|
|
213
|
+
break;
|
|
214
|
+
case 'none':
|
|
215
|
+
combinedToolConfig.functionCallingConfig = { mode: 'NONE' };
|
|
216
|
+
break;
|
|
217
|
+
case 'required':
|
|
218
|
+
combinedToolConfig.functionCallingConfig = { mode: 'ANY' };
|
|
219
|
+
break;
|
|
220
|
+
case 'tool':
|
|
221
|
+
combinedToolConfig.functionCallingConfig = {
|
|
222
|
+
mode: 'ANY',
|
|
223
|
+
allowedFunctionNames: [toolChoice.toolName],
|
|
224
|
+
};
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
tools: [...googleTools, { functionDeclarations }],
|
|
231
|
+
toolConfig: combinedToolConfig,
|
|
232
|
+
toolWarnings,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
181
236
|
return {
|
|
182
237
|
tools: googleTools.length > 0 ? googleTools : undefined,
|
|
183
238
|
toolConfig: undefined,
|
package/src/google-provider.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
EmbeddingModelV4,
|
|
3
|
+
Experimental_VideoModelV4,
|
|
4
|
+
FilesV4,
|
|
5
|
+
ImageModelV4,
|
|
6
|
+
LanguageModelV4,
|
|
7
|
+
ProviderV4,
|
|
7
8
|
} from '@ai-sdk/provider';
|
|
8
9
|
import {
|
|
9
10
|
FetchFunction,
|
|
@@ -24,15 +25,16 @@ import {
|
|
|
24
25
|
GoogleGenerativeAIImageModelId,
|
|
25
26
|
} from './google-generative-ai-image-settings';
|
|
26
27
|
import { GoogleGenerativeAIImageModel } from './google-generative-ai-image-model';
|
|
28
|
+
import { GoogleGenerativeAIFiles } from './google-generative-ai-files';
|
|
27
29
|
import { GoogleGenerativeAIVideoModel } from './google-generative-ai-video-model';
|
|
28
30
|
import { GoogleGenerativeAIVideoModelId } from './google-generative-ai-video-settings';
|
|
29
31
|
|
|
30
|
-
export interface GoogleGenerativeAIProvider extends
|
|
31
|
-
(modelId: GoogleGenerativeAIModelId):
|
|
32
|
+
export interface GoogleGenerativeAIProvider extends ProviderV4 {
|
|
33
|
+
(modelId: GoogleGenerativeAIModelId): LanguageModelV4;
|
|
32
34
|
|
|
33
|
-
languageModel(modelId: GoogleGenerativeAIModelId):
|
|
35
|
+
languageModel(modelId: GoogleGenerativeAIModelId): LanguageModelV4;
|
|
34
36
|
|
|
35
|
-
chat(modelId: GoogleGenerativeAIModelId):
|
|
37
|
+
chat(modelId: GoogleGenerativeAIModelId): LanguageModelV4;
|
|
36
38
|
|
|
37
39
|
/**
|
|
38
40
|
* Creates a model for image generation.
|
|
@@ -40,46 +42,48 @@ export interface GoogleGenerativeAIProvider extends ProviderV3 {
|
|
|
40
42
|
image(
|
|
41
43
|
modelId: GoogleGenerativeAIImageModelId,
|
|
42
44
|
settings?: GoogleGenerativeAIImageSettings,
|
|
43
|
-
):
|
|
45
|
+
): ImageModelV4;
|
|
44
46
|
|
|
45
47
|
/**
|
|
46
48
|
* @deprecated Use `chat()` instead.
|
|
47
49
|
*/
|
|
48
|
-
generativeAI(modelId: GoogleGenerativeAIModelId):
|
|
50
|
+
generativeAI(modelId: GoogleGenerativeAIModelId): LanguageModelV4;
|
|
49
51
|
|
|
50
52
|
/**
|
|
51
53
|
* Creates a model for text embeddings.
|
|
52
54
|
*/
|
|
53
|
-
embedding(modelId: GoogleGenerativeAIEmbeddingModelId):
|
|
55
|
+
embedding(modelId: GoogleGenerativeAIEmbeddingModelId): EmbeddingModelV4;
|
|
54
56
|
|
|
55
57
|
/**
|
|
56
58
|
* Creates a model for text embeddings.
|
|
57
59
|
*/
|
|
58
|
-
embeddingModel(modelId: GoogleGenerativeAIEmbeddingModelId):
|
|
60
|
+
embeddingModel(modelId: GoogleGenerativeAIEmbeddingModelId): EmbeddingModelV4;
|
|
59
61
|
|
|
60
62
|
/**
|
|
61
63
|
* @deprecated Use `embedding` instead.
|
|
62
64
|
*/
|
|
63
|
-
textEmbedding(modelId: GoogleGenerativeAIEmbeddingModelId):
|
|
65
|
+
textEmbedding(modelId: GoogleGenerativeAIEmbeddingModelId): EmbeddingModelV4;
|
|
64
66
|
|
|
65
67
|
/**
|
|
66
68
|
* @deprecated Use `embeddingModel` instead.
|
|
67
69
|
*/
|
|
68
70
|
textEmbeddingModel(
|
|
69
71
|
modelId: GoogleGenerativeAIEmbeddingModelId,
|
|
70
|
-
):
|
|
72
|
+
): EmbeddingModelV4;
|
|
71
73
|
|
|
72
74
|
/**
|
|
73
75
|
* Creates a model for video generation.
|
|
74
76
|
*/
|
|
75
|
-
video(modelId: GoogleGenerativeAIVideoModelId):
|
|
77
|
+
video(modelId: GoogleGenerativeAIVideoModelId): Experimental_VideoModelV4;
|
|
76
78
|
|
|
77
79
|
/**
|
|
78
80
|
* Creates a model for video generation.
|
|
79
81
|
*/
|
|
80
82
|
videoModel(
|
|
81
83
|
modelId: GoogleGenerativeAIVideoModelId,
|
|
82
|
-
):
|
|
84
|
+
): Experimental_VideoModelV4;
|
|
85
|
+
|
|
86
|
+
files(): FilesV4;
|
|
83
87
|
|
|
84
88
|
tools: typeof googleTools;
|
|
85
89
|
}
|
|
@@ -185,6 +189,14 @@ export function createGoogleGenerativeAI(
|
|
|
185
189
|
fetch: options.fetch,
|
|
186
190
|
});
|
|
187
191
|
|
|
192
|
+
const createFiles = () =>
|
|
193
|
+
new GoogleGenerativeAIFiles({
|
|
194
|
+
provider: providerName,
|
|
195
|
+
baseURL,
|
|
196
|
+
headers: getHeaders,
|
|
197
|
+
fetch: options.fetch,
|
|
198
|
+
});
|
|
199
|
+
|
|
188
200
|
const createVideoModel = (modelId: GoogleGenerativeAIVideoModelId) =>
|
|
189
201
|
new GoogleGenerativeAIVideoModel(modelId, {
|
|
190
202
|
provider: providerName,
|
|
@@ -204,7 +216,7 @@ export function createGoogleGenerativeAI(
|
|
|
204
216
|
return createChatModel(modelId);
|
|
205
217
|
};
|
|
206
218
|
|
|
207
|
-
provider.specificationVersion = '
|
|
219
|
+
provider.specificationVersion = 'v4' as const;
|
|
208
220
|
provider.languageModel = createChatModel;
|
|
209
221
|
provider.chat = createChatModel;
|
|
210
222
|
provider.generativeAI = createChatModel;
|
|
@@ -216,6 +228,7 @@ export function createGoogleGenerativeAI(
|
|
|
216
228
|
provider.imageModel = createImageModel;
|
|
217
229
|
provider.video = createVideoModel;
|
|
218
230
|
provider.videoModel = createVideoModel;
|
|
231
|
+
provider.files = createFiles;
|
|
219
232
|
provider.tools = googleTools;
|
|
220
233
|
|
|
221
234
|
return provider as GoogleGenerativeAIProvider;
|
package/src/index.ts
CHANGED
|
@@ -21,6 +21,7 @@ export type {
|
|
|
21
21
|
GoogleVideoModelOptions as GoogleGenerativeAIVideoProviderOptions,
|
|
22
22
|
} from './google-generative-ai-video-model';
|
|
23
23
|
export type { GoogleGenerativeAIVideoModelId } from './google-generative-ai-video-settings';
|
|
24
|
+
export type { GoogleFilesUploadOptions } from './google-generative-ai-files';
|
|
24
25
|
export { createGoogleGenerativeAI, google } from './google-provider';
|
|
25
26
|
export type {
|
|
26
27
|
GoogleGenerativeAIProvider,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export function mapGoogleGenerativeAIFinishReason({
|
|
4
4
|
finishReason,
|
|
@@ -6,7 +6,7 @@ export function mapGoogleGenerativeAIFinishReason({
|
|
|
6
6
|
}: {
|
|
7
7
|
finishReason: string | null | undefined;
|
|
8
8
|
hasToolCalls: boolean;
|
|
9
|
-
}):
|
|
9
|
+
}): LanguageModelV4FinishReason['unified'] {
|
|
10
10
|
switch (finishReason) {
|
|
11
11
|
case 'STOP':
|
|
12
12
|
return hasToolCalls ? 'tool-calls' : 'stop';
|