Skip to content

Commit

Permalink
fix(vertexai): in safety samples, do not write a main func (#3786)
Browse files Browse the repository at this point in the history
* fix(vertexai): in safety samples, do not write a main func

* feat(vertexai): model names gemini 1.0

* fix(vertexai): visible sample values in comments

---------

Co-authored-by: Eric Dong <itseric@google.com>
  • Loading branch information
Deleplace and gericdong committed Feb 21, 2024
1 parent f25c61b commit 39f12fc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 62 deletions.
26 changes: 5 additions & 21 deletions vertexai/safety-settings-multimodal/safety-settings-multimodal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// safety-settings-multimodal shows how to adjust safety settings for mixed text and image input
package main
package safetysettingsmultimodal

// [START aiplatform_gemini_safety_settings]
import (
"context"
"fmt"
"io"
"log"
"mime"
"os"
"path/filepath"

"cloud.google.com/go/vertexai/genai"
)

func main() {
projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
location := "us-central1"
modelName := "gemini-1.0-pro-vision"

prompt := "describe what is in this picture"
image := "gs://generativeai-downloads/images/scones.jpg"

if projectID == "" {
log.Fatal("require environment variable GOOGLE_CLOUD_PROJECT")
}

err := generateMultimodalContent(os.Stdout, prompt, image, projectID, location, modelName)
if err != nil {
log.Fatalf("unable to generate: %v", err)
}
}

// generateMultimodalContent generates a response into w, based upon the prompt
// and image provided.
func generateMultimodalContent(w io.Writer, prompt, image, projectID, location, modelName string) error {
// prompt := "describe this image."
// location := "us-central1"
// model := "gemini-1.0-pro-vision"
// image := "gs://cloud-samples-data/generative-ai/image/320px-Felis_catus-cat_on_snow.jpg"
ctx := context.Background()

client, err := genai.NewClient(ctx, projectID, location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package safetysettingsmultimodal

import (
"bytes"
Expand All @@ -23,23 +23,15 @@ import (
)

func TestGenerateContent(t *testing.T) {
t.Skip("TODO(muncus): remove skip")
tc := testutil.SystemTest(t)

prompt := "describe this image."
projectID := tc.ProjectID
location := "us-central1"

model := "gemini-1.0-pro-vision"

image := "gs://cloud-samples-data/generative-ai/image/320px-Felis_catus-cat_on_snow.jpg"

if projectID == "" {
t.Fatal("require environment variable GOOGLE_CLOUD_PROJECT")
}

var buf bytes.Buffer
if err := generateMultimodalContent(&buf, prompt, image, projectID, location, model); err != nil {
if err := generateMultimodalContent(&buf, prompt, image, tc.ProjectID, location, model); err != nil {
t.Fatal(err)
}

Expand Down
28 changes: 6 additions & 22 deletions vertexai/safety-settings/safety-settings.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -13,38 +13,22 @@
// limitations under the License.
// safety-settings shows how to adjust safety settings for a generative model

package main
package safetysettings

import (
"context"
"fmt"
"io"
"log"
"os"

"cloud.google.com/go/vertexai/genai"
)

func main() {
projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
location := "us-central1"
modelName := "gemini-1.0-pro"
temperature := 0.4

prompt := "say something nice to me, but be angry"

if projectID == "" {
log.Fatal("require environment variable GOOGLE_CLOUD_PROJECT")
}

err := generateContent(os.Stdout, prompt, projectID, location, modelName, float32(temperature))
if err != nil {
fmt.Printf("unable to generate: %v\n", err)
}
}

// generateContent generates text from prompt and configurations provided.
func generateContent(w io.Writer, prompt, projectID, location, modelName string, temperature float32) error {
// prompt := "hello, say something mean to me."
// location := "us-central1"
// model := "gemini-1.0-pro"
// temp := float32(0.8)
ctx := context.Background()

client, err := genai.NewClient(ctx, projectID, location)
Expand Down
12 changes: 3 additions & 9 deletions vertexai/safety-settings/safety-settings_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package safetysettings

import (
"bytes"
Expand All @@ -27,18 +27,12 @@ func TestGenerateContent(t *testing.T) {
tc := testutil.SystemTest(t)

prompt := "hello, say something mean to me."
projectID := tc.ProjectID
location := "us-central1"

model := "gemini-1.0-pro"
temp := 0.8

if projectID == "" {
t.Fatal("require environment variable GOOGLE_CLOUD_PROJECT")
}

var buf bytes.Buffer
if err := generateContent(&buf, prompt, projectID, location, model, float32(temp)); err != nil {
if err := generateContent(&buf, prompt, tc.ProjectID, location, model, float32(temp)); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit 39f12fc

Please sign in to comment.