包详细信息

@google/genai

googleapis1mApache-2.01.7.0

NPM Downloads Node Current

自述文件

Google Gen AI SDK for TypeScript and JavaScript

NPM Downloads Node Current


Documentation: https://googleapis.github.io/js-genai/


The Google Gen AI JavaScript SDK is designed for TypeScript and JavaScript developers to build applications powered by Gemini. The SDK supports both the Gemini Developer API and Vertex AI.

The Google Gen AI SDK is designed to work with Gemini 2.0 features.

[!CAUTION] API Key Security: Avoid exposing API keys in client-side code. Use server-side implementations in production environments.

Prerequisites

  1. Node.js version 20 or later

The following are required for Vertex AI users (excluding Vertex AI Studio)

  1. Select or create a Google Cloud project.
  2. Enable billing for your project.
  3. Enable the Vertex AI API.
  4. Configure authentication for your project.

    gcloud auth application-default login
    

    A list of accepted authentication options are listed in GoogleAuthOptions interface of google-auth-library-node.js GitHub repo.

Installation

To install the SDK, run the following command:

npm install @google/genai

Quickstart

The simplest way to get started is to use an API key from Google AI Studio:

import {GoogleGenAI} from '@google/genai';
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;

const ai = new GoogleGenAI({apiKey: GEMINI_API_KEY});

async function main() {
  const response = await ai.models.generateContent({
    model: 'gemini-2.0-flash-001',
    contents: 'Why is the sky blue?',
  });
  console.log(response.text);
}

main();

Initialization

The Google Gen AI SDK provides support for both the Google AI Studio and Vertex AI implementations of the Gemini API.

Gemini Developer API

For server-side applications, initialize using an API key, which can be acquired from Google AI Studio:

import { GoogleGenAI } from '@google/genai';
const ai = new GoogleGenAI({apiKey: 'GEMINI_API_KEY'});

Browser

[!CAUTION] API Key Security: Avoid exposing API keys in client-side code. Use server-side implementations in production environments.

In the browser the initialization code is identical:

import { GoogleGenAI } from '@google/genai';
const ai = new GoogleGenAI({apiKey: 'GEMINI_API_KEY'});

Vertex AI

Sample code for VertexAI initialization:

import { GoogleGenAI } from '@google/genai';

const ai = new GoogleGenAI({
    vertexai: true,
    project: 'your_project',
    location: 'your_location',
});

(Optional) (NodeJS only) Using environment variables:

For NodeJS environments, you can create a client by configuring the necessary environment variables. Configuration setup instructions depends on whether you're using the Gemini Developer API or the Gemini API in Vertex AI.

Gemini Developer API: Set GOOGLE_API_KEY as shown below:

export GOOGLE_API_KEY='your-api-key'

Gemini API on Vertex AI: Set GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION, as shown below:

export GOOGLE_GENAI_USE_VERTEXAI=true
export GOOGLE_CLOUD_PROJECT='your-project-id'
export GOOGLE_CLOUD_LOCATION='us-central1'
import {GoogleGenAI} from '@google/genai';

const ai = new GoogleGenAI();

API Selection

By default, the SDK uses the beta API endpoints provided by Google to support preview features in the APIs. The stable API endpoints can be selected by setting the API version to v1.

To set the API version use apiVersion. For example, to set the API version to v1 for Vertex AI:

const ai = new GoogleGenAI({
    vertexai: true,
    project: 'your_project',
    location: 'your_location',
    apiVersion: 'v1'
});

To set the API version to v1alpha for the Gemini Developer API:

const ai = new GoogleGenAI({
    apiKey: 'GEMINI_API_KEY',
    apiVersion: 'v1alpha'
});

GoogleGenAI overview

All API features are accessed through an instance of the GoogleGenAI classes. The submodules bundle together related API methods:

  • ai.models: Use models to query models (generateContent, generateImages, ...), or examine their metadata.
  • ai.caches: Create and manage caches to reduce costs when repeatedly using the same large prompt prefix.
  • ai.chats: Create local stateful chat objects to simplify multi turn interactions.
  • ai.files: Upload files to the API and reference them in your prompts. This reduces bandwidth if you use a file many times, and handles files too large to fit inline with your prompt.
  • ai.live: Start a live session for real time interaction, allows text + audio + video input, and text or audio output.

