@@ -3,7 +3,6 @@ package examples
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "io"
7
6
"log"
8
7
"os"
9
8
"path/filepath"
@@ -24,30 +23,27 @@ func CacheCreate() (*genai.GenerateContentResponse, error) {
24
23
}
25
24
26
25
modelName := "gemini-1.5-flash-001"
27
-
28
- // Open the file.
29
- file , err := os .Open (filepath .Join (getMedia (), "a11.txt" ))
30
- if err != nil {
31
- log .Fatal ("Error opening file:" , err )
32
- }
33
- defer file .Close ()
34
-
35
- // Read the file.
36
- data , err := io .ReadAll (file )
26
+ document , err := client .Files .UploadFromPath (
27
+ ctx ,
28
+ filepath .Join (getMedia (), "a11.txt" ),
29
+ & genai.UploadFileConfig {
30
+ MIMEType : "text/plain" ,
31
+ },
32
+ )
37
33
if err != nil {
38
- log .Fatal ("Error reading file:" , err )
34
+ log .Fatal (err )
39
35
}
40
-
41
36
parts := []* genai.Part {
42
- {Text : "Please summarize this transcript" },
43
- {InlineData : & genai.Blob {Data : data , MIMEType : "text/plain" }},
37
+ genai .NewPartFromURI (document .URI , document .MIMEType ),
44
38
}
45
39
contents := []* genai.Content {
46
- genai .NewUserContentFromParts (parts ),
40
+ genai .NewContentFromParts (parts , "user" ),
47
41
}
48
42
cache , err := client .Caches .Create (ctx , modelName , & genai.CreateCachedContentConfig {
49
43
Contents : contents ,
50
- SystemInstruction : genai .NewUserContentFromText ("You are an expert analyzing transcripts." ),
44
+ SystemInstruction : genai .NewContentFromText (
45
+ "You are an expert analyzing transcripts." , "user" ,
46
+ ),
51
47
})
52
48
if err != nil {
53
49
log .Fatal (err )
@@ -67,7 +63,6 @@ func CacheCreate() (*genai.GenerateContentResponse, error) {
67
63
if err != nil {
68
64
log .Fatal (err )
69
65
}
70
- fmt .Println ("Generated content:" )
71
66
printResponse (response )
72
67
// [END cache_create]
73
68
@@ -88,29 +83,27 @@ func CacheCreateFromName() (*genai.GenerateContentResponse, error) {
88
83
}
89
84
90
85
modelName := "gemini-1.5-flash-001"
91
- // Open the file.
92
- file , err := os .Open (filepath .Join (getMedia (), "a11.txt" ))
93
- if err != nil {
94
- log .Fatal ("Error opening file:" , err )
95
- }
96
- defer file .Close ()
97
-
98
- // Read the file.
99
- data , err := io .ReadAll (file )
86
+ document , err := client .Files .UploadFromPath (
87
+ ctx ,
88
+ filepath .Join (getMedia (), "a11.txt" ),
89
+ & genai.UploadFileConfig {
90
+ MIMEType : "text/plain" ,
91
+ },
92
+ )
100
93
if err != nil {
101
- log .Fatal ("Error reading file:" , err )
94
+ log .Fatal (err )
102
95
}
103
-
104
96
parts := []* genai.Part {
105
- {Text : "Please summarize this transcript" },
106
- {InlineData : & genai.Blob {Data : data , MIMEType : "text/plain" }},
97
+ genai .NewPartFromURI (document .URI , document .MIMEType ),
107
98
}
108
99
contents := []* genai.Content {
109
- genai .NewUserContentFromParts (parts ),
100
+ genai .NewContentFromParts (parts , "user" ),
110
101
}
111
102
cache , err := client .Caches .Create (ctx , modelName , & genai.CreateCachedContentConfig {
112
103
Contents : contents ,
113
- SystemInstruction : genai .NewUserContentFromText ("You are an expert analyzing transcripts." ),
104
+ SystemInstruction : genai .NewContentFromText (
105
+ "You are an expert analyzing transcripts." , "user" ,
106
+ ),
114
107
})
115
108
if err != nil {
116
109
log .Fatal (err )
@@ -142,8 +135,8 @@ func CacheCreateFromName() (*genai.GenerateContentResponse, error) {
142
135
return response , err
143
136
}
144
137
145
- func CacheDelete () error {
146
- // [START cache_delete ]
138
+ func CacheCreateFromChat () ( * genai. GenerateContentResponse , error ) {
139
+ // [START cache_create_from_chat ]
147
140
ctx := context .Background ()
148
141
client , err := genai .NewClient (ctx , & genai.ClientConfig {
149
142
APIKey : os .Getenv ("GEMINI_API_KEY" ),
@@ -154,28 +147,125 @@ func CacheDelete() error {
154
147
}
155
148
156
149
modelName := "gemini-1.5-flash-001"
157
- // Open the file.
158
- file , err := os .Open (filepath .Join (getMedia (), "a11.txt" ))
150
+ systemInstruction := "You are an expert analyzing transcripts."
151
+
152
+ // Create initial chat with a system instruction.
153
+ chat , err := client .Chats .Create (ctx , modelName , & genai.GenerateContentConfig {
154
+ SystemInstruction : genai .NewContentFromText (systemInstruction , "user" ),
155
+ }, nil )
156
+ if err != nil {
157
+ log .Fatal (err )
158
+ }
159
+
160
+ document , err := client .Files .UploadFromPath (
161
+ ctx ,
162
+ filepath .Join (getMedia (), "a11.txt" ),
163
+ & genai.UploadFileConfig {
164
+ MIMEType : "text/plain" ,
165
+ },
166
+ )
159
167
if err != nil {
160
- log .Fatal ("Error opening file:" , err )
168
+ log .Fatal (err )
169
+ }
170
+
171
+ // Send first message with the transcript.
172
+ parts := make ([]genai.Part , 2 )
173
+ parts [0 ] = genai.Part {Text : "Hi, could you summarize this transcript?" }
174
+ parts [1 ] = genai.Part {
175
+ FileData : & genai.FileData {
176
+ FileURI : document .URI ,
177
+ MIMEType : document .MIMEType ,
178
+ },
179
+ }
180
+
181
+ // Send chat message.
182
+ resp , err := chat .SendMessage (ctx , parts ... )
183
+ if err != nil {
184
+ log .Fatal (err )
185
+ }
186
+ fmt .Println ("\n \n model: " , resp .Text ())
187
+
188
+ resp , err = chat .SendMessage (
189
+ ctx ,
190
+ genai.Part {
191
+ Text : "Okay, could you tell me more about the trans-lunar injection" ,
192
+ },
193
+ )
194
+ if err != nil {
195
+ log .Fatal (err )
196
+ }
197
+ fmt .Println ("\n \n model: " , resp .Text ())
198
+
199
+ // To cache the conversation so far, pass the chat history as the list of contents.
200
+ cache , err := client .Caches .Create (ctx , modelName , & genai.CreateCachedContentConfig {
201
+ Contents : chat .History (false ),
202
+ SystemInstruction : genai .NewContentFromText (systemInstruction , "user" ),
203
+ })
204
+ if err != nil {
205
+ log .Fatal (err )
161
206
}
162
- defer file .Close ()
163
207
164
- // Read the file.
165
- data , err := io .ReadAll (file )
208
+ // Continue the conversation using the cached history.
209
+ chat , err = client .Chats .Create (ctx , modelName , & genai.GenerateContentConfig {
210
+ CachedContent : cache .Name ,
211
+ }, nil )
166
212
if err != nil {
167
- log .Fatal ("Error reading file:" , err )
213
+ log .Fatal (err )
168
214
}
169
215
216
+ resp , err = chat .SendMessage (
217
+ ctx ,
218
+ genai.Part {
219
+ Text : "I didn't understand that last part, could you explain it in simpler language?" ,
220
+ },
221
+ )
222
+ if err != nil {
223
+ log .Fatal (err )
224
+ }
225
+ fmt .Println ("\n \n model: " , resp .Text ())
226
+ // [END cache_create_from_chat]
227
+
228
+ // Clean up the cache.
229
+ if _ , err := client .Caches .Delete (ctx , cache .Name , nil ); err != nil {
230
+ log .Fatal (err )
231
+ }
232
+ return resp , nil
233
+ }
234
+
235
+ func CacheDelete () error {
236
+ // [START cache_delete]
237
+ ctx := context .Background ()
238
+ client , err := genai .NewClient (ctx , & genai.ClientConfig {
239
+ APIKey : os .Getenv ("GEMINI_API_KEY" ),
240
+ Backend : genai .BackendGeminiAPI ,
241
+ })
242
+ if err != nil {
243
+ log .Fatal (err )
244
+ }
245
+
246
+ modelName := "gemini-1.5-flash-001"
247
+ document , err := client .Files .UploadFromPath (
248
+ ctx ,
249
+ filepath .Join (getMedia (), "a11.txt" ),
250
+ & genai.UploadFileConfig {
251
+ MIMEType : "text/plain" ,
252
+ },
253
+ )
254
+ if err != nil {
255
+ log .Fatal (err )
256
+ }
170
257
parts := []* genai.Part {
171
- { InlineData : & genai.Blob { Data : data , MIMEType : "text/plain" }} ,
258
+ genai .NewPartFromURI ( document . URI , document . MIMEType ) ,
172
259
}
173
260
contents := []* genai.Content {
174
- genai .NewUserContentFromParts (parts ),
261
+ genai .NewContentFromParts (parts , "user" ),
175
262
}
263
+
176
264
cache , err := client .Caches .Create (ctx , modelName , & genai.CreateCachedContentConfig {
177
265
Contents : contents ,
178
- SystemInstruction : genai .NewUserContentFromText ("You are an expert analyzing transcripts." ),
266
+ SystemInstruction : genai .NewContentFromText (
267
+ "You are an expert analyzing transcripts." , "user" ,
268
+ ),
179
269
})
180
270
if err != nil {
181
271
log .Fatal (err )
@@ -202,28 +292,28 @@ func CacheGet() error {
202
292
}
203
293
204
294
modelName := "gemini-1.5-flash-001"
205
- // Open the file.
206
- file , err := os .Open (filepath .Join (getMedia (), "a11.txt" ))
207
- if err != nil {
208
- log .Fatal ("Error opening file:" , err )
209
- }
210
- defer file .Close ()
211
-
212
- // Read the file.
213
- data , err := io .ReadAll (file )
295
+ document , err := client .Files .UploadFromPath (
296
+ ctx ,
297
+ filepath .Join (getMedia (), "a11.txt" ),
298
+ & genai.UploadFileConfig {
299
+ MIMEType : "text/plain" ,
300
+ },
301
+ )
214
302
if err != nil {
215
- log .Fatal ("Error reading file:" , err )
303
+ log .Fatal (err )
216
304
}
217
-
218
305
parts := []* genai.Part {
219
- { InlineData : & genai.Blob { Data : data , MIMEType : "text/plain" }} ,
306
+ genai .NewPartFromURI ( document . URI , document . MIMEType ) ,
220
307
}
221
308
contents := []* genai.Content {
222
- genai .NewUserContentFromParts (parts ),
309
+ genai .NewContentFromParts (parts , "user" ),
223
310
}
311
+
224
312
cache , err := client .Caches .Create (ctx , modelName , & genai.CreateCachedContentConfig {
225
313
Contents : contents ,
226
- SystemInstruction : genai .NewUserContentFromText ("You are an expert analyzing transcripts." ),
314
+ SystemInstruction : genai .NewContentFromText (
315
+ "You are an expert analyzing transcripts." , "user" ,
316
+ ),
227
317
})
228
318
if err != nil {
229
319
log .Fatal (err )
@@ -254,28 +344,27 @@ func CacheList() error {
254
344
255
345
// For demonstration, create a cache first.
256
346
modelName := "gemini-1.5-flash-001"
257
- // Open the file.
258
- file , err := os .Open (filepath .Join (getMedia (), "a11.txt" ))
259
- if err != nil {
260
- log .Fatal ("Error opening file:" , err )
261
- }
262
- defer file .Close ()
263
-
264
- // Read the file.
265
- data , err := io .ReadAll (file )
347
+ document , err := client .Files .UploadFromPath (
348
+ ctx ,
349
+ filepath .Join (getMedia (), "a11.txt" ),
350
+ & genai.UploadFileConfig {
351
+ MIMEType : "text/plain" ,
352
+ },
353
+ )
266
354
if err != nil {
267
- log .Fatal ("Error reading file:" , err )
355
+ log .Fatal (err )
268
356
}
269
-
270
357
parts := []* genai.Part {
271
- { InlineData : & genai.Blob { Data : data , MIMEType : "text/plain" }} ,
358
+ genai .NewPartFromURI ( document . URI , document . MIMEType ) ,
272
359
}
273
360
contents := []* genai.Content {
274
- genai .NewUserContentFromParts (parts ),
361
+ genai .NewContentFromParts (parts , "user" ),
275
362
}
276
363
cache , err := client .Caches .Create (ctx , modelName , & genai.CreateCachedContentConfig {
277
364
Contents : contents ,
278
- SystemInstruction : genai .NewUserContentFromText ("You are an expert analyzing transcripts." ),
365
+ SystemInstruction : genai .NewContentFromText (
366
+ "You are an expert analyzing transcripts." , "user" ,
367
+ ),
279
368
})
280
369
if err != nil {
281
370
log .Fatal (err )
@@ -322,48 +411,46 @@ func CacheUpdate() error {
322
411
}
323
412
324
413
modelName := "gemini-1.5-flash-001"
325
- // Open the file.
326
- file , err := os .Open (filepath .Join (getMedia (), "a11.txt" ))
327
- if err != nil {
328
- log .Fatal ("Error opening file:" , err )
329
- }
330
- defer file .Close ()
331
-
332
- // Read the file.
333
- data , err := io .ReadAll (file )
414
+ document , err := client .Files .UploadFromPath (
415
+ ctx ,
416
+ filepath .Join (getMedia (), "a11.txt" ),
417
+ & genai.UploadFileConfig {
418
+ MIMEType : "text/plain" ,
419
+ },
420
+ )
334
421
if err != nil {
335
- log .Fatal ("Error reading file:" , err )
422
+ log .Fatal (err )
336
423
}
337
-
338
424
parts := []* genai.Part {
339
- { InlineData : & genai.Blob { Data : data , MIMEType : "text/plain" }} ,
425
+ genai .NewPartFromURI ( document . URI , document . MIMEType ) ,
340
426
}
341
427
contents := []* genai.Content {
342
- genai .NewUserContentFromParts (parts ),
428
+ genai .NewContentFromParts (parts , "user" ),
343
429
}
344
430
cache , err := client .Caches .Create (ctx , modelName , & genai.CreateCachedContentConfig {
345
431
Contents : contents ,
346
- SystemInstruction : genai .NewUserContentFromText ("You are an expert analyzing transcripts." ),
432
+ SystemInstruction : genai .NewContentFromText (
433
+ "You are an expert analyzing transcripts." , "user" ,
434
+ ),
347
435
})
348
436
if err != nil {
349
437
log .Fatal (err )
350
438
}
351
439
352
440
// Update the TTL (2 hours).
353
- ttl := "7200s"
354
441
cache , err = client .Caches .Update (ctx , cache .Name , & genai.UpdateCachedContentConfig {
355
- TTL : ttl ,
442
+ TTL : 7200 * time . Second ,
356
443
})
357
444
if err != nil {
358
445
log .Fatal (err )
359
446
}
360
- fmt .Println ("After TTL update:" )
447
+ fmt .Println ("After update:" )
361
448
fmt .Println (cache )
362
449
363
450
// Alternatively, update expire_time directly.
364
451
expire := time .Now ().Add (15 * time .Minute ).UTC ()
365
452
cache , err = client .Caches .Update (ctx , cache .Name , & genai.UpdateCachedContentConfig {
366
- ExpireTime : & expire ,
453
+ ExpireTime : expire ,
367
454
})
368
455
if err != nil {
369
456
log .Fatal (err )
0 commit comments