1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/manta/mahi_provider.h"
#include <memory>
#include <string>
#include <vector>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/values.h"
#include "base/version_info/channel.h"
#include "components/endpoint_fetcher/endpoint_fetcher.h"
#include "components/manta/base_provider.h"
#include "components/manta/features.h"
#include "components/manta/manta_service_callbacks.h"
#include "components/manta/manta_status.h"
#include "components/manta/proto/manta.pb.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace manta {
namespace {
constexpr char kOauthConsumerName[] = "manta_mahi";
constexpr base::TimeDelta kTimeout = base::Seconds(30);
const net::NetworkTrafficAnnotationTag kMahiTrafficAnnotationTag =
net::DefineNetworkTrafficAnnotation("help_me_read_request",
R"(
semantics {
sender: "Help Me Read"
description:
"ChromeOS can help you read articles on webpages or PDF files by "
"summarizing or answering questions by sending the content, along "
"with user input question if provided, to Google's servers. Google "
"returns summary or answers that will be displayed on the screen."
trigger: "User right clicks within a distillable surface, e.g. a "
"webpage or a PDF file opened in the Gallery app with "
"enough extractable text content, clicks 'Summarize' button "
"or asks a question then clicks 'Send' icon."
internal {
contacts {
email: "cros-manta-team@google.com"
}
}
user_data {
type: ACCESS_TOKEN
type: USER_CONTENT
type: WEB_CONTENT
}
data: "The text content of the webpage or the PDF file where the "
"'Help Me Read' feature is triggered, along with user input "
"question if provided."
destination: GOOGLE_OWNED_SERVICE
last_reviewed: "2024-08-24"
}
policy {
cookies_allowed: NO
setting:
"You can enable or disable this feature via 'Help me read' in "
"ChromeOS's settings under 'System preferences > Search engine > "
"Use Google AI to get help reading and writing'."
chrome_policy {
HelpMeReadSettings {
HelpMeReadSettings: 2
}
}
})");
constexpr char kUsedHistogram[] = "ChromeOS.Mahi.Used";
// Each feature within Mahi, logged with `kUsedHistogram`.
//
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class MahiFeature {
kSummary = 0,
kQA = 1,
kElucidation = 2,
kMaxValue = kElucidation,
};
void OnServerResponseOrErrorReceived(
MantaGenericCallback callback,
std::unique_ptr<proto::Response> manta_response,
MantaStatus manta_status) {
if (manta_status.status_code != MantaStatusCode::kOk) {
CHECK(manta_response == nullptr);
std::move(callback).Run(base::Value::Dict(), std::move(manta_status));
return;
}
CHECK(manta_response != nullptr);
if (manta_response->output_data_size() < 1 ||
!manta_response->output_data(0).has_text()) {
std::string message = std::string();
// Tries to find more information from filtered_data
if (manta_response->filtered_data_size() > 0 &&
manta_response->filtered_data(0).is_output_data()) {
message = base::StringPrintf(
"filtered output for: %s",
proto::FilteredReason_Name(manta_response->filtered_data(0).reason())
.c_str());
}
std::move(callback).Run(base::Value::Dict(),
{MantaStatusCode::kBlockedOutputs, message});
return;
}
std::move(callback).Run(
base::Value::Dict().Set("outputData",
std::move(manta_response->output_data(0).text())),
std::move(manta_status));
}
} // namespace
MahiProvider::MahiProvider(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
signin::IdentityManager* identity_manager,
const ProviderParams& provider_params)
: BaseProvider(url_loader_factory, identity_manager, provider_params) {}
MahiProvider::MahiProvider(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
signin::IdentityManager* identity_manager)
: BaseProvider(url_loader_factory, identity_manager) {}
MahiProvider::~MahiProvider() = default;
void MahiProvider::Summarize(const std::string& input,
const std::string& title,
const std::optional<std::string>& context,
const std::optional<std::string>& url,
MantaGenericCallback done_callback) {
proto::Request request;
request.set_feature_name(proto::FeatureName::CHROMEOS_READER_SUMMARY);
auto* input_data = request.add_input_data();
input_data->set_tag("model_input");
input_data->set_text(input);
if (!title.empty()) {
input_data = request.add_input_data();
input_data->set_tag("title");
input_data->set_text(title);
}
if (context.has_value() && !context->empty()) {
input_data = request.add_input_data();
input_data->set_tag("context");
input_data->set_text(context.value());
}
if (url.has_value() && !url->empty()) {
input_data = request.add_input_data();
input_data->set_tag("url");
input_data->set_text(url.value());
}
RequestInternal(
GURL{GetProviderEndpoint(features::IsMahiUseProdServerEnabled())},
kOauthConsumerName, kMahiTrafficAnnotationTag, request,
MantaMetricType::kMahiSummary,
base::BindOnce(&OnServerResponseOrErrorReceived,
std::move(done_callback)),
kTimeout);
base::UmaHistogramEnumeration(kUsedHistogram, MahiFeature::kSummary);
}
void MahiProvider::Elucidate(const std::string& input,
const std::string& context,
const std::string& title,
const std::optional<std::string>& url,
MantaGenericCallback done_callback) {
proto::Request request;
request.set_feature_name(proto::FeatureName::CHROMEOS_READER_ELUCIDATION);
auto* input_data = request.add_input_data();
input_data->set_tag("model_input");
input_data->set_text(input);
input_data = request.add_input_data();
input_data->set_tag("context");
input_data->set_text(context);
if (!title.empty()) {
input_data = request.add_input_data();
input_data->set_tag("title");
input_data->set_text(title);
}
if (url.has_value() && !url->empty()) {
input_data = request.add_input_data();
input_data->set_tag("url");
input_data->set_text(url.value());
}
RequestInternal(
GURL{GetProviderEndpoint(features::IsMahiUseProdServerEnabled())},
kOauthConsumerName, kMahiTrafficAnnotationTag, request,
MantaMetricType::kMahiElucidation,
base::BindOnce(&OnServerResponseOrErrorReceived,
std::move(done_callback)),
kTimeout);
base::UmaHistogramEnumeration(kUsedHistogram, MahiFeature::kElucidation);
}
void MahiProvider::Outline(const std::string& input,
const std::string& title,
const std::optional<std::string>& url,
MantaGenericCallback done_callback) {
std::move(done_callback)
.Run(base::Value::Dict(),
{MantaStatusCode::kGenericError, "Unimplemented"});
}
void MahiProvider::QuestionAndAnswer(const std::string& original_content,
const std::string& title,
const std::optional<std::string>& url,
const std::vector<MahiQAPair> QAHistory,
const std::string& question,
MantaGenericCallback done_callback) {
proto::Request request;
request.set_feature_name(proto::FeatureName::CHROMEOS_READER_Q_AND_A);
auto* input_data = request.add_input_data();
input_data->set_tag("original_content");
input_data->set_text(original_content);
if (!title.empty()) {
input_data = request.add_input_data();
input_data->set_tag("title");
input_data->set_text(title);
}
if (url.has_value() && !url->empty()) {
input_data = request.add_input_data();
input_data->set_tag("url");
input_data->set_text(url.value());
}
input_data = request.add_input_data();
input_data->set_tag("new_question");
input_data->set_text(question);
for (const auto& [previous_question, previous_answer] : QAHistory) {
input_data = request.add_input_data();
input_data->set_tag("previous_question");
input_data->set_text(previous_question);
input_data = request.add_input_data();
input_data->set_tag("previous_answer");
input_data->set_text(previous_answer);
}
RequestInternal(
GURL{GetProviderEndpoint(features::IsMahiUseProdServerEnabled())},
kOauthConsumerName, kMahiTrafficAnnotationTag, request,
MantaMetricType::kMahiQA,
base::BindOnce(&OnServerResponseOrErrorReceived,
std::move(done_callback)),
kTimeout);
base::UmaHistogramEnumeration(kUsedHistogram, MahiFeature::kQA);
}
} // namespace manta
|