Skip to content

Commit eabb68f

Browse files
IvanLHcopybara-github
authored andcommitted
feat: Added createCachedContents standalone function
PiperOrigin-RevId: 747527721
1 parent dec937e commit eabb68f

File tree

4 files changed

+111
-72
lines changed

4 files changed

+111
-72
lines changed

api-report/genai-node.api.md

+3
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ export class CountTokensResponse {
235235
totalTokens?: number;
236236
}
237237

238+
// @public
239+
export function createCachedContent(apiClient: ApiClient, params: types.CreateCachedContentParameters): Promise<types.CachedContent>;
240+
238241
// @public
239242
export interface CreateCachedContentConfig {
240243
contents?: ContentListUnion;

api-report/genai-web.api.md

+3
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ export class CountTokensResponse {
235235
totalTokens?: number;
236236
}
237237

238+
// @public
239+
export function createCachedContent(apiClient: ApiClient, params: types.CreateCachedContentParameters): Promise<types.CachedContent>;
240+
238241
// @public
239242
export interface CreateCachedContentConfig {
240243
contents?: ContentListUnion;

api-report/genai.api.md

+3
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ export class CountTokensResponse {
235235
totalTokens?: number;
236236
}
237237

238+
// @public
239+
export function createCachedContent(apiClient: ApiClient, params: types.CreateCachedContentParameters): Promise<types.CachedContent>;
240+
238241
// @public
239242
export interface CreateCachedContentConfig {
240243
contents?: ContentListUnion;

src/caches.ts

+102-72
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,107 @@ import * as converters from './converters/_caches_converters';
1313
import {PagedItem, Pager} from './pagers';
1414
import * as types from './types';
1515

16+
/**
17+
* Creates a cached contents resource.
18+
*
19+
* @remarks
20+
* Context caching is only supported for specific models. See [Gemini
21+
* Developer API reference](https://ai.google.dev/gemini-api/docs/caching?lang=node/context-cac)
22+
* and [Vertex AI reference](https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview#supported_models)
23+
* for more information.
24+
*
25+
* @param apiClient - The API client to use.
26+
* @param params - The parameters for the create request.
27+
* @return The created cached content.
28+
*
29+
* @example
30+
* ```ts
31+
* import {createCachedContent} from '@google/genai'
32+
*
33+
* const contents = ...; // Initialize the content to cache.
34+
* const response = await createCachedContent(apiClient, {
35+
* model: 'gemini-1.5-flash',
36+
* config: {
37+
* 'contents': contents,
38+
* 'displayName': 'test cache',
39+
* 'systemInstruction': 'What is the sum of the two pdfs?',
40+
* 'ttl': '86400s',
41+
* }
42+
* });
43+
* ```
44+
*/
45+
export async function createCachedContent(
46+
apiClient: ApiClient,
47+
params: types.CreateCachedContentParameters,
48+
): Promise<types.CachedContent> {
49+
let response: Promise<types.CachedContent>;
50+
let path: string = '';
51+
let queryParams: Record<string, string> = {};
52+
if (apiClient.isVertexAI()) {
53+
const body = converters.createCachedContentParametersToVertex(
54+
apiClient,
55+
params,
56+
);
57+
path = common.formatMap(
58+
'cachedContents',
59+
body['_url'] as Record<string, unknown>,
60+
);
61+
queryParams = body['_query'] as Record<string, string>;
62+
delete body['config'];
63+
delete body['_url'];
64+
delete body['_query'];
65+
66+
response = apiClient
67+
.request({
68+
path: path,
69+
queryParams: queryParams,
70+
body: JSON.stringify(body),
71+
httpMethod: 'POST',
72+
httpOptions: params.config?.httpOptions,
73+
})
74+
.then((httpResponse) => {
75+
return httpResponse.json();
76+
}) as Promise<types.CachedContent>;
77+
78+
return response.then((apiResponse) => {
79+
const resp = converters.cachedContentFromVertex(apiClient, apiResponse);
80+
81+
return resp as types.CachedContent;
82+
});
83+
} else {
84+
const body = converters.createCachedContentParametersToMldev(
85+
apiClient,
86+
params,
87+
);
88+
path = common.formatMap(
89+
'cachedContents',
90+
body['_url'] as Record<string, unknown>,
91+
);
92+
queryParams = body['_query'] as Record<string, string>;
93+
delete body['config'];
94+
delete body['_url'];
95+
delete body['_query'];
96+
97+
response = apiClient
98+
.request({
99+
path: path,
100+
queryParams: queryParams,
101+
body: JSON.stringify(body),
102+
httpMethod: 'POST',
103+
httpOptions: params.config?.httpOptions,
104+
})
105+
.then((httpResponse) => {
106+
return httpResponse.json();
107+
}) as Promise<types.CachedContent>;
108+
109+
return response.then((apiResponse) => {
110+
const resp = converters.cachedContentFromMldev(apiClient, apiResponse);
111+
112+
return resp as types.CachedContent;
113+
});
114+
}
115+
}
116+
16117
export class Caches extends BaseModule {
17118
constructor(private readonly apiClient: ApiClient) {
18119
super();
@@ -72,78 +173,7 @@ export class Caches extends BaseModule {
72173
async create(
73174
params: types.CreateCachedContentParameters,
74175
): Promise<types.CachedContent> {
75-
let response: Promise<types.CachedContent>;
76-
let path: string = '';
77-
let queryParams: Record<string, string> = {};
78-
if (this.apiClient.isVertexAI()) {
79-
const body = converters.createCachedContentParametersToVertex(
80-
this.apiClient,
81-
params,
82-
);
83-
path = common.formatMap(
84-
'cachedContents',
85-
body['_url'] as Record<string, unknown>,
86-
);
87-
queryParams = body['_query'] as Record<string, string>;
88-
delete body['config'];
89-
delete body['_url'];
90-
delete body['_query'];
91-
92-
response = this.apiClient
93-
.request({
94-
path: path,
95-
queryParams: queryParams,
96-
body: JSON.stringify(body),
97-
httpMethod: 'POST',
98-
httpOptions: params.config?.httpOptions,
99-
})
100-
.then((httpResponse) => {
101-
return httpResponse.json();
102-
}) as Promise<types.CachedContent>;
103-
104-
return response.then((apiResponse) => {
105-
const resp = converters.cachedContentFromVertex(
106-
this.apiClient,
107-
apiResponse,
108-
);
109-
110-
return resp as types.CachedContent;
111-
});
112-
} else {
113-
const body = converters.createCachedContentParametersToMldev(
114-
this.apiClient,
115-
params,
116-
);
117-
path = common.formatMap(
118-
'cachedContents',
119-
body['_url'] as Record<string, unknown>,
120-
);
121-
queryParams = body['_query'] as Record<string, string>;
122-
delete body['config'];
123-
delete body['_url'];
124-
delete body['_query'];
125-
126-
response = this.apiClient
127-
.request({
128-
path: path,
129-
queryParams: queryParams,
130-
body: JSON.stringify(body),
131-
httpMethod: 'POST',
132-
httpOptions: params.config?.httpOptions,
133-
})
134-
.then((httpResponse) => {
135-
return httpResponse.json();
136-
}) as Promise<types.CachedContent>;
137-
138-
return response.then((apiResponse) => {
139-
const resp = converters.cachedContentFromMldev(
140-
this.apiClient,
141-
apiResponse,
142-
);
143-
144-
return resp as types.CachedContent;
145-
});
146-
}
176+
return createCachedContent(this.apiClient, params);
147177
}
148178

149179
/**

0 commit comments

Comments
 (0)