Skip to content

Commit

Permalink
feat: gemini translate sample
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBlackWolf committed Oct 1, 2024
1 parent 6e7b24c commit 049d43c
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
91 changes: 91 additions & 0 deletions vertexai/snippets/gemini_translate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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 aiplatform_gemini_translate]
import (
"context"
"encoding/json"
"fmt"
"io"

"cloud.google.com/go/vertexai/genai"
"google.golang.org/protobuf/proto"
)

// geminiTranslate shows how translate text, using gemini
func geminiTranslate(w io.Writer, project, location string) error {
sourceLanguageCode := "en"
targetLanguageCode := "fr"
modelName := "gemini-1.0-pro"

ctx := context.Background()
client, err := genai.NewClient(ctx, project, location)
if err != nil {
return fmt.Errorf("error creating client: %w", err)
}
model := client.GenerativeModel(modelName)

model.GenerationConfig = genai.GenerationConfig{
TopP: proto.Float32(1),
TopK: proto.Int32(32),
Temperature: proto.Float32(0.4),
MaxOutputTokens: proto.Int32(2048),
}

// configure the safety settings thresholds
model.SafetySettings = []*genai.SafetySetting{
{
Category: genai.HarmCategoryHateSpeech,
Threshold: genai.HarmBlockMediumAndAbove,
},
{
Category: genai.HarmCategoryDangerousContent,
Threshold: genai.HarmBlockMediumAndAbove,
},
{
Category: genai.HarmCategorySexuallyExplicit,
Threshold: genai.HarmBlockMediumAndAbove,
},
{
Category: genai.HarmCategoryHarassment,
Threshold: genai.HarmBlockMediumAndAbove,
},
}
systemInstruction := fmt.Sprintf("Your mission is to translate text from %xs to %s", sourceLanguageCode, targetLanguageCode)

model.SystemInstruction = &genai.Content{
Role: "user",
Parts: []genai.Part{genai.Text(systemInstruction)},
}

prompt := genai.Text("Translate: how are you doing today?")

resp, err := model.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 aiplatform_gemini_translate]
36 changes: 36 additions & 0 deletions vertexai/snippets/gemini_translate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 TestGeminiTranslate(t *testing.T) {
tc := testutil.SystemTest(t)
buf := &bytes.Buffer{}
err := geminiTranslate(buf, tc.ProjectID, "us-east1")
if err != nil {
t.Errorf("geminiTranslate error: %v", err)
}

want := "Comment allez-vous"
if got := buf.String(); !strings.Contains(got, want) {
t.Errorf("wrong response, got: %s want: %s", got, want)
}
}
2 changes: 1 addition & 1 deletion vertexai/snippets/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
cloud.google.com/go/vertexai v0.12.0
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20240724083556-7f760db013b7
google.golang.org/protobuf v1.34.1
)

require (
Expand Down Expand Up @@ -43,5 +44,4 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.1 // indirect
)

0 comments on commit 049d43c

Please sign in to comment.