@ai-sdk/google 2.0.0-canary.11 → 2.0.0-canary.13

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/dist/index.js CHANGED
@@ -56,37 +56,54 @@ var googleGenerativeAIEmbeddingProviderOptions = import_zod2.z.object({
56
56
  * Optional. Optional reduced dimension for the output embedding.
57
57
  * If set, excessive values in the output embedding are truncated from the end.
58
58
  */
59
- outputDimensionality: import_zod2.z.number().optional()
59
+ outputDimensionality: import_zod2.z.number().optional(),
60
+ /**
61
+ * Optional. Specifies the task type for generating embeddings.
62
+ * Supported task types:
63
+ * - SEMANTIC_SIMILARITY: Optimized for text similarity.
64
+ * - CLASSIFICATION: Optimized for text classification.
65
+ * - CLUSTERING: Optimized for clustering texts based on similarity.
66
+ * - RETRIEVAL_DOCUMENT: Optimized for document retrieval.
67
+ * - RETRIEVAL_QUERY: Optimized for query-based retrieval.
68
+ * - QUESTION_ANSWERING: Optimized for answering questions.
69
+ * - FACT_VERIFICATION: Optimized for verifying factual information.
70
+ * - CODE_RETRIEVAL_QUERY: Optimized for retrieving code blocks based on natural language queries.
71
+ */
72
+ taskType: import_zod2.z.enum([
73
+ "SEMANTIC_SIMILARITY",
74
+ "CLASSIFICATION",
75
+ "CLUSTERING",
76
+ "RETRIEVAL_DOCUMENT",
77
+ "RETRIEVAL_QUERY",
78
+ "QUESTION_ANSWERING",
79
+ "FACT_VERIFICATION",
80
+ "CODE_RETRIEVAL_QUERY"
81
+ ]).optional()
60
82
  });
61
83
 
62
84
  // src/google-generative-ai-embedding-model.ts