Samples

More samples can be found in the github samples directory.

Streaming

For quicker, more responsive API interactions use the generateContentStream method which yields chunks as they're generated:

import {GoogleGenAI} from '@google/genai';
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;

const ai = new GoogleGenAI({apiKey: GEMINI_API_KEY});

async function main() {
  const response = await ai.models.generateContentStream({
    model: 'gemini-2.0-flash-001',
    contents: 'Write a 100-word poem.',
  });
  for await (const chunk of response) {
    console.log(chunk.text);
  }
}

main();

Function Calling

To let Gemini to interact with external systems, you can provide provide functionDeclaration objects as tools. To use these tools it's a 4 step

  1. Declare the function name, description, and parameters
  2. Call generateContent with function calling enabled
  3. Use the returned FunctionCall parameters to call your actual function
  4. Send the result back to the model (with history, easier in ai.chat) as a FunctionResponse
import {GoogleGenAI, FunctionCallingConfigMode, FunctionDeclaration, Type} from '@google/genai';
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;

async function main() {
  const controlLightDeclaration: FunctionDeclaration = {
    name: 'controlLight',
    parameters: {
      type: Type.OBJECT,
      description: 'Set the brightness and color temperature of a room light.',
      properties: {
        brightness: {
          type: Type.NUMBER,
          description:
              'Light level from 0 to 100. Zero is off and 100 is full brightness.',
        },
        colorTemperature: {
          type: Type.STRING,
          description:
              'Color temperature of the light fixture which can be `daylight`, `cool`, or `warm`.',
        },
      },
      required: ['brightness', 'colorTemperature'],
    },
  };

  const ai = new GoogleGenAI({apiKey: GEMINI_API_KEY});
  const response = await ai.models.generateContent({
    model: 'gemini-2.0-flash-001',
    contents: 'Dim the lights so the room feels cozy and warm.',
    config: {
      toolConfig: {
        functionCallingConfig: {
          // Force it to call any function
          mode: FunctionCallingConfigMode.ANY,
          allowedFunctionNames: ['controlLight'],
        }
      },
      tools: [{functionDeclarations: [controlLightDeclaration]}]
    }
  });

  console.log(response.functionCalls);
}

main();

Generate Content

How to structure contents argument for generateContent

The SDK allows you to specify the following types in the contents parameter:

Content

  • Content: The SDK will wrap the singular Content instance in an array which contains only the given content instance
  • Content[]: No transformation happens

Part

Parts will be aggregated on a singular Content, with role 'user'.

  • Part | string: The SDK will wrap the string or Part in a Content instance with role 'user'.
  • Part[] | string[]: The SDK will wrap the full provided list into a single Content with role 'user'.

NOTE: This doesn't apply to FunctionCall and FunctionResponse parts, if you are specifying those, you need to explicitly provide the full Content[] structure making it explicit which Parts are 'spoken' by the model, or the user. The SDK will throw an exception if you try this.

How is this different from the other Google AI SDKs

This SDK (@google/genai) is Google Deepmind’s "vanilla" SDK for its generative AI offerings, and is where Google Deepmind adds new AI features.

Models hosted either on the Vertex AI platform or the Gemini Developer platform are accessible through this SDK.

Other SDKs may be offering additional AI frameworks on top of this SDK, or may be targeting specific project environments (like Firebase).

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.

更新日志

Changelog

1.7.0 (2025-06-25)

Features

  • Add compressionQuality enum for generate_videos (4325773)
  • Add enhance_input_image and image_preservation_factor fields for upscale_image (13eedda)
  • Batches support in TS (dd7355e)
  • expose the responseJsonSchema in GenerateContentConfig (db54a5f)

Bug Fixes

  • live: support ArrayBuffer in WebSocket messages (5a8aeac), closes #714

1.6.0 (2025-06-21)

Features

  • allow users to access headers for generateContent method and generateContentStream (f5f0e02)
  • enable json schema for controlled output and function declaration. (7d53d57)
  • Include status code and export HTTP errors (bcabcb6)
  • support extra_body in HttpOptions (1d48b6e)

