Skip to content

Commit 4c3c039

Browse files
committed
docs(samples): Add Vertex AI Text Prompt Sample
1 parent 1e8d1b5 commit 4c3c039

5 files changed

Lines changed: 207 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2023 Google, Inc
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+
# http://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+
source "https://rubygems.org"
16+
17+
master = ENV["GOOGLE_CLOUD_SAMPLES_TEST"] == "master"
18+
19+
gem "google-cloud-ai_platform", path: master ? "../../google-cloud-ai_platform" : nil
20+
21+
group :test do
22+
gem "google-style", "~> 1.26.2"
23+
gem "minitest", "~> 5.17"
24+
gem "minitest-focus", "~> 1.4"
25+
gem "minitest-rg", "~> 5.2"
26+
gem "rake"
27+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2023 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+
# http://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+
require "rake/testtask"
16+
require "rubocop/rake_task"
17+
18+
Rake::TestTask.new "test" do |t|
19+
t.test_files = FileList["acceptance/*_test.rb"]
20+
t.warning = false
21+
end
22+
23+
RuboCop::RakeTask.new
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2023 Google, Inc
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+
# http://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+
require "google/cloud/ai_platform"
16+
17+
require "minitest/autorun"
18+
require "minitest/focus"
19+
require "minitest/rg"
20+
21+
require_relative "../../../.toys/.lib/sample_loader"
22+
23+
describe "Vertex AI Quickstart" do
24+
let(:project_id) { ENV["GOOGLE_CLOUD_PROJECT"] || raise("missing GOOGLE_CLOUD_PROJECT") }
25+
let(:location_id) { "us-central1" }
26+
let(:publisher) { "google" }
27+
let(:model) { "text-bison@001" }
28+
29+
it "generates text" do
30+
sample = SampleLoader.load "quickstart.rb"
31+
32+
assert_output(/\S/) do
33+
sample.run project_id: project_id, location_id: location_id, publisher: publisher, model: model
34+
end
35+
end
36+
end
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2023 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+
# http://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+
# [START aiplatform_sdk_ideation]
16+
require "google/cloud/ai_platform"
17+
18+
##
19+
# Vertex AI Predict Text Prompt
20+
#
21+
# @param project_id [String] Your Google Cloud project (e.g. "my-project")
22+
# @param location_id [String] Your Processor Location (e.g. "us-central1")
23+
# @param publisher [String] The Model Publisher (e.g. "google")
24+
# @param model [String] The Model Identifier (e.g. "text-bison@001")
25+
#
26+
def predict_text_prompt project_id:, location_id:, publisher:, model:
27+
# Create the Vertex AI client.
28+
client = ::Google::Cloud::AIPlatform::V1::PredictionService::Client.new do |config|
29+
config.endpoint = "#{location_id}-aiplatform.googleapis.com"
30+
end
31+
32+
# Build the resource name from the project.
33+
endpoint = client.endpoint_path(
34+
project: project_id,
35+
location: location_id,
36+
publisher: publisher,
37+
model: model
38+
)
39+
40+
prompt = "Give me ten interview questions for the role of program manager."
41+
42+
# Initialize the request arguments
43+
instance = Google::Protobuf::Value.new(
44+
struct_value: Google::Protobuf::Struct.new(
45+
fields: {
46+
"prompt" => Google::Protobuf::Value.new(
47+
string_value: prompt
48+
)
49+
}
50+
)
51+
)
52+
53+
instances = [instance]
54+
55+
parameters = Google::Protobuf::Value.new(
56+
struct_value: Google::Protobuf::Struct.new(
57+
fields: {
58+
"temperature" => Google::Protobuf::Value.new(number_value: 0.2),
59+
"maxOutputTokens" => Google::Protobuf::Value.new(number_value: 256),
60+
"topP" => Google::Protobuf::Value.new(number_value: 0.95),
61+
"topK" => Google::Protobuf::Value.new(number_value: 40)
62+
}
63+
)
64+
)
65+
66+
# Make the prediction request
67+
response = client.predict(endpoint: endpoint, instances: [instances], parameters: parameters)
68+
69+
# Handle the prediction response
70+
puts "Predict Response"
71+
puts response
72+
return response
73+
end
74+
# [END aiplatform_sdk_ideation]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Google Vertex AI API Samples
2+
3+
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.
4+
5+
## Description
6+
7+
These samples show how to use the [Google Vertex AI API](https://cloud.google.com/vertex-ai/).
8+
9+
## Build and Run
10+
1. **Enable APIs** - [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com)
11+
and create a new project or select an existing project.
12+
13+
1. **Install and Initialize Cloud SDK**
14+
Follow instructions from the available [quickstarts](https://cloud.google.com/sdk/docs/quickstarts)
15+
16+
1. **Clone the repo** and cd into this directory
17+
18+
```text
19+
git clone https://github.com/googleapis/google-cloud-ruby.git
20+
$ cd google-cloud-ruby/google-cloud-ai_platform
21+
```
22+
23+
1. **Install Dependencies** via [Bundler](https://bundler.io).
24+
25+
```text
26+
$ bundle install
27+
```
28+
29+
1. **Set Environment Variables**
30+
31+
```text
32+
$ export GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"
33+
```
34+
35+
1. **Run samples**
36+
37+
```text
38+
bundle exec rake test
39+
```
40+
41+
## Contributing changes
42+
43+
* See [CONTRIBUTING.md](../CONTRIBUTING.md)
44+
45+
## Licensing
46+
47+
* See [LICENSE](../LICENSE)

0 commit comments

Comments
 (0)