@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.
@@ -1388,6 +1388,18 @@ exports.Environment = void 0;
1388
1388
  */
1389
1389
  Environment["ENVIRONMENT_BROWSER"] = "ENVIRONMENT_BROWSER";
1390
1390
  })(exports.Environment || (exports.Environment = {}));
1391
+ /** Enum representing the Vertex embedding API to use. */
1392
+ exports.EmbeddingApiType = void 0;
1393
+ (function (EmbeddingApiType) {
1394
+ /**
1395
+ * predict API endpoint (default)
1396
+ */
1397
+ EmbeddingApiType["PREDICT"] = "PREDICT";
1398
+ /**
1399
+ * embedContent API Endpoint
1400
+ */
1401
+ EmbeddingApiType["EMBED_CONTENT"] = "EMBED_CONTENT";
1402
+ })(exports.EmbeddingApiType || (exports.EmbeddingApiType = {}));
1391
1403
  /** Enum that controls the safety filter level for objectionable content. */
1392
1404
  exports.SafetyFilterLevel = void 0;
1393
1405
  (function (SafetyFilterLevel) {
@@ -3419,6 +3431,10 @@ function tJobState(state) {
3419
3431
  return stateString;
3420
3432
  }
3421
3433
  }
3434
+ function tIsVertexEmbedContentModel(model) {
3435
+ return ((model.includes('gemini') && model !== 'gemini-embedding-001') ||
3436
+ model.includes('maas'));
3437
+ }
3422
3438
 
3423
3439
  /**
3424
3440
  * @license
@@ -8660,33 +8676,103 @@ function embedContentConfigToMldev(fromObject, parentObject, _rootObject) {
8660
8676
  }
8661
8677
  return toObject;
8662
8678
  }
8663
- function embedContentConfigToVertex(fromObject, parentObject, _rootObject) {
8679
+ function embedContentConfigToVertex(fromObject, parentObject, rootObject) {
8664
8680
  const toObject = {};
8665
- const fromTaskType = getValueByPath(fromObject, ['taskType']);
8666
- if (parentObject !== undefined && fromTaskType != null) {
8667
- setValueByPath(parentObject, ['instances[]', 'task_type'], fromTaskType);
8681
+ let discriminatorTaskType = getValueByPath(rootObject, [
8682
+ 'embeddingApiType',
8683
+ ]);
8684
+ if (discriminatorTaskType === undefined) {
8685
+ discriminatorTaskType = 'PREDICT';
8668
8686
  }
8669
- const fromTitle = getValueByPath(fromObject, ['title']);
8670
- if (parentObject !== undefined && fromTitle != null) {
8671
- setValueByPath(parentObject, ['instances[]', 'title'], fromTitle);
8687
+ if (discriminatorTaskType === 'PREDICT') {
8688
+ const fromTaskType = getValueByPath(fromObject, ['taskType']);
8689
+ if (parentObject !== undefined && fromTaskType != null) {
8690
+ setValueByPath(parentObject, ['instances[]', 'task_type'], fromTaskType);
8691
+ }
8672
8692
  }
8673
- const fromOutputDimensionality = getValueByPath(fromObject, [
8674
- 'outputDimensionality',
8693
+ else if (discriminatorTaskType === 'EMBED_CONTENT') {
8694
+ const fromTaskType = getValueByPath(fromObject, ['taskType']);
8695
+ if (parentObject !== undefined && fromTaskType != null) {
8696
+ setValueByPath(parentObject, ['taskType'], fromTaskType);
8697
+ }
8698
+ }
8699
+ let discriminatorTitle = getValueByPath(rootObject, [
8700
+ 'embeddingApiType',
8675
8701
  ]);
8676
- if (parentObject !== undefined && fromOutputDimensionality != null) {
8677
- setValueByPath(parentObject, ['parameters', 'outputDimensionality'], fromOutputDimensionality);
8702
+ if (discriminatorTitle === undefined) {
8703
+ discriminatorTitle = 'PREDICT';
8678
8704
  }
8679
- const fromMimeType = getValueByPath(fromObject, ['mimeType']);
8680
- if (parentObject !== undefined && fromMimeType != null) {
8681
- setValueByPath(parentObject, ['instances[]', 'mimeType'], fromMimeType);
8705
+ if (discriminatorTitle === 'PREDICT') {
8706
+ const fromTitle = getValueByPath(fromObject, ['title']);
8707
+ if (parentObject !== undefined && fromTitle != null) {
8708
+ setValueByPath(parentObject, ['instances[]', 'title'], fromTitle);
8709
+ }
8682
8710
  }
8683
- const fromAutoTruncate = getValueByPath(fromObject, ['autoTruncate']);
8684
- if (parentObject !== undefined && fromAutoTruncate != null) {
8685
- setValueByPath(parentObject, ['parameters', 'autoTruncate'], fromAutoTruncate);
8711
+ else if (discriminatorTitle === 'EMBED_CONTENT') {
8712
+ const fromTitle = getValueByPath(fromObject, ['title']);
8713
+ if (parentObject !== undefined && fromTitle != null) {
8714
+ setValueByPath(parentObject, ['title'], fromTitle);
8715
+ }
8716
+ }
8717
+ let discriminatorOutputDimensionality = getValueByPath(rootObject, [
8718
+ 'embeddingApiType',
8719
+ ]);
8720
+ if (discriminatorOutputDimensionality === undefined) {
8721
+ discriminatorOutputDimensionality = 'PREDICT';
8722
+ }
8723
+ if (discriminatorOutputDimensionality === 'PREDICT') {
8724
+ const fromOutputDimensionality = getValueByPath(fromObject, [
8725
+ 'outputDimensionality',
8726
+ ]);
8727
+ if (parentObject !== undefined && fromOutputDimensionality != null) {
8728
+ setValueByPath(parentObject, ['parameters', 'outputDimensionality'], fromOutputDimensionality);
8729
+ }
8730
+ }
8731
+ else if (discriminatorOutputDimensionality === 'EMBED_CONTENT') {
8732
+ const fromOutputDimensionality = getValueByPath(fromObject, [
8733
+ 'outputDimensionality',
8734
+ ]);
8735
+ if (parentObject !== undefined && fromOutputDimensionality != null) {
8736
+ setValueByPath(parentObject, ['outputDimensionality'], fromOutputDimensionality);
8737
+ }
8738
+ }
8739
+ let discriminatorMimeType = getValueByPath(rootObject, [
8740
+ 'embeddingApiType',
8741
+ ]);
8742
+ if (discriminatorMimeType === undefined) {
8743
+ discriminatorMimeType = 'PREDICT';
8744
+ }
8745
+ if (discriminatorMimeType === 'PREDICT') {
8746
+ const fromMimeType = getValueByPath(fromObject, ['mimeType']);
8747
+ if (parentObject !== undefined && fromMimeType != null) {
8748
+ setValueByPath(parentObject, ['instances[]', 'mimeType'], fromMimeType);
8749
+ }
8750
+ }
8751
+ let discriminatorAutoTruncate = getValueByPath(rootObject, [
8752
+ 'embeddingApiType',
8753
+ ]);
8754
+ if (discriminatorAutoTruncate === undefined) {
8755
+ discriminatorAutoTruncate = 'PREDICT';
8756
+ }
8757
+ if (discriminatorAutoTruncate === 'PREDICT') {
8758
+ const fromAutoTruncate = getValueByPath(fromObject, [
8759
+ 'autoTruncate',
8760
+ ]);
8761
+ if (parentObject !== undefined && fromAutoTruncate != null) {
8762
+ setValueByPath(parentObject, ['parameters', 'autoTruncate'], fromAutoTruncate);
8763
+ }
8764
+ }
8765
+ else if (discriminatorAutoTruncate === 'EMBED_CONTENT') {
8766
+ const fromAutoTruncate = getValueByPath(fromObject, [
8767
+ 'autoTruncate',
8768
+ ]);
8769
+ if (parentObject !== undefined && fromAutoTruncate != null) {
8770
+ setValueByPath(parentObject, ['autoTruncate'], fromAutoTruncate);
8771
+ }
8686
8772
  }
8687
8773
  return toObject;
8688
8774
  }
8689
- function embedContentParametersToMldev(apiClient, fromObject, rootObject) {
8775
+ function embedContentParametersPrivateToMldev(apiClient, fromObject, rootObject) {
8690
8776
  const toObject = {};
8691
8777
  const fromModel = getValueByPath(fromObject, ['model']);
8692
8778
  if (fromModel != null) {
@@ -8702,6 +8788,10 @@ function embedContentParametersToMldev(apiClient, fromObject, rootObject) {
8702
8788
  }
8703
8789
  setValueByPath(toObject, ['requests[]', 'content'], transformedList);
8704
8790
  }
8791
+ const fromContent = getValueByPath(fromObject, ['content']);
8792
+ if (fromContent != null) {
8793
+ contentToMldev$1(tContent(fromContent));
8794
+ }
8705
8795
  const fromConfig = getValueByPath(fromObject, ['config']);
8706
8796
  if (fromConfig != null) {
8707
8797
  embedContentConfigToMldev(fromConfig, toObject);
@@ -8712,25 +8802,45 @@ function embedContentParametersToMldev(apiClient, fromObject, rootObject) {
8712
8802
  }
8713
8803
  return toObject;
8714
8804
  }
8715
- function embedContentParametersToVertex(apiClient, fromObject, rootObject) {
8805
+ function embedContentParametersPrivateToVertex(apiClient, fromObject, rootObject) {
8716
8806
  const toObject = {};
8717
8807
  const fromModel = getValueByPath(fromObject, ['model']);
8718
8808
  if (fromModel != null) {
8719
8809
  setValueByPath(toObject, ['_url', 'model'], tModel(apiClient, fromModel));
8720
8810
  }
8721
- const fromContents = getValueByPath(fromObject, ['contents']);
8722
- if (fromContents != null) {
8723
- let transformedList = tContentsForEmbed(apiClient, fromContents);
8724
- if (Array.isArray(transformedList)) {
8725
- transformedList = transformedList.map((item) => {
8726
- return item;
8727
- });
8811
+ let discriminatorContents = getValueByPath(rootObject, [
8812
+ 'embeddingApiType',
8813
+ ]);
8814
+ if (discriminatorContents === undefined) {
8815
+ discriminatorContents = 'PREDICT';
8816
+ }
8817
+ if (discriminatorContents === 'PREDICT') {
8818
+ const fromContents = getValueByPath(fromObject, ['contents']);
8819
+ if (fromContents != null) {
8820
+ let transformedList = tContentsForEmbed(apiClient, fromContents);
8821
+ if (Array.isArray(transformedList)) {
8822
+ transformedList = transformedList.map((item) => {
8823
+ return item;
8824
+ });
8825
+ }
8826
+ setValueByPath(toObject, ['instances[]', 'content'], transformedList);
8827
+ }
8828
+ }
8829
+ let discriminatorContent = getValueByPath(rootObject, [
8830
+ 'embeddingApiType',
8831
+ ]);
8832
+ if (discriminatorContent === undefined) {
8833
+ discriminatorContent = 'PREDICT';
8834
+ }
8835
+ if (discriminatorContent === 'EMBED_CONTENT') {
8836
+ const fromContent = getValueByPath(fromObject, ['content']);
8837
+ if (fromContent != null) {
8838
+ setValueByPath(toObject, ['content'], tContent(fromContent));
8728
8839
  }
8729
- setValueByPath(toObject, ['instances[]', 'content'], transformedList);
8730
8840
  }
8731
8841
  const fromConfig = getValueByPath(fromObject, ['config']);
8732
8842
  if (fromConfig != null) {
8733
- embedContentConfigToVertex(fromConfig, toObject);
8843
+ embedContentConfigToVertex(fromConfig, toObject, rootObject);
8734
8844
  }
8735
8845
  return toObject;
8736
8846
  }
@@ -8783,6 +8893,24 @@ function embedContentResponseFromVertex(fromObject, rootObject) {
8783
8893
  if (fromMetadata != null) {
8784
8894
  setValueByPath(toObject, ['metadata'], fromMetadata);
8785
8895
  }
8896
+ if (rootObject &&
8897
+ getValueByPath(rootObject, ['embeddingApiType']) === 'EMBED_CONTENT') {
8898
+ const embedding = getValueByPath(fromObject, ['embedding']);
8899
+ const usageMetadata = getValueByPath(fromObject, ['usageMetadata']);
8900
+ const truncated = getValueByPath(fromObject, ['truncated']);
8901
+ if (embedding) {
8902
+ const stats = {};
8903
+ if (usageMetadata &&
8904
+ usageMetadata['promptTokenCount']) {
8905
+ stats.tokenCount = usageMetadata['promptTokenCount'];
8906
+ }
8907
+ if (truncated) {
8908
+ stats.truncated = truncated;
8909
+ }
8910
+ embedding.statistics = stats;
8911
+ setValueByPath(toObject, ['embeddings'], [embedding]);
8912
+ }
8913
+ }
8786
8914
  return toObject;
8787
8915
  }
8788
8916
  function endpointFromVertex(fromObject, _rootObject) {
@@ -11640,7 +11768,7 @@ const CONTENT_TYPE_HEADER = 'Content-Type';
11640
11768
  const SERVER_TIMEOUT_HEADER = 'X-Server-Timeout';
11641
11769
  const USER_AGENT_HEADER = 'User-Agent';
11642
11770
  const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
11643
- const SDK_VERSION = '1.41.0'; // x-release-please-version
11771
+ const SDK_VERSION = '1.42.0'; // x-release-please-version
11644
11772
  const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
11645
11773
  const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
11646
11774
  const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
@@ -13241,6 +13369,47 @@ class Models extends BaseModule {
13241
13369
  constructor(apiClient) {
13242
13370
  super();
13243
13371
  this.apiClient = apiClient;
13372
+ /**
13373
+ * Calculates embeddings for the given contents.
13374
+ *
13375
+ * @param params - The parameters for embedding contents.
13376
+ * @return The response from the API.
13377
+ *
13378
+ * @example
13379
+ * ```ts
13380
+ * const response = await ai.models.embedContent({
13381
+ * model: 'text-embedding-004',
13382
+ * contents: [
13383
+ * 'What is your name?',
13384
+ * 'What is your favorite color?',
13385
+ * ],
13386
+ * config: {
13387
+ * outputDimensionality: 64,
13388
+ * },
13389
+ * });
13390
+ * console.log(response);
13391
+ * ```
13392
+ */
13393
+ this.embedContent = async (params) => {
13394
+ if (!this.apiClient.isVertexAI()) {
13395
+ return await this.embedContentInternal(params);
13396
+ }
13397
+ const isVertexEmbedContentModel = (params.model.includes('gemini') &&
13398
+ params.model !== 'gemini-embedding-001') ||
13399
+ params.model.includes('maas');
13400
+ if (isVertexEmbedContentModel) {
13401
+ const contents = tContents(params.contents);
13402
+ if (contents.length > 1) {
13403
+ throw new Error('The embedContent API for this model only supports one content at a time.');
13404
+ }
13405
+ const paramsPrivate = Object.assign(Object.assign({}, params), { content: contents[0], embeddingApiType: exports.EmbeddingApiType.EMBED_CONTENT });
13406
+ return await this.embedContentInternal(paramsPrivate);
13407
+ }
13408
+ else {
13409
+ const paramsPrivate = Object.assign(Object.assign({}, params), { embeddingApiType: exports.EmbeddingApiType.PREDICT });
13410
+ return await this.embedContentInternal(paramsPrivate);
13411
+ }
13412
+ };
13244
13413
  /**
13245
13414
  * Makes an API request to generate content with a given model.
13246
13415
  *
@@ -13928,14 +14097,17 @@ class Models extends BaseModule {
13928
14097
  * console.log(response);
13929
14098
  * ```
13930
14099
  */
13931
- async embedContent(params) {
14100
+ async embedContentInternal(params) {
13932
14101
  var _a, _b, _c, _d;
13933
14102
  let response;
13934
14103
  let path = '';
13935
14104
  let queryParams = {};
13936
14105
  if (this.apiClient.isVertexAI()) {
13937
- const body = embedContentParametersToVertex(this.apiClient, params);
13938
- path = formatMap('{model}:predict', body['_url']);
14106
+ const body = embedContentParametersPrivateToVertex(this.apiClient, params, params);
14107
+ const endpointUrl = tIsVertexEmbedContentModel(params.model)
14108
+ ? '{model}:embedContent'
14109
+ : '{model}:predict';
14110
+ path = formatMap(endpointUrl, body['_url']);
13939
14111
  queryParams = body['_query'];
13940
14112
  delete body['_url'];
13941
14113
  delete body['_query'];
@@ -13958,14 +14130,14 @@ class Models extends BaseModule {
13958
14130
  });
13959
14131
  });
13960
14132
  return response.then((apiResponse) => {
13961
- const resp = embedContentResponseFromVertex(apiResponse);
14133
+ const resp = embedContentResponseFromVertex(apiResponse, params);
13962
14134
  const typedResp = new EmbedContentResponse();
13963
14135
  Object.assign(typedResp, resp);
13964
14136
  return typedResp;
13965
14137
  });
13966
14138
  }
13967
14139
  else {
13968
- const body = embedContentParametersToMldev(this.apiClient, params);
14140
+ const body = embedContentParametersPrivateToMldev(this.apiClient, params);
13969
14141
  path = formatMap('{model}:batchEmbedContents', body['_url']);
13970
14142
  queryParams = body['_query'];
13971
14143
  delete body['_url'];
@@ -18055,6 +18227,13 @@ class BaseGeminiNextGenAPIClient {
18055
18227
  (Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))) {
18056
18228
  return { bodyHeaders: undefined, body: ReadableStreamFrom(body) };
18057
18229
  }
18230
+ else if (typeof body === 'object' &&
18231
+ headers.values.get('content-type') === 'application/x-www-form-urlencoded') {
18232
+ return {
18233
+ bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
18234
+ body: this.stringifyQuery(body),
18235
+ };
18236
+ }
18058
18237
  else {
18059
18238
  return this.encoder({ body, headers });
18060
18239
  }