63
85
  var GoogleGenerativeAIEmbeddingModel = class {
64
86
  constructor(modelId, config) {
65
87
  this.specificationVersion = "v2";
88
+ this.maxEmbeddingsPerCall = 2048;
89
+ this.supportsParallelCalls = true;
66
90
  this.modelId = modelId;
67
91
  this.config = config;
68
92
  }
69
93
  get provider() {
70
94
  return this.config.provider;
71
95
  }
72
- get maxEmbeddingsPerCall() {
73
- return 2048;
74
- }
75
- get supportsParallelCalls() {
76
- return true;
77
- }
78
96
  async doEmbed({
79
97
  values,
80
98
  headers,
81
99
  abortSignal,
82
100
  providerOptions
83
101
  }) {
84
- var _a;
85
- const googleOptions = (_a = (0, import_provider_utils2.parseProviderOptions)({
102
+ const googleOptions = await (0, import_provider_utils2.parseProviderOptions)({
86
103
  provider: "google",
87
104
  providerOptions,
88
105
  schema: googleGenerativeAIEmbeddingProviderOptions
89
- })) != null ? _a : {};
106
+ });
90
107
  if (values.length > this.maxEmbeddingsPerCall) {
91
108
  throw new import_provider.TooManyEmbeddingValuesForCallError({
92
109
  provider: this.provider,
@@ -110,7 +127,8 @@ var GoogleGenerativeAIEmbeddingModel = class {
110
127
  requests: values.map((value) => ({
111
128
  model: `models/${this.modelId}`,
112
129
  content: { role: "user", parts: [{ text: value }] },
113
- outputDimensionality: googleOptions.outputDimensionality
130
+ outputDimensionality: googleOptions == null ? void 0 : googleOptions.outputDimensionality,
131
+ taskType: googleOptions == null ? void 0 : googleOptions.taskType
114
132
  }))
115
133
  },
116
134
  failedResponseHandler: googleFailedResponseHandler,
@@ -133,7 +151,7 @@ var googleGenerativeAITextEmbeddingResponseSchema = import_zod3.z.object({
133
151
 
134
152
  // src/google-generative-ai-language-model.ts
135
153
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
136
- var import_zod4 = require("zod");
154
+ var import_zod5 = require("zod");
137
155
 
138
156
  // src/convert-json-schema-to-openapi-schema.ts
139
157
  function convertJSONSchemaToOpenAPISchema(jsonSchema) {
@@ -350,13 +368,99 @@ function getModelPath(modelId) {
350
368
  return modelId.includes("/") ? modelId : `models/${modelId}`;
351
369
  }
352
370
 
371
+ // src/google-generative-ai-options.ts
372
+ var import_zod4 = require("zod");
373
+ var dynamicRetrievalConfig = import_zod4.z.object({
374
+ /**
375
+ * The mode of the predictor to be used in dynamic retrieval.
376
+ */
377
+ mode: import_zod4.z.enum(["MODE_UNSPECIFIED", "MODE_DYNAMIC"]).optional(),
378
+ /**
379
+ * The threshold to be used in dynamic retrieval. If not set, a system default
380
+ * value is used.
381
+ */
382
+ dynamicThreshold: import_zod4.z.number().optional()
383
+ });
384
+ var googleGenerativeAIProviderOptions = import_zod4.z.object({
385
+ responseModalities: import_zod4.z.array(import_zod4.z.enum(["TEXT", "IMAGE"])).optional(),
386
+ thinkingConfig: import_zod4.z.object({
387
+ thinkingBudget: import_zod4.z.number().optional()
388
+ }).optional(),
389
+ /**
390
+ Optional.
391
+ The name of the cached content used as context to serve the prediction.
392
+ Format: cachedContents/{cachedContent}
393
+ */
394
+ cachedContent: import_zod4.z.string().optional(),
395
+ /**
396
+ * Optional. Enable structured output. Default is true.
397
+ *
398
+ * This is useful when the JSON Schema contains elements that are
399
+ * not supported by the OpenAPI schema version that
400
+ * Google Generative AI uses. You can use this to disable
401
+ * structured outputs if you need to.
402
+ */
403
+ structuredOutputs: import_zod4.z.boolean().optional(),
404
+ /**
405
+ Optional. A list of unique safety settings for blocking unsafe content.
406
+ */
407
+ safetySettings: import_zod4.z.array(
408
+ import_zod4.z.object({
409
+ category: import_zod4.z.enum([
410
+ "HARM_CATEGORY_UNSPECIFIED",
411
+ "HARM_CATEGORY_HATE_SPEECH",
412
+ "HARM_CATEGORY_DANGEROUS_CONTENT",
413
+ "HARM_CATEGORY_HARASSMENT",
414
+ "HARM_CATEGORY_SEXUALLY_EXPLICIT",
415
+ "HARM_CATEGORY_CIVIC_INTEGRITY"
416
+ ]),
417
+ threshold: import_zod4.z.enum([
418
+ "HARM_BLOCK_THRESHOLD_UNSPECIFIED",
419
+ "BLOCK_LOW_AND_ABOVE",
420
+ "BLOCK_MEDIUM_AND_ABOVE",
421
+ "BLOCK_ONLY_HIGH",
422
+ "BLOCK_NONE",
423
+ "OFF"
424
+ ])
425
+ })
426
+ ).optional(),
427
+ threshold: import_zod4.z.enum([
428
+ "HARM_BLOCK_THRESHOLD_UNSPECIFIED",
429
+ "BLOCK_LOW_AND_ABOVE",
430
+ "BLOCK_MEDIUM_AND_ABOVE",
431
+ "BLOCK_ONLY_HIGH",
432
+ "BLOCK_NONE",
433
+ "OFF"
434
+ ]).optional(),
435
+ /**
436
+ * Optional. Enables timestamp understanding for audio-only files.
437
+ *
438
+ * https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding
439
+ */
440
+ audioTimestamp: import_zod4.z.boolean().optional(),
441
+ /**
442
+ Optional. When enabled, the model will use Google search to ground the response.
443
+
444
+ @see https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/overview
445
+ */
446
+ useSearchGrounding: import_zod4.z.boolean().optional(),
447
+ /**
448
+ Optional. Specifies the dynamic retrieval configuration.
449
+
450
+ @note Dynamic retrieval is only compatible with Gemini 1.5 Flash.
451
+
452
+ @see https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/ground-with-google-search#dynamic-retrieval
453
+ */
454
+ dynamicRetrievalConfig: dynamicRetrievalConfig.optional()
455
+ });
456
+
353
457
  // src/google-prepare-tools.ts
354
458
  var import_provider3 = require("@ai-sdk/provider");
355
459
  function prepareTools({
356
460
  tools,
357
461
  toolChoice,
358
462
  useSearchGrounding,
359
- dynamicRetrievalConfig,
463
+ dynamicRetrievalConfig: dynamicRetrievalConfig2,
360
464
  modelId
361
465
  }) {
362
466
  var _a;
@@ -367,7 +471,7 @@ function prepareTools({
367
471
  if (useSearchGrounding) {
368
472
  return {
369
473
  tools: isGemini2 ? { googleSearch: {} } : {
370
- googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig ? {} : { dynamicRetrievalConfig }
474
+ googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig2 ? {} : { dynamicRetrievalConfig: dynamicRetrievalConfig2 }
371
475
  },
372
476
  toolConfig: void 0,
373
477
  toolWarnings
@@ -464,10 +568,9 @@ function mapGoogleGenerativeAIFinishReason({
464
568
 
465
569
  // src/google-generative-ai-language-model.ts
466
570
  var GoogleGenerativeAILanguageModel = class {
467
- constructor(modelId, settings, config) {
571
+ constructor(modelId, config) {
468
572
  this.specificationVersion = "v2";
469
573
  this.modelId = modelId;
470
- this.settings = settings;
471
574
  this.config = config;
472
575
  }
473
576
  get provider() {
@@ -494,10 +597,10 @@ var GoogleGenerativeAILanguageModel = class {
494
597
  }) {
495
598
  var _a, _b;
496
599
  const warnings = [];
497
- const googleOptions = (0, import_provider_utils4.parseProviderOptions)({
600
+ const googleOptions = await (0, import_provider_utils4.parseProviderOptions)({
498
601
  provider: "google",
499
602
  providerOptions,
500
- schema: googleGenerativeAIProviderOptionsSchema
603
+ schema: googleGenerativeAIProviderOptions
501
604
  });
502
605
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(prompt);
503
606
  const {
@@ -507,8 +610,8 @@ var GoogleGenerativeAILanguageModel = class {
507
610
  } = prepareTools({
508
611
  tools,
509
612
  toolChoice,
510
- useSearchGrounding: (_a = this.settings.useSearchGrounding) != null ? _a : false,
511
- dynamicRetrievalConfig: this.settings.dynamicRetrievalConfig,
613
+ useSearchGrounding: (_a = googleOptions == null ? void 0 : googleOptions.useSearchGrounding) != null ? _a : false,
614
+ dynamicRetrievalConfig: googleOptions == null ? void 0 : googleOptions.dynamicRetrievalConfig,
512
615
  modelId: this.modelId
513
616
  });
514
617
  return {
@@ -528,9 +631,9 @@ var GoogleGenerativeAILanguageModel = class {
528
631
  responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
529
632
  // so this is needed as an escape hatch:
530
633
  // TODO convert into provider option
531
- ((_b = this.settings.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
532
- ...this.settings.audioTimestamp && {
533
- audioTimestamp: this.settings.audioTimestamp
634
+ ((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
635
+ ...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
636
+ audioTimestamp: googleOptions.audioTimestamp
534
637
  },
535
638
  // provider options:
536
639
  responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities,
@@ -538,10 +641,10 @@ var GoogleGenerativeAILanguageModel = class {
538
641
  },
539
642
  contents,
540
643
  systemInstruction,
541
- safetySettings: this.settings.safetySettings,
644
+ safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
542
645
  tools: googleTools,
543
646
  toolConfig: googleToolConfig,
544
- cachedContent: this.settings.cachedContent
647
+ cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent
545
648
  },
546
649
  warnings: [...warnings, ...toolWarnings]
547
650
  };
@@ -789,100 +892,94 @@ function extractSources({
789
892
  title: chunk.web.title
790
893
  }));
791
894
  }
792
- var contentSchema = import_zod4.z.object({
793
- role: import_zod4.z.string(),
794
- parts: import_zod4.z.array(
795
- import_zod4.z.union([
796
- import_zod4.z.object({
797
- text: import_zod4.z.string()
895
+ var contentSchema = import_zod5.z.object({
896
+ role: import_zod5.z.string(),
897
+ parts: import_zod5.z.array(
898
+ import_zod5.z.union([
899
+ import_zod5.z.object({
900
+ text: import_zod5.z.string()
798
901
  }),
799
- import_zod4.z.object({
800
- functionCall: import_zod4.z.object({
801
- name: import_zod4.z.string(),
802
- args: import_zod4.z.unknown()
902
+ import_zod5.z.object({
903
+ functionCall: import_zod5.z.object({
904
+ name: import_zod5.z.string(),
905
+ args: import_zod5.z.unknown()
803
906
  })
804
907
  }),
805
- import_zod4.z.object({
806
- inlineData: import_zod4.z.object({
807
- mimeType: import_zod4.z.string(),
808
- data: import_zod4.z.string()
908
+ import_zod5.z.object({
909
+ inlineData: import_zod5.z.object({
910
+ mimeType: import_zod5.z.string(),
911
+ data: import_zod5.z.string()
809
912
  })
810
913
  })
811
914
  ])
812
915
  ).nullish()
813
916
  });
814
- var groundingChunkSchema = import_zod4.z.object({
815
- web: import_zod4.z.object({ uri: import_zod4.z.string(), title: import_zod4.z.string() }).nullish(),
816
- retrievedContext: import_zod4.z.object({ uri: import_zod4.z.string(), title: import_zod4.z.string() }).nullish()
917
+ var groundingChunkSchema = import_zod5.z.object({
918
+ web: import_zod5.z.object({ uri: import_zod5.z.string(), title: import_zod5.z.string() }).nullish(),
919
+ retrievedContext: import_zod5.z.object({ uri: import_zod5.z.string(), title: import_zod5.z.string() }).nullish()
817
920
  });
818
- var groundingMetadataSchema = import_zod4.z.object({
819
- webSearchQueries: import_zod4.z.array(import_zod4.z.string()).nullish(),
820
- retrievalQueries: import_zod4.z.array(import_zod4.z.string()).nullish(),
821
- searchEntryPoint: import_zod4.z.object({ renderedContent: import_zod4.z.string() }).nullish(),
822
- groundingChunks: import_zod4.z.array(groundingChunkSchema).nullish(),
823
- groundingSupports: import_zod4.z.array(
824
- import_zod4.z.object({
825
- segment: import_zod4.z.object({
826
- startIndex: import_zod4.z.number().nullish(),
827
- endIndex: import_zod4.z.number().nullish(),
828
- text: import_zod4.z.string().nullish()
921
+ var groundingMetadataSchema = import_zod5.z.object({
922
+ webSearchQueries: import_zod5.z.array(import_zod5.z.string()).nullish(),
923
+ retrievalQueries: import_zod5.z.array(import_zod5.z.string()).nullish(),
924
+ searchEntryPoint: import_zod5.z.object({ renderedContent: import_zod5.z.string() }).nullish(),
925
+ groundingChunks: import_zod5.z.array(groundingChunkSchema).nullish(),
926
+ groundingSupports: import_zod5.z.array(
927
+ import_zod5.z.object({
928
+ segment: import_zod5.z.object({
929
+ startIndex: import_zod5.z.number().nullish(),
930
+ endIndex: import_zod5.z.number().nullish(),
931
+ text: import_zod5.z.string().nullish()
829
932
  }),
830
- segment_text: import_zod4.z.string().nullish(),
831
- groundingChunkIndices: import_zod4.z.array(import_zod4.z.number()).nullish(),
832
- supportChunkIndices: import_zod4.z.array(import_zod4.z.number()).nullish(),
833
- confidenceScores: import_zod4.z.array(import_zod4.z.number()).nullish(),
834
- confidenceScore: import_zod4.z.array(import_zod4.z.number()).nullish()
933
+ segment_text: import_zod5.z.string().nullish(),
934
+ groundingChunkIndices: import_zod5.z.array(import_zod5.z.number()).nullish(),
935
+ supportChunkIndices: import_zod5.z.array(import_zod5.z.number()).nullish(),
936
+ confidenceScores: import_zod5.z.array(import_zod5.z.number()).nullish(),
937
+ confidenceScore: import_zod5.z.array(import_zod5.z.number()).nullish()
835
938
  })
836
939
  ).nullish(),
837
- retrievalMetadata: import_zod4.z.union([
838
- import_zod4.z.object({
839
- webDynamicRetrievalScore: import_zod4.z.number()
940
+ retrievalMetadata: import_zod5.z.union([
941
+ import_zod5.z.object({
942
+ webDynamicRetrievalScore: import_zod5.z.number()
840
943
  }),
841
- import_zod4.z.object({})
944
+ import_zod5.z.object({})
842
945
  ]).nullish()
843
946
  });
844
- var safetyRatingSchema = import_zod4.z.object({
845
- category: import_zod4.z.string(),
846
- probability: import_zod4.z.string(),
847
- probabilityScore: import_zod4.z.number().nullish(),
848
- severity: import_zod4.z.string().nullish(),
849
- severityScore: import_zod4.z.number().nullish(),
850
- blocked: import_zod4.z.boolean().nullish()
947
+ var safetyRatingSchema = import_zod5.z.object({
948
+ category: import_zod5.z.string(),
949
+ probability: import_zod5.z.string(),
950
+ probabilityScore: import_zod5.z.number().nullish(),
951
+ severity: import_zod5.z.string().nullish(),
952
+ severityScore: import_zod5.z.number().nullish(),
953
+ blocked: import_zod5.z.boolean().nullish()
851
954
  });
852
- var responseSchema = import_zod4.z.object({
853
- candidates: import_zod4.z.array(
854
- import_zod4.z.object({
855
- content: contentSchema.nullish().or(import_zod4.z.object({}).strict()),
856
- finishReason: import_zod4.z.string().nullish(),
857
- safetyRatings: import_zod4.z.array(safetyRatingSchema).nullish(),
955
+ var responseSchema = import_zod5.z.object({
956
+ candidates: import_zod5.z.array(
957
+ import_zod5.z.object({
958
+ content: contentSchema.nullish().or(import_zod5.z.object({}).strict()),
959
+ finishReason: import_zod5.z.string().nullish(),
960
+ safetyRatings: import_zod5.z.array(safetyRatingSchema).nullish(),
858
961
  groundingMetadata: groundingMetadataSchema.nullish()
859
962
  })
860
963
  ),
861
- usageMetadata: import_zod4.z.object({
862
- promptTokenCount: import_zod4.z.number().nullish(),
863
- candidatesTokenCount: import_zod4.z.number().nullish(),
864
- totalTokenCount: import_zod4.z.number().nullish()
964
+ usageMetadata: import_zod5.z.object({
965
+ promptTokenCount: import_zod5.z.number().nullish(),
966
+ candidatesTokenCount: import_zod5.z.number().nullish(),
967
+ totalTokenCount: import_zod5.z.number().nullish()
865
968
  }).nullish()
866
969
  });
867
- var chunkSchema = import_zod4.z.object({
868
- candidates: import_zod4.z.array(
869
- import_zod4.z.object({
970
+ var chunkSchema = import_zod5.z.object({
971
+ candidates: import_zod5.z.array(
972
+ import_zod5.z.object({
870
973
  content: contentSchema.nullish(),
871
- finishReason: import_zod4.z.string().nullish(),
872
- safetyRatings: import_zod4.z.array(safetyRatingSchema).nullish(),
974
+ finishReason: import_zod5.z.string().nullish(),
975
+ safetyRatings: import_zod5.z.array(safetyRatingSchema).nullish(),
873
976
  groundingMetadata: groundingMetadataSchema.nullish()
874
977
  })
875
978
  ).nullish(),
876
- usageMetadata: import_zod4.z.object({
877
- promptTokenCount: import_zod4.z.number().nullish(),
878
- candidatesTokenCount: import_zod4.z.number().nullish(),
879
- totalTokenCount: import_zod4.z.number().nullish()
880
- }).nullish()
881
- });
882
- var googleGenerativeAIProviderOptionsSchema = import_zod4.z.object({
883
- responseModalities: import_zod4.z.array(import_zod4.z.enum(["TEXT", "IMAGE"])).nullish(),
884
- thinkingConfig: import_zod4.z.object({
885
- thinkingBudget: import_zod4.z.number().nullish()
979
+ usageMetadata: import_zod5.z.object({
980
+ promptTokenCount: import_zod5.z.number().nullish(),
981
+ candidatesTokenCount: import_zod5.z.number().nullish(),
982
+ totalTokenCount: import_zod5.z.number().nullish()
886
983
  }).nullish()
887
984
  });
888
985
 
@@ -898,9 +995,9 @@ function createGoogleGenerativeAI(options = {}) {
898
995
  }),
899
996
  ...options.headers
900
997
  });
901
- const createChatModel = (modelId, settings = {}) => {
998
+ const createChatModel = (modelId) => {
902
999
  var _a2;
903
- return new GoogleGenerativeAILanguageModel(modelId, settings, {
1000
+ return new GoogleGenerativeAILanguageModel(modelId, {
904
1001
  provider: "google.generative-ai",
905
1002
  baseURL,
906
1003
  headers: getHeaders,
@@ -920,13 +1017,13 @@ function createGoogleGenerativeAI(options = {}) {
920
1017
  headers: getHeaders,
921
1018
  fetch: options.fetch
922
1019
  });
923
- const provider = function(modelId, settings) {
1020
+ const provider = function(modelId) {
924
1021
  if (new.target) {
925
1022
  throw new Error(
926
1023
  "The Google Generative AI model function cannot be called with the new keyword."
927
1024
  );
928
1025
  }
929
- return createChatModel(modelId, settings);
1026
+ return createChatModel(modelId);
930
1027
  };
931
1028
  provider.languageModel = createChatModel;
932
1029
  provider.chat = createChatModel;