Skip to content

Commit

Permalink
sample(ai_platform): Add Vertex AI text prompt sample (#23499)
Browse files Browse the repository at this point in the history
  • Loading branch information
holtskinner authored Nov 6, 2023
1 parent 313de05 commit 4338b83
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 2 deletions.
27 changes: 27 additions & 0 deletions google-cloud-ai_platform/samples/Gemfile
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
23 changes: 23 additions & 0 deletions google-cloud-ai_platform/samples/Rakefile
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
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
73 changes: 73 additions & 0 deletions google-cloud-ai_platform/samples/predict_text_prompt.rb
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]
47 changes: 47 additions & 0 deletions google-cloud-ai_platform/test/README.md
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)
6 changes: 4 additions & 2 deletions google-cloud-document_ai/samples/quickstart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

# [START documentai_quickstart]
require "google/cloud/document_ai"
require "google/cloud/document_ai/v1"

##
# Document AI quickstart
Expand All @@ -26,7 +26,9 @@
#
def quickstart project_id:, location_id:, processor_id:, file_path:, mime_type:
# Create the Document AI client.
client = Google::Cloud::DocumentAI.document_processor_service
client = ::Google::Cloud::DocumentAI::V1::DocumentProcessorService::Client.new do |config|
config.endpoint = "#{location_id}-documentai.googleapis.com"
end

# Build the resource name from the project.
name = client.processor_path(
Expand Down

0 comments on commit 4338b83

Please sign in to comment.