Skip to content

Commit 7b8a908

Browse files
authored
doc(generativelanguage): add samples (#14701)
1 parent 9b51e16 commit 7b8a908

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

ci/cloudbuild/builds/integration-production.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ io::run bazel test "${args[@]}" --test_tag_filters=-integration-test "${BAZEL_TA
3232
excluded_rules=(
3333
"-//examples:grpc_credential_types"
3434
"-//google/cloud/bigtable/examples:bigtable_grpc_credentials"
35+
# TODO(#14702): Enable this when auth scope configuration is resolved.
36+
"-//google/cloud/generativelanguage/samples:samples"
3537
# This sample uses HMAC keys, which are very limited in production (at most
3638
# 5 per service account). Disabled for now.
3739
"-//google/cloud/storage/examples:storage_service_account_samples"

google/cloud/generativelanguage/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ if (BUILD_TESTING AND GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
3333
PROPERTIES LABELS "integration-test;quickstart" PASS_REGULAR_EXPRESSION
3434
"Request had insufficient authentication scopes")
3535
endif ()
36+
37+
add_subdirectory(samples)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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(default_visibility = ["//visibility:private"])
16+
17+
licenses(["notice"]) # Apache 2.0
18+
19+
[cc_test(
20+
name = sample.replace(".cc", ""),
21+
srcs = [sample],
22+
tags = [
23+
"integration-test",
24+
],
25+
deps = [
26+
"//:generativelanguage",
27+
"//google/cloud/testing_util:google_cloud_cpp_testing_private",
28+
],
29+
) for sample in glob(["*.cc"])]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
# use this file except in compliance with the License. You may obtain a copy of
5+
# 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, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations under
13+
# the License.
14+
15+
if ((NOT BUILD_TESTING) OR (NOT GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS))
16+
return()
17+
endif ()
18+
19+
google_cloud_cpp_add_samples(generativelanguage)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
#include "google/cloud/generativelanguage/v1/generative_client.h"
16+
#include "google/cloud/common_options.h"
17+
#include "google/cloud/internal/getenv.h"
18+
#include "google/cloud/testing_util/example_driver.h"
19+
#include <string>
20+
#include <vector>
21+
22+
namespace {
23+
24+
void GeminiTextGenTextOnlyPrompt(std::vector<std::string> const& argv) {
25+
if (argv.size() < 3) {
26+
throw google::cloud::testing_util::Usage(
27+
"gemini-text-gen-text-only-prompt <quota-project-id> <model-name> "
28+
"[<content>]+");
29+
}
30+
// [START text_gen_text_only_prompt]
31+
namespace gemini = ::google::cloud::generativelanguage_v1;
32+
namespace gemini_proto = ::google::ai::generativelanguage::v1;
33+
[](std::string const& quota_project_id, std::string const& model,
34+
std::vector<std::string> const& content) {
35+
auto options =
36+
google::cloud::Options{}.set<google::cloud::UserProjectOption>(
37+
quota_project_id);
38+
gemini::GenerativeServiceClient client(
39+
gemini::MakeGenerativeServiceConnection(options));
40+
std::vector<gemini_proto::Content> contents;
41+
for (auto const& c : content) {
42+
gemini_proto::Content content;
43+
content.add_parts()->set_text(c);
44+
contents.push_back(std::move(content));
45+
}
46+
47+
auto response = client.GenerateContent(model, contents);
48+
if (!response) throw std::move(response).status();
49+
50+
for (auto const& candidate : response->candidates()) {
51+
for (auto const& p : candidate.content().parts()) {
52+
std::cout << p.text() << "\n";
53+
}
54+
}
55+
}
56+
// [END text_gen_text_only_prompt]
57+
(argv.at(0), argv.at(1), {argv.begin() + 2, argv.end()});
58+
}
59+
60+
void AutoRun(std::vector<std::string> const& argv) {
61+
namespace examples = ::google::cloud::testing_util;
62+
if (!argv.empty()) throw examples::Usage{"auto"};
63+
examples::CheckEnvironmentVariablesAreSet({
64+
"GOOGLE_CLOUD_PROJECT",
65+
});
66+
auto const project_id =
67+
google::cloud::internal::GetEnv("GOOGLE_CLOUD_PROJECT").value();
68+
69+
GeminiTextGenTextOnlyPrompt({project_id, "models/gemini-1.5-flash",
70+
"Write a story about a magic backpack."});
71+
72+
std::cout << "\nAutoRun done" << std::endl;
73+
}
74+
75+
} // namespace
76+
77+
int main(int argc, char* argv[]) { // NOLINT(bugprone-exception-escape)
78+
google::cloud::testing_util::Example example(
79+
{{"gemini-text-gen-text-only-prompt", GeminiTextGenTextOnlyPrompt},
80+
{"auto", AutoRun}});
81+
return example.Run(argc, argv);
82+
}

0 commit comments

Comments
 (0)