FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

doc(generativelanguage): add samples by scotthart · Pull Request #14701 · googleapis/google-cloud-cpp · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .bazel  (1) .cc  (1) .sh  (1) .txt  (2) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions ci/cloudbuild/builds/integration-production.sh
This file contains hidden or 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
Expand Up @@ -32,6 +32,8 @@ io::run bazel test "${args[@]}" --test_tag_filters=-integration-test "${BAZEL_TA
excluded_rules=(
"-//examples:grpc_credential_types"
"-//google/cloud/bigtable/examples:bigtable_grpc_credentials"
# TODO(#14702): Enable this when auth scope configuration is resolved.
"-//google/cloud/generativelanguage/samples:samples"
# This sample uses HMAC keys, which are very limited in production (at most
# 5 per service account). Disabled for now.
"-//google/cloud/storage/examples:storage_service_account_samples"
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/generativelanguage/CMakeLists.txt
This file contains hidden or 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
Expand Up @@ -33,3 +33,5 @@ if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
PROPERTIES LABELS "integration-test;quickstart" PASS_REGULAR_EXPRESSION
"Request had insufficient authentication scopes")
endif ()

add_subdirectory(samples)
29 changes: 29 additions & 0 deletions google/cloud/generativelanguage/samples/BUILD.bazel
This file contains hidden or 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,29 @@
# 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
#
# https://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(default_visibility = ["//visibility:private"])

licenses(["notice"]) # Apache 2.0

[cc_test(
name = sample.replace(".cc", ""),
srcs = [sample],
tags = [
"integration-test",
],
deps = [
"//:generativelanguage",
"//google/cloud/testing_util:google_cloud_cpp_testing_private",
],
) for sample in glob(["*.cc"])]
This file contains hidden or 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,19 @@
# 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
#
# https://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.

if ((NOT BUILD_TESTING) OR (NOT GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS))
return()
endif ()

google_cloud_cpp_add_samples(generativelanguage)
82 changes: 82 additions & 0 deletions google/cloud/generativelanguage/samples/samples.cc
This file contains hidden or 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,82 @@
// 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
//
// https://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.

#include "google/cloud/generativelanguage/v1/generative_client.h"
#include "google/cloud/common_options.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/example_driver.h"
#include <string>
#include <vector>

namespace {

void GeminiTextGenTextOnlyPrompt(std::vector<std::string> const& argv) {
if (argv.size() < 3) {
throw google::cloud::testing_util::Usage(
"gemini-text-gen-text-only-prompt <quota-project-id> <model-name> "
"[<content>]+");
}
// [START text_gen_text_only_prompt]
namespace gemini = ::google::cloud::generativelanguage_v1;
namespace gemini_proto = ::google::ai::generativelanguage::v1;
[](std::string const& quota_project_id, std::string const& model,
std::vector<std::string> const& content) {
auto options =
google::cloud::Options{}.set<google::cloud::UserProjectOption>(
quota_project_id);
gemini::GenerativeServiceClient client(
gemini::MakeGenerativeServiceConnection(options));
std::vector<gemini_proto::Content> contents;
for (auto const& c : content) {
gemini_proto::Content content;
content.add_parts()->set_text(c);
contents.push_back(std::move(content));
}

auto response = client.GenerateContent(model, contents);
if (!response) throw std::move(response).status();

for (auto const& candidate : response->candidates()) {
for (auto const& p : candidate.content().parts()) {
std::cout << p.text() << "\n";
}
}
}
// [END text_gen_text_only_prompt]
(argv.at(0), argv.at(1), {argv.begin() + 2, argv.end()});
}

void AutoRun(std::vector<std::string> const& argv) {
namespace examples = ::google::cloud::testing_util;
if (!argv.empty()) throw examples::Usage{"auto"};
examples::CheckEnvironmentVariablesAreSet({
"GOOGLE_CLOUD_PROJECT",
});
auto const project_id =
google::cloud::internal::GetEnv("GOOGLE_CLOUD_PROJECT").value();

GeminiTextGenTextOnlyPrompt({project_id, "models/gemini-1.5-flash",
"Write a story about a magic backpack."});

std::cout << "\nAutoRun done" << std::endl;
}

} // namespace

int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
google::cloud::testing_util::Example example(
{{"gemini-text-gen-text-only-prompt", GeminiTextGenTextOnlyPrompt},
{"auto", AutoRun}});
return example.Run(argc, argv);
}

Back | FazBrowse Home | New Git URL