|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package content_cache shows examples of using content caching with the GenAI SDK. |
| 16 | +package content_cache |
| 17 | + |
| 18 | +// [START googlegenaisdk_contentcache_list] |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "net/http" |
| 24 | + "time" |
| 25 | + |
| 26 | + "google.golang.org/genai" |
| 27 | +) |
| 28 | + |
| 29 | +// listContentCache shows how to retrieve details about cached content. |
| 30 | +func listContentCache(w io.Writer) error { |
| 31 | + ctx := context.Background() |
| 32 | + |
| 33 | + client, err := genai.NewClient(ctx, &genai.ClientConfig{ |
| 34 | + HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, |
| 35 | + }) |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("failed to create genai client: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + // Retrieve cached content metadata |
| 41 | + cache, err := client.Caches.List(ctx, &genai.ListCachedContentsConfig{ |
| 42 | + HTTPOptions: &genai.HTTPOptions{ |
| 43 | + Headers: http.Header{"X-Custom-Header": []string{"example"}}, |
| 44 | + APIVersion: "v1", |
| 45 | + }, |
| 46 | + }) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("failed to get content cache: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Print basic info about the cached content |
| 52 | + fmt.Fprintf(w, "Cache name: %s\n", cache.Name) |
| 53 | + fmt.Fprintf(w, "Display name: %s\n", cache.Items[0].DisplayName) |
| 54 | + fmt.Fprintf(w, "Model: %s\n", cache.Items[0].Model) |
| 55 | + fmt.Fprintf(w, "Create time: %s\n", cache.Items[0].CreateTime.Format(time.RFC3339)) |
| 56 | + fmt.Fprintf(w, "Update time: %s\n", cache.Items[0].UpdateTime.Format(time.RFC3339)) |
| 57 | + fmt.Fprintf(w, "Expire time: %s (in %s)\n", cache.Items[0].ExpireTime.Format(time.RFC3339), time.Until(cache.Items[0].ExpireTime).Round(time.Second)) |
| 58 | + |
| 59 | + if cache.Items[0].UsageMetadata != nil { |
| 60 | + fmt.Fprintf(w, "Usage metadata: %+v\n", cache.Items[0].UsageMetadata) |
| 61 | + } |
| 62 | + |
| 63 | + // Example response: |
| 64 | + // Cache name: projects/111111111111/locations/us-central1/cachedContents/1234567890123456789 |
| 65 | + // Display name: product_recommendations_prompt |
| 66 | + // Model: models/gemini-2.5-flash |
| 67 | + // Create time: 2025-04-08T02:15:23Z |
| 68 | + // Update time: 2025-04-08T03:05:11Z |
| 69 | + // Expire time: 2025-04-20T03:05:11Z (in 167h59m59s) |
| 70 | + // Usage metadata: &{AudioDurationSeconds:0 ImageCount:167 TextCount:153 TotalTokenCount:43124 VideoDurationSeconds:0} |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// [END googlegenaisdk_contentcache_list] |
0 commit comments