Bug Fixes

  • Fix build errors when the mcp package is not installed. (5b7e695)

1.5.1 (2025-06-13)

Bug Fixes

  • Update MCP dependencies to be type-only (648019b)

1.5.0 (2025-06-11)

Features

  • add a timeout parameter to CallableToolConfig (06f31fd)
  • Add datastore_spec field for VertexAISearch (1b18a52)
  • Add support for Veo frame interpolation and video extension (53f22e6)
  • Make the MCP SDK an optional peer dependency. (2472fd3)
  • RAG - Introducing context storing for Gemini Live API. (bdc2355)

Bug Fixes

  • Prioritize credentials over implicit api key (from env) for node client using Vertex AI (d82aba2), closes #616

1.4.0 (2025-06-04)

Features

  • Add enhance_prompt field for Gemini Developer API generate_videos (ea3cc8e)
  • Enable url_context for Vertex (e377f58)
  • js: Support GEMINI_API_KEY as environment variable for setting API key. (97850ad)

Bug Fixes

  • defined Type becomes TYPE_UNSPECIFIED when obfuscation is enabled (1ddf4f5)
  • Update live SDK sample to address choppy audio quality (319bcbb)
  • use Enum's value instead of keys for obfuscation. (132bd04)

1.3.0 (2025-05-30)

Features

  • Adding thought_signature field to the Part to store the signature for thoughts. (20815b2)
  • include UNEXPECTED_TOOL_CALL enum value to FinishReason for Vertex AI APIs. (bd5a2bf)
  • Support ephemeral auth tokens as API keys for live connections in TS. (507bfb5)
  • Support ephemeral token creation in TS (425cfe6)

Bug Fixes

  • Rename LiveEphemeralParameters to LiveConnectConstraints. (86e9652)

1.2.0 (2025-05-28)

⚠ BREAKING CHANGES TO EXPERIMENTAL FEATURES

  • Remove unsupported Lyria enum for music generation mode

Features

  • Add generate_audio field for private testing of video generation (37e14c5)

Documentation

Miscellaneous Chores

  • Remove unsupported Lyria enum for music generation mode (0b935cd)

1.1.0 (2025-05-26)

Features

  • Add CommonJS modules (f40c47c)
  • support new fields in FileData, GenerationConfig, GroundingChunkRetrievedContext, RetrievalConfig, Schema, TuningJob, VertexAISearch, (cd04548)

1.0.1 (2025-05-22)

[!NOTE] This version drops support for end-of-life Node.js v18.

Bug Fixes

  • After an error on sendMessage, all subsequent calls fail with the same error (778abcc)
  • Fixed sendMessage subsequent calls test to follow the arrange-act-assert pattern (778abcc)
  • Unbreak direct requires from node. (023efd5)

1.0.0 (2025-05-19)

⚠ BREAKING CHANGES

  • Fix Lyria method name for JS, update parameters type

Features

  • Support ephemeral token creation in Python (8e12730)
  • Support using ephemeral token in Live session connection in Python (8e12730)

Bug Fixes

  • allow McpClient to be passed in with AFC disabled. (b13f1f8)
  • Blob handling in realtime music (f760755)
  • Prevent MCP label from being appended multiple times if they already exist (c59ffe7)

Documentation

Miscellaneous Chores

  • Fix Lyria method name for JS, update parameters type (99dba6e)

0.15.0 (2025-05-19)

Features

  • add time range filter to Google Search Tool (a938111)
  • Add basic support for async function calling. (8e2f357)
  • Add CallableToolConfig to specify behavior for FunctionDeclarations (c4148d0)
  • add configurations for afc. (ce7855b)
  • add live proactivity_audio and enable_affective_dialog (20e3892)
  • Add Lyria Realtime music generation support for JS (aefcaa5)
  • Add Lyria Realtime Music Types (99255d8)
  • Add MCP telemetry usage to TS SDK. (09a83e9)
  • add multi-speaker voice config (6fe6074)
  • Add support for lat/long in search. (4cd79f6)
  • Add support for MCP in TS SDK. (921a4be)
  • add support for propertyOrdering field. (a77822b)
  • Add Video FPS, and enable start/end_offset for MLDev (19f20e9)
  • Enable AFC on Generate content stream (ff2ce35)
  • export the createJsonSchemaValidator methods to be available for other library. (b3359a1)
  • List all mcp tools to max and mcpToTool taking a spread. (44cd9c9)
  • MCP: Add a new interface for automatic function calling (dc49ffc)
  • MCP: Add mcpToTool to pass MCP clients for automatic function calling (825f385)
  • Remove MCP Tool and MCP Client from ToolUnion (d35e16d)
  • support customer-managed encryption key in cached content (3e7437a)
  • Support Url Context Retrieval tool (aaaf9a9)

