@@ -13,6 +13,107 @@ import * as converters from './converters/_caches_converters';
13
13
import { PagedItem , Pager } from './pagers' ;
14
14
import * as types from './types' ;
15
15
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
+
16
117
export class Caches extends BaseModule {
17
118
constructor ( private readonly apiClient : ApiClient ) {
18
119
super ( ) ;
@@ -72,78 +173,7 @@ export class Caches extends BaseModule {
72
173
async create (
73
174
params : types . CreateCachedContentParameters ,
74
175
) : 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 ) ;
147
177
}
148
178
149
179
/**
0 commit comments