Class v1beta2.TextServiceClient (2.6.0)

API for using Generative Language Models (GLMs) trained to generate text.

Also known as Large Language Models (LLM)s, these generate text given an input prompt from the user. v1beta2

Package

@google-ai/generativelanguage

Constructors

(constructor)(opts, gaxInstance)

constructor(opts?: ClientOptions, gaxInstance?: typeof gax | typeof gax.fallback);

Construct an instance of TextServiceClient.

Parameters
Name Description
opts ClientOptions
gaxInstance typeof gax | typeof fallback

: loaded instance of google-gax. Useful if you need to avoid loading the default gRPC version and want to use the fallback HTTP implementation. Load only fallback version and pass it to the constructor: ``` const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC const client = new TextServiceClient({fallback: true}, gax); ```

Properties

apiEndpoint

get apiEndpoint(): string;

The DNS address for this API service.

apiEndpoint

static get apiEndpoint(): string;

The DNS address for this API service - same as servicePath.

auth

auth: gax.GoogleAuth;

descriptors

descriptors: Descriptors;

innerApiCalls

innerApiCalls: {
        [name: string]: Function;
    };

pathTemplates

pathTemplates: {
        [name: string]: gax.PathTemplate;
    };

port

static get port(): number;

The port for this API service.

scopes

static get scopes(): never[];

The scopes needed to make gRPC calls for every method defined in this service.

servicePath

static get servicePath(): string;

The DNS address for this API service.

textServiceStub

textServiceStub?: Promise<{
        [name: string]: Function;
    }>;

universeDomain

get universeDomain(): string;

warn

warn: (code: string, message: string, warnType?: string) => void;

Methods

close()

close(): Promise<void>;

Terminate the gRPC channel and close the client.

The client will no longer be usable and all future behavior is undefined.

Returns
Type Description
Promise<void>

{Promise} A promise that resolves when the client is closed.

embedText(request, options)

embedText(request?: protos.google.ai.generativelanguage.v1beta2.IEmbedTextRequest, options?: CallOptions): Promise<[
        protos.google.ai.generativelanguage.v1beta2.IEmbedTextResponse,
        protos.google.ai.generativelanguage.v1beta2.IEmbedTextRequest | undefined,
        {} | undefined
    ]>;

Generates an embedding from the model given an input message.

Parameters
Name Description
request IEmbedTextRequest

The request object that will be sent.

options CallOptions

Call options. See CallOptions for more details.

Returns
Type Description
Promise<[ protos.google.ai.generativelanguage.v1beta2.IEmbedTextResponse, protos.google.ai.generativelanguage.v1beta2.IEmbedTextRequest | undefined, {} | undefined ]>

{Promise} - The promise which resolves to an array. The first element of the array is an object representing EmbedTextResponse. Please see the documentation for more details and examples.

Example

  /**
   * 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 model=models/{model}.
   */
  // const model = 'abc123'
  /**
   *  Required. The free-form input text that the model will turn into an
   *  embedding.
   */
  // const text = 'abc123'

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

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

  async function callEmbedText() {
    // Construct request
    const request = {
      model,
      text,
    };

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

  callEmbedText();

embedText(request, options, callback)

embedText(request: protos.google.ai.generativelanguage.v1beta2.IEmbedTextRequest, options: CallOptions, callback: Callback<protos.google.ai.generativelanguage.v1beta2.IEmbedTextResponse, protos.google.ai.generativelanguage.v1beta2.IEmbedTextRequest | null | undefined, {} | null | undefined>): void;
Parameters
Name Description
request IEmbedTextRequest
options CallOptions
callback Callback<protos.google.ai.generativelanguage.v1beta2.IEmbedTextResponse, protos.google.ai.generativelanguage.v1beta2.IEmbedTextRequest | null | undefined, {} | null | undefined>
Returns
Type Description
void

embedText(request, callback)

embedText(request: protos.google.ai.generativelanguage.v1beta2.IEmbedTextRequest, callback: Callback<protos.google.ai.generativelanguage.v1beta2.IEmbedTextResponse, protos.google.ai.generativelanguage.v1beta2.IEmbedTextRequest | null | undefined, {} | null | undefined>): void;
Parameters
Name Description
request IEmbedTextRequest
callback Callback<protos.google.ai.generativelanguage.v1beta2.IEmbedTextResponse, protos.google.ai.generativelanguage.v1beta2.IEmbedTextRequest | null | undefined, {} | null | undefined>
Returns
Type Description
void

generateText(request, options)

generateText(request?: protos.google.ai.generativelanguage.v1beta2.IGenerateTextRequest, options?: CallOptions): Promise<[
        protos.google.ai.generativelanguage.v1beta2.IGenerateTextResponse,
        (protos.google.ai.generativelanguage.v1beta2.IGenerateTextRequest | undefined),
        {} | undefined
    ]>;

Generates a response from the model given an input message.

Parameters
Name Description
request IGenerateTextRequest

The request object that will be sent.

options CallOptions

Call options. See CallOptions for more details.

Returns
Type Description
Promise<[ protos.google.ai.generativelanguage.v1beta2.IGenerateTextResponse, (protos.google.ai.generativelanguage.v1beta2.IGenerateTextRequest | undefined), {} | undefined ]>

{Promise} - The promise which resolves to an array. The first element of the array is an object representing GenerateTextResponse. Please see the documentation for more details and examples.

Example

  /**
   * 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 = {}
  /**
   *  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
  /**
   *  A list of unique `SafetySetting` instances for blocking unsafe content.
   *  that will be enforced on the `GenerateTextRequest.prompt` and
   *  `GenerateTextResponse.candidates`. There should not be more than one
   *  setting for each `SafetyCategory` type. The API will block any prompts and
   *  responses that fail to meet the thresholds set by these settings. This list
   *  overrides the default settings for each `SafetyCategory` specified in the
   *  safety_settings. If there is no `SafetySetting` for a given
   *  `SafetyCategory` provided in the list, the API will use the default safety
   *  setting for that category.
   */
  // const safetySettings = [1,2,3,4]
  /**
   *  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 = ['abc','def']

  // 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();

generateText(request, options, callback)

generateText(request: protos.google.ai.generativelanguage.v1beta2.IGenerateTextRequest, options: CallOptions, callback: Callback<protos.google.ai.generativelanguage.v1beta2.IGenerateTextResponse, protos.google.ai.generativelanguage.v1beta2.IGenerateTextRequest | null | undefined, {} | null | undefined>): void;
Parameters
Name Description
request IGenerateTextRequest
options CallOptions
callback Callback<protos.google.ai.generativelanguage.v1beta2.IGenerateTextResponse, protos.google.ai.generativelanguage.v1beta2.IGenerateTextRequest | null | undefined, {} | null | undefined>
Returns
Type Description
void

generateText(request, callback)

generateText(request: protos.google.ai.generativelanguage.v1beta2.IGenerateTextRequest, callback: Callback<protos.google.ai.generativelanguage.v1beta2.IGenerateTextResponse, protos.google.ai.generativelanguage.v1beta2.IGenerateTextRequest | null | undefined, {} | null | undefined>): void;
Parameters
Name Description
request IGenerateTextRequest
callback Callback<protos.google.ai.generativelanguage.v1beta2.IGenerateTextResponse, protos.google.ai.generativelanguage.v1beta2.IGenerateTextRequest | null | undefined, {} | null | undefined>
Returns
Type Description
void

getProjectId()

getProjectId(): Promise<string>;
Returns
Type Description
Promise<string>

getProjectId(callback)

getProjectId(callback: Callback<string, undefined, undefined>): void;
Parameter
Name Description
callback Callback<string, undefined, undefined>
Returns
Type Description
void

initialize()

initialize(): Promise<{
        [name: string]: Function;
    }>;

Initialize the client. Performs asynchronous operations (such as authentication) and prepares the client. This function will be called automatically when any class method is called for the first time, but if you need to initialize it before calling an actual method, feel free to call initialize() directly.

You can await on this method if you want to make sure the client is initialized.

Returns
Type Description
Promise<{ [name: string]: Function; }>

{Promise} A promise that resolves to an authenticated service stub.

matchModelFromModelName(modelName)

matchModelFromModelName(modelName: string): string | number;

Parse the model from Model resource.

Parameter
Name Description
modelName string

A fully-qualified path representing Model resource.

Returns
Type Description
string | number

{string} A string representing the model.

modelPath(model)

modelPath(model: string): string;

Return a fully-qualified model resource name string.

Parameter
Name Description
model string
Returns
Type Description
string

{string} Resource name string.