@ai-sdk/google 4.0.0-canary.70 → 4.0.0-canary.72
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/index.d.ts +12 -0
- package/dist/index.js +48 -15
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +1 -0
- package/dist/internal/index.js +5 -6
- package/dist/internal/index.js.map +1 -1
- package/docs/15-google.mdx +24 -4
- package/package.json +1 -1
- package/src/convert-google-usage.ts +1 -0
- package/src/google-image-model-options.ts +12 -0
- package/src/google-image-model.ts +42 -3
- package/src/google-language-model.ts +3 -4
- package/src/tool/google-search.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @ai-sdk/google
|
|
2
2
|
|
|
3
|
+
## 4.0.0-canary.72
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- b71c0d7: feat(provider/google): support Google search grounding when using `generateImage` with Gemini
|
|
8
|
+
|
|
9
|
+
## 4.0.0-canary.71
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 045d2e8: fix(google): read `serviceTier` from `usageMetadata.serviceTier` in both generate and stream paths
|
|
14
|
+
|
|
15
|
+
The previous implementation read `serviceTier` from the `x-gemini-service-tier`
|
|
16
|
+
response header, which is only populated on non-streaming responses. Gemini
|
|
17
|
+
streaming includes the value in `usageMetadata.serviceTier` on every chunk, so
|
|
18
|
+
`providerMetadata.google.serviceTier` was always `null` for streams. Read from
|
|
19
|
+
`usageMetadata` for both paths instead.
|
|
20
|
+
|
|
3
21
|
## 4.0.0-canary.70
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -171,6 +171,7 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
|
|
|
171
171
|
candidatesTokenCount?: number | null | undefined;
|
|
172
172
|
totalTokenCount?: number | null | undefined;
|
|
173
173
|
trafficType?: string | null | undefined;
|
|
174
|
+
serviceTier?: string | null | undefined;
|
|
174
175
|
promptTokensDetails?: {
|
|
175
176
|
modality: string;
|
|
176
177
|
tokenCount: number;
|
|
@@ -216,6 +217,17 @@ interface GoogleProviderMetadata {
|
|
|
216
217
|
declare const googleImageModelOptionsSchema: _ai_sdk_provider_utils.LazySchema<{
|
|
217
218
|
personGeneration?: "dont_allow" | "allow_adult" | "allow_all" | null | undefined;
|
|
218
219
|
aspectRatio?: "1:1" | "3:4" | "4:3" | "9:16" | "16:9" | null | undefined;
|
|
220
|
+
googleSearch?: {
|
|
221
|
+
[x: string]: unknown;
|
|
222
|
+
searchTypes?: {
|
|
223
|
+
webSearch?: Record<string, never> | undefined;
|
|
224
|
+
imageSearch?: Record<string, never> | undefined;
|
|
225
|
+
} | undefined;
|
|
226
|
+
timeRangeFilter?: {
|
|
227
|
+
startTime: string;
|
|
228
|
+
endTime: string;
|
|
229
|
+
} | undefined;
|
|
230
|
+
} | undefined;
|
|
219
231
|
}>;
|
|
220
232
|
type GoogleImageModelOptions = InferSchema<typeof googleImageModelOptionsSchema>;
|
|
221
233
|
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from "@ai-sdk/provider-utils";
|
|
8
8
|
|
|
9
9
|
// src/version.ts
|
|
10
|
-
var VERSION = true ? "4.0.0-canary.
|
|
10
|
+
var VERSION = true ? "4.0.0-canary.72" : "0.0.0-test";
|
|
11
11
|
|
|
12
12
|
// src/google-embedding-model.ts
|
|
13
13
|
import {
|
|
@@ -1855,7 +1855,7 @@ var GoogleLanguageModel = class _GoogleLanguageModel {
|
|
|
1855
1855
|
safetyRatings: (_p = candidate.safetyRatings) != null ? _p : null,
|
|
1856
1856
|
usageMetadata: usageMetadata != null ? usageMetadata : null,
|
|
1857
1857
|
finishMessage: (_q = candidate.finishMessage) != null ? _q : null,
|
|
1858
|
-
serviceTier: (_r =
|
|
1858
|
+
serviceTier: (_r = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _r : null
|
|
1859
1859
|
}),
|
|
1860
1860
|
request: { body: args },
|
|
1861
1861
|
response: {
|
|
@@ -1866,7 +1866,6 @@ var GoogleLanguageModel = class _GoogleLanguageModel {
|
|
|
1866
1866
|
};
|
|
1867
1867
|
}
|
|
1868
1868
|
async doStream(options) {
|
|
1869
|
-
var _a;
|
|
1870
1869
|
const { args, warnings, providerOptionsNames, extraHeaders } = await this.getArgs(options, { isStreaming: true });
|
|
1871
1870
|
const wrapProviderMetadata = (payload) => Object.fromEntries(
|
|
1872
1871
|
providerOptionsNames.map((name) => [name, payload])
|
|
@@ -1895,7 +1894,6 @@ var GoogleLanguageModel = class _GoogleLanguageModel {
|
|
|
1895
1894
|
let providerMetadata = void 0;
|
|
1896
1895
|
let lastGroundingMetadata = null;
|
|
1897
1896
|
let lastUrlContextMetadata = null;
|
|
1898
|
-
const serviceTier = (_a = responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"]) != null ? _a : null;
|
|
1899
1897
|
const generateId3 = this.config.generateId;
|
|
1900
1898
|
let hasToolCalls = false;
|
|
1901
1899
|
let currentTextBlockId = null;
|
|
@@ -1940,7 +1938,7 @@ var GoogleLanguageModel = class _GoogleLanguageModel {
|
|
|
1940
1938
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1941
1939
|
},
|
|
1942
1940
|
transform(chunk, controller) {
|
|
1943
|
-
var
|
|
1941
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
1944
1942
|
if (options.includeRawChunks) {
|
|
1945
1943
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1946
1944
|
}
|
|
@@ -1953,7 +1951,7 @@ var GoogleLanguageModel = class _GoogleLanguageModel {
|
|
|
1953
1951
|
if (usageMetadata != null) {
|
|
1954
1952
|
usage = usageMetadata;
|
|
1955
1953
|
}
|
|
1956
|
-
const candidate = (
|
|
1954
|
+
const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
|
|
1957
1955
|
if (candidate == null) {
|
|
1958
1956
|
return;
|
|
1959
1957
|
}
|
|
@@ -2248,7 +2246,7 @@ var GoogleLanguageModel = class _GoogleLanguageModel {
|
|
|
2248
2246
|
safetyRatings: (_n = candidate.safetyRatings) != null ? _n : null,
|
|
2249
2247
|
usageMetadata: usageMetadata != null ? usageMetadata : null,
|
|
2250
2248
|
finishMessage: (_o = candidate.finishMessage) != null ? _o : null,
|
|
2251
|
-
serviceTier
|
|
2249
|
+
serviceTier: (_p = usage == null ? void 0 : usage.serviceTier) != null ? _p : null
|
|
2252
2250
|
});
|
|
2253
2251
|
}
|
|
2254
2252
|
},
|
|
@@ -2575,6 +2573,7 @@ var usageSchema = z5.object({
|
|
|
2575
2573
|
totalTokenCount: z5.number().nullish(),
|
|
2576
2574
|
// https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/GenerateContentResponse#TrafficType
|
|
2577
2575
|
trafficType: z5.string().nullish(),
|
|
2576
|
+
serviceTier: z5.string().nullish(),
|
|
2578
2577
|
// https://ai.google.dev/api/generate-content#Modality
|
|
2579
2578
|
promptTokensDetails: tokenDetailsSchema,
|
|
2580
2579
|
candidatesTokensDetails: tokenDetailsSchema
|
|
@@ -2836,7 +2835,17 @@ var googleImageModelOptionsSchema = lazySchema12(
|
|
|
2836
2835
|
() => zodSchema12(
|
|
2837
2836
|
z13.object({
|
|
2838
2837
|
personGeneration: z13.enum(["dont_allow", "allow_adult", "allow_all"]).nullish(),
|
|
2839
|
-
aspectRatio: z13.enum(["1:1", "3:4", "4:3", "9:16", "16:9"]).nullish()
|
|
2838
|
+
aspectRatio: z13.enum(["1:1", "3:4", "4:3", "9:16", "16:9"]).nullish(),
|
|
2839
|
+
/**
|
|
2840
|
+
* Enable Google Search grounding for Gemini image models. The value is
|
|
2841
|
+
* forwarded as the args of the `google.tools.googleSearch` provider
|
|
2842
|
+
* tool on the underlying language-model call. Pass `{}` for defaults.
|
|
2843
|
+
*
|
|
2844
|
+
* `generateImage` does not accept a `tools` parameter, so this is the
|
|
2845
|
+
* dedicated escape hatch for grounding image generation the same way
|
|
2846
|
+
* `generateText` does.
|
|
2847
|
+
*/
|
|
2848
|
+
googleSearch: googleSearchToolArgsBaseSchema.optional()
|
|
2840
2849
|
})
|
|
2841
2850
|
)
|
|
2842
2851
|
);
|
|
@@ -2928,7 +2937,15 @@ var GoogleImageModel = class _GoogleImageModel {
|
|
|
2928
2937
|
parameters.aspectRatio = aspectRatio;
|
|
2929
2938
|
}
|
|
2930
2939
|
if (googleOptions) {
|
|
2931
|
-
|
|
2940
|
+
const { googleSearch: imagenGoogleSearch, ...imagenOptions } = googleOptions;
|
|
2941
|
+
if (imagenGoogleSearch != null) {
|
|
2942
|
+
warnings.push({
|
|
2943
|
+
type: "unsupported",
|
|
2944
|
+
feature: "googleSearch",
|
|
2945
|
+
details: "Google Search grounding is only supported on Gemini image models."
|
|
2946
|
+
});
|
|
2947
|
+
}
|
|
2948
|
+
Object.assign(parameters, imagenOptions);
|
|
2932
2949
|
}
|
|
2933
2950
|
const body = {
|
|
2934
2951
|
instances: [{ prompt }],
|
|
@@ -2968,7 +2985,7 @@ var GoogleImageModel = class _GoogleImageModel {
|
|
|
2968
2985
|
};
|
|
2969
2986
|
}
|
|
2970
2987
|
async doGenerateGemini(options) {
|
|
2971
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
2988
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
2972
2989
|
const {
|
|
2973
2990
|
prompt,
|
|
2974
2991
|
n,
|
|
@@ -3026,12 +3043,18 @@ var GoogleImageModel = class _GoogleImageModel {
|
|
|
3026
3043
|
const languageModelPrompt = [
|
|
3027
3044
|
{ role: "user", content: userContent }
|
|
3028
3045
|
];
|
|
3046
|
+
const googleImageOptions = await parseProviderOptions3({
|
|
3047
|
+
provider: "google",
|
|
3048
|
+
providerOptions,
|
|
3049
|
+
schema: googleImageModelOptionsSchema
|
|
3050
|
+
});
|
|
3051
|
+
const { googleSearch: _strippedGoogleSearch, ...passthroughGoogleOptions } = (_a = providerOptions == null ? void 0 : providerOptions.google) != null ? _a : {};
|
|
3029
3052
|
const languageModel = new GoogleLanguageModel(this.modelId, {
|
|
3030
3053
|
provider: this.config.provider,
|
|
3031
3054
|
baseURL: this.config.baseURL,
|
|
3032
|
-
headers: (
|
|
3055
|
+
headers: (_b = this.config.headers) != null ? _b : {},
|
|
3033
3056
|
fetch: this.config.fetch,
|
|
3034
|
-
generateId: (
|
|
3057
|
+
generateId: (_c = this.config.generateId) != null ? _c : defaultGenerateId
|
|
3035
3058
|
});
|
|
3036
3059
|
const result = await languageModel.doGenerate({
|
|
3037
3060
|
prompt: languageModelPrompt,
|
|
@@ -3042,9 +3065,17 @@ var GoogleImageModel = class _GoogleImageModel {
|
|
|
3042
3065
|
imageConfig: aspectRatio ? {
|
|
3043
3066
|
aspectRatio
|
|
3044
3067
|
} : void 0,
|
|
3045
|
-
...
|
|
3068
|
+
...passthroughGoogleOptions
|
|
3046
3069
|
}
|
|
3047
3070
|
},
|
|
3071
|
+
tools: (googleImageOptions == null ? void 0 : googleImageOptions.googleSearch) != null ? [
|
|
3072
|
+
{
|
|
3073
|
+
type: "provider",
|
|
3074
|
+
id: "google.google_search",
|
|
3075
|
+
name: "google_search",
|
|
3076
|
+
args: googleImageOptions.googleSearch
|
|
3077
|
+
}
|
|
3078
|
+
] : void 0,
|
|
3048
3079
|
headers,
|
|
3049
3080
|
abortSignal
|
|
3050
3081
|
});
|
|
@@ -3055,23 +3086,25 @@ var GoogleImageModel = class _GoogleImageModel {
|
|
|
3055
3086
|
images.push(convertToBase642(part.data.data));
|
|
3056
3087
|
}
|
|
3057
3088
|
}
|
|
3089
|
+
const languageModelGoogleMetadata = (_h = (_g = result.providerMetadata) == null ? void 0 : _g.google) != null ? _h : {};
|
|
3058
3090
|
return {
|
|
3059
3091
|
images,
|
|
3060
3092
|
warnings,
|
|
3061
3093
|
providerMetadata: {
|
|
3062
3094
|
google: {
|
|
3095
|
+
...languageModelGoogleMetadata,
|
|
3063
3096
|
images: images.map(() => ({}))
|
|
3064
3097
|
}
|
|
3065
3098
|
},
|
|
3066
3099
|
response: {
|
|
3067
3100
|
timestamp: currentDate,
|
|
3068
3101
|
modelId: this.modelId,
|
|
3069
|
-
headers: (
|
|
3102
|
+
headers: (_i = result.response) == null ? void 0 : _i.headers
|
|
3070
3103
|
},
|
|
3071
3104
|
usage: result.usage ? {
|
|
3072
3105
|
inputTokens: result.usage.inputTokens.total,
|
|
3073
3106
|
outputTokens: result.usage.outputTokens.total,
|
|
3074
|
-
totalTokens: ((
|
|
3107
|
+
totalTokens: ((_j = result.usage.inputTokens.total) != null ? _j : 0) + ((_k = result.usage.outputTokens.total) != null ? _k : 0)
|
|
3075
3108
|
} : void 0
|
|
3076
3109
|
};
|
|
3077
3110
|
}
|