From 16e7a93296edff58528a31c73526c6a0d4c25e5e Mon Sep 17 00:00:00 2001 From: CadillacBurgess1 Date: Mon, 12 May 2025 18:25:43 +0000 Subject: [PATCH 1/4] feat(genaisdk): update samples nested objects for better reability --- .../contentcache_create_with_txt_gcs_pdf.go | 185 +++++++++--------- genai/content_cache/contentcache_delete.go | 62 +++--- 2 files changed, 128 insertions(+), 119 deletions(-) diff --git a/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go b/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go index c82f58c228..9dde938c68 100644 --- a/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go +++ b/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go @@ -1,102 +1,109 @@ -// Copyright 2025 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. + // Copyright 2025 Google LLC + // + // Licensed under the Apache License, Version 2.0 (the "License"); + // you may not use this file except in compliance with the License. + // You may obtain a copy of the License at + // + // https://www.apache.org/licenses/LICENSE-2.0 + // + // Unless required by applicable law or agreed to in writing, software + // distributed under the License is distributed on an "AS IS" BASIS, + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + // See the License for the specific language governing permissions and + // limitations under the License. -// Package content_cache shows examples of using content caching with the GenAI SDK. -package content_cache + // Package content_cache shows examples of using content caching with the GenAI SDK. + package content_cache -// [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf] -import ( - "context" - "encoding/json" - "fmt" - "io" + // [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf] + import ( + "context" + "encoding/json" + "fmt" + "io" - genai "google.golang.org/genai" -) + genai "google.golang.org/genai" + ) -// createContentCache shows how to create a content cache with an expiration parameter. -func createContentCache(w io.Writer) (string, error) { - ctx := context.Background() + // createContentCache shows how to create a content cache with an expiration parameter. + func createContentCache(w io.Writer) (string, error) { + ctx := context.Background() - client, err := genai.NewClient(ctx, &genai.ClientConfig{ - HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, - }) - if err != nil { - return "", fmt.Errorf("failed to create genai client: %w", err) - } - - modelName := "gemini-2.0-flash-001" + client, err := genai.NewClient(ctx, &genai.ClientConfig{ + HTTPOptions: genai.HTTPOptions{ + APIVersion: "v1", + }, + }) + if err != nil { + return "", fmt.Errorf("failed to create genai client: %w", err) + } - systemInstruction := "You are an expert researcher. You always stick to the facts " + - "in the sources provided, and never make up new facts. " + - "Now look at these research papers, and answer the following questions." + modelName := "gemini-2.0-flash-001" - cacheContents := []*genai.Content{ - { - Parts: []*genai.Part{ - {FileData: &genai.FileData{ - FileURI: "gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf", - MIMEType: "application/pdf", - }}, - {FileData: &genai.FileData{ - FileURI: "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf", - MIMEType: "application/pdf", - }}, - }, - Role: "user", - }, - } - config := &genai.CreateCachedContentConfig{ - Contents: cacheContents, - SystemInstruction: &genai.Content{ - Parts: []*genai.Part{ - {Text: systemInstruction}, - }, - }, - DisplayName: "example-cache", - TTL: "86400s", - } + systemInstruction := "You are an expert researcher. You always stick to the facts " + + "in the sources provided, and never make up new facts. " + + "Now look at these research papers, and answer the following questions." - res, err := client.Caches.Create(ctx, modelName, config) - if err != nil { - return "", fmt.Errorf("failed to create content cache: %w", err) - } + cacheContents := []*genai.Content{ + { + Role: "user", + Parts: []*genai.Part{ + { // Part 1 + FileData: &genai.FileData{ + FileURI: "gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf", + MIMEType: "application/pdf", + }, + }, + { // Part 2 + FileData: &genai.FileData{ + FileURI: "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf", + MIMEType: "application/pdf", + }, + }, + }, + }, + } + config := &genai.CreateCachedContentConfig{ + Contents: cacheContents, + DisplayName: "example-cache", + TTL: "86400s", + SystemInstruction: &genai.Content{ + Parts: []*genai.Part{ + { + Text: systemInstruction, + }, + }, + }, + } - cachedContent, err := json.MarshalIndent(res, "", " ") - if err != nil { - return "", fmt.Errorf("failed to marshal cache info: %w", err) - } + res, err := client.Caches.Create(ctx, modelName, config) + if err != nil { + return "", fmt.Errorf("failed to create content cache: %w", err) + } - // See the documentation: https://pkg.go.dev/google.golang.org/genai#CachedContent - fmt.Fprintln(w, string(cachedContent)) + cachedContent, err := json.MarshalIndent(res, "", " ") // Use 2 spaces for indent + if err != nil { + return "", fmt.Errorf("failed to marshal cache info: %w", err) + } - // Example response: - // { - // "name": "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111", - // "displayName": "example-cache", - // "model": "projects/111111111111/locations/us-central1/publishers/google/models/gemini-2.0-flash-001", - // "createTime": "2025-02-18T15:05:08.29468Z", - // "updateTime": "2025-02-18T15:05:08.29468Z", - // "expireTime": "2025-02-19T15:05:08.280828Z", - // "usageMetadata": { - // "imageCount": 167, - // "textCount": 153, - // "totalTokenCount": 43125 - // } - // } + // See the documentation: https://pkg.go.dev/google.golang.org/genai#CachedContent + fmt.Fprintln(w, string(cachedContent)) - return res.Name, nil -} + // Example response: + // { + // "name": "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111", + // "displayName": "example-cache", + // "model": "projects/111111111111/locations/us-central1/publishers/google/models/gemini-2.0-flash-001", + // "createTime": "2025-02-18T15:05:08.29468Z", + // "updateTime": "2025-02-18T15:05:08.29468Z", + // "expireTime": "2025-02-19T15:05:08.280828Z", + // "usageMetadata": { + // "imageCount": 167, + // "textCount": 153, + // "totalTokenCount": 43125 + // } + // } -// [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf] + return res.Name, nil + } + // [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf] diff --git a/genai/content_cache/contentcache_delete.go b/genai/content_cache/contentcache_delete.go index 9e0853bd34..f03734945d 100644 --- a/genai/content_cache/contentcache_delete.go +++ b/genai/content_cache/contentcache_delete.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// https://www.apache.org/licenses/LICENSE-2.0 +// https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -13,39 +13,41 @@ // limitations under the License. // Package content_cache shows examples of using content caching with the GenAI SDK. -package content_cache + package content_cache // [START googlegenaisdk_contentcache_delete] -import ( - "context" - "fmt" - "io" + import ( + "context" + "fmt" + "io" - genai "google.golang.org/genai" -) + genai "google.golang.org/genai" + ) // deleteContentCache shows how to delete content cache. -func deleteContentCache(w io.Writer, cacheName string) error { - ctx := context.Background() - - client, err := genai.NewClient(ctx, &genai.ClientConfig{ - HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, - }) - if err != nil { - return fmt.Errorf("failed to create genai client: %w", err) - } - - _, err = client.Caches.Delete(ctx, cacheName, &genai.DeleteCachedContentConfig{}) - if err != nil { - return fmt.Errorf("failed to delete content cache: %w", err) - } - - fmt.Fprintf(w, "Deleted cache %q\n", cacheName) - - // Example response: - // Deleted cache "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111" - - return nil -} + func deleteContentCache(w io.Writer, cacheName string) error { + ctx := context.Background() + + client, err := genai.NewClient(ctx, &genai.ClientConfig{ + HTTPOptions: genai.HTTPOptions{ + APIVersion: "v1", + }, + }) + if err != nil { + return fmt.Errorf("failed to create genai client: %w", err) + } + + _, err = client.Caches.Delete(ctx, cacheName, &genai.DeleteCachedContentConfig{}) + if err != nil { + return fmt.Errorf("failed to delete content cache: %w", err) + } + + fmt.Fprintf(w, "Deleted cache %q\n", cacheName) + + // Example response: + // Deleted cache "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111" + + return nil + } // [END googlegenaisdk_contentcache_delete] From b5b2f52b4e3b62cf73bfa66fa2319640143e7d01 Mon Sep 17 00:00:00 2001 From: CadillacBurgess1 Date: Mon, 12 May 2025 18:30:13 +0000 Subject: [PATCH 2/4] feat(genaisdk): update samples nested objects for better readability tidy fix --- .../contentcache_create_with_txt_gcs_pdf.go | 185 +++++++++--------- genai/content_cache/contentcache_delete.go | 50 ++--- 2 files changed, 118 insertions(+), 117 deletions(-) diff --git a/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go b/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go index 9dde938c68..a1839e4804 100644 --- a/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go +++ b/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go @@ -1,109 +1,110 @@ - // Copyright 2025 Google LLC - // - // Licensed under the Apache License, Version 2.0 (the "License"); - // you may not use this file except in compliance with the License. - // You may obtain a copy of the License at - // - // https://www.apache.org/licenses/LICENSE-2.0 - // - // Unless required by applicable law or agreed to in writing, software - // distributed under the License is distributed on an "AS IS" BASIS, - // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - // See the License for the specific language governing permissions and - // limitations under the License. +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. - // Package content_cache shows examples of using content caching with the GenAI SDK. - package content_cache +// Package content_cache shows examples of using content caching with the GenAI SDK. +package content_cache - // [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf] - import ( - "context" - "encoding/json" - "fmt" - "io" +// [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf] +import ( + "context" + "encoding/json" + "fmt" + "io" - genai "google.golang.org/genai" - ) + genai "google.golang.org/genai" +) - // createContentCache shows how to create a content cache with an expiration parameter. - func createContentCache(w io.Writer) (string, error) { - ctx := context.Background() +// createContentCache shows how to create a content cache with an expiration parameter. +func createContentCache(w io.Writer) (string, error) { + ctx := context.Background() - client, err := genai.NewClient(ctx, &genai.ClientConfig{ - HTTPOptions: genai.HTTPOptions{ + client, err := genai.NewClient(ctx, &genai.ClientConfig{ + HTTPOptions: genai.HTTPOptions{ APIVersion: "v1", - }, - }) - if err != nil { - return "", fmt.Errorf("failed to create genai client: %w", err) - } + }, + }) + if err != nil { + return "", fmt.Errorf("failed to create genai client: %w", err) + } - modelName := "gemini-2.0-flash-001" + modelName := "gemini-2.0-flash-001" - systemInstruction := "You are an expert researcher. You always stick to the facts " + - "in the sources provided, and never make up new facts. " + - "Now look at these research papers, and answer the following questions." + systemInstruction := "You are an expert researcher. You always stick to the facts " + + "in the sources provided, and never make up new facts. " + + "Now look at these research papers, and answer the following questions." - cacheContents := []*genai.Content{ - { - Role: "user", - Parts: []*genai.Part{ - { // Part 1 - FileData: &genai.FileData{ - FileURI: "gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf", - MIMEType: "application/pdf", - }, - }, + cacheContents := []*genai.Content{ + { + Role: "user", + Parts: []*genai.Part{ + { // Part 1 + FileData: &genai.FileData{ + FileURI: "gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf", + MIMEType: "application/pdf", + }, + }, { // Part 2 - FileData: &genai.FileData{ - FileURI: "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf", - MIMEType: "application/pdf", - }, - }, - }, - }, - } - config := &genai.CreateCachedContentConfig{ - Contents: cacheContents, - DisplayName: "example-cache", - TTL: "86400s", - SystemInstruction: &genai.Content{ - Parts: []*genai.Part{ - { + FileData: &genai.FileData{ + FileURI: "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf", + MIMEType: "application/pdf", + }, + }, + }, + }, + } + config := &genai.CreateCachedContentConfig{ + Contents: cacheContents, + DisplayName: "example-cache", + TTL: "86400s", + SystemInstruction: &genai.Content{ + Parts: []*genai.Part{ + { Text: systemInstruction, }, - }, - }, - } + }, + }, + } + + res, err := client.Caches.Create(ctx, modelName, config) + if err != nil { + return "", fmt.Errorf("failed to create content cache: %w", err) + } - res, err := client.Caches.Create(ctx, modelName, config) - if err != nil { - return "", fmt.Errorf("failed to create content cache: %w", err) - } + cachedContent, err := json.MarshalIndent(res, "", " ") // Use 2 spaces for indent + if err != nil { + return "", fmt.Errorf("failed to marshal cache info: %w", err) + } - cachedContent, err := json.MarshalIndent(res, "", " ") // Use 2 spaces for indent - if err != nil { - return "", fmt.Errorf("failed to marshal cache info: %w", err) - } + // See the documentation: https://pkg.go.dev/google.golang.org/genai#CachedContent + fmt.Fprintln(w, string(cachedContent)) - // See the documentation: https://pkg.go.dev/google.golang.org/genai#CachedContent - fmt.Fprintln(w, string(cachedContent)) + // Example response: + // { + // "name": "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111", + // "displayName": "example-cache", + // "model": "projects/111111111111/locations/us-central1/publishers/google/models/gemini-2.0-flash-001", + // "createTime": "2025-02-18T15:05:08.29468Z", + // "updateTime": "2025-02-18T15:05:08.29468Z", + // "expireTime": "2025-02-19T15:05:08.280828Z", + // "usageMetadata": { + // "imageCount": 167, + // "textCount": 153, + // "totalTokenCount": 43125 + // } + // } - // Example response: - // { - // "name": "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111", - // "displayName": "example-cache", - // "model": "projects/111111111111/locations/us-central1/publishers/google/models/gemini-2.0-flash-001", - // "createTime": "2025-02-18T15:05:08.29468Z", - // "updateTime": "2025-02-18T15:05:08.29468Z", - // "expireTime": "2025-02-19T15:05:08.280828Z", - // "usageMetadata": { - // "imageCount": 167, - // "textCount": 153, - // "totalTokenCount": 43125 - // } - // } + return res.Name, nil +} - return res.Name, nil - } - // [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf] +// [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf] diff --git a/genai/content_cache/contentcache_delete.go b/genai/content_cache/contentcache_delete.go index f03734945d..b90d25eb17 100644 --- a/genai/content_cache/contentcache_delete.go +++ b/genai/content_cache/contentcache_delete.go @@ -13,41 +13,41 @@ // limitations under the License. // Package content_cache shows examples of using content caching with the GenAI SDK. - package content_cache +package content_cache // [START googlegenaisdk_contentcache_delete] - import ( - "context" - "fmt" - "io" +import ( + "context" + "fmt" + "io" - genai "google.golang.org/genai" - ) + genai "google.golang.org/genai" +) // deleteContentCache shows how to delete content cache. - func deleteContentCache(w io.Writer, cacheName string) error { - ctx := context.Background() +func deleteContentCache(w io.Writer, cacheName string) error { + ctx := context.Background() - client, err := genai.NewClient(ctx, &genai.ClientConfig{ - HTTPOptions: genai.HTTPOptions{ + client, err := genai.NewClient(ctx, &genai.ClientConfig{ + HTTPOptions: genai.HTTPOptions{ APIVersion: "v1", - }, - }) - if err != nil { - return fmt.Errorf("failed to create genai client: %w", err) - } + }, + }) + if err != nil { + return fmt.Errorf("failed to create genai client: %w", err) + } - _, err = client.Caches.Delete(ctx, cacheName, &genai.DeleteCachedContentConfig{}) - if err != nil { - return fmt.Errorf("failed to delete content cache: %w", err) - } + _, err = client.Caches.Delete(ctx, cacheName, &genai.DeleteCachedContentConfig{}) + if err != nil { + return fmt.Errorf("failed to delete content cache: %w", err) + } - fmt.Fprintf(w, "Deleted cache %q\n", cacheName) + fmt.Fprintf(w, "Deleted cache %q\n", cacheName) - // Example response: - // Deleted cache "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111" + // Example response: + // Deleted cache "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111" - return nil - } + return nil +} // [END googlegenaisdk_contentcache_delete] From 35a1a02bbd8841d296be1b383ae1f1fcfee47e33 Mon Sep 17 00:00:00 2001 From: CadillacBurgess1 Date: Mon, 12 May 2025 18:34:18 +0000 Subject: [PATCH 3/4] feat(genaisdk): update samples nested objects for better readability removing local testing comments --- genai/content_cache/contentcache_create_with_txt_gcs_pdf.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go b/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go index a1839e4804..5d9b28c662 100644 --- a/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go +++ b/genai/content_cache/contentcache_create_with_txt_gcs_pdf.go @@ -48,13 +48,13 @@ func createContentCache(w io.Writer) (string, error) { { Role: "user", Parts: []*genai.Part{ - { // Part 1 + { FileData: &genai.FileData{ FileURI: "gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf", MIMEType: "application/pdf", }, }, - { // Part 2 + { FileData: &genai.FileData{ FileURI: "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf", MIMEType: "application/pdf", @@ -81,7 +81,7 @@ func createContentCache(w io.Writer) (string, error) { return "", fmt.Errorf("failed to create content cache: %w", err) } - cachedContent, err := json.MarshalIndent(res, "", " ") // Use 2 spaces for indent + cachedContent, err := json.MarshalIndent(res, "", " ") if err != nil { return "", fmt.Errorf("failed to marshal cache info: %w", err) } From 5c317166865a872577918ba7358a3cdea4dc5924 Mon Sep 17 00:00:00 2001 From: CadillacBurgess1 Date: Mon, 12 May 2025 18:59:11 +0000 Subject: [PATCH 4/4] feat(genaisdk): update samples nested objects for better readability update and use_with_text script --- genai/content_cache/contentcache_update.go | 6 ++++-- genai/content_cache/contentcache_use_with_txt.go | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/genai/content_cache/contentcache_update.go b/genai/content_cache/contentcache_update.go index 62ddbc6897..f0457049b5 100644 --- a/genai/content_cache/contentcache_update.go +++ b/genai/content_cache/contentcache_update.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// https://www.apache.org/licenses/LICENSE-2.0 +// https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -30,7 +30,9 @@ func updateContentCache(w io.Writer, cacheName string) error { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ - HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, + HTTPOptions: genai.HTTPOptions{ + APIVersion: "v1", + }, }) if err != nil { return fmt.Errorf("failed to create genai client: %w", err) diff --git a/genai/content_cache/contentcache_use_with_txt.go b/genai/content_cache/contentcache_use_with_txt.go index a8b650e7cc..abadcc64c8 100644 --- a/genai/content_cache/contentcache_use_with_txt.go +++ b/genai/content_cache/contentcache_use_with_txt.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// https://www.apache.org/licenses/LICENSE-2.0 +// https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -29,7 +29,9 @@ func useContentCacheWithTxt(w io.Writer, cacheName string) error { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ - HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, + HTTPOptions: genai.HTTPOptions{ + APIVersion: "v1", + }, }) if err != nil { return fmt.Errorf("failed to create genai client: %w", err)