@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.
@@ -1366,6 +1366,18 @@ var Environment;
1366
1366
  */
1367
1367
  Environment["ENVIRONMENT_BROWSER"] = "ENVIRONMENT_BROWSER";
1368
1368
  })(Environment || (Environment = {}));
1369
+ /** Enum representing the Vertex embedding API to use. */
1370
+ var EmbeddingApiType;
1371
+ (function (EmbeddingApiType) {
1372
+ /**
1373
+ * predict API endpoint (default)
1374
+ */
1375
+ EmbeddingApiType["PREDICT"] = "PREDICT";
1376
+ /**
1377
+ * embedContent API Endpoint
1378
+ */
1379
+ EmbeddingApiType["EMBED_CONTENT"] = "EMBED_CONTENT";
1380
+ })(EmbeddingApiType || (EmbeddingApiType = {}));
1369
1381
  /** Enum that controls the safety filter level for objectionable content. */
1370
1382
  var SafetyFilterLevel;
1371
1383
  (function (SafetyFilterLevel) {
@@ -3397,6 +3409,10 @@ function tJobState(state) {
3397
3409
  return stateString;
3398
3410
  }
3399
3411
  }
3412
+ function tIsVertexEmbedContentModel(model) {
3413
+ return ((model.includes('gemini') && model !== 'gemini-embedding-001') ||
3414
+ model.includes('maas'));
3415
+ }
3400
3416
 
3401
3417
  /**
3402
3418
  * @license
@@ -8638,33 +8654,103 @@ function embedContentConfigToMldev(fromObject, parentObject, _rootObject) {
8638
8654
  }
8639
8655
  return toObject;
8640
8656
  }
8641
- function embedContentConfigToVertex(fromObject, parentObject, _rootObject) {
8657
+ function embedContentConfigToVertex(fromObject, parentObject, rootObject) {
8642
8658
  const toObject = {};
8643
- const fromTaskType = getValueByPath(fromObject, ['taskType']);
8644
- if (parentObject !== undefined && fromTaskType != null) {
8645
- setValueByPath(parentObject, ['instances[]', 'task_type'], fromTaskType);
8659
+ let discriminatorTaskType = getValueByPath(rootObject, [
8660
+ 'embeddingApiType',
8661
+ ]);
8662
+ if (discriminatorTaskType === undefined) {
8663
+ discriminatorTaskType = 'PREDICT';
8646
8664
  }
8647
- const fromTitle = getValueByPath(fromObject, ['title']);
8648
- if (parentObject !== undefined && fromTitle != null) {
8649
- setValueByPath(parentObject, ['instances[]', 'title'], fromTitle);
8665
+ if (discriminatorTaskType === 'PREDICT') {
8666
+ const fromTaskType = getValueByPath(fromObject, ['taskType']);
8667
+ if (parentObject !== undefined && fromTaskType != null) {
8668
+ setValueByPath(parentObject, ['instances[]', 'task_type'], fromTaskType);
8669
+ }
8650
8670
  }
8651
- const fromOutputDimensionality = getValueByPath(fromObject, [
8652
- 'outputDimensionality',
8671
+ else if (discriminatorTaskType === 'EMBED_CONTENT') {
8672
+ const fromTaskType = getValueByPath(fromObject, ['taskType']);
8673
+ if (parentObject !== undefined && fromTaskType != null) {
8674
+ setValueByPath(parentObject, ['taskType'], fromTaskType);
8675
+ }
8676
+ }
8677
+ let discriminatorTitle = getValueByPath(rootObject, [
8678
+ 'embeddingApiType',
8653
8679
  ]);
8654
- if (parentObject !== undefined && fromOutputDimensionality != null) {
8655
- setValueByPath(parentObject, ['parameters', 'outputDimensionality'], fromOutputDimensionality);
8680
+ if (discriminatorTitle === undefined) {
8681
+ discriminatorTitle = 'PREDICT';
8656
8682
  }
8657
- const fromMimeType = getValueByPath(fromObject, ['mimeType']);
8658
- if (parentObject !== undefined && fromMimeType != null) {
8659
- setValueByPath(parentObject, ['instances[]', 'mimeType'], fromMimeType);
8683
+ if (discriminatorTitle === 'PREDICT') {
8684
+ const fromTitle = getValueByPath(fromObject, ['title']);
8685
+ if (parentObject !== undefined && fromTitle != null) {
8686
+ setValueByPath(parentObject, ['instances[]', 'title'], fromTitle);
8687
+ }
8660
8688
  }
8661
- const fromAutoTruncate = getValueByPath(fromObject, ['autoTruncate']);
8662
- if (parentObject !== undefined && fromAutoTruncate != null) {
8663
- setValueByPath(parentObject, ['parameters', 'autoTruncate'], fromAutoTruncate);
8689
+ else if (discriminatorTitle === 'EMBED_CONTENT') {
8690
+ const fromTitle = getValueByPath(fromObject, ['title']);
8691
+ if (parentObject !== undefined && fromTitle != null) {
8692
+ setValueByPath(parentObject, ['title'], fromTitle);
8693
+ }
8694
+ }
8695
+ let discriminatorOutputDimensionality = getValueByPath(rootObject, [
8696
+ 'embeddingApiType',
8697
+ ]);
8698
+ if (discriminatorOutputDimensionality === undefined) {
8699
+ discriminatorOutputDimensionality = 'PREDICT';
8700
+ }
8701
+ if (discriminatorOutputDimensionality === 'PREDICT') {
8702
+ const fromOutputDimensionality = getValueByPath(fromObject, [
8703
+ 'outputDimensionality',
8704
+ ]);
8705
+ if (parentObject !== undefined && fromOutputDimensionality != null) {
8706
+ setValueByPath(parentObject, ['parameters', 'outputDimensionality'], fromOutputDimensionality);
8707
+ }
8708
+ }
8709
+ else if (discriminatorOutputDimensionality === 'EMBED_CONTENT') {
8710
+ const fromOutputDimensionality = getValueByPath(fromObject, [
8711
+ 'outputDimensionality',
8712
+ ]);
8713
+ if (parentObject !== undefined && fromOutputDimensionality != null) {
8714
+ setValueByPath(parentObject, ['outputDimensionality'], fromOutputDimensionality);
8715
+ }
8716
+ }
8717
+ let discriminatorMimeType = getValueByPath(rootObject, [
8718
+ 'embeddingApiType',
8719
+ ]);
8720
+ if (discriminatorMimeType === undefined) {
8721
+ discriminatorMimeType = 'PREDICT';
8722
+ }
8723
+ if (discriminatorMimeType === 'PREDICT') {
8724
+ const fromMimeType = getValueByPath(fromObject, ['mimeType']);
8725
+ if (parentObject !== undefined && fromMimeType != null) {
8726
+ setValueByPath(parentObject, ['instances[]', 'mimeType'], fromMimeType);
8727
+ }
8728
+ }
8729
+ let discriminatorAutoTruncate = getValueByPath(rootObject, [
8730
+ 'embeddingApiType',
8731
+ ]);
8732
+ if (discriminatorAutoTruncate === undefined) {
8733
+ discriminatorAutoTruncate = 'PREDICT';
8734
+ }
8735
+ if (discriminatorAutoTruncate === 'PREDICT') {
8736
+ const fromAutoTruncate = getValueByPath(fromObject, [
8737
+ 'autoTruncate',
8738
+ ]);
8739
+ if (parentObject !== undefined && fromAutoTruncate != null) {
8740
+ setValueByPath(parentObject, ['parameters', 'autoTruncate'], fromAutoTruncate);
8741
+ }
8742
+ }
8743
+ else if (discriminatorAutoTruncate === 'EMBED_CONTENT') {
8744
+ const fromAutoTruncate = getValueByPath(fromObject, [
8745
+ 'autoTruncate',
8746
+ ]);
8747
+ if (parentObject !== undefined && fromAutoTruncate != null) {
8748
+ setValueByPath(parentObject, ['autoTruncate'], fromAutoTruncate);
8749
+ }
8664
8750
  }
8665
8751
  return toObject;
8666
8752
  }
8667
- function embedContentParametersToMldev(apiClient, fromObject, rootObject) {
8753
+ function embedContentParametersPrivateToMldev(apiClient, fromObject, rootObject) {
8668
8754
  const toObject = {};
8669
8755
  const fromModel = getValueByPath(fromObject, ['model']);
8670
8756
  if (fromModel != null) {
@@ -8680,6 +8766,10 @@ function embedContentParametersToMldev(apiClient, fromObject, rootObject) {
8680
8766
  }
8681
8767
  setValueByPath(toObject, ['requests[]', 'content'], transformedList);
8682
8768
  }
8769
+ const fromContent = getValueByPath(fromObject, ['content']);
8770
+ if (fromContent != null) {
8771
+ contentToMldev$1(tContent(fromContent));
8772
+ }
8683
8773
  const fromConfig = getValueByPath(fromObject, ['config']);
8684
8774
  if (fromConfig != null) {
8685
8775
  embedContentConfigToMldev(fromConfig, toObject);
@@ -8690,25 +8780,45 @@ function embedContentParametersToMldev(apiClient, fromObject, rootObject) {
8690
8780
  }
8691
8781
  return toObject;
8692
8782
  }
8693
- function embedContentParametersToVertex(apiClient, fromObject, rootObject) {
8783
+ function embedContentParametersPrivateToVertex(apiClient, fromObject, rootObject) {
8694
8784
  const toObject = {};
8695
8785
  const fromModel = getValueByPath(fromObject, ['model']);
8696
8786
  if (fromModel != null) {
8697
8787
  setValueByPath(toObject, ['_url', 'model'], tModel(apiClient, fromModel));
8698
8788
  }
8699
- const fromContents = getValueByPath(fromObject, ['contents']);
8700
- if (fromContents != null) {
8701
- let transformedList = tContentsForEmbed(apiClient, fromContents);
8702
- if (Array.isArray(transformedList)) {
8703
- transformedList = transformedList.map((item) => {
8704
- return item;
8705
- });
8789
+ let discriminatorContents = getValueByPath(rootObject, [
8790
+ 'embeddingApiType',
8791
+ ]);
8792
+ if (discriminatorContents === undefined) {
8793
+ discriminatorContents = 'PREDICT';
8794
+ }
8795
+ if (discriminatorContents === 'PREDICT') {
8796
+ const fromContents = getValueByPath(fromObject, ['contents']);
8797
+ if (fromContents != null) {
8798
+ let transformedList = tContentsForEmbed(apiClient, fromContents);
8799
+ if (Array.isArray(transformedList)) {
8800
+ transformedList = transformedList.map((item) => {
8801
+ return item;
8802
+ });
8803
+ }
8804
+ setValueByPath(toObject, ['instances[]', 'content'], transformedList);
8805
+ }
8806
+ }
8807
+ let discriminatorContent = getValueByPath(rootObject, [
8808
+ 'embeddingApiType',
8809
+ ]);
8810
+ if (discriminatorContent === undefined) {
8811
+ discriminatorContent = 'PREDICT';
8812
+ }
8813
+ if (discriminatorContent === 'EMBED_CONTENT') {
8814
+ const fromContent = getValueByPath(fromObject, ['content']);
8815
+ if (fromContent != null) {
8816
+ setValueByPath(toObject, ['content'], tContent(fromContent));
8706
8817
  }
8707
- setValueByPath(toObject, ['instances[]', 'content'], transformedList);
8708
8818
  }
8709
8819
  const fromConfig = getValueByPath(fromObject, ['config']);
8710
8820
  if (fromConfig != null) {
8711
- embedContentConfigToVertex(fromConfig, toObject);
8821
+ embedContentConfigToVertex(fromConfig, toObject, rootObject);
8712
8822
  }
8713
8823
  return toObject;
8714
8824
  }
@@ -8761,6 +8871,24 @@ function embedContentResponseFromVertex(fromObject, rootObject) {
8761
8871
  if (fromMetadata != null) {
8762
8872
  setValueByPath(toObject, ['metadata'], fromMetadata);
8763
8873
  }
8874
+ if (rootObject &&
8875
+ getValueByPath(rootObject, ['embeddingApiType']) === 'EMBED_CONTENT') {
8876
+ const embedding = getValueByPath(fromObject, ['embedding']);
8877
+ const usageMetadata = getValueByPath(fromObject, ['usageMetadata']);
8878
+ const truncated = getValueByPath(fromObject, ['truncated']);
8879
+ if (embedding) {
8880
+ const stats = {};
8881
+ if (usageMetadata &&
8882
+ usageMetadata['promptTokenCount']) {
8883
+ stats.tokenCount = usageMetadata['promptTokenCount'];
8884
+ }
8885
+ if (truncated) {
8886
+ stats.truncated = truncated;
8887
+ }
8888
+ embedding.statistics = stats;
8889
+ setValueByPath(toObject, ['embeddings'], [embedding]);
8890
+ }
8891
+ }
8764
8892
  return toObject;
8765
8893
  }
8766
8894
  function endpointFromVertex(fromObject, _rootObject) {
@@ -11618,7 +11746,7 @@ const CONTENT_TYPE_HEADER = 'Content-Type';
11618
11746
  const SERVER_TIMEOUT_HEADER = 'X-Server-Timeout';
11619
11747
  const USER_AGENT_HEADER = 'User-Agent';
11620
11748
  const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
11621
- const SDK_VERSION = '1.41.0'; // x-release-please-version
11749
+ const SDK_VERSION = '1.42.0'; // x-release-please-version
11622
11750
  const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
11623
11751
  const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
11624
11752
  const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
@@ -13219,6 +13347,47 @@ class Models extends BaseModule {
13219
13347
  constructor(apiClient) {
13220
13348
  super();
13221
13349
  this.apiClient = apiClient;
13350
+ /**
13351
+ * Calculates embeddings for the given contents.
13352
+ *
13353
+ * @param params - The parameters for embedding contents.
13354
+ * @return The response from the API.
13355
+ *
13356
+ * @example
13357
+ * ```ts
13358
+ * const response = await ai.models.embedContent({
13359
+ * model: 'text-embedding-004',
13360
+ * contents: [
13361
+ * 'What is your name?',
13362
+ * 'What is your favorite color?',
13363
+ * ],
13364
+ * config: {
13365
+ * outputDimensionality: 64,
13366
+ * },
13367
+ * });
13368
+ * console.log(response);
13369
+ * ```
13370
+ */
13371
+ this.embedContent = async (params) => {
13372
+ if (!this.apiClient.isVertexAI()) {
13373
+ return await this.embedContentInternal(params);
13374
+ }
13375
+ const isVertexEmbedContentModel = (params.model.includes('gemini') &&
13376
+ params.model !== 'gemini-embedding-001') ||
13377
+ params.model.includes('maas');
13378
+ if (isVertexEmbedContentModel) {
13379
+ const contents = tContents(params.contents);
13380
+ if (contents.length > 1) {
13381
+ throw new Error('The embedContent API for this model only supports one content at a time.');
13382
+ }
13383
+ const paramsPrivate = Object.assign(Object.assign({}, params), { content: contents[0], embeddingApiType: EmbeddingApiType.EMBED_CONTENT });
13384
+ return await this.embedContentInternal(paramsPrivate);
13385
+ }
13386
+ else {
13387
+ const paramsPrivate = Object.assign(Object.assign({}, params), { embeddingApiType: EmbeddingApiType.PREDICT });
13388
+ return await this.embedContentInternal(paramsPrivate);
13389
+ }
13390
+ };
13222
13391
  /**
13223
13392
  * Makes an API request to generate content with a given model.
13224
13393
  *
@@ -13906,14 +14075,17 @@ class Models extends BaseModule {
13906
14075
  * console.log(response);
13907
14076
  * ```
13908
14077
  */
13909
- async embedContent(params) {
14078
+ async embedContentInternal(params) {
13910
14079
  var _a, _b, _c, _d;
13911
14080
  let response;
13912
14081
  let path = '';
13913
14082
  let queryParams = {};
13914
14083
  if (this.apiClient.isVertexAI()) {
13915
- const body = embedContentParametersToVertex(this.apiClient, params);
13916
- path = formatMap('{model}:predict', body['_url']);
14084
+ const body = embedContentParametersPrivateToVertex(this.apiClient, params, params);
14085
+ const endpointUrl = tIsVertexEmbedContentModel(params.model)
14086
+ ? '{model}:embedContent'
14087
+ : '{model}:predict';
14088
+ path = formatMap(endpointUrl, body['_url']);
13917
14089
  queryParams = body['_query'];
13918
14090
  delete body['_url'];
13919
14091
  delete body['_query'];
@@ -13936,14 +14108,14 @@ class Models extends BaseModule {
13936
14108
  });
13937
14109
  });
13938
14110
  return response.then((apiResponse) => {
13939
- const resp = embedContentResponseFromVertex(apiResponse);
14111
+ const resp = embedContentResponseFromVertex(apiResponse, params);
13940
14112
  const typedResp = new EmbedContentResponse();
13941
14113
  Object.assign(typedResp, resp);
13942
14114
  return typedResp;
13943
14115
  });
13944
14116
  }
13945
14117
  else {
13946
- const body = embedContentParametersToMldev(this.apiClient, params);
14118
+ const body = embedContentParametersPrivateToMldev(this.apiClient, params);
13947
14119
  path = formatMap('{model}:batchEmbedContents', body['_url']);
13948
14120
  queryParams = body['_query'];
13949
14121
  delete body['_url'];
@@ -18033,6 +18205,13 @@ class BaseGeminiNextGenAPIClient {
18033
18205
  (Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))) {
18034
18206
  return { bodyHeaders: undefined, body: ReadableStreamFrom(body) };
18035
18207
  }
18208
+ else if (typeof body === 'object' &&
18209
+ headers.values.get('content-type') === 'application/x-www-form-urlencoded') {
18210
+ return {
18211
+ bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
18212
+ body: this.stringifyQuery(body),
18213
+ };
18214
+ }
18036
18215
  else {
18037
18216
  return this.encoder({ body, headers });
18038
18217
  }
@@ -19982,5 +20161,5 @@ function getApiKeyFromEnv() {
19982
20161
  return envGoogleApiKey || envGeminiApiKey || undefined;
19983
20162
  }
19984
20163
 
19985
- 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 };
20164
+ 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 };
19986
20165
  //# sourceMappingURL=index.mjs.map