Skip to content

Commit

Permalink
feat: add gemini code sample and test for text-only input (#4130)
Browse files Browse the repository at this point in the history
## Description

Fixes b:336785030

Please merge on approval
  • Loading branch information
irataxy authored May 11, 2024
1 parent 78aa082 commit 1626b23
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
53 changes: 53 additions & 0 deletions vertexai/snippets/text_input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.
// You may obtain a copy of the License at
//
// http://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 snippets

// [START generativeaionvertexai_gemini_generate_from_text_input]
import (
"context"
"encoding/json"
"fmt"
"io"

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

func generateContentFromText(w io.Writer, projectID string) error {
location := "us-central1"
modelName := "gemini-1.0-pro-vision-001"

ctx := context.Background()
client, err := genai.NewClient(ctx, projectID, location)
if err != nil {
return fmt.Errorf("error creating client: %w", err)
}
gemini := client.GenerativeModel(modelName)
prompt := genai.Text(
"What's a good name for a flower shop that specializes in selling bouquets of dried flowers?")

resp, err := gemini.GenerateContent(ctx, prompt)
if err != nil {
return fmt.Errorf("error generating content: %w", err)
}
// See the JSON response in
// https://pkg.go.dev/cloud.google.com/go/vertexai/genai#GenerateContentResponse.
rb, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintln(w, string(rb))
return nil
}

// [END generativeaionvertexai_gemini_generate_from_text_input]
34 changes: 34 additions & 0 deletions vertexai/snippets/text_input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.
// You may obtain a copy of the License at
//
// http://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 snippets

import (
"bytes"
"strings"
"testing"

"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
)

func TestTextInput(t *testing.T) {
tc := testutil.SystemTest(t)
buf := &bytes.Buffer{}
err := generateContentFromText(buf, tc.ProjectID)
if err != nil {
t.Errorf("generateContentFromText error: %v", err)
}
if got := buf.String(); !strings.Contains(got, "Candidates") {
t.Error("Candidates JSON not found in response")
}
}

0 comments on commit 1626b23

Please sign in to comment.