Skip to content

Commit d096ad1

Browse files
committed
Update Python, Go and JS samples to use the newest SDK
1 parent 6779d28 commit d096ad1

27 files changed

+1400
-766
lines changed

go/cache.go

+178-91
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package examples
33
import (
44
"context"
55
"fmt"
6-
"io"
76
"log"
87
"os"
98
"path/filepath"
@@ -24,30 +23,27 @@ func CacheCreate() (*genai.GenerateContentResponse, error) {
2423
}
2524

2625
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+
)
3733
if err != nil {
38-
log.Fatal("Error reading file:", err)
34+
log.Fatal(err)
3935
}
40-
4136
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),
4438
}
4539
contents := []*genai.Content{
46-
genai.NewUserContentFromParts(parts),
40+
genai.NewContentFromParts(parts, "user"),
4741
}
4842
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
4943
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+
),
5147
})
5248
if err != nil {
5349
log.Fatal(err)
@@ -67,7 +63,6 @@ func CacheCreate() (*genai.GenerateContentResponse, error) {
6763
if err != nil {
6864
log.Fatal(err)
6965
}
70-
fmt.Println("Generated content:")
7166
printResponse(response)
7267
// [END cache_create]
7368

@@ -88,29 +83,27 @@ func CacheCreateFromName() (*genai.GenerateContentResponse, error) {
8883
}
8984

9085
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+
)
10093
if err != nil {
101-
log.Fatal("Error reading file:", err)
94+
log.Fatal(err)
10295
}
103-
10496
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),
10798
}
10899
contents := []*genai.Content{
109-
genai.NewUserContentFromParts(parts),
100+
genai.NewContentFromParts(parts, "user"),
110101
}
111102
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
112103
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+
),
114107
})
115108
if err != nil {
116109
log.Fatal(err)
@@ -142,8 +135,8 @@ func CacheCreateFromName() (*genai.GenerateContentResponse, error) {
142135
return response, err
143136
}
144137

145-
func CacheDelete() error {
146-
// [START cache_delete]
138+
func CacheCreateFromChat() (*genai.GenerateContentResponse, error) {
139+
// [START cache_create_from_chat]
147140
ctx := context.Background()
148141
client, err := genai.NewClient(ctx, &genai.ClientConfig{
149142
APIKey: os.Getenv("GEMINI_API_KEY"),
@@ -154,28 +147,125 @@ func CacheDelete() error {
154147
}
155148

156149
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+
)
159167
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\nmodel: ", 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\nmodel: ", 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)
161206
}
162-
defer file.Close()
163207

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)
166212
if err != nil {
167-
log.Fatal("Error reading file:", err)
213+
log.Fatal(err)
168214
}
169215

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\nmodel: ", 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+
}
170257
parts := []*genai.Part{
171-
{InlineData: &genai.Blob{Data: data, MIMEType: "text/plain"}},
258+
genai.NewPartFromURI(document.URI, document.MIMEType),
172259
}
173260
contents := []*genai.Content{
174-
genai.NewUserContentFromParts(parts),
261+
genai.NewContentFromParts(parts, "user"),
175262
}
263+
176264
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
177265
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+
),
179269
})
180270
if err != nil {
181271
log.Fatal(err)
@@ -202,28 +292,28 @@ func CacheGet() error {
202292
}
203293

204294
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+
)
214302
if err != nil {
215-
log.Fatal("Error reading file:", err)
303+
log.Fatal(err)
216304
}
217-
218305
parts := []*genai.Part{
219-
{InlineData: &genai.Blob{Data: data, MIMEType: "text/plain"}},
306+
genai.NewPartFromURI(document.URI, document.MIMEType),
220307
}
221308
contents := []*genai.Content{
222-
genai.NewUserContentFromParts(parts),
309+
genai.NewContentFromParts(parts, "user"),
223310
}
311+
224312
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
225313
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+
),
227317
})
228318
if err != nil {
229319
log.Fatal(err)
@@ -254,28 +344,27 @@ func CacheList() error {
254344

255345
// For demonstration, create a cache first.
256346
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+
)
266354
if err != nil {
267-
log.Fatal("Error reading file:", err)
355+
log.Fatal(err)
268356
}
269-
270357
parts := []*genai.Part{
271-
{InlineData: &genai.Blob{Data: data, MIMEType: "text/plain"}},
358+
genai.NewPartFromURI(document.URI, document.MIMEType),
272359
}
273360
contents := []*genai.Content{
274-
genai.NewUserContentFromParts(parts),
361+
genai.NewContentFromParts(parts, "user"),
275362
}
276363
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
277364
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+
),
279368
})
280369
if err != nil {
281370
log.Fatal(err)
@@ -322,48 +411,46 @@ func CacheUpdate() error {
322411
}
323412

324413
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+
)
334421
if err != nil {
335-
log.Fatal("Error reading file:", err)
422+
log.Fatal(err)
336423
}
337-
338424
parts := []*genai.Part{
339-
{InlineData: &genai.Blob{Data: data, MIMEType: "text/plain"}},
425+
genai.NewPartFromURI(document.URI, document.MIMEType),
340426
}
341427
contents := []*genai.Content{
342-
genai.NewUserContentFromParts(parts),
428+
genai.NewContentFromParts(parts, "user"),
343429
}
344430
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
345431
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+
),
347435
})
348436
if err != nil {
349437
log.Fatal(err)
350438
}
351439

352440
// Update the TTL (2 hours).
353-
ttl := "7200s"
354441
cache, err = client.Caches.Update(ctx, cache.Name, &genai.UpdateCachedContentConfig{
355-
TTL: ttl,
442+
TTL: 7200 * time.Second,
356443
})
357444
if err != nil {
358445
log.Fatal(err)
359446
}
360-
fmt.Println("After TTL update:")
447+
fmt.Println("After update:")
361448
fmt.Println(cache)
362449

363450
// Alternatively, update expire_time directly.
364451
expire := time.Now().Add(15 * time.Minute).UTC()
365452
cache, err = client.Caches.Update(ctx, cache.Name, &genai.UpdateCachedContentConfig{
366-
ExpireTime: &expire,
453+
ExpireTime: expire,
367454
})
368455
if err != nil {
369456
log.Fatal(err)

0 commit comments

Comments
 (0)