UNPKG

@google-ai/generativelanguage

Version:
542 lines 24.9 kB
"use strict"; // Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // ** This file is automatically generated by gapic-generator-typescript. ** // ** https://github.com/googleapis/gapic-generator-typescript ** // ** All changes to this file may be overwritten. ** Object.defineProperty(exports, "__esModule", { value: true }); exports.GenerativeServiceClient = void 0; const stream_1 = require("stream"); const jsonProtos = require("../../protos/protos.json"); const google_gax_1 = require("google-gax"); /** * Client JSON configuration object, loaded from * `src/v1/generative_service_client_config.json`. * This file defines retry strategy and timeouts for all API methods in this library. */ const gapicConfig = require("./generative_service_client_config.json"); const version = require('../../../package.json').version; /** * API for using Large Models that generate multimodal content and have * additional capabilities beyond text generation. * @class * @memberof v1 */ class GenerativeServiceClient { _terminated = false; _opts; _providedCustomServicePath; _gaxModule; _gaxGrpc; _protos; _defaults; _universeDomain; _servicePath; _log = google_gax_1.loggingUtils.log('generativelanguage'); auth; descriptors = { page: {}, stream: {}, longrunning: {}, batching: {}, }; warn; innerApiCalls; pathTemplates; generativeServiceStub; /** * Construct an instance of GenerativeServiceClient. * * @param {object} [options] - The configuration object. * The options accepted by the constructor are described in detail * in [this document](https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#creating-the-client-instance). * The common options are: * @param {object} [options.credentials] - Credentials object. * @param {string} [options.credentials.client_email] * @param {string} [options.credentials.private_key] * @param {string} [options.email] - Account email address. Required when * using a .pem or .p12 keyFilename. * @param {string} [options.keyFilename] - Full path to the a .json, .pem, or * .p12 key downloaded from the Google Developers Console. If you provide * a path to a JSON file, the projectId option below is not necessary. * NOTE: .pem and .p12 require you to specify options.email as well. * @param {number} [options.port] - The port on which to connect to * the remote host. * @param {string} [options.projectId] - The project ID from the Google * Developer's Console, e.g. 'grape-spaceship-123'. We will also check * the environment variable GCLOUD_PROJECT for your project ID. If your * app is running in an environment which supports * {@link https://cloud.google.com/docs/authentication/application-default-credentials Application Default Credentials}, * your project ID will be detected automatically. * @param {string} [options.apiEndpoint] - The domain name of the * API remote host. * @param {gax.ClientConfig} [options.clientConfig] - Client configuration override. * Follows the structure of {@link gapicConfig}. * @param {boolean} [options.fallback] - Use HTTP/1.1 REST mode. * For more information, please check the * {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}. * @param {gax} [gaxInstance]: 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 GenerativeServiceClient({fallback: true}, gax); * ``` */ constructor(opts, gaxInstance) { // Ensure that options include all the required fields. const staticMembers = this.constructor; if (opts?.universe_domain && opts?.universeDomain && opts?.universe_domain !== opts?.universeDomain) { throw new Error('Please set either universe_domain or universeDomain, but not both.'); } const universeDomainEnvVar = typeof process === 'object' && typeof process.env === 'object' ? process.env['GOOGLE_CLOUD_UNIVERSE_DOMAIN'] : undefined; this._universeDomain = opts?.universeDomain ?? opts?.universe_domain ?? universeDomainEnvVar ?? 'googleapis.com'; this._servicePath = 'generativelanguage.' + this._universeDomain; const servicePath = opts?.servicePath || opts?.apiEndpoint || this._servicePath; this._providedCustomServicePath = !!(opts?.servicePath || opts?.apiEndpoint); const port = opts?.port || staticMembers.port; const clientConfig = opts?.clientConfig ?? {}; const fallback = opts?.fallback ?? (typeof window !== 'undefined' && typeof window?.fetch === 'function'); opts = Object.assign({ servicePath, port, clientConfig, fallback }, opts); // Request numeric enum values if REST transport is used. opts.numericEnums = true; // If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case. if (servicePath !== this._servicePath && !('scopes' in opts)) { opts['scopes'] = staticMembers.scopes; } // Load google-gax module synchronously if needed if (!gaxInstance) { gaxInstance = require('google-gax'); } // Choose either gRPC or proto-over-HTTP implementation of google-gax. this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance; // Create a `gaxGrpc` object, with any grpc-specific options sent to the client. this._gaxGrpc = new this._gaxModule.GrpcClient(opts); // Save options to use in initialize() method. this._opts = opts; // Save the auth object to the client, for use by other methods. this.auth = this._gaxGrpc.auth; // Set useJWTAccessWithScope on the auth object. this.auth.useJWTAccessWithScope = true; // Set defaultServicePath on the auth object. this.auth.defaultServicePath = this._servicePath; // Set the default scopes in auth client if needed. if (servicePath === this._servicePath) { this.auth.defaultScopes = staticMembers.scopes; } // Determine the client header string. const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`]; if (typeof process === 'object' && 'versions' in process) { clientHeader.push(`gl-node/${process.versions.node}`); } else { clientHeader.push(`gl-web/${this._gaxModule.version}`); } if (!opts.fallback) { clientHeader.push(`grpc/${this._gaxGrpc.grpcVersion}`); } else { clientHeader.push(`rest/${this._gaxGrpc.grpcVersion}`); } if (opts.libName && opts.libVersion) { clientHeader.push(`${opts.libName}/${opts.libVersion}`); } // Load the applicable protos. this._protos = this._gaxGrpc.loadProtoJSON(jsonProtos); // This API contains "path templates"; forward-slash-separated // identifiers to uniquely identify resources within the API. // Create useful helper objects for these. this.pathTemplates = { modelPathTemplate: new this._gaxModule.PathTemplate('models/{model}'), }; // Some of the methods on this service provide streaming responses. // Provide descriptors for these. this.descriptors.stream = { streamGenerateContent: new this._gaxModule.StreamDescriptor(this._gaxModule.StreamType.SERVER_STREAMING, !!opts.fallback, !!opts.gaxServerStreamingRetries), }; // Put together the default options sent with requests. this._defaults = this._gaxGrpc.constructSettings('google.ai.generativelanguage.v1.GenerativeService', gapicConfig, opts.clientConfig || {}, { 'x-goog-api-client': clientHeader.join(' ') }); // Set up a dictionary of "inner API calls"; the core implementation // of calling the API is handled in `google-gax`, with this code // merely providing the destination and request information. this.innerApiCalls = {}; // Add a warn function to the client constructor so it can be easily tested. this.warn = this._gaxModule.warn; } /** * 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 {Promise} A promise that resolves to an authenticated service stub. */ initialize() { // If the client stub promise is already initialized, return immediately. if (this.generativeServiceStub) { return this.generativeServiceStub; } // Put together the "service stub" for // google.ai.generativelanguage.v1.GenerativeService. this.generativeServiceStub = this._gaxGrpc.createStub(this._opts.fallback ? this._protos.lookupService('google.ai.generativelanguage.v1.GenerativeService') : // eslint-disable-next-line @typescript-eslint/no-explicit-any this._protos.google.ai.generativelanguage.v1 .GenerativeService, this._opts, this._providedCustomServicePath); // Iterate over each of the methods that the service provides // and create an API call method for each. const generativeServiceStubMethods = [ 'generateContent', 'streamGenerateContent', 'embedContent', 'batchEmbedContents', 'countTokens', ]; for (const methodName of generativeServiceStubMethods) { const callPromise = this.generativeServiceStub.then(stub => (...args) => { if (this._terminated) { if (methodName in this.descriptors.stream) { const stream = new stream_1.PassThrough({ objectMode: true }); setImmediate(() => { stream.emit('error', new this._gaxModule.GoogleError('The client has already been closed.')); }); return stream; } return Promise.reject('The client has already been closed.'); } const func = stub[methodName]; return func.apply(stub, args); }, (err) => () => { throw err; }); const descriptor = this.descriptors.stream[methodName] || undefined; const apiCall = this._gaxModule.createApiCall(callPromise, this._defaults[methodName], descriptor, this._opts.fallback); this.innerApiCalls[methodName] = apiCall; } return this.generativeServiceStub; } /** * The DNS address for this API service. * @deprecated Use the apiEndpoint method of the client instance. * @returns {string} The DNS address for this service. */ static get servicePath() { if (typeof process === 'object' && typeof process.emitWarning === 'function') { process.emitWarning('Static servicePath is deprecated, please use the instance method instead.', 'DeprecationWarning'); } return 'generativelanguage.googleapis.com'; } /** * The DNS address for this API service - same as servicePath. * @deprecated Use the apiEndpoint method of the client instance. * @returns {string} The DNS address for this service. */ static get apiEndpoint() { if (typeof process === 'object' && typeof process.emitWarning === 'function') { process.emitWarning('Static apiEndpoint is deprecated, please use the instance method instead.', 'DeprecationWarning'); } return 'generativelanguage.googleapis.com'; } /** * The DNS address for this API service. * @returns {string} The DNS address for this service. */ get apiEndpoint() { return this._servicePath; } get universeDomain() { return this._universeDomain; } /** * The port for this API service. * @returns {number} The default port for this service. */ static get port() { return 443; } /** * The scopes needed to make gRPC calls for every method defined * in this service. * @returns {string[]} List of default scopes. */ static get scopes() { return []; } /** * Return the project ID used by this class. * @returns {Promise} A promise that resolves to string containing the project ID. */ getProjectId(callback) { if (callback) { this.auth.getProjectId(callback); return; } return this.auth.getProjectId(); } generateContent(request, optionsOrCallback, callback) { request = request || {}; let options; if (typeof optionsOrCallback === 'function' && callback === undefined) { callback = optionsOrCallback; options = {}; } else { options = optionsOrCallback; } options = options || {}; options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ model: request.model ?? '', }); this.initialize().catch(err => { throw err; }); this._log.info('generateContent request %j', request); const wrappedCallback = callback ? (error, response, options, rawResponse) => { this._log.info('generateContent response %j', response); callback(error, response, options, rawResponse); // We verified callback above. } : undefined; return this.innerApiCalls .generateContent(request, options, wrappedCallback) ?.then(([response, options, rawResponse]) => { this._log.info('generateContent response %j', response); return [response, options, rawResponse]; }); } embedContent(request, optionsOrCallback, callback) { request = request || {}; let options; if (typeof optionsOrCallback === 'function' && callback === undefined) { callback = optionsOrCallback; options = {}; } else { options = optionsOrCallback; } options = options || {}; options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ model: request.model ?? '', }); this.initialize().catch(err => { throw err; }); this._log.info('embedContent request %j', request); const wrappedCallback = callback ? (error, response, options, rawResponse) => { this._log.info('embedContent response %j', response); callback(error, response, options, rawResponse); // We verified callback above. } : undefined; return this.innerApiCalls .embedContent(request, options, wrappedCallback) ?.then(([response, options, rawResponse]) => { this._log.info('embedContent response %j', response); return [response, options, rawResponse]; }); } batchEmbedContents(request, optionsOrCallback, callback) { request = request || {}; let options; if (typeof optionsOrCallback === 'function' && callback === undefined) { callback = optionsOrCallback; options = {}; } else { options = optionsOrCallback; } options = options || {}; options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ model: request.model ?? '', }); this.initialize().catch(err => { throw err; }); this._log.info('batchEmbedContents request %j', request); const wrappedCallback = callback ? (error, response, options, rawResponse) => { this._log.info('batchEmbedContents response %j', response); callback(error, response, options, rawResponse); // We verified callback above. } : undefined; return this.innerApiCalls .batchEmbedContents(request, options, wrappedCallback) ?.then(([response, options, rawResponse]) => { this._log.info('batchEmbedContents response %j', response); return [response, options, rawResponse]; }); } countTokens(request, optionsOrCallback, callback) { request = request || {}; let options; if (typeof optionsOrCallback === 'function' && callback === undefined) { callback = optionsOrCallback; options = {}; } else { options = optionsOrCallback; } options = options || {}; options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ model: request.model ?? '', }); this.initialize().catch(err => { throw err; }); this._log.info('countTokens request %j', request); const wrappedCallback = callback ? (error, response, options, rawResponse) => { this._log.info('countTokens response %j', response); callback(error, response, options, rawResponse); // We verified callback above. } : undefined; return this.innerApiCalls .countTokens(request, options, wrappedCallback) ?.then(([response, options, rawResponse]) => { this._log.info('countTokens response %j', response); return [response, options, rawResponse]; }); } /** * Generates a [streamed * response](https://ai.google.dev/gemini-api/docs/text-generation?lang=python#generate-a-text-stream) * from the model given an input `GenerateContentRequest`. * * @param {Object} request * The request object that will be sent. * @param {string} request.model * Required. The name of the `Model` to use for generating the completion. * * Format: `models/{model}`. * @param {number[]} request.contents * Required. The content of the current conversation with the model. * * For single-turn queries, this is a single instance. For multi-turn queries * like [chat](https://ai.google.dev/gemini-api/docs/text-generation#chat), * this is a repeated field that contains the conversation history and the * latest request. * @param {number[]} [request.safetySettings] * Optional. A list of unique `SafetySetting` instances for blocking unsafe * content. * * This will be enforced on the `GenerateContentRequest.contents` and * `GenerateContentResponse.candidates`. There should not be more than one * setting for each `SafetyCategory` type. The API will block any contents 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. Harm categories HARM_CATEGORY_HATE_SPEECH, * HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, * HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_CIVIC_INTEGRITY are supported. * Refer to the [guide](https://ai.google.dev/gemini-api/docs/safety-settings) * for detailed information on available safety settings. Also refer to the * [Safety guidance](https://ai.google.dev/gemini-api/docs/safety-guidance) to * learn how to incorporate safety considerations in your AI applications. * @param {google.ai.generativelanguage.v1.GenerationConfig} [request.generationConfig] * Optional. Configuration options for model generation and outputs. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Stream} * An object stream which emits {@link protos.google.ai.generativelanguage.v1.GenerateContentResponse|GenerateContentResponse} on 'data' event. * Please see the {@link https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#server-streaming | documentation } * for more details and examples. * @example <caption>include:samples/generated/v1/generative_service.stream_generate_content.js</caption> * region_tag:generativelanguage_v1_generated_GenerativeService_StreamGenerateContent_async */ streamGenerateContent(request, options) { request = request || {}; options = options || {}; options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ model: request.model ?? '', }); this.initialize().catch(err => { throw err; }); this._log.info('streamGenerateContent stream %j', options); return this.innerApiCalls.streamGenerateContent(request, options); } // -------------------- // -- Path templates -- // -------------------- /** * Return a fully-qualified model resource name string. * * @param {string} model * @returns {string} Resource name string. */ modelPath(model) { return this.pathTemplates.modelPathTemplate.render({ model: model, }); } /** * Parse the model from Model resource. * * @param {string} modelName * A fully-qualified path representing Model resource. * @returns {string} A string representing the model. */ matchModelFromModelName(modelName) { return this.pathTemplates.modelPathTemplate.match(modelName).model; } /** * Terminate the gRPC channel and close the client. * * The client will no longer be usable and all future behavior is undefined. * @returns {Promise} A promise that resolves when the client is closed. */ close() { if (this.generativeServiceStub && !this._terminated) { return this.generativeServiceStub.then(stub => { this._log.info('ending gRPC channel'); this._terminated = true; stub.close(); }); } return Promise.resolve(); } } exports.GenerativeServiceClient = GenerativeServiceClient; //# sourceMappingURL=generative_service_client.js.map