Retain selected glossary instructions
This commit is contained in:
@@ -47,6 +47,8 @@ before editing and use the graph-first discovery instructions in `AGENTS.md`.
|
||||
|
||||
## Stage 1: Add collection instructions and retain selected context
|
||||
|
||||
Status: Complete.
|
||||
|
||||
Work:
|
||||
|
||||
1. Add `Instruction string` with YAML key `instruction` to
|
||||
|
||||
@@ -38,6 +38,7 @@ glossary_collections:
|
||||
- path: shared/extra.yml
|
||||
required: true
|
||||
party:
|
||||
instruction: Use established names.
|
||||
entries:
|
||||
- id: hero
|
||||
name: Original
|
||||
@@ -54,6 +55,9 @@ party:
|
||||
t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics())
|
||||
}
|
||||
party := result.Config.Application.GlossaryCollections["party"].Entries
|
||||
if got := result.Config.Application.GlossaryCollections["party"].Instruction; got != "Use established names." {
|
||||
t.Fatalf("instruction not inherited: %q", got)
|
||||
}
|
||||
if len(party) != 2 || party[0].Name != "Revised" || len(party[0].Aliases) != 0 || party[1].ID != "other" {
|
||||
t.Fatalf("party=%+v", party)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,8 @@ type Config struct {
|
||||
}
|
||||
|
||||
type GlossaryCollection struct {
|
||||
Entries []GlossaryEntry `yaml:"entries"`
|
||||
Instruction string `yaml:"instruction"`
|
||||
Entries []GlossaryEntry `yaml:"entries"`
|
||||
}
|
||||
type GlossaryEntry struct {
|
||||
ID string `yaml:"id"`
|
||||
@@ -106,11 +107,18 @@ func defaults() Config {
|
||||
}
|
||||
}
|
||||
|
||||
// GlossaryEntryIdentity identifies entries beneath a collections mapping.
|
||||
func GlossaryEntryIdentity(prefix ck.SequenceSelector) ck.SequenceIdentity {
|
||||
selector := append(ck.SequenceSelector(nil), prefix...)
|
||||
selector = append(selector, ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries"))
|
||||
return ck.SequenceIdentityMatching(selector, "id")
|
||||
}
|
||||
|
||||
// Definition describes the application settings independently of their sources.
|
||||
func Definition() ck.Definition[Config] {
|
||||
return ck.Definition[Config]{
|
||||
Defaults: defaults,
|
||||
SequenceIdentities: []ck.SequenceIdentity{ck.SequenceIdentityMatching(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections"), ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")}, "id")},
|
||||
SequenceIdentities: []ck.SequenceIdentity{GlossaryEntryIdentity(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections")})},
|
||||
Version: &ck.VersionRule{Path: ck.Keys("version"), Check: func(version ck.Version, v *ck.Validation) error {
|
||||
if version.Present && (version.Null || version.Text != "3") {
|
||||
v.Error(ck.Keys("version"), "use application configuration version 3; see docs/migrations/v3.md")
|
||||
|
||||
@@ -31,6 +31,11 @@ type TranscriptModule interface {
|
||||
type StructuredCompletionRequest = structuredoutput.Request
|
||||
type StructuredCompletionResponse = structuredoutput.Response
|
||||
|
||||
type GlossaryInstruction struct {
|
||||
Collection string
|
||||
Text string
|
||||
}
|
||||
|
||||
// ModuleRunSpec identifies one resolved module instance in a pipeline.
|
||||
type ModuleRunSpec struct {
|
||||
ModuleKey string `json:"module_key"`
|
||||
@@ -40,6 +45,7 @@ type ModuleRunSpec struct {
|
||||
ID *string `json:"-"`
|
||||
Settings config.Stage `json:"-"`
|
||||
Glossary []glossary.Entry `json:"-"`
|
||||
GlossaryInstructions []GlossaryInstruction `json:"-"`
|
||||
ProtectedTerms []glossary.Term `json:"-"`
|
||||
GlossaryView *validators.GlossaryView `json:"-"`
|
||||
GlossaryVariables *promptcontext.GlossaryVariables `json:"-"`
|
||||
@@ -140,10 +146,16 @@ func ResolveModuleRunSpecs(cfg *config.Config) []ModuleRunSpec {
|
||||
key := GlossarySelection(item)
|
||||
if prior, ok := selections[key]; ok {
|
||||
spec.Glossary = specs[prior].Glossary
|
||||
spec.GlossaryInstructions = specs[prior].GlossaryInstructions
|
||||
spec.ProtectedTerms = specs[prior].ProtectedTerms
|
||||
continue
|
||||
}
|
||||
selected := glossary.Select(cfg.GlossaryCollections, item.Glossaries)
|
||||
for _, name := range item.Glossaries {
|
||||
if collection, ok := cfg.GlossaryCollections[name]; ok {
|
||||
spec.GlossaryInstructions = append(spec.GlossaryInstructions, GlossaryInstruction{Collection: name, Text: collection.Instruction})
|
||||
}
|
||||
}
|
||||
protected := glossary.Select(cfg.GlossaryCollections, item.ProtectedTerms)
|
||||
var excluded []glossary.Entry
|
||||
if key.exempt {
|
||||
|
||||
@@ -92,6 +92,29 @@ func TestResolveModuleRunSpecs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveGlossaryInstructions(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
GlossaryCollections: map[string]config.GlossaryCollection{
|
||||
"one": {Instruction: "First"}, "two": {Instruction: "Second"}, "protected": {Instruction: "Protection only"},
|
||||
},
|
||||
Pipeline: config.Pipeline{Stages: []config.Stage{
|
||||
{Module: "grammar", Glossaries: []string{"two", "one"}, ProtectedTerms: []string{"protected"}},
|
||||
{Module: "grammar", Glossaries: []string{"two", "one"}, ProtectedTerms: []string{"protected"}},
|
||||
{Module: "grammar", Glossaries: []string{"one"}, ProtectedTerms: []string{"protected"}},
|
||||
}},
|
||||
}
|
||||
specs := ResolveModuleRunSpecs(cfg)
|
||||
want := []GlossaryInstruction{{Collection: "two", Text: "Second"}, {Collection: "one", Text: "First"}}
|
||||
for _, spec := range specs[:2] {
|
||||
if !reflect.DeepEqual(spec.GlossaryInstructions, want) {
|
||||
t.Fatalf("instructions = %+v, want %+v", spec.GlossaryInstructions, want)
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(specs[2].GlossaryInstructions, want[1:]) {
|
||||
t.Fatalf("specific selection = %+v", specs[2].GlossaryInstructions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSectionMetadataFromSection(t *testing.T) {
|
||||
section := chunking.Section{
|
||||
Index: 3,
|
||||
|
||||
Reference in New Issue
Block a user