88 lines
2.9 KiB
Go
88 lines
2.9 KiB
Go
// policy judges each action against the organisation's plain-language policy.
|
|
// A System One judge reads the rules and the action and answers with a level
|
|
// and the rule it rests on. The guard is advisory, its verdicts recorded and no
|
|
// ceiling set, until the eval result shipped in this package shows the judge
|
|
// model passing its threshold.
|
|
//
|
|
// Settings: policy is the rules, one per line. judge_api selects systemone (the
|
|
// default) or openai-decisions. judge_base_url and judge_model name the
|
|
// endpoint and model; systemone requires both, and openai-decisions defaults
|
|
// them to the OpenAI API and gpt-6-luna. judge_min_confidence is the confidence
|
|
// below which a violation is reported as unclear (default 0.7) and
|
|
// eval_max_age_days is how long a passing eval stays trusted (default 90).
|
|
// Secret: judge_api_key, optional.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archipelago/guards/policy/internal/judge"
|
|
"github.com/samcharles93/archipelago/sdk/guard"
|
|
"github.com/samcharles93/archipelago/sdk/judgegate"
|
|
)
|
|
|
|
//go:embed eval_result.json
|
|
var recordedEval []byte
|
|
|
|
type policy struct {
|
|
judge *judge.Judge
|
|
// standing is the judge's recorded eval standing; the guard is advisory
|
|
// unless it is trusted.
|
|
standing judgegate.Standing
|
|
}
|
|
|
|
func build(cfg guard.Config) (guard.Guard, error) {
|
|
return newPolicy(cfg, recordedEval, time.Now)
|
|
}
|
|
|
|
func newPolicy(cfg guard.Config, evalJSON []byte, now func() time.Time) (*policy, error) {
|
|
rules := judge.ParseRules(cfg.Settings["policy"])
|
|
if len(rules) == 0 {
|
|
return nil, errors.New("policy: write at least one rule")
|
|
}
|
|
client, ok, err := judgegate.NewClient(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !ok {
|
|
return nil, errors.New("judge_base_url and judge_model are required")
|
|
}
|
|
minConf, err := judgegate.PositiveNumber(cfg.Settings["judge_min_confidence"], judge.DefaultMinConfidence)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("judge_min_confidence: %w", err)
|
|
}
|
|
maxAge, err := judgegate.MaxAge(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
j, err := judge.New(client, rules, minConf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &policy{judge: j, standing: judgegate.StandingOf(evalJSON, client.ModelID(), now(), maxAge)}, nil
|
|
}
|
|
|
|
// Describe is advisory until the recorded eval passes: the whole guard is a
|
|
// model's opinion, so there is nothing else to count.
|
|
func (p *policy) Describe() guard.Description {
|
|
return guard.Description{Name: "policy", Version: "1.0.0", Advisory: !p.standing.Trusted}
|
|
}
|
|
|
|
func (p *policy) Judge(ctx context.Context, a guard.Action) (guard.Verdict, error) {
|
|
out, err := p.judge.Assess(ctx, judgegate.ActionOf(a))
|
|
if err != nil {
|
|
return judgegate.FailedVerdict(out.Call.Failed(err, p.standing)), nil
|
|
}
|
|
v := out.Verdict(p.standing)
|
|
if !p.standing.Trusted {
|
|
v.BlindSpots = append(v.BlindSpots, "advisory, so it sets no ceiling: "+p.standing.Reason)
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
func main() { guard.Serve(build) }
|