@google/genai 1.41.0 → 1.42.0

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.
@@ -1358,6 +1358,18 @@ var Environment;
1358
1358
  */
1359
1359
  Environment["ENVIRONMENT_BROWSER"] = "ENVIRONMENT_BROWSER";
1360
1360
  })(Environment || (Environment = {}));
1361
+ /** Enum representing the Vertex embedding API to use. */
1362
+ var EmbeddingApiType;
1363
+ (function (EmbeddingApiType) {
1364
+ /**
1365
+ * predict API endpoint (default)
1366
+ */
1367
+ EmbeddingApiType["PREDICT"] = "PREDICT";
1368
+ /**
1369
+ * embedContent API Endpoint
1370
+ */
1371
+ EmbeddingApiType["EMBED_CONTENT"] = "EMBED_CONTENT";
1372
+ })(EmbeddingApiType || (EmbeddingApiType = {}));
1361
1373
  /** Enum that controls the safety filter level for objectionable content. */
1362
1374
  var SafetyFilterLevel;
1363
1375
  (function (SafetyFilterLevel) {
@@ -3389,6 +3401,10 @@ function tJobState(state) {
3389
3401
  return stateString;
3390
3402
  }
3391
3403
  }
3404
+ function tIsVertexEmbedContentModel(model) {
3405
+ return ((model.includes('gemini') && model !== 'gemini-embedding-001') ||
3406
+ model.includes('maas'));
3407
+ }
3392
3408
 
3393
3409
  /**
3394
3410
  * @license
@@ -8630,33 +8646,103 @@ function embedContentConfigToMldev(fromObject, parentObject, _rootObject) {
8630
8646
  }
8631
8647
  return toObject;
8632
8648
  }
8633
- function embedContentConfigToVertex(fromObject, parentObject, _rootObject) {
8649
+ function embedContentConfigToVertex(fromObject, parentObject, rootObject) {
8634
8650
  const toObject = {};
8635
- const fromTaskType = getValueByPath(fromObject, ['taskType']);
8636
- if (parentObject !== undefined && fromTaskType != null) {
8637
- setValueByPath(parentObject, ['instances[]', 'task_type'], fromTaskType);
8651
+ let discriminatorTaskType = getValueByPath(rootObject, [
8652
+ 'embeddingApiType',
8653
+ ]);
8654
+ if (discriminatorTaskType === undefined) {
8655
+ discriminatorTaskType = 'PREDICT';
8638
8656
  }
8639
- const fromTitle = getValueByPath(fromObject, ['title']);
8640
- if (parentObject !== undefined && fromTitle != null) {
8641
- setValueByPath(parentObject, ['instances[]', 'title'], fromTitle);
8657
+ if (discriminatorTaskType === 'PREDICT') {
8658
+ const fromTaskType = getValueByPath(fromObject, ['taskType']);
8659
+ if (parentObject !== undefined && fromTaskType != null) {
8660
+ setValueByPath(parentObject, ['instances[]', 'task_type'], fromTaskType);
8661
+ }
8642
8662
  }
8643
- const fromOutputDimensionality = getValueByPath(fromObject, [
8644
- 'outputDimensionality',
8663
+ else if (discriminatorTaskType === 'EMBED_CONTENT') {
8664
+ const fromTaskType = getValueByPath(fromObject, ['taskType']);
8665
+ if (parentObject !== undefined && fromTaskType != null) {
8666
+ setValueByPath(parentObject, ['taskType'], fromTaskType);
8667
+ }
8668
+ }
8669
+ let discriminatorTitle = getValueByPath(rootObject, [
8670
+ 'embeddingApiType',
8645
8671
  ]);
8646
- if (parentObject !== undefined && fromOutputDimensionality != null) {
8647
- setValueByPath(parentObject, ['parameters', 'outputDimensionality'], fromOutputDimensionality);
8672
+ if (discriminatorTitle === undefined) {
8673
+ discriminatorTitle = 'PREDICT';
8648
8674
  }
8649
- const fromMimeType = getValueByPath(fromObject, ['mimeType']);
8650
- if (parentObject !== undefined && fromMimeType != null) {
8651
- setValueByPath(parentObject, ['instances[]', 'mimeType'], fromMimeType);
8675
+ if (discriminatorTitle === 'PREDICT') {
8676
+ const fromTitle = getValueByPath(fromObject, ['title']);
8677
+ if (parentObject !== undefined && fromTitle != null) {
8678
+ setValueByPath(parentObject, ['instances[]', 'title'], fromTitle);
8679
+ }
8652
8680
  }
8653
- const fromAutoTruncate = getValueByPath(fromObject, ['autoTruncate']);
8654
- if (parentObject !== undefined && fromAutoTruncate != null) {
8655
- setValueByPath(parentObject, ['parameters', 'autoTruncate'], fromAutoTruncate);
8681
+ else if (discriminatorTitle === 'EMBED_CONTENT') {
8682
+ const fromTitle = getValueByPath(fromObject, ['title']);
8683
+ if (parentObject !== undefined && fromTitle != null) {
8684
+ setValueByPath(parentObject, ['title'], fromTitle);
8685
+ }
8686
+ }
8687
+ let discriminatorOutputDimensionality = getValueByPath(rootObject, [
8688
+ 'embeddingApiType',
8689
+ ]);
8690
+ if (discriminatorOutputDimensionality === undefined) {
8691
+ discriminatorOutputDimensionality = 'PREDICT';
8692
+ }
8693
+ if (discriminatorOutputDimensionality === 'PREDICT') {
8694
+ const fromOutputDimensionality = getValueByPath(fromObject, [
8695
+ 'outputDimensionality',
8696
+ ]);
8697
+ if (parentObject !== undefined && fromOutputDimensionality != null) {
8698
+ setValueByPath(parentObject, ['parameters', 'outputDimensionality'], fromOutputDimensionality);
8699
+ }
8700
+ }
8701
+ else if (discriminatorOutputDimensionality === 'EMBED_CONTENT') {
8702
+ const fromOutputDimensionality = getValueByPath(fromObject, [
8703
+ 'outputDimensionality',
8704
+ ]);
8705
+ if (parentObject !== undefined && fromOutputDimensionality != null) {
8706
+ setValueByPath(parentObject, ['outputDimensionality'], fromOutputDimensionality);
8707
+ }
8708
+ }
8709
+ let discriminatorMimeType = getValueByPath(rootObject, [
8710
+ 'embeddingApiType',
8711
+ ]);
8712
+ if (discriminatorMimeType === undefined) {
8713
+ discriminatorMimeType = 'PREDICT';
8714
+ }
8715
+ if (discriminatorMimeType === 'PREDICT') {
8716
+ const fromMimeType = getValueByPath(fromObject, ['mimeType']);
8717
+ if (parentObject !== undefined && fromMimeType != null) {
8718
+ setValueByPath(parentObject, ['instances[]', 'mimeType'], fromMimeType);
8719
+ }
8720
+ }
8721
+ let discriminatorAutoTruncate = getValueByPath(rootObject, [
8722
+ 'embeddingApiType',
8723
+ ]);
8724
+ if (discriminatorAutoTruncate === undefined) {
8725
+ discriminatorAutoTruncate = 'PREDICT';
8726
+ }
8727
+ if (discriminatorAutoTruncate === 'PREDICT') {
8728
+ const fromAutoTruncate = getValueByPath(fromObject, [
8729
+ 'autoTruncate',
8730
+ ]);
8731
+ if (parentObject !== undefined && fromAutoTruncate != null) {
8732
+ setValueByPath(parentObject, ['parameters', 'autoTruncate'], fromAutoTruncate);
8733
+ }
8734
+ }
8735
+ else if (discriminatorAutoTruncate === 'EMBED_CONTENT') {
8736
+ const fromAutoTruncate = getValueByPath(fromObject, [
8737
+ 'autoTruncate',
8738
+ ]);
8739
+ if (parentObject !== undefined && fromAutoTruncate != null) {
8740
+ setValueByPath(parentObject, ['autoTruncate'], fromAutoTruncate);
8741
+ }
8656
8742
  }
8657
8743
  return toObject;
8658
8744
  }
8659
- function embedContentParametersToMldev(apiClient, fromObject, rootObject) {
8745
+ function embedContentParametersPrivateToMldev(apiClient, fromObject, rootObject) {
8660
8746
  const toObject = {};
8661
8747
  const fromModel = getValueByPath(fromObject, ['model']);
8662
8748
  if (fromModel != null) {
@@ -8672,6 +8758,10 @@ function embedContentParametersToMldev(apiClient, fromObject, rootObject) {
8672
8758
  }
8673
8759
  setValueByPath(toObject, ['requests[]', 'content'], transformedList);
8674
8760
  }
8761
+ const fromContent = getValueByPath(fromObject, ['content']);
8762
+ if (fromContent != null) {
8763
+ contentToMldev$1(tContent(fromContent));
8764
+ }
8675
8765
  const fromConfig = getValueByPath(fromObject, ['config']);
8676
8766
  if (fromConfig != null) {
8677
8767
  embedContentConfigToMldev(fromConfig, toObject);
@@ -8682,25 +8772,45 @@ function embedContentParametersToMldev(apiClient, fromObject, rootObject) {
8682
8772
  }
8683
8773
  return toObject;
8684
8774
  }
8685
- function embedContentParametersToVertex(apiClient, fromObject, rootObject) {
8775
+ function embedContentParametersPrivateToVertex(apiClient, fromObject, rootObject) {
8686
8776
  const toObject = {};
8687
8777
  const fromModel = getValueByPath(fromObject, ['model']);
8688
8778
  if (fromModel != null) {
8689
8779
  setValueByPath(toObject, ['_url', 'model'], tModel(apiClient, fromModel));
8690
8780
  }
8691
- const fromContents = getValueByPath(fromObject, ['contents']);
8692
- if (fromContents != null) {
8693
- let transformedList = tContentsForEmbed(apiClient, fromContents);
8694
- if (Array.isArray(transformedList)) {
8695
- transformedList = transformedList.map((item) => {
8696
- return item;
8697
- });
8781
+ let discriminatorContents = getValueByPath(rootObject, [
8782
+ 'embeddingApiType',
8783
+ ]);
8784
+ if (discriminatorContents === undefined) {
8785
+ discriminatorContents = 'PREDICT';
8786
+ }
8787
+ if (discriminatorContents === 'PREDICT') {
8788
+ const fromContents = getValueByPath(fromObject, ['contents']);
8789
+ if (fromContents != null) {
8790
+ let transformedList = tContentsForEmbed(apiClient, fromContents);
8791
+ if (Array.isArray(transformedList)) {
8792
+ transformedList = transformedList.map((item) => {
8793
+ return item;
8794
+ });
8795
+ }
8796
+ setValueByPath(toObject, ['instances[]', 'content'], transformedList);
8797
+ }
8798
+ }
8799
+ let discriminatorContent = getValueByPath(rootObject, [
8800
+ 'embeddingApiType',
8801
+ ]);
8802
+ if (discriminatorContent === undefined) {
8803
+ discriminatorContent = 'PREDICT';
8804
+ }
8805
+ if (discriminatorContent === 'EMBED_CONTENT') {
8806
+ const fromContent = getValueByPath(fromObject, ['content']);
8807
+ if (fromContent != null) {
8808
+ setValueByPath(toObject, ['content'], tContent(fromContent));
8698
8809
  }
8699
- setValueByPath(toObject, ['instances[]', 'content'], transformedList);
8700
8810
  }
8701
8811
  const fromConfig = getValueByPath(fromObject, ['config']);
8702
8812
  if (fromConfig != null) {
8703
- embedContentConfigToVertex(fromConfig, toObject);
8813
+ embedContentConfigToVertex(fromConfig, toObject, rootObject);
8704
8814
  }
8705
8815
  return toObject;
8706
8816
  }
@@ -8753,6 +8863,24 @@ function embedContentResponseFromVertex(fromObject, rootObject) {
8753
8863
  if (fromMetadata != null) {
8754
8864
  setValueByPath(toObject, ['metadata'], fromMetadata);
8755
8865
  }
8866
+ if (rootObject &&
8867
+ getValueByPath(rootObject, ['embeddingApiType']) === 'EMBED_CONTENT') {
8868
+ const embedding = getValueByPath(fromObject, ['embedding']);
8869
+ const usageMetadata = getValueByPath(fromObject, ['usageMetadata']);
8870
+ const truncated = getValueByPath(fromObject, ['truncated']);
8871
+ if (embedding) {
8872
+ const stats = {};
8873
+ if (usageMetadata &&
8874
+ usageMetadata['promptTokenCount']) {
8875
+ stats.tokenCount = usageMetadata['promptTokenCount'];
8876
+ }
8877
+ if (truncated) {
8878
+ stats.truncated = truncated;
8879
+ }
8880
+ embedding.statistics = stats;
8881
+ setValueByPath(toObject, ['embeddings'], [embedding]);
8882
+ }
8883
+ }
8756
8884
  return toObject;
8757
8885
  }
8758
8886
  function endpointFromVertex(fromObject, _rootObject) {
@@ -11610,7 +11738,7 @@ const CONTENT_TYPE_HEADER = 'Content-Type';
11610
11738
  const SERVER_TIMEOUT_HEADER = 'X-Server-Timeout';
11611
11739
  const USER_AGENT_HEADER = 'User-Agent';
11612
11740
  const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
11613
- const SDK_VERSION = '1.41.0'; // x-release-please-version
11741
+ const SDK_VERSION = '1.42.0'; // x-release-please-version
11614
11742
  const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
11615
11743
  const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
11616
11744
  const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
@@ -13211,6 +13339,47 @@ class Models extends BaseModule {
13211
13339
  constructor(apiClient) {
13212
13340
  super();
13213
13341
  this.apiClient = apiClient;
13342
+ /**
13343
+ * Calculates embeddings for the given contents.
13344
+ *
13345
+ * @param params - The parameters for embedding contents.
13346
+ * @return The response from the API.
13347
+ *
13348
+ * @example
13349
+ * ```ts
13350
+ * const response = await ai.models.embedContent({
13351
+ * model: 'text-embedding-004',
13352
+ * contents: [
13353
+ * 'What is your name?',
13354
+ * 'What is your favorite color?',
13355
+ * ],
13356
+ * config: {
13357
+ * outputDimensionality: 64,
13358
+ * },
13359
+ * });
13360
+ * console.log(response);
13361
+ * ```
13362
+ */
13363
+ this.embedContent = async (params) => {
13364
+ if (!this.apiClient.isVertexAI()) {
13365
+ return await this.embedContentInternal(params);
13366
+ }
13367
+ const isVertexEmbedContentModel = (params.model.includes('gemini') &&
13368
+ params.model !== 'gemini-embedding-001') ||
13369
+ params.model.includes('maas');
13370
+ if (isVertexEmbedContentModel) {
13371
+ const contents = tContents(params.contents);
13372
+ if (contents.length > 1) {
13373
+ throw new Error('The embedContent API for this model only supports one content at a time.');
13374
+ }
13375
+ const paramsPrivate = Object.assign(Object.assign({}, params), { content: contents[0], embeddingApiType: EmbeddingApiType.EMBED_CONTENT });
13376
+ return await this.embedContentInternal(paramsPrivate);
13377
+ }
13378
+ else {
13379
+ const paramsPrivate = Object.assign(Object.assign({}, params), { embeddingApiType: EmbeddingApiType.PREDICT });
13380
+ return await this.embedContentInternal(paramsPrivate);
13381
+ }
13382
+ };
13214
13383
  /**
13215
13384
  * Makes an API request to generate content with a given model.
13216
13385
  *
@@ -13898,14 +14067,17 @@ class Models extends BaseModule {
13898
14067
  * console.log(response);
13899
14068
  * ```
13900
14069
  */
13901
- async embedContent(params) {
14070
+ async embedContentInternal(params) {
13902
14071
  var _a, _b, _c, _d;
13903
14072
  let response;
13904
14073
  let path = '';
13905
14074
  let queryParams = {};
13906
14075
  if (this.apiClient.isVertexAI()) {
13907
- const body = embedContentParametersToVertex(this.apiClient, params);
13908
- path = formatMap('{model}:predict', body['_url']);
14076
+ const body = embedContentParametersPrivateToVertex(this.apiClient, params, params);
14077
+ const endpointUrl = tIsVertexEmbedContentModel(params.model)
14078
+ ? '{model}:embedContent'
14079
+ : '{model}:predict';
14080
+ path = formatMap(endpointUrl, body['_url']);
13909
14081
  queryParams = body['_query'];
13910
14082
  delete body['_url'];
13911
14083
  delete body['_query'];
@@ -13928,14 +14100,14 @@ class Models extends BaseModule {
13928
14100
  });
13929
14101
  });
13930
14102
  return response.then((apiResponse) => {
13931
- const resp = embedContentResponseFromVertex(apiResponse);
14103
+ const resp = embedContentResponseFromVertex(apiResponse, params);
13932
14104
  const typedResp = new EmbedContentResponse();
13933
14105
  Object.assign(typedResp, resp);
13934
14106
  return typedResp;
13935
14107
  });
13936
14108
  }
