|
| 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