Skip to content

Commit 5d12b58

Browse files
committed
internal/gaby: allow policy checks via flag for overviews in Gaby
If the -enforcepolicy flag is set (default false), check all safety categories when generating overviews. Since overviews are not yet published anywhere, this would only affect users of the web UI. For #70 Change-Id: I100070b5726ca0ff21cea4dec9f7f68e74018f08 Reviewed-on: https://go-review.googlesource.com/c/oscar/+/637978 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent a315118 commit 5d12b58

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

internal/gaby/main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"golang.org/x/oscar/internal/discussion"
3131
"golang.org/x/oscar/internal/docs"
3232
"golang.org/x/oscar/internal/embeddocs"
33+
"golang.org/x/oscar/internal/gcp/checks"
3334
"golang.org/x/oscar/internal/gcp/firestore"
3435
"golang.org/x/oscar/internal/gcp/gcphandler"
3536
"golang.org/x/oscar/internal/gcp/gcpmetrics"
@@ -57,6 +58,7 @@ type gabyFlags struct {
5758
level string
5859
overlay string
5960
requireApproval string
61+
enforcePolicy bool
6062
}
6163

6264
var flags gabyFlags
@@ -70,6 +72,7 @@ func init() {
7072
flag.StringVar(&flags.level, "level", "info", "initial log level")
7173
flag.StringVar(&flags.overlay, "overlay", "", "spec for overlay to DB; see internal/dbspec for syntax")
7274
flag.StringVar(&flags.requireApproval, "requireapproval", "", "comma-separated list of packages whose actions require approval")
75+
flag.BoolVar(&flags.enforcePolicy, "enforcepolicy", false, "whether to enforce safety policies on LLM inputs and outputs")
7376
}
7477

7578
// Gaby holds the state for gaby's execution.
@@ -91,6 +94,7 @@ type Gaby struct {
9194
docs *docs.Corpus // document corpus to use
9295
embed llm.Embedder // LLM embedder to use
9396
llm llm.ContentGenerator // LLM content generator to use
97+
policy llm.PolicyChecker // LLM checker to use
9498
llmapp *llmapp.Client // LLM client to use
9599
github *github.Client // github client to use
96100
disc *discussion.Client // github discussion client to use
@@ -167,7 +171,7 @@ func main() {
167171
}
168172
g.embed = ai
169173
g.llm = ai
170-
g.llmapp = llmapp.New(g.slog, ai, g.db)
174+
g.llmapp = llmapp.NewWithChecker(g.slog, ai, g.policy, g.db)
171175

172176
cr := crawl.New(g.slog, g.db, g.http)
173177
cr.Add("https://go.dev/")
@@ -339,6 +343,15 @@ func (g *Gaby) initGCP() (shutdown func()) {
339343
}
340344
g.secret = sdb
341345

346+
if flags.enforcePolicy {
347+
llmchecker, err := checks.New(g.ctx, g.slog, flags.project)
348+
if err != nil {
349+
log.Fatal(err)
350+
}
351+
llmchecker.SetPolicies(llm.AllPolicyTypes())
352+
g.policy = llmchecker
353+
}
354+
342355
// Initialize error reporting if we are running on Cloud Run.
343356
if g.cloud {
344357
rep, err := errorreporting.NewClient(g.ctx, flags.project, errorreporting.Config{

internal/llmapp/check.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
//
1919
// When any of the Overview functions are called, the prompts and outputs of the LLM
2020
// will be checked for safety violations.
21+
//
22+
// If the checker is nil, [NewWithChecker] is identical to [New].
2123
func NewWithChecker(lg *slog.Logger, g llm.ContentGenerator, checker llm.PolicyChecker, db storage.DB) *Client {
2224
return &Client{slog: lg, g: g, checker: checker, db: db}
2325
}

0 commit comments

Comments
 (0)