13937
14109
  else {
13938
- const body = embedContentParametersToMldev(this.apiClient, params);
14110
+ const body = embedContentParametersPrivateToMldev(this.apiClient, params);
13939
14111
  path = formatMap('{model}:batchEmbedContents', body['_url']);
13940
14112
  queryParams = body['_query'];
13941
14113
  delete body['_url'];
@@ -18025,6 +18197,13 @@ class BaseGeminiNextGenAPIClient {
18025
18197
  (Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))) {
18026
18198
  return { bodyHeaders: undefined, body: ReadableStreamFrom(body) };
18027
18199
  }
18200
+ else if (typeof body === 'object' &&
18201
+ headers.values.get('content-type') === 'application/x-www-form-urlencoded') {
18202
+ return {
18203
+ bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
18204
+ body: this.stringifyQuery(body),
18205
+ };
18206
+ }
18028
18207
  else {
18029
18208
  return this.encoder({ body, headers });
18030
18209
  }
@@ -19569,5 +19748,5 @@ class GoogleGenAI {
19569
19748
  }
19570
19749
  }
19571
19750
 
19572
- export { ActivityHandling, AdapterSize, ApiError, ApiSpec, AuthType, Batches, Behavior, BlockedReason, Caches, CancelTuningJobResponse, Chat, Chats, ComputeTokensResponse, ContentReferenceImage, ControlReferenceImage, ControlReferenceType, CountTokensResponse, CreateFileResponse, DeleteCachedContentResponse, DeleteFileResponse, DeleteModelResponse, DocumentState, DynamicRetrievalConfigMode, EditImageResponse, EditMode, EmbedContentResponse, EndSensitivity, Environment, FeatureSelectionPreference, FileSource, FileState, Files, FinishReason, FunctionCallingConfigMode, FunctionResponse, FunctionResponseBlob, FunctionResponseFileData, FunctionResponsePart, FunctionResponseScheduling, GenerateContentResponse, GenerateContentResponsePromptFeedback, GenerateContentResponseUsageMetadata, GenerateImagesResponse, GenerateVideosOperation, GenerateVideosResponse, GoogleGenAI, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, HttpElementLocation, HttpResponse, ImagePromptLanguage, ImportFileOperation, ImportFileResponse, InlinedEmbedContentResponse, InlinedResponse, JobState, Language, ListBatchJobsResponse, ListCachedContentsResponse, ListDocumentsResponse, ListFileSearchStoresResponse, ListFilesResponse, ListModelsResponse, ListTuningJobsResponse, Live, LiveClientToolResponse, LiveMusicPlaybackControl, LiveMusicServerMessage, LiveSendToolResponseParameters, LiveServerMessage, MaskReferenceImage, MaskReferenceMode, MediaModality, MediaResolution, Modality, Models, MusicGenerationMode, Operations, Outcome, PagedItem, Pager, PartMediaResolutionLevel, PersonGeneration, PhishBlockThreshold, RawReferenceImage, RecontextImageResponse, RegisterFilesResponse, ReplayResponse, ResourceScope, SafetyFilterLevel, Scale, SegmentImageResponse, SegmentMode, Session, SingleEmbedContentResponse, StartSensitivity, StyleReferenceImage, SubjectReferenceImage, SubjectReferenceType, ThinkingLevel, Tokens, TrafficType, TuningMethod, TuningMode, TuningTask, TurnCompleteReason, TurnCoverage, Type, UploadToFileSearchStoreOperation, UploadToFileSearchStoreResponse, UploadToFileSearchStoreResumableResponse, UpscaleImageResponse, UrlRetrievalStatus, VadSignalType, VideoCompressionQuality, VideoGenerationMaskMode, VideoGenerationReferenceType, VoiceActivityType, createFunctionResponsePartFromBase64, createFunctionResponsePartFromUri, createModelContent, createPartFromBase64, createPartFromCodeExecutionResult, createPartFromExecutableCode, createPartFromFunctionCall, createPartFromFunctionResponse, createPartFromText, createPartFromUri, createUserContent, mcpToTool, setDefaultBaseUrls };
19751
+ export { ActivityHandling, AdapterSize, ApiError, ApiSpec, AuthType, Batches, Behavior, BlockedReason, Caches, CancelTuningJobResponse, Chat, Chats, ComputeTokensResponse, ContentReferenceImage, ControlReferenceImage, ControlReferenceType, CountTokensResponse, CreateFileResponse, DeleteCachedContentResponse, DeleteFileResponse, DeleteModelResponse, DocumentState, DynamicRetrievalConfigMode, EditImageResponse, EditMode, EmbedContentResponse, EmbeddingApiType, EndSensitivity, Environment, FeatureSelectionPreference, FileSource, FileState, Files, FinishReason, FunctionCallingConfigMode, FunctionResponse, FunctionResponseBlob, FunctionResponseFileData, FunctionResponsePart, FunctionResponseScheduling, GenerateContentResponse, GenerateContentResponsePromptFeedback, GenerateContentResponseUsageMetadata, GenerateImagesResponse, GenerateVideosOperation, GenerateVideosResponse, GoogleGenAI, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, HttpElementLocation, HttpResponse, ImagePromptLanguage, ImportFileOperation, ImportFileResponse, InlinedEmbedContentResponse, InlinedResponse, JobState, Language, ListBatchJobsResponse, ListCachedContentsResponse, ListDocumentsResponse, ListFileSearchStoresResponse, ListFilesResponse, ListModelsResponse, ListTuningJobsResponse, Live, LiveClientToolResponse, LiveMusicPlaybackControl, LiveMusicServerMessage, LiveSendToolResponseParameters, LiveServerMessage, MaskReferenceImage, MaskReferenceMode, MediaModality, MediaResolution, Modality, Models, MusicGenerationMode, Operations, Outcome, PagedItem, Pager, PartMediaResolutionLevel, PersonGeneration, PhishBlockThreshold, RawReferenceImage, RecontextImageResponse, RegisterFilesResponse, ReplayResponse, ResourceScope, SafetyFilterLevel, Scale, SegmentImageResponse, SegmentMode, Session, SingleEmbedContentResponse, StartSensitivity, StyleReferenceImage, SubjectReferenceImage, SubjectReferenceType, ThinkingLevel, Tokens, TrafficType, TuningMethod, TuningMode, TuningTask, TurnCompleteReason, TurnCoverage, Type, UploadToFileSearchStoreOperation, UploadToFileSearchStoreResponse, UploadToFileSearchStoreResumableResponse, UpscaleImageResponse, UrlRetrievalStatus, VadSignalType, VideoCompressionQuality, VideoGenerationMaskMode, VideoGenerationReferenceType, VoiceActivityType, createFunctionResponsePartFromBase64, createFunctionResponsePartFromUri, createModelContent, createPartFromBase64, createPartFromCodeExecutionResult, createPartFromExecutableCode, createPartFromFunctionCall, createPartFromFunctionResponse, createPartFromText, createPartFromUri, createUserContent, mcpToTool, setDefaultBaseUrls };
19573
19752
  //# sourceMappingURL=index.mjs.map