-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sample(ai_platform): Add Vertex AI text prompt sample (#23499)
- Loading branch information
1 parent
313de05
commit 4338b83
Showing
6 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright 2023 Google, Inc | ||
# | ||
# 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. | ||
|
||
source "https://rubygems.org" | ||
|
||
master = ENV["GOOGLE_CLOUD_SAMPLES_TEST"] == "master" | ||
|
||
gem "google-cloud-ai_platform", path: master ? "../../google-cloud-ai_platform" : nil | ||
|
||
group :test do | ||
gem "google-style", "~> 1.26.2" | ||
gem "minitest", "~> 5.17" | ||
gem "minitest-focus", "~> 1.4" | ||
gem "minitest-rg", "~> 5.2" | ||
gem "rake" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2023 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. | ||
|
||
require "rake/testtask" | ||
require "rubocop/rake_task" | ||
|
||
Rake::TestTask.new "test" do |t| | ||
t.test_files = FileList["acceptance/*_test.rb"] | ||
t.warning = false | ||
end | ||
|
||
RuboCop::RakeTask.new |
36 changes: 36 additions & 0 deletions
36
google-cloud-ai_platform/samples/acceptance/predict_text_prompt_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2023 Google, Inc | ||
# | ||
# 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. | ||
|
||
require "google/cloud/ai_platform" | ||
|
||
require "minitest/autorun" | ||
require "minitest/focus" | ||
require "minitest/rg" | ||
|
||
require_relative "../../../.toys/.lib/sample_loader" | ||
|
||
describe "Vertex AI Quickstart" do | ||
let(:project_id) { ENV["GOOGLE_CLOUD_PROJECT"] || raise("missing GOOGLE_CLOUD_PROJECT") } | ||
let(:location_id) { "us-central1" } | ||
let(:publisher) { "google" } | ||
let(:model) { "text-bison@001" } | ||
|
||
it "generates text" do | ||
sample = SampleLoader.load "predict_text_prompt.rb" | ||
|
||
assert_output(/\S/) do | ||
sample.run project_id: project_id, location_id: location_id, publisher: publisher, model: model | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright 2023 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. | ||
|
||
# [START aiplatform_sdk_ideation] | ||
require "google/cloud/ai_platform/v1" | ||
|
||
## | ||
# Vertex AI Predict Text Prompt | ||
# | ||
# @param project_id [String] Your Google Cloud project (e.g. "my-project") | ||
# @param location_id [String] Your Processor Location (e.g. "us-central1") | ||
# @param publisher [String] The Model Publisher (e.g. "google") | ||
# @param model [String] The Model Identifier (e.g. "text-bison@001") | ||
# | ||
def predict_text_prompt project_id:, location_id:, publisher:, model: | ||
# Create the Vertex AI client. | ||
client = ::Google::Cloud::AIPlatform::V1::PredictionService::Client.new do |config| | ||
config.endpoint = "#{location_id}-aiplatform.googleapis.com" | ||
end | ||
|
||
# Build the resource name from the project. | ||
endpoint = client.endpoint_path( | ||
project: project_id, | ||
location: location_id, | ||
publisher: publisher, | ||
model: model | ||
) | ||
|
||
prompt = "Give me ten interview questions for the role of program manager." | ||
|
||
# Initialize the request arguments | ||
instance = Google::Protobuf::Value.new( | ||
struct_value: Google::Protobuf::Struct.new( | ||
fields: { | ||
"prompt" => Google::Protobuf::Value.new( | ||
string_value: prompt | ||
) | ||
} | ||
) | ||
) | ||
|
||
instances = [instance] | ||
|
||
parameters = Google::Protobuf::Value.new( | ||
struct_value: Google::Protobuf::Struct.new( | ||
fields: { | ||
"temperature" => Google::Protobuf::Value.new(number_value: 0.2), | ||
"maxOutputTokens" => Google::Protobuf::Value.new(number_value: 256), | ||
"topP" => Google::Protobuf::Value.new(number_value: 0.95), | ||
"topK" => Google::Protobuf::Value.new(number_value: 40) | ||
} | ||
) | ||
) | ||
|
||
# Make the prediction request | ||
response = client.predict endpoint: endpoint, instances: instances, parameters: parameters | ||
|
||
# Handle the prediction response | ||
puts "Predict Response" | ||
puts response | ||
end | ||
# [END aiplatform_sdk_ideation] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Google Vertex AI API Samples | ||
|
||
Vertex AI offers everything you need to build and use generative AI—from AI solutions, to Search and Conversation, to 100+ foundation models, to a unified AI platform. | ||
|
||
## Description | ||
|
||
These samples show how to use the [Google Vertex AI API](https://cloud.google.com/vertex-ai/). | ||
|
||
## Build and Run | ||
1. **Enable APIs** - [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com) | ||
and create a new project or select an existing project. | ||
|
||
1. **Install and Initialize Cloud SDK** | ||
Follow instructions from the available [quickstarts](https://cloud.google.com/sdk/docs/quickstarts) | ||
|
||
1. **Clone the repo** and cd into this directory | ||
|
||
```text | ||
git clone https://github.com/googleapis/google-cloud-ruby.git | ||
$ cd google-cloud-ruby/google-cloud-ai_platform | ||
``` | ||
1. **Install Dependencies** via [Bundler](https://bundler.io). | ||
```text | ||
$ bundle install | ||
``` | ||
1. **Set Environment Variables** | ||
```text | ||
$ export GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID" | ||
``` | ||
1. **Run samples** | ||
```text | ||
bundle exec rake test | ||
``` | ||
## Contributing changes | ||
* See [CONTRIBUTING.md](../CONTRIBUTING.md) | ||
## Licensing | ||
* See [LICENSE](../LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters