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
|
// 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.
#ifndef COMPONENTS_OPTIMIZATION_GUIDE_CORE_MODEL_EXECUTION_SAFETY_CONFIG_H_
#define COMPONENTS_OPTIMIZATION_GUIDE_CORE_MODEL_EXECUTION_SAFETY_CONFIG_H_
#include <cstddef>
#include <optional>
#include <string>
#include "components/optimization_guide/core/model_execution/substitution.h"
#include "components/optimization_guide/core/optimization_guide_enums.h"
#include "components/optimization_guide/proto/text_safety_model_metadata.pb.h"
#include "services/on_device_model/public/mojom/on_device_model.mojom.h"
namespace optimization_guide {
class SafetyConfig final {
public:
SafetyConfig();
explicit SafetyConfig(proto::FeatureTextSafetyConfiguration);
SafetyConfig(const SafetyConfig&);
SafetyConfig(SafetyConfig&&);
SafetyConfig& operator=(SafetyConfig&&);
~SafetyConfig();
// Returns true if partial output is ready to be evaluated.
bool CanCheckPartialOutput(uint32_t num_output_tokens,
uint32_t num_unchecked_output_tokens) const;
// The number of request safety checks to perform.
int NumRequestChecks() const;
// Constructs input for a request safety check.
// `check_idx` must be < `NumResponseChecks()`.
std::optional<SubstitutionResult> GetRequestCheckInput(
int check_idx,
MultimodalMessageReadView request_metadata) const;
// Whether this check is only for allowed languages.
bool IsRequestCheckLanguageOnly(int check_idx) const;
// Evaluates scores for a request safety check.
// `check_idx` must be < `NumResponseChecks()`.
bool IsRequestUnsafe(
int check_idx,
const on_device_model::mojom::SafetyInfoPtr& safety_info) const;
// Evaluates language requirements of a request safety check.
// `check_idx` must be < `NumResponseChecks()`.
bool IsRequestUnsupportedLanguage(
int check_idx,
const on_device_model::mojom::SafetyInfoPtr& safety_info) const;
// Whether this config has a special raw output check.
bool HasRawOutputCheck() const;
// Get the input for the raw output check.
std::optional<SubstitutionResult> GetRawOutputCheckInput(
const std::string&) const;
// Evaluates scores of a raw output unsafe.
bool IsRawOutputUnsafe(
const on_device_model::mojom::SafetyInfoPtr& safety_info) const;
// Evaluates language requirements of the raw output check.
bool IsRawOutputUnsupportedLanguage(
ResponseCompleteness completeness,
const on_device_model::mojom::SafetyInfoPtr& safety_info) const;
// The number of request safety checks to perform.
int NumResponseChecks() const;
std::optional<SubstitutionResult> GetResponseCheckInput(
int check_idx,
MultimodalMessageReadView request,
MultimodalMessageReadView response) const;
// Evaluates scores for a response safety check.
// `check_idx` must be < `NumResponseChecks()`.
bool IsResponseUnsafe(
int check_idx,
const on_device_model::mojom::SafetyInfoPtr& safety_info) const;
// Evaluates language requirements for a response safety check.
// `check_idx` must be < `NumResponseChecks()`.
bool IsResponseUnsupportedLanguage(
int check_idx,
ResponseCompleteness completeness,
const on_device_model::mojom::SafetyInfoPtr& safety_info) const;
// Whether this config waits until a unsafe response is complete before
// canceling.
bool OnlyCancelUnsafeResponseOnComplete() const;
const proto::FeatureTextSafetyConfiguration& proto() const { return proto_; }
private:
// Whether the text is in a language not supported by the safety classifier,
// or the language could not be detected despite the classifier requiring one
// or more specific languages.
bool IsTextInUnsupportedOrUndeterminedLanguage(
const on_device_model::mojom::SafetyInfoPtr& safety_info,
double threshold) const;
proto::FeatureTextSafetyConfiguration proto_;
};
} // namespace optimization_guide
#endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_MODEL_EXECUTION_SAFETY_CONFIG_H_
|