|
| 1 | +// Copyright 2024 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 snippets |
| 16 | + |
| 17 | +// [START generativeaionvertexai_gemma2_predict_gpu] |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + |
| 23 | + "cloud.google.com/go/aiplatform/apiv1/aiplatformpb" |
| 24 | + "github.com/googleapis/gax-go" |
| 25 | + |
| 26 | + "google.golang.org/protobuf/types/known/structpb" |
| 27 | +) |
| 28 | + |
| 29 | +type ClientInterface interface { |
| 30 | + Close() error |
| 31 | + Predict(ctx context.Context, req *aiplatformpb.PredictRequest, opts ...gax.CallOption) (*aiplatformpb.PredictResponse, error) |
| 32 | +} |
| 33 | + |
| 34 | +// predictGPU demopnstrates how to run interference on a Gemma2 model deployed to a Vertex AI endpoint with GPU accellerators. |
| 35 | +func predictGPU(w io.Writer, client ClientInterface, projectID, location, endpointID string) error { |
| 36 | + ctx := context.Background() |
| 37 | + |
| 38 | + // Note: client can be initialised in the following way: |
| 39 | + // apiEndpoint := fmt.Sprintf("%s-aiplatform.googleapis.com:443", location) |
| 40 | + // client, err := aiplatform.NewPredictionClient(ctx, option.WithEndpoint(apiEndpoint)) |
| 41 | + // if err != nil { |
| 42 | + // return fmt.Errorf("unable to create prediction client: %v", err) |
| 43 | + // } |
| 44 | + // defer client.Close() |
| 45 | + |
| 46 | + gemma2Endpoint := fmt.Sprintf("projects/%s/locations/%s/endpoints/%s", projectID, location, endpointID) |
| 47 | + prompt := "Why is the sky blue?" |
| 48 | + parameters := map[string]interface{}{ |
| 49 | + "temperature": 0.9, |
| 50 | + "maxOutputTokens": 1024, |
| 51 | + "topP": 1.0, |
| 52 | + "topK": 1, |
| 53 | + } |
| 54 | + |
| 55 | + // Encapsulate the prompt in a correct format for TPUs. |
| 56 | + // Pay attention that prompt should be set in "inputs" field. |
| 57 | + promptValue, err := structpb.NewValue(map[string]interface{}{ |
| 58 | + "inputs": prompt, |
| 59 | + "parameters": parameters, |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + fmt.Fprintf(w, "unable to convert prompt to Value: %v", err) |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + req := &aiplatformpb.PredictRequest{ |
| 67 | + Endpoint: gemma2Endpoint, |
| 68 | + Instances: []*structpb.Value{promptValue}, |
| 69 | + } |
| 70 | + |
| 71 | + resp, err := client.Predict(ctx, req) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + prediction := resp.GetPredictions() |
| 77 | + value := prediction[0].GetStringValue() |
| 78 | + fmt.Fprintf(w, "%v", value) |
| 79 | + |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +// [END generativeaionvertexai_gemma2_predict_gpu] |
0 commit comments