@google/genai 0.8.0 → 0.9.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.
package/README.md CHANGED
@@ -23,7 +23,6 @@ The Google Gen AI SDK is designed to work with Gemini 2.0 features.
23
23
  > **API Key Security:** Avoid exposing API keys in client-side code.
24
24
  > Use server-side implementations in production environments.
25
25
 
26
-
27
26
  ## Prerequisites
28
27
 
29
28
  * Node.js version 18 or later
@@ -58,39 +57,6 @@ async function main() {
58
57
  main();
59
58
  ```
60
59
 
61
- ## Web quickstart
62
-
63
- The package contents are also available unzipped in the
64
- `package/` directory of the bucket, so an equivalent web example is:
65
-
66
- ```html
67
- <!DOCTYPE html>
68
- <html lang="en">
69
- <head>
70
- <meta charset="UTF-8" />
71
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
72
- <title>Using My Package</title>
73
- </head>
74
- <body>
75
- <script type="module">
76
- import {GoogleGenAI} from 'https://cdn.jsdelivr.net/npm/@google/genai@latest/+esm'
77
-
78
- const ai = new GoogleGenAI({apiKey:"GEMINI_API_KEY"});
79
-
80
- async function main() {
81
- const response = await ai.models.generateContent({
82
- model: 'gemini-2.0-flash-001',
83
- contents: 'Why is the sky blue?',
84
- });
85
- console.log(response.text);
86
- }
87
-
88
- main();
89
- </script>
90
- </body>
91
- </html>
92
- ```
93
-
94
60
  ## Initialization
95
61
 
96
62
  The Google Gen AI SDK provides support for both the
@@ -253,3 +219,12 @@ The SDK is curently in a preview launch stage, per [Google's launch stages](http
253
219
 
254
220
  > At Preview, products or features are ready for testing by customers. Preview offerings are often publicly announced, but are not necessarily feature-complete, and no SLAs or technical support commitments are provided for these. Unless stated otherwise by Google, Preview offerings are intended for use in test environments only. The average Preview stage lasts about six months.
255
221
 
222
+ ## How is this different from the other Google AI SDKs
223
+ This SDK (`@google/genai`) is Google Deepmind’s "vanilla" SDK for its generative AI offerings, and is where Google Deepmind adds new AI features.
224
+
225
+ Models hosted either on the [Vertex AI platform](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview) or the [Gemini Developer platform](https://ai.google.dev/gemini-api/docs) are accessible through this SDK.
226
+
227
+ Other SDKs may be offering additional AI frameworks on top of this SDK, or may be targeting specific project environments (like Firebase).
228
+
229
+ The `@google/generative_language` and `@google-cloud/vertexai` SDKs are previous iterations of this SDK and are no longer receiving new Gemini 2.0+ features.
230
+
package/dist/genai.d.ts CHANGED
@@ -46,11 +46,11 @@ declare class ApiClient {
46
46
  private shouldPrependVertexProjectPath;
47
47
  request(request: HttpRequest): Promise<HttpResponse>;
48
48
  private patchHttpOptions;
49
- requestStream(request: HttpRequest): Promise<any>;
49
+ requestStream(request: HttpRequest): Promise<AsyncGenerator<HttpResponse>>;
50
50
  private includeExtraHttpOptionsToRequestInit;
51
51
  private unaryApiCall;
52
52
  private streamApiCall;
53
- processStreamResponse(response: Response): AsyncGenerator<any>;
53
+ processStreamResponse(response: Response): AsyncGenerator<HttpResponse>;
54
54
  private apiCall;
55
55
  getDefaultHeaders(): Record<string, string>;
56
56
  private getHeadersInternal;
@@ -237,8 +237,8 @@ export declare class Caches extends BaseModule {
237
237
  *
238
238
  * @remarks
239
239
  * Context caching is only supported for specific models. See [Gemini
240
- * Developer API reference] (https://ai.google.dev/gemini-api/docs/caching?lang=node/context-cac)
241
- * and [Vertex AI reference] (https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview#supported_models)
240
+ * Developer API reference](https://ai.google.dev/gemini-api/docs/caching?lang=node/context-cac)
241
+ * and [Vertex AI reference](https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview#supported_models)
242
242
  * for more information.
243
243
  *
244
244
  * @param params - The parameters for the create request.
@@ -540,7 +540,7 @@ export declare interface ContentEmbeddingStatistics {
540
540
  tokenCount?: number;
541
541
  }
542
542
 
543
- export declare type ContentListUnion = ContentUnion[] | ContentUnion;
543
+ export declare type ContentListUnion = Content | Content[] | PartUnion | PartUnion[];
544
544
 
545
545
  export declare type ContentUnion = Content | PartUnion[] | PartUnion;
546
546
 
@@ -897,6 +897,14 @@ export declare interface ExecutableCode {
897
897
  language?: Language;
898
898
  }
899
899
 
900
+ /** Options for feature selection preference. */
901
+ export declare enum FeatureSelectionPreference {
902
+ FEATURE_SELECTION_PREFERENCE_UNSPECIFIED = "FEATURE_SELECTION_PREFERENCE_UNSPECIFIED",
903
+ PRIORITIZE_QUALITY = "PRIORITIZE_QUALITY",
904
+ BALANCED = "BALANCED",
905
+ PRIORITIZE_COST = "PRIORITIZE_COST"
906
+ }
907
+
900
908
  export declare interface FetchPredictOperationConfig {
901
909
  /** Used to override HTTP request options. */
902
910
  httpOptions?: HttpOptions;
@@ -1239,6 +1247,9 @@ export declare interface GenerateContentConfig {
1239
1247
  /** Configuration for model router requests.
1240
1248
  */
1241
1249
  routingConfig?: GenerationConfigRoutingConfig;
1250
+ /** Configuration for model selection.
1251
+ */
1252
+ modelSelectionConfig?: ModelSelectionConfig;
1242
1253
  /** Safety settings in the request to block unsafe content in the
1243
1254
  response.
1244
1255
  */
@@ -2128,17 +2139,20 @@ export declare class Live {
2128
2139
  @experimental
2129
2140
 
2130
2141
  @remarks
2131
- If using the Gemini API, Live is currently only supported behind API
2132
- version `v1alpha`. Ensure that the API version is set to `v1alpha` when
2133
- initializing the SDK if relying on the Gemini API.
2134
2142
 
2135
2143
  @param params - The parameters for establishing a connection to the model.
2136
2144
  @return A live session.
2137
2145
 
2138
2146
  @example
2139
2147
  ```ts
2148
+ let model: string;
2149
+ if (GOOGLE_GENAI_USE_VERTEXAI) {
2150
+ model = 'gemini-2.0-flash-live-preview-04-09';
2151
+ } else {
2152
+ model = 'gemini-2.0-flash-live-001';
2153
+ }
2140
2154
  const session = await ai.live.connect({
2141
- model: 'gemini-2.0-flash-exp',
2155
+ model: model,
2142
2156
  config: {
2143
2157
  responseModalities: [Modality.AUDIO],
2144
2158
  },
@@ -2253,7 +2267,7 @@ export declare interface LiveClientSetup {
2253
2267
  /** The user provided system instructions for the model.
2254
2268
  Note: only text should be used in parts and content in each part will be
2255
2269
  in a separate paragraph. */
2256
- systemInstruction?: Content;
2270
+ systemInstruction?: ContentUnion;
2257
2271
  /** A list of `Tools` the model may use to generate the next response.
2258
2272
 
2259
2273
  A `Tool` is a piece of code that enables the system to interact with
@@ -2270,6 +2284,13 @@ export declare interface LiveClientSetup {
2270
2284
 
2271
2285
  If included, server will compress context window to fit into given length. */
2272
2286
  contextWindowCompression?: ContextWindowCompressionConfig;
2287
+ /** The transcription of the input aligns with the input audio language.
2288
+ */
2289
+ inputAudioTranscription?: AudioTranscriptionConfig;
2290
+ /** The transcription of the output aligns with the language code
2291
+ specified for the output audio.
2292
+ */
2293
+ outputAudioTranscription?: AudioTranscriptionConfig;
2273
2294
  }
2274
2295
 
2275
2296
  /** Client generated response to a `ToolCall` received from the server.
@@ -2295,13 +2316,42 @@ export declare interface LiveConnectConfig {
2295
2316
  modalities that the model can return. Defaults to AUDIO if not specified.
2296
2317
  */
2297
2318
  responseModalities?: Modality[];
2319
+ /** Value that controls the degree of randomness in token selection.
2320
+ Lower temperatures are good for prompts that require a less open-ended or
2321
+ creative response, while higher temperatures can lead to more diverse or
2322
+ creative results.
2323
+ */
2324
+ temperature?: number;
2325
+ /** Tokens are selected from the most to least probable until the sum
2326
+ of their probabilities equals this value. Use a lower value for less
2327
+ random responses and a higher value for more random responses.
2328
+ */
2329
+ topP?: number;
2330
+ /** For each token selection step, the ``top_k`` tokens with the
2331
+ highest probabilities are sampled. Then tokens are further filtered based
2332
+ on ``top_p`` with the final token selected using temperature sampling. Use
2333
+ a lower number for less random responses and a higher number for more
2334
+ random responses.
2335
+ */
2336
+ topK?: number;
2337
+ /** Maximum number of tokens that can be generated in the response.
2338
+ */
2339
+ maxOutputTokens?: number;
2340
+ /** If specified, the media resolution specified will be used.
2341
+ */
2342
+ mediaResolution?: MediaResolution;
2343
+ /** When ``seed`` is fixed to a specific number, the model makes a best
2344
+ effort to provide the same response for repeated requests. By default, a
2345
+ random number is used.
2346
+ */
2347
+ seed?: number;
2298
2348
  /** The speech generation configuration.
2299
2349
  */
2300
2350
  speechConfig?: SpeechConfig;
2301
2351
  /** The user provided system instructions for the model.
2302
2352
  Note: only text should be used in parts and content in each part will be
2303
2353
  in a separate paragraph. */
2304
- systemInstruction?: Content;
2354
+ systemInstruction?: ContentUnion;
2305
2355
  /** A list of `Tools` the model may use to generate the next response.
2306
2356
 
2307
2357
  A `Tool` is a piece of code that enables the system to interact with
@@ -2312,6 +2362,13 @@ export declare interface LiveConnectConfig {
2312
2362
 
2313
2363
  If included the server will send SessionResumptionUpdate messages. */
2314
2364
  sessionResumption?: SessionResumptionConfig;
2365
+ /** The transcription of the input aligns with the input audio language.
2366
+ */
2367
+ inputAudioTranscription?: AudioTranscriptionConfig;
2368
+ /** The transcription of the output aligns with the language code
2369
+ specified for the output audio.
2370
+ */
2371
+ outputAudioTranscription?: AudioTranscriptionConfig;
2315
2372
  /** Configures the realtime input behavior in BidiGenerateContent. */
2316
2373
  realtimeInputConfig?: RealtimeInputConfig;
2317
2374
  /** Configures context window compression mechanism.
@@ -2379,6 +2436,15 @@ export declare interface LiveServerContent {
2379
2436
  has finished generating all content. This is a signal to the client
2380
2437
  that it can stop sending messages. */
2381
2438
  generationComplete?: boolean;
2439
+ /** Input transcription. The transcription is independent to the model
2440
+ turn which means it doesn’t imply any ordering between transcription and
2441
+ model turn. */
2442
+ inputTranscription?: Transcription;
2443
+ /** Output transcription. The transcription is independent to the model
2444
+ turn which means it doesn’t imply any ordering between transcription and
2445
+ model turn.
2446
+ */
2447
+ outputTranscription?: Transcription;
2382
2448
  }
2383
2449
 
2384
2450
  /** Server will not be able to service client soon. */
@@ -2799,6 +2865,12 @@ export declare class Models extends BaseModule {
2799
2865
  generateVideos(params: types.GenerateVideosParameters): Promise<types.GenerateVideosOperation>;
2800
2866
  }
2801
2867
 
2868
+ /** Config for model selection. */
2869
+ export declare interface ModelSelectionConfig {
2870
+ /** Options for feature selection preference. */
2871
+ featureSelectionPreference?: FeatureSelectionPreference;
2872
+ }
2873
+
2802
2874
  /** Parameters for the get method of the operations module. */
2803
2875
  export declare interface OperationGetParameters {
2804
2876
  /** The operation to be retrieved. */
@@ -3006,6 +3078,54 @@ export declare interface PrebuiltVoiceConfig {
3006
3078
  voiceName?: string;
3007
3079
  }
3008
3080
 
3081
+ /** Specifies the context retrieval config. */
3082
+ export declare interface RagRetrievalConfig {
3083
+ /** Optional. Config for filters. */
3084
+ filter?: RagRetrievalConfigFilter;
3085
+ /** Optional. Config for Hybrid Search. */
3086
+ hybridSearch?: RagRetrievalConfigHybridSearch;
3087
+ /** Optional. Config for ranking and reranking. */
3088
+ ranking?: RagRetrievalConfigRanking;
3089
+ /** Optional. The number of contexts to retrieve. */
3090
+ topK?: number;
3091
+ }
3092
+
3093
+ /** Config for filters. */
3094
+ export declare interface RagRetrievalConfigFilter {
3095
+ /** Optional. String for metadata filtering. */
3096
+ metadataFilter?: string;
3097
+ /** Optional. Only returns contexts with vector distance smaller than the threshold. */
3098
+ vectorDistanceThreshold?: number;
3099
+ /** Optional. Only returns contexts with vector similarity larger than the threshold. */
3100
+ vectorSimilarityThreshold?: number;
3101
+ }
3102
+
3103
+ /** Config for Hybrid Search. */
3104
+ export declare interface RagRetrievalConfigHybridSearch {
3105
+ /** Optional. Alpha value controls the weight between dense and sparse vector search results. The range is [0, 1], while 0 means sparse vector search only and 1 means dense vector search only. The default value is 0.5 which balances sparse and dense vector search equally. */
3106
+ alpha?: number;
3107
+ }
3108
+
3109
+ /** Config for ranking and reranking. */
3110
+ export declare interface RagRetrievalConfigRanking {
3111
+ /** Optional. Config for LlmRanker. */
3112
+ llmRanker?: RagRetrievalConfigRankingLlmRanker;
3113
+ /** Optional. Config for Rank Service. */
3114
+ rankService?: RagRetrievalConfigRankingRankService;
3115
+ }
3116
+
3117
+ /** Config for LlmRanker. */
3118
+ export declare interface RagRetrievalConfigRankingLlmRanker {
3119
+ /** Optional. The model name used for ranking. Format: `gemini-1.5-pro` */
3120
+ modelName?: string;
3121
+ }
3122
+
3123
+ /** Config for Rank Service. */
3124
+ export declare interface RagRetrievalConfigRankingRankService {
3125
+ /** Optional. The model name of the rank service. Format: `semantic-ranker-512@latest` */
3126
+ modelName?: string;
3127
+ }
3128
+
3009
3129
  /** A raw reference image.
3010
3130
 
3011
3131
  A raw reference image represents the base image to edit, provided by the user.
@@ -3328,8 +3448,14 @@ export declare class Session {
3328
3448
 
3329
3449
  @example
3330
3450
  ```ts
3451
+ let model: string;
3452
+ if (GOOGLE_GENAI_USE_VERTEXAI) {
3453
+ model = 'gemini-2.0-flash-live-preview-04-09';
3454
+ } else {
3455
+ model = 'gemini-2.0-flash-live-001';
3456
+ }
3331
3457
  const session = await ai.live.connect({
3332
- model: 'gemini-2.0-flash-exp',
3458
+ model: model,
3333
3459
  config: {
3334
3460
  responseModalities: [Modality.AUDIO],
3335
3461
  }
@@ -3372,6 +3498,10 @@ export declare interface SpeechConfig {
3372
3498
  /** The configuration for the speaker to use.
3373
3499
  */
3374
3500
  voiceConfig?: VoiceConfig;
3501
+ /** Language code (ISO 639. e.g. en-US) for the speech synthesization.
3502
+ Only available for Live API.
3503
+ */
3504
+ languageCode?: string;
3375
3505
  }
3376
3506
 
3377
3507
  export declare type SpeechConfigUnion = SpeechConfig | string;
@@ -3526,6 +3656,16 @@ export declare enum TrafficType {
3526
3656
  PROVISIONED_THROUGHPUT = "PROVISIONED_THROUGHPUT"
3527
3657
  }
3528
3658
 
3659
+ /** Audio transcription in Server Conent. */
3660
+ export declare interface Transcription {
3661
+ /** Transcription text.
3662
+ */
3663
+ text?: string;
3664
+ /** The bool indicates the end of the transcription.
3665
+ */
3666
+ finished?: boolean;
3667
+ }
3668
+
3529
3669
  /** A tuned machine learning model. */
3530
3670
  export declare interface TunedModelInfo {
3531
3671
  /** ID of the base model that you want to tune. */
@@ -3579,6 +3719,7 @@ declare namespace types {
3579
3719
  TrafficType,
3580
3720
  Modality,
3581
3721
  MediaResolution,
3722
+ FeatureSelectionPreference,
3582
3723
  DynamicRetrievalConfigMode,
3583
3724
  FunctionCallingConfigMode,
3584
3725
  SafetyFilterLevel,
@@ -3605,6 +3746,7 @@ declare namespace types {
3605
3746
  Content,
3606
3747
  HttpOptions,
3607
3748
  Schema,
3749
+ ModelSelectionConfig,
3608
3750
  SafetySetting,
3609
3751
  FunctionDeclaration,
3610
3752
  GoogleSearch,
@@ -3612,6 +3754,12 @@ declare namespace types {
3612
3754
  GoogleSearchRetrieval,
3613
3755
  VertexAISearch,
3614
3756
  VertexRagStoreRagResource,
3757
+ RagRetrievalConfigFilter,
3758
+ RagRetrievalConfigHybridSearch,
3759
+ RagRetrievalConfigRankingLlmRanker,
3760
+ RagRetrievalConfigRankingRankService,
3761
+ RagRetrievalConfigRanking,
3762
+ RagRetrievalConfig,
3615
3763
  VertexRagStore,
3616
3764
  Retrieval,
3617
3765
  ToolCodeExecution,
@@ -3732,6 +3880,7 @@ declare namespace types {
3732
3880
  SubjectReferenceConfig,
3733
3881
  SubjectReferenceImage,
3734
3882
  LiveServerSetupComplete,
3883
+ Transcription,
3735
3884
  LiveServerContent,
3736
3885
  LiveServerToolCall,
3737
3886
  LiveServerToolCallCancellation,
@@ -3744,6 +3893,7 @@ declare namespace types {
3744
3893
  SessionResumptionConfig,
3745
3894
  SlidingWindow,
3746
3895
  ContextWindowCompressionConfig,
3896
+ AudioTranscriptionConfig,
3747
3897
  LiveClientSetup,
3748
3898
  LiveClientContent,
3749
3899
  ActivityStart,
@@ -3751,7 +3901,6 @@ declare namespace types {
3751
3901
  LiveClientRealtimeInput,
3752
3902
  LiveClientToolResponse,
3753
3903
  LiveClientMessage,
3754
- AudioTranscriptionConfig,
3755
3904
  LiveConnectConfig,
3756
3905
  LiveConnectParameters,
3757
3906
  CreateChatParameters,
@@ -3904,6 +4053,8 @@ export declare interface VertexRagStore {
3904
4053
  ragCorpora?: string[];
3905
4054
  /** Optional. The representation of the rag source. It can be used to specify corpus only or ragfiles. Currently only support one corpus or multiple files from one corpus. In the future we may open up multiple corpora support. */
3906
4055
  ragResources?: VertexRagStoreRagResource[];
4056
+ /** Optional. The retrieval config for the Rag query. */
4057
+ ragRetrievalConfig?: RagRetrievalConfig;
3907
4058
  /** Optional. Number of top k results to return from the selected corpora. */
3908
4059
  similarityTopK?: number;
3909
4060
  /** Optional. Only return results with vector distance smaller than the threshold. */