Skip to content

feat: Added createCachedContents standalone function #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,767 changes: 1,892 additions & 1,875 deletions api-report/genai-node.api.md

Large diffs are not rendered by default.

3,767 changes: 1,892 additions & 1,875 deletions api-report/genai-web.api.md

Large diffs are not rendered by default.

3,760 changes: 1,885 additions & 1,875 deletions api-report/genai.api.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/_api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ export interface ApiClientInitOptions {
userAgentExtra?: string;
}

/**
* Base client interface.
*/
export interface BaseClient {
apiClient: ApiClient;
}

/**
* Represents the necessary information to send a request to an API endpoint.
* This interface defines the structure for constructing and executing HTTP
Expand Down
177 changes: 104 additions & 73 deletions src/caches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,115 @@

// Code generated by the Google Gen AI SDK generator DO NOT EDIT.

import {ApiClient} from './_api_client';
import {ApiClient, BaseClient} from './_api_client';
import * as common from './_common';
import {BaseModule} from './_common';
import * as converters from './converters/_caches_converters';
import {PagedItem, Pager} from './pagers';
import * as types from './types';

/**
* Creates a cached contents resource.
*
* @remarks
* Context caching is only supported for specific models. See [Gemini
* Developer API reference](https://ai.google.dev/gemini-api/docs/caching?lang=node/context-cac)
* and [Vertex AI reference](https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview#supported_models)
* for more information.
*
* @param apiClient - The API client to use.
* @param params - The parameters for the create request.
* @return The created cached content.
*
* @example
* ```ts
* import {createCachedContent} from '@google/genai'
*
* const contents = ...; // Initialize the content to cache.
* const response = await createCachedContent(apiClient, {
* model: 'gemini-1.5-flash',
* config: {
* 'contents': contents,
* 'displayName': 'test cache',
* 'systemInstruction': 'What is the sum of the two pdfs?',
* 'ttl': '86400s',
* }
* });
* ```
*/
export async function createCachedContent(
baseClient: BaseClient,
params: types.CreateCachedContentParameters,
): Promise<types.CachedContent> {
let response: Promise<types.CachedContent>;
let path: string = '';
let queryParams: Record<string, string> = {};
const apiClient = baseClient.apiClient;
if (apiClient.isVertexAI()) {
const body = converters.createCachedContentParametersToVertex(
apiClient,
params,
);
path = common.formatMap(
'cachedContents',
body['_url'] as Record<string, unknown>,
);
queryParams = body['_query'] as Record<string, string>;
delete body['config'];
delete body['_url'];
delete body['_query'];

response = apiClient
.request({
path: path,
queryParams: queryParams,
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
})
.then((httpResponse) => {
return httpResponse.json();
}) as Promise<types.CachedContent>;

return response.then((apiResponse) => {
const resp = converters.cachedContentFromVertex(apiClient, apiResponse);

return resp as types.CachedContent;
});
} else {
const body = converters.createCachedContentParametersToMldev(
apiClient,
params,
);
path = common.formatMap(
'cachedContents',
body['_url'] as Record<string, unknown>,
);
queryParams = body['_query'] as Record<string, string>;
delete body['config'];
delete body['_url'];
delete body['_query'];

response = apiClient
.request({
path: path,
queryParams: queryParams,
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
})
.then((httpResponse) => {
return httpResponse.json();
}) as Promise<types.CachedContent>;

return response.then((apiResponse) => {
const resp = converters.cachedContentFromMldev(apiClient, apiResponse);

return resp as types.CachedContent;
});
}
}