Bug Fixes

  • Add an ES module for node environments. (ff4bbd1)
  • Add default headers to model calls when MCP is enabled (9442eea)
  • Allow contents with no-text thoughts in chat history (4112d1c)
  • chats: Enforce internal management of chat history (abe592f)
  • chats: Relax the constraint on chat turns (68115a8)
  • make the system-test build. (dd7154c)
  • move test-server-sdk from dependencies to devDependencies (233a909)
  • Move test-server-sdk to devDependencies (#574) (b64deeb)
  • Run tests against Node 24 (28a56ac)
  • Skip MCP tool call when function name not present in the tool (9f3d360)

0.14.1 (2025-05-15)

Bug Fixes

  • Move test-server-sdk to devDependencies (#574) (5913e59)

0.14.0 (2025-05-13)

Features

  • Add Imagen edit_image support in JS SDK (6c99936)
  • Add Imagen upscale_image support for JS (6fe1a68)
  • add support for audio, video, text and session resumption in java. (e5542c6)
  • support display_name for Blob class when calling Vertex AI (fc35f51)
  • Support tuning checkpoints (6bd9c9e)

0.13.0 (2025-05-07)

Features

  • Add text and data accessors to LiveServerMessage (a3c4201)
  • Add Tool.enterprise_web_search field (29b097d)
  • Add a models.list function to list available models. (477d9ec)
  • Add Files.download method. (8f44c99)
  • Add support for Grounding with Google Maps (41f0cc2)
  • enable input transcription for Gemini API. (767c5d5)
  • Support global location (fixes #502) (ff906fb)

Bug Fixes

  • add retry logic for missing X-Goog-Upload-Status header for js (8cf039e)

0.12.0 (2025-04-30)

Features

  • add support for live grounding metadata (f5ed429)

0.11.0 (2025-04-30)

Features

  • add models.delete and models.update to manage tuned models (0766718)
  • Added support for Tuning operations (d5a035f)
  • make min_property, max_property, min_length, max_length, example, patter fields available for Schema class when calling Gemini API (5f91ee7)

Bug Fixes

  • Apply converters to list items when the API value isn't an array (249769f)
  • chats: Properly handle streaming errors to not throw an error that couldn't be caught, which might result in crash in web. Fixes #487 (0b62e15)
  • CI: Fix docs generation for release event (899969e)
  • Clean the CHANGELOG to remove the changes not included in the bundle. (89b1d66)
  • do not raise error for default field in Schema for Gemini API calls (6f72396)
  • Don't transform lists twice (249769f)

0.10.0 (2025-04-23)

Features

  • add additional realtime input fields (2150416)
  • Add helper function GenerateContentResponse.data to return concatenation of all inline data parts. (97b59a2)
  • Allow users to set AbortSignal inside per request config to cancel http request (c44f48d)
  • support default field in Schema when users call Gemini API (2e61cce)
  • Support setting the default base URL in clients via setDefaultBaseUrls() (df978b8)
  • Use ws:// for websockets established over http base URLs. (774505b)

Bug Fixes

  • Return actual error text from streaming response (808e0b5), closes #346
  • Update _api_client to parse and throw errors during processing stream responses (fixes #461) (1932f1d)

0.9.0 (2025-04-17)

⚠ BREAKING CHANGES

  • Simplified the types allowed on the generateContent contents parameter.

Features

  • add a helper module to process zod objecsts. (fad2789)
  • add support for model_selection_config to GenerateContentConfig (37a9cae)
  • Expose transcription configurations for audio in TS, and move generationConfig to the top level LiveConnectConfig (d3a841d)
  • Simplified the types allowed on the generateContent contents parameter. (324d158)
  • Support audio transcription in Vertex Live API (8d11c04)
  • Support RealtimeInputConfig, and language_code in SpeechConfig in python (004ff6a)
  • Update VertexRagStore (043d06c)

Bug Fixes

  • CI: Fix stable docs generation with release event (89c61b9)

0.8.0 (2025-04-09)

Features

  • Add domain to Web GroundingChunk (dc56670)
  • Add generationComplete notification to LiveServerContent (4d9811b)
  • add session resumption to live module (b5c6975)
  • add session resumption. (fff2b66)
  • Add thinking_budget to ThinkingConfig for Gemini Thinking Models (76e0e18)
  • Add traffic type to GenerateContentResponseUsageMetadata (d84156e)
  • Add types for configurable speech detection (fc861bc)
  • Add types to support continuous sessions with a sliding window (0fd8256)
  • Add UsageMetadata to LiveServerMessage (67b34f7)
  • expose generation_complete, input/output_transcription & input/output_audio_transcription to SDK for Vertex Live API (1e8be50)
  • merge GenerationConfig into LiveConnectConfig (d25d77d)
  • Populate X-Server-Timeout header when a request timeout is set. (6f00495)
  • support media resolution (9ebd58b)
  • Support models.get() for getting model information (fc62381)
  • Update Live converters to pass along configurable speech detection parameters (8301fa2)
  • Update Live converters to pass along params to support continuous sessions with a sliding window (3814d92)

Bug Fixes

  • Use authentication headers as provided by google-auth-library (94b11a1)

0.7.0 (2025-03-27)

⚠ BREAKING CHANGES

  • Change File.sizeBytes from number type to string type

Features

  • Add experimental generate_video support (0fa1f05)
  • add MediaModalities for ModalityTokenCount (9869098)

Bug Fixes

  • Change File.sizeBytes from number type to string type (184c7db)
  • Update tLiveClienttToolResponse() to accept FunctionResponse[] (4cab8bf)

0.6.1 (2025-03-25)

Features

  • Add engine to VertexAISearch (69dfbaf)
  • allow title property to be sent to Gemini API. Gemini API now supports the title property, so it's ok to pass this onto both Vertex and Gemini API. (c5855a3)
  • implement files.delete for JS client SDK. (4ac44de)
  • Save prompt safety attributes in dedicated field for generate_images (1a774fa)

Bug Fixes

  • schema transformer logic fix. (6311f60)

0.6.0 (2025-03-20)

⚠ BREAKING CHANGES

  • Unexport Content converter functions

Features

  • add IMAGE_SAFTY enum value to FinishReason (81ae907)

Code Refactoring

  • Separate converter functions to dedicated _{module}_converters.ts file for readability (bb9ba98)

0.5.0 (2025-03-20)

⚠ BREAKING CHANGES

  • Make "turnComplete:true" the default.

Features

  • Add sendClientContent, sendRealtimeInput, sendToolResponse to live session (e7ec3c0)
  • Make "turnComplete:true" the default. (5f77e3e)
  • Support Google Cloud Express for Vertex AI (e15c7f3)
  • support new UsageMetadata fields (fe000ed)
  • Support Vertex AI on browser runtimes (e15c7f3)
  • Upgrade the SDK launch stage to preview. (da38b6d)

0.4.0 (2025-03-14)

⚠ BREAKING CHANGES

  • remove the createPartFromVideoMetadata usability function.

Features

  • enable union type for Schema when calling Gemini API. (180983c)
  • Provide a better error message when trying to use VertexAI in browsers. (1ab1402)
  • Support returned safety attributes for generate_images (a0e0fcf)
  • throw exception when given method is not supported in Gemini API or Vertex AI (96ccb6f)

Bug Fixes

  • remove the createPartFromVideoMetadata usability function. (d660a7f)

0.3.1 (2025-03-11)

0.3.0 (2025-03-11)

⚠ BREAKING CHANGES

  • Make file.upload use named parameters.

Features

  • Enable Live for Vertex AI. (2bda9d4)

Bug Fixes

  • Set web as the browser entry points for bundlers that don't support the exports key (18cb728)

Miscellaneous Chores

  • Make file.upload use named parameters. (60433f4)