Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@google-ai/generativelanguage

googleapis437.5kApache-2.03.2.0TypeScript support: included

Generative Language API client for Node.js

google apis client, google api client, google apis, google api, google, google cloud platform, google cloud, cloud, google generativelanguage, generativelanguage, Generative Language API

readme

Google Cloud Platform logo

Generative Language API: Node.js Client

release level npm version

Generative Language API client for Node.js

A comprehensive list of changes in each version may be found in the CHANGELOG.

Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.

Table of contents:

Quickstart

Before you begin

  1. [Select or create a Cloud Platform project][projects].
  2. [Enable billing for your project][billing].
  3. [Enable the Generative Language API API][enable_api].
  4. [Set up authentication][auth] so you can access the API from your local workstation.

Installing the client library

npm install @google-ai/generativelanguage

Using the client library

/**
 * This snippet has been automatically generated and should be regarded as a code template only.
 * It will require modifications to work.
 * It may require correct/in-range values for request initialization.
 * TODO(developer): Uncomment these variables before running the sample.
 */
/**
 *  Required. The model name to use with the format name=models/{model}.
 */
// const model = 'abc123'
/**
 *  Required. The free-form input text given to the model as a prompt.
 *  Given a prompt, the model will generate a TextCompletion response it
 *  predicts as the completion of the input text.
 */
// const prompt = {
//  text: 'abc123'
// }
/**
 *  Controls the randomness of the output.
 *  Note: The default value varies by model, see the `Model.temperature`
 *  attribute of the `Model` returned the `getModel` function.
 *  Values can range from 0.0,1.0,
 *  inclusive. A value closer to 1.0 will produce responses that are more
 *  varied and creative, while a value closer to 0.0 will typically result in
 *  more straightforward responses from the model.
 */
// const temperature = 1234
/**
 *  Number of generated responses to return.
 *  This value must be between 1, 8, inclusive. If unset, this will default
 *  to 1.
 */
// const candidateCount = 1234
/**
 *  The maximum number of tokens to include in a candidate.
 *  If unset, this will default to 64.
 */
// const maxOutputTokens = 1234
/**
 *  The maximum cumulative probability of tokens to consider when sampling.
 *  The model uses combined Top-k and nucleus sampling.
 *  Tokens are sorted based on their assigned probabilities so that only the
 *  most liekly tokens are considered. Top-k sampling directly limits the
 *  maximum number of tokens to consider, while Nucleus sampling limits number
 *  of tokens based on the cumulative probability.
 *  Note: The default value varies by model, see the `Model.top_p`
 *  attribute of the `Model` returned the `getModel` function.
 */
// const topP = 1234
/**
 *  The maximum number of tokens to consider when sampling.
 *  The model uses combined Top-k and nucleus sampling.
 *  Top-k sampling considers the set of `top_k` most probable tokens.
 *  Defaults to 40.
 *  Note: The default value varies by model, see the `Model.top_k`
 *  attribute of the `Model` returned the `getModel` function.
 */
// const topK = 1234
/**
 *  The set of character sequences (up to 5) that will stop output generation.
 *  If specified, the API will stop at the first appearance of a stop
 *  sequence. The stop sequence will not be included as part of the response.
 */
// const stopSequences = 'abc123'

// Imports the Generativelanguage library
const {TextServiceClient} = require('@google-ai/generativelanguage').v1beta2;

// Instantiates a client
const generativelanguageClient = new TextServiceClient();

async function callGenerateText() {
  // Construct request
  const request = {
    model,
    prompt,
  };

  // Run request
  const response = await generativelanguageClient.generateText(request);
  console.log(response);
}

callGenerateText();

Samples

Samples are in the samples/ directory. Each sample's README.md has instructions for running its sample.

Sample Source Code Try it
Generative_service.batch_embed_contents source code ![Open in Cloud Shell][shell_img]
Generative_service.count_tokens source code ![Open in Cloud Shell][shell_img]
Generative_service.embed_content source code ![Open in Cloud Shell][shell_img]
Generative_service.generate_content source code ![Open in Cloud Shell][shell_img]
Generative_service.stream_generate_content source code ![Open in Cloud Shell][shell_img]
Model_service.get_model source code ![Open in Cloud Shell][shell_img]
Model_service.list_models source code ![Open in Cloud Shell][shell_img]
Cache_service.create_cached_content source code ![Open in Cloud Shell][shell_img]
Cache_service.delete_cached_content source code ![Open in Cloud Shell][shell_img]
Cache_service.get_cached_content source code ![Open in Cloud Shell][shell_img]
Cache_service.list_cached_contents source code ![Open in Cloud Shell][shell_img]
Cache_service.update_cached_content source code ![Open in Cloud Shell][shell_img]
Discuss_service.count_message_tokens source code ![Open in Cloud Shell][shell_img]
Discuss_service.generate_message source code ![Open in Cloud Shell][shell_img]
File_service.create_file source code ![Open in Cloud Shell][shell_img]
File_service.delete_file source code ![Open in Cloud Shell][shell_img]
File_service.get_file source code ![Open in Cloud Shell][shell_img]
File_service.list_files source code ![Open in Cloud Shell][shell_img]
Generative_service.batch_embed_contents source code ![Open in Cloud Shell][shell_img]
Generative_service.bidi_generate_content source code ![Open in Cloud Shell][shell_img]
Generative_service.count_tokens source code ![Open in Cloud Shell][shell_img]
Generative_service.embed_content source code ![Open in Cloud Shell][shell_img]
Generative_service.generate_answer source code ![Open in Cloud Shell][shell_img]
Generative_service.generate_content source code ![Open in Cloud Shell][shell_img]
Generative_service.stream_generate_content source code ![Open in Cloud Shell][shell_img]
Model_service.create_tuned_model source code ![Open in Cloud Shell][shell_img]
Model_service.delete_tuned_model source code ![Open in Cloud Shell][shell_img]
Model_service.get_model source code ![Open in Cloud Shell][shell_img]
Model_service.get_tuned_model source code ![Open in Cloud Shell][shell_img]
Model_service.list_models source code ![Open in Cloud Shell][shell_img]
Model_service.list_tuned_models source code ![Open in Cloud Shell][shell_img]
Model_service.update_tuned_model source code ![Open in Cloud Shell][shell_img]
Permission_service.create_permission source code ![Open in Cloud Shell][shell_img]
Permission_service.delete_permission source code ![Open in Cloud Shell][shell_img]
Permission_service.get_permission source code ![Open in Cloud Shell][shell_img]
Permission_service.list_permissions source code ![Open in Cloud Shell][shell_img]
Permission_service.transfer_ownership source code ![Open in Cloud Shell][shell_img]
Permission_service.update_permission source code ![Open in Cloud Shell][shell_img]
Prediction_service.predict source code ![Open in Cloud Shell][shell_img]
Retriever_service.batch_create_chunks source code ![Open in Cloud Shell][shell_img]
Retriever_service.batch_delete_chunks source code ![Open in Cloud Shell][shell_img]
Retriever_service.batch_update_chunks source code ![Open in Cloud Shell][shell_img]
Retriever_service.create_chunk source code ![Open in Cloud Shell][shell_img]
Retriever_service.create_corpus source code ![Open in Cloud Shell][shell_img]
Retriever_service.create_document source code ![Open in Cloud Shell][shell_img]
Retriever_service.delete_chunk source code ![Open in Cloud Shell][shell_img]
Retriever_service.delete_corpus source code ![Open in Cloud Shell][shell_img]
Retriever_service.delete_document source code ![Open in Cloud Shell][shell_img]
Retriever_service.get_chunk source code ![Open in Cloud Shell][shell_img]
Retriever_service.get_corpus source code ![Open in Cloud Shell][shell_img]
Retriever_service.get_document source code ![Open in Cloud Shell][shell_img]
Retriever_service.list_chunks source code ![Open in Cloud Shell][shell_img]
Retriever_service.list_corpora source code ![Open in Cloud Shell][shell_img]
Retriever_service.list_documents source code ![Open in Cloud Shell][shell_img]
Retriever_service.query_corpus source code ![Open in Cloud Shell][shell_img]
Retriever_service.query_document source code ![Open in Cloud Shell][shell_img]
Retriever_service.update_chunk source code ![Open in Cloud Shell][shell_img]
Retriever_service.update_corpus source code ![Open in Cloud Shell][shell_img]
Retriever_service.update_document source code ![Open in Cloud Shell][shell_img]
Text_service.batch_embed_text source code ![Open in Cloud Shell][shell_img]
Text_service.count_text_tokens source code ![Open in Cloud Shell][shell_img]
Text_service.embed_text source code ![Open in Cloud Shell][shell_img]
Text_service.generate_text source code ![Open in Cloud Shell][shell_img]
Cache_service.create_cached_content source code ![Open in Cloud Shell][shell_img]
Cache_service.delete_cached_content source code ![Open in Cloud Shell][shell_img]
Cache_service.get_cached_content source code ![Open in Cloud Shell][shell_img]
Cache_service.list_cached_contents source code ![Open in Cloud Shell][shell_img]
Cache_service.update_cached_content source code ![Open in Cloud Shell][shell_img]
Discuss_service.count_message_tokens source code ![Open in Cloud Shell][shell_img]
Discuss_service.generate_message source code ![Open in Cloud Shell][shell_img]
File_service.create_file source code ![Open in Cloud Shell][shell_img]
File_service.delete_file source code ![Open in Cloud Shell][shell_img]
File_service.download_file source code ![Open in Cloud Shell][shell_img]
File_service.get_file source code ![Open in Cloud Shell][shell_img]
File_service.list_files source code ![Open in Cloud Shell][shell_img]
Generative_service.batch_embed_contents source code ![Open in Cloud Shell][shell_img]
Generative_service.bidi_generate_content source code ![Open in Cloud Shell][shell_img]
Generative_service.count_tokens source code ![Open in Cloud Shell][shell_img]
Generative_service.embed_content source code ![Open in Cloud Shell][shell_img]
Generative_service.generate_answer source code ![Open in Cloud Shell][shell_img]
Generative_service.generate_content source code ![Open in Cloud Shell][shell_img]
Generative_service.stream_generate_content source code ![Open in Cloud Shell][shell_img]
Model_service.create_tuned_model source code ![Open in Cloud Shell][shell_img]
Model_service.delete_tuned_model source code ![Open in Cloud Shell][shell_img]
Model_service.get_model source code ![Open in Cloud Shell][shell_img]
Model_service.get_tuned_model source code ![Open in Cloud Shell][shell_img]
Model_service.list_models source code ![Open in Cloud Shell][shell_img]
Model_service.list_tuned_models source code ![Open in Cloud Shell][shell_img]
Model_service.update_tuned_model source code ![Open in Cloud Shell][shell_img]
Permission_service.create_permission source code ![Open in Cloud Shell][shell_img]
Permission_service.delete_permission source code ![Open in Cloud Shell][shell_img]
Permission_service.get_permission source code ![Open in Cloud Shell][shell_img]
Permission_service.list_permissions source code ![Open in Cloud Shell][shell_img]
Permission_service.transfer_ownership source code ![Open in Cloud Shell][shell_img]
Permission_service.update_permission source code ![Open in Cloud Shell][shell_img]
Prediction_service.predict source code ![Open in Cloud Shell][shell_img]
Prediction_service.predict_long_running source code ![Open in Cloud Shell][shell_img]
Retriever_service.batch_create_chunks source code ![Open in Cloud Shell][shell_img]
Retriever_service.batch_delete_chunks source code ![Open in Cloud Shell][shell_img]
Retriever_service.batch_update_chunks source code ![Open in Cloud Shell][shell_img]
Retriever_service.create_chunk source code ![Open in Cloud Shell][shell_img]
Retriever_service.create_corpus source code ![Open in Cloud Shell][shell_img]
Retriever_service.create_document source code ![Open in Cloud Shell][shell_img]
Retriever_service.delete_chunk source code ![Open in Cloud Shell][shell_img]
Retriever_service.delete_corpus source code ![Open in Cloud Shell][shell_img]
Retriever_service.delete_document source code ![Open in Cloud Shell][shell_img]
Retriever_service.get_chunk source code ![Open in Cloud Shell][shell_img]
Retriever_service.get_corpus source code ![Open in Cloud Shell][shell_img]
Retriever_service.get_document source code ![Open in Cloud Shell][shell_img]
Retriever_service.list_chunks source code ![Open in Cloud Shell][shell_img]
Retriever_service.list_corpora source code ![Open in Cloud Shell][shell_img]
Retriever_service.list_documents [source code](https://github.com/googleapis/google-cloud-node/blob/main/pack

changelog

Changelog

3.2.0 (2025-05-09)

Features

  • [generativelanguage] add CODE_RETRIEVAL_QUERY embedding task type (#6307) (484c6e0)
  • [generativelanguage] add support for live API (https://togithub.com/googleapis/go-genai/issues/275) (484c6e0)
  • Add CODE_RETRIEVAL_QUERY embedding task type (484c6e0)
  • Add language_code to speech config (484c6e0)
  • Add minimum, maximum, any_of, property_ordering, default and null type to Schema (484c6e0)
  • Add seed to GenerationConfig (484c6e0)
  • Add support for ModalityTokenCounts in UsageMetadata responses (484c6e0)
  • Add support for ModalityTokenCounts in UsageMetadata responses (484c6e0)
  • Add support for Veo: PredictLongRunning and generated/downloadable files (484c6e0)
  • Add ThinkingConfig, MediaResolution, and seed to GenerationConfig (484c6e0)

3.1.0 (2025-03-19)

Features

  • [Many APIs] await/catch promises, and update listOperationsAsync return type (#6182) (c41ff07)

3.0.0 (2025-03-18)

⚠ BREAKING CHANGES

  • upgrade to Node 18 (#6096)

Features

  • [Many APIs] add request/response debug logging to gapics, update templates to gax 5 (54a73fe)

Miscellaneous Chores

2.9.1 (2025-02-12)

Bug Fixes

  • [Many APIs] finalize fixing typings for headers in generator (#6011) (ee865ff)

2.9.0 (2025-01-11)

Features

  • [generativelanguage] Add GoogleSearch tool type (#5929) (e5a665a)

2.8.0 (2024-11-21)

Features

  • [generativelanguage] Adds search grounding (#5828) (0b27dc2)

2.7.0 (2024-10-10)

Features

  • [generativelanguage] Add GoogleSearchRetrieval tool and candidate.grounding_metadata (#5699) (3eb6375)

2.6.0 (2024-06-26)

Features

2.5.0 (2024-05-21)

Features

  • [generativelanguage] Add video metadata to files API (#5359) (4185d57)
  • [Many APIs] update Nodejs generator to send API versions in headers for GAPICs (#5350) (6c03873)
  • [Many APIs] update Nodejs generator to send API versions in headers for GAPICs (#5351) (01f48fc)
  • [Many APIs] update Nodejs generator to send API versions in headers for GAPICs (#5354) (a9784ed)

2.4.0 (2024-05-08)

Features

2.3.0 (2024-04-18)

Features

  • [generativelanguage] Add rest binding for tuned models (#5228) (b0ddbbe)

2.2.0 (2024-04-05)

Features

  • [generativelanguage] Add system instructions (#5208) (54c5762)

2.1.1 (2024-03-26)

Bug Fixes

  • [generativelanguage] make learning rate a one-of (#5142) (b2a5f9d)

2.1.0 (2024-02-09)

Features

  • Trusted Private Cloud support, use the universeDomain parameter (#5022) (b6498d8)

2.0.1 (2024-01-23)

Bug Fixes

  • [generativelanguage] improve retry logic for streaming API calls (#4955) (cafe82f)

2.0.0 (2023-12-11)

Features

  • [generativelanguage] Add v1 and v1beta, adds GenerativeService and RetrievalService (#4879) (d98f897).
  • BREAKING CHANGE: version v1beta is now the default version.

1.1.0 (2023-09-20)

Features

  • [generativelanguage] Upgrade to API v1beta3 (#4681) (bf0f0e2)

1.0.1 (2023-09-06)

Bug Fixes

  • [Many APIs] simplify logic for HTTP/1.1 REST fallback option (#4583) (c3ddba8)

1.0.0 (2023-08-06)

⚠ BREAKING CHANGES

  • migrate to Node 14 (#4443)

Bug Fixes

Miscellaneous Chores

0.2.1 (2023-07-04)

Bug Fixes

  • [generativelanguage] Update safety protos (#4382) (d06c733)

0.2.0 (2023-05-06)

Features

  • [generativelanguage] Add safety settings (#4248) (76b2df7)

0.1.1 (2023-05-04)

Bug Fixes

0.1.0 (2023-05-02)

Features

  • Add initial files for google.ai.generativelanguage.v1beta2 (#4224) (8db807a)