Files
archipelago/guards/confidence/internal/judge/judge.go
T

163 lines
4.9 KiB
Go

// Package judge asks a System One model whether the evidence an agent read
// supports the diagnosis behind an action.
package judge
import (
"context"
"embed"
"fmt"
"strings"
"github.com/samcharles93/archipelago/sdk/guard"
"github.com/samcharles93/archipelago/sdk/judgegate"
"github.com/samcharles93/archipelago/sdk/systemone"
)
//go:embed prompts/*.json
var promptFiles embed.FS
// What the judge shows of the evidence: the latest results, each cut short.
const (
maxEvidence = 12
maxEvidenceChars = 1500
)
// The labels the support question answers with.
const (
Supported = "supported"
Partial = "partial"
Unsupported = "unsupported"
)
// Evidence is one result the agent read.
type Evidence struct {
Source string `json:"source"`
Text string `json:"text"`
}
// Case is one action with the reasoning behind it.
type Case struct {
Diagnosis string `json:"diagnosis"`
Evidence []Evidence `json:"evidence"`
Action judgegate.Action `json:"action"`
}
// CaseOf presents a guard action to the judge. ok is false when no agent
// reasoning came with it, so there is nothing to check.
func CaseOf(a guard.Action) (c Case, ok bool) {
c = Case{Diagnosis: strings.TrimSpace(a.Diagnosis), Action: judgegate.ActionOf(a)}
for _, e := range a.Evidence {
c.Evidence = append(c.Evidence, Evidence{Source: e.Source, Text: e.Text})
}
return c, c.Diagnosis != ""
}
// Judge asks about the reasoning behind actions.
type Judge struct {
client judgegate.Client
questions map[string]systemone.Question
}
// New returns a judge.
func New(client judgegate.Client) (*Judge, error) {
support, err := judgegate.LoadChoice(promptFiles, "support")
if err != nil {
return nil, err
}
gap, err := judgegate.LoadChoice(promptFiles, "gap")
if err != nil {
return nil, err
}
return &Judge{client: client, questions: map[string]systemone.Question{
"support": support.Question(), "gap": gap.Question(),
}}, nil
}
// Outcome is what the judge found about one diagnosis.
type Outcome struct {
Label string
Confidence float64
// Gap says what is missing, empty when the evidence supports the diagnosis.
Gap string
// Read is how many results the agent read.
Read int
// Called says whether a model was asked. A diagnosis with nothing read
// behind it is settled without one.
Called bool
// Call is what the judge was asked and returned, set even when it failed.
Call judgegate.Call
}
// Assess asks whether the evidence supports the diagnosis. A diagnosis with
// nothing read behind it is unsupported without asking. The judge's own
// confidence is not used to downgrade an answer: the model's confidence is low
// even when it is right, so a floor on it throws away real findings.
func (j *Judge) Assess(ctx context.Context, c Case) (Outcome, error) {
if len(c.Evidence) == 0 {
return Outcome{Label: Unsupported, Gap: "the agent read nothing before proposing this"}, nil
}
shown := c.Evidence[max(0, len(c.Evidence)-maxEvidence):]
state := map[string]any{"diagnosis": c.Diagnosis, "evidence": clip(shown), "action": c.Action}
res, err := j.client.Classify(ctx, state, j.questions)
call := judgegate.CallOf(j.client.ModelID(), "", res)
if err != nil {
return Outcome{Called: true, Call: call}, fmt.Errorf("judge: %w", err)
}
s := res.Answers["support"]
out := Outcome{Label: s.Label(), Confidence: s.Confidence, Read: len(c.Evidence), Called: true, Call: call}
if g := res.Answers["gap"].Label(); g != "" && g != "none" {
out.Gap = g
}
return out, nil
}
func clip(in []Evidence) []Evidence {
out := make([]Evidence, len(in))
for i, e := range in {
if len(e.Text) > maxEvidenceChars {
e.Text = strings.ToValidUTF8(e.Text[:maxEvidenceChars], "") + "..."
}
out[i] = e
}
return out
}
// Verdict turns what the judge found into a level and its reason. A supported
// diagnosis acts quietly, a partly supported one is reported and goes ahead,
// and an unsupported one waits for a person.
func (o Outcome) Verdict(st judgegate.Standing) guard.Verdict {
v := o.verdict()
if o.Called {
v.Judges = []guard.JudgeRecord{o.Call.Record(o.Label, o.Confidence, o.evidence(), st)}
}
return v
}
func (o Outcome) verdict() guard.Verdict {
switch o.Label {
case Unsupported:
return guard.Verdict{Ceiling: guard.ProposeAndWait, Findings: []guard.Finding{{
Text: "the evidence the agent read does not support its diagnosis", Evidence: o.evidence(), Strength: strength(o.Confidence),
}}}
case Partial:
return guard.Verdict{Ceiling: guard.ActAndTell, Findings: []guard.Finding{{
Text: "the evidence the agent read only partly supports its diagnosis", Evidence: o.evidence(), Strength: guard.Weak,
}}}
}
return guard.Verdict{Ceiling: guard.ActQuietly}
}
func (o Outcome) evidence() string {
if o.Gap == "" {
return fmt.Sprintf("%d results read", o.Read)
}
return fmt.Sprintf("%d results read; gap: %s", o.Read, o.Gap)
}
func strength(confidence float64) guard.Strength {
if confidence >= 0.9 {
return guard.Strong
}
return guard.Moderate
}