export class Caches extends BaseModule {
constructor(private readonly apiClient: ApiClient) {
super();
Expand Down Expand Up @@ -72,78 +174,7 @@ export class Caches extends BaseModule {
async create(
params: types.CreateCachedContentParameters,
): Promise<types.CachedContent> {
let response: Promise<types.CachedContent>;
let path: string = '';
let queryParams: Record<string, string> = {};
if (this.apiClient.isVertexAI()) {
const body = converters.createCachedContentParametersToVertex(
this.apiClient,
params,
);
path = common.formatMap(
'cachedContents',
body['_url'] as Record<string, unknown>,
);
queryParams = body['_query'] as Record<string, string>;
delete body['config'];
delete body['_url'];
delete body['_query'];

response = this.apiClient
.request({
path: path,
queryParams: queryParams,
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
})
.then((httpResponse) => {
return httpResponse.json();
}) as Promise<types.CachedContent>;

return response.then((apiResponse) => {
const resp = converters.cachedContentFromVertex(
this.apiClient,
apiResponse,
);

return resp as types.CachedContent;
});
} else {
const body = converters.createCachedContentParametersToMldev(
this.apiClient,
params,
);
path = common.formatMap(
'cachedContents',
body['_url'] as Record<string, unknown>,
);
queryParams = body['_query'] as Record<string, string>;
delete body['config'];
delete body['_url'];
delete body['_query'];

response = this.apiClient
.request({
path: path,
queryParams: queryParams,
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
})
.then((httpResponse) => {
return httpResponse.json();
}) as Promise<types.CachedContent>;

return response.then((apiResponse) => {
const resp = converters.cachedContentFromMldev(
this.apiClient,
apiResponse,
);

return resp as types.CachedContent;
});
}
return createCachedContent({apiClient: this.apiClient}, params);
}

/**
Expand Down
108 changes: 88 additions & 20 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import {GoogleAuthOptions} from 'google-auth-library';

import {ApiClient} from './_api_client';
import {ApiClient, ApiClientInitOptions, BaseClient} from './_api_client';
import {Caches} from './caches';
import {Chats} from './chats';
import {crossError} from './cross/_cross_error';
Expand All @@ -21,6 +21,31 @@ import {WebAuth} from './web/_web_auth';

const LANGUAGE_LABEL_PREFIX = 'gl-node/';

function getApiClientInitOptions(
options: GoogleGenAIOptions,
): ApiClientInitOptions {
if (options.apiKey == null) {
throw new Error(
`An API Key must be set when running in an unspecified environment.\n + ${
crossError().message
}`,
);
}
const vertexai = options.vertexai ?? false;
const apiKey = options.apiKey;
const apiVersion = options.apiVersion;
const auth = new WebAuth(apiKey);
return {
auth: auth,
apiVersion: apiVersion,
apiKey: apiKey,
vertexai: vertexai,
httpOptions: options.httpOptions,
userAgentExtra: LANGUAGE_LABEL_PREFIX + 'cross',
uploader: new CrossUploader(),
};
}

/**
* Google Gen AI SDK's configuration options.
*
Expand Down Expand Up @@ -126,29 +151,72 @@ export class GoogleGenAI {
readonly operations: Operations;

constructor(options: GoogleGenAIOptions) {
if (options.apiKey == null) {
throw new Error(
`An API Key must be set when running in an unspecified environment.\n + ${crossError().message}`,
);
}
this.vertexai = options.vertexai ?? false;
this.apiKey = options.apiKey;
this.apiVersion = options.apiVersion;
const auth = new WebAuth(this.apiKey);
this.apiClient = new ApiClient({
auth: auth,
apiVersion: this.apiVersion,
apiKey: this.apiKey,
vertexai: this.vertexai,
httpOptions: options.httpOptions,
userAgentExtra: LANGUAGE_LABEL_PREFIX + 'cross',
uploader: new CrossUploader(),
});
const apiClientInitOptions = getApiClientInitOptions(options);

this.apiKey = apiClientInitOptions.apiKey;
this.vertexai = apiClientInitOptions.vertexai ?? false;
this.apiVersion = apiClientInitOptions.apiVersion;

this.apiClient = new ApiClient(apiClientInitOptions);
this.models = new Models(this.apiClient);
this.live = new Live(this.apiClient, auth, new CrossWebSocketFactory());
this.live = new Live(
this.apiClient,
apiClientInitOptions.auth,
new CrossWebSocketFactory(),
);
this.chats = new Chats(this.models, this.apiClient);
this.caches = new Caches(this.apiClient);
this.files = new Files(this.apiClient);
this.operations = new Operations(this.apiClient);
}
}

/**
* The Google GenAI SDK Client for use with Standalone Functions.
*
* @remarks
* Provides access to the GenAI features through either the {@link
* https://cloud.google.com/vertex-ai/docs/reference/rest | Gemini API} or
* the {@link https://cloud.google.com/vertex-ai/docs/reference/rest | Vertex AI
* API}.
*
* The {@link GoogleGenAIOptions.vertexai} value determines which of the API
* services to use.
*
* When using the Gemini API, a {@link GoogleGenAIOptions.apiKey} must also be
* set. When using Vertex AI, both {@link GoogleGenAIOptions.project} and {@link
* GoogleGenAIOptions.location} must be set, or a {@link
* GoogleGenAIOptions.apiKey} must be set when using Express Mode.
*
* Explicitly passed in values in {@link GoogleGenAIOptions} will always take
* precedence over environment variables. If both project/location and api_key
* exist in the environment variables, the project/location will be used.
*
* @example
* Initializing the SDK for using the Gemini API:
* ```ts
* import {GoogleGenAiClient} from '@google/genai';
* const ai = new GoogleGenAiClient({apiKey: 'GEMINI_API_KEY'});
* ```
*
* @example
* Initializing the SDK for using the Vertex AI API:
* ```ts
* import {GoogleGenAiClient} from '@google/genai';
* const ai = new GoogleGenAiClient({
* vertexai: true,
* project: 'PROJECT_ID',
* location: 'PROJECT_LOCATION'
* });
* ```
*
*/
export class GoogleGenAiClient implements BaseClient {
public readonly apiClient: ApiClient;

constructor(options: GoogleGenAIOptions) {
const apiClientInitOptions = getApiClientInitOptions(options);

this.apiClient = new ApiClient(apiClientInitOptions);
}
}
2 changes: 1 addition & 1 deletion src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// Code generated by the Google Gen AI SDK generator DO NOT EDIT.

import {ApiClient} from './_api_client';
import {ApiClient, BaseClient} from './_api_client';
import * as common from './_common';
import {BaseModule} from './_common';
import * as converters from './converters/_files_converters';
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

export {BaseClient} from './_api_client';
export * from './caches';
export * from './chats';
export {GoogleGenAI, GoogleGenAIOptions} from './client';
Expand Down
2 changes: 1 addition & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// Code generated by the Google Gen AI SDK generator DO NOT EDIT.

import {ApiClient} from './_api_client';
import {ApiClient, BaseClient} from './_api_client';
import * as common from './_common';
import {BaseModule} from './_common';
import * as converters from './converters/_models_converters';
Expand Down
1 change: 1 addition & 0 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

export {BaseClient} from '../_api_client';
export * from '../caches';
export * from '../chats';
export {GoogleGenAIOptions} from '../client';
Expand Down
Loading
Loading