Include glossary guidance in proposal conversations
This commit is contained in:
@@ -31,10 +31,7 @@ type TranscriptModule interface {
|
||||
type StructuredCompletionRequest = structuredoutput.Request
|
||||
type StructuredCompletionResponse = structuredoutput.Response
|
||||
|
||||
type GlossaryInstruction struct {
|
||||
Collection string
|
||||
Text string
|
||||
}
|
||||
type GlossaryInstruction = promptcontext.GlossaryInstruction
|
||||
|
||||
// ModuleRunSpec identifies one resolved module instance in a pipeline.
|
||||
type ModuleRunSpec struct {
|
||||
@@ -62,6 +59,7 @@ func (s *ModuleRunSpec) PrepareGlossary() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
variables.InstructionsBlock = promptcontext.GlossaryInstructionsBlock(s.GlossaryInstructions)
|
||||
s.GlossaryVariables = &variables
|
||||
}
|
||||
if s.GlossaryView == nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/chunking"
|
||||
@@ -95,24 +96,31 @@ 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"},
|
||||
"one": {Instruction: "First {{literal}}"}, "two": {Instruction: "Second"}, "protected": {Instruction: "Protection only"}, "empty": {},
|
||||
},
|
||||
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{"two", "empty", "one"}, ProtectedTerms: []string{"protected"}},
|
||||
{Module: "grammar", Glossaries: []string{"two", "empty", "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"}}
|
||||
want := []GlossaryInstruction{{Collection: "two", Text: "Second"}, {Collection: "empty"}, {Collection: "one", Text: "First {{literal}}"}}
|
||||
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:]) {
|
||||
if !reflect.DeepEqual(specs[2].GlossaryInstructions, want[2:]) {
|
||||
t.Fatalf("specific selection = %+v", specs[2].GlossaryInstructions)
|
||||
}
|
||||
if err := specs[0].PrepareGlossary(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
block := specs[0].GlossaryVariables.InstructionsBlock
|
||||
if !strings.Contains(block, "two") || !strings.Contains(block, "one") || !strings.Contains(block, "First {{literal}}") || strings.Index(block, "Second") > strings.Index(block, "First") || strings.Contains(block, "empty") || strings.Contains(block, "Protection only") {
|
||||
t.Fatalf("instruction block = %q", block)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSectionMetadataFromSection(t *testing.T) {
|
||||
|
||||
@@ -69,7 +69,7 @@ func TestRoleClientCoordinatesIndependentRepairConversations(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vars := map[string]string{"TranscriptDescriptionBlock": "", "GlossaryJSON": "[]", "SectionJSON": "[]"}
|
||||
vars := map[string]string{"TranscriptDescriptionBlock": "", "GlossaryJSON": "[]", "SectionJSON": "[]", "GlossaryInstructionsBlock": "party:\nKeep {{literal}} names"}
|
||||
initial, err := proposal.CompleteStructured(context.Background(), structuredoutput.Request{PromptID: prompts.PromptIDModuleGrammarProposal, Variables: vars, CaptureExchange: true}, &map[string]any{})
|
||||
if err != nil || initial.Exchange == nil {
|
||||
t.Fatalf("capture: %+v %v", initial, err)
|
||||
@@ -95,6 +95,11 @@ func TestRoleClientCoordinatesIndependentRepairConversations(t *testing.T) {
|
||||
t.Fatalf("models = %v", models)
|
||||
}
|
||||
prefix := messages[0]
|
||||
initialMessages, _ := json.Marshal(prefix)
|
||||
repairMessages, _ := json.Marshal(messages[1])
|
||||
if !strings.Contains(string(initialMessages), "Keep {{literal}} names") || !strings.Contains(string(repairMessages), "Keep {{literal}} names") {
|
||||
t.Fatalf("guidance missing from proposal or repair: %s %s", initialMessages, repairMessages)
|
||||
}
|
||||
if len(messages) != 3 || !reflect.DeepEqual(messages[1][:len(prefix)], prefix) || !reflect.DeepEqual(messages[2][:len(prefix)], prefix) {
|
||||
t.Fatalf("prefixes: %+v", messages)
|
||||
}
|
||||
|
||||
@@ -74,8 +74,8 @@ func TestProcessingPlanReusesOnlyMatchingGlossaryViews(t *testing.T) {
|
||||
exempt.Module = "glossary"
|
||||
cfg := &config.Config{
|
||||
GlossaryCollections: map[string]config.GlossaryCollection{
|
||||
"a": {Entries: []config.GlossaryEntry{{ID: "a", Name: "A"}}},
|
||||
"b": {Entries: []config.GlossaryEntry{{ID: "b", Name: "B"}}},
|
||||
"a": {Instruction: "First instruction", Entries: []config.GlossaryEntry{{ID: "a", Name: "A"}}},
|
||||
"b": {Instruction: "Second instruction", Entries: []config.GlossaryEntry{{ID: "b", Name: "B"}}},
|
||||
},
|
||||
Pipeline: config.Pipeline{Stages: []config.Stage{item, item, reversed, exempt}},
|
||||
}
|
||||
@@ -93,6 +93,9 @@ func TestProcessingPlanReusesOnlyMatchingGlossaryViews(t *testing.T) {
|
||||
plan[2].Glossary[0].ID != "b" || plan[3].GlossaryVariables.ProtectionJSON != "[]" {
|
||||
t.Fatalf("incorrect glossary reuse or exemptions: %+v", plan)
|
||||
}
|
||||
if !strings.Contains(plan[1].GlossaryVariables.InstructionsBlock, "First instruction") || strings.Index(plan[2].GlossaryVariables.InstructionsBlock, "Second instruction") > strings.Index(plan[2].GlossaryVariables.InstructionsBlock, "First instruction") {
|
||||
t.Fatalf("instruction reuse/order: %+v", plan)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessingPlanPreservesFirstInspectionFailureAndCause(t *testing.T) {
|
||||
|
||||
@@ -3,14 +3,31 @@ package promptcontext
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/glossary"
|
||||
)
|
||||
|
||||
// GlossaryVariables contains the glossary inputs shared by a stage's proposals.
|
||||
type GlossaryVariables struct {
|
||||
ContextJSON string
|
||||
ProtectionJSON string
|
||||
ContextJSON string
|
||||
ProtectionJSON string
|
||||
InstructionsBlock string
|
||||
}
|
||||
|
||||
type GlossaryInstruction struct {
|
||||
Collection string
|
||||
Text string
|
||||
}
|
||||
|
||||
func GlossaryInstructionsBlock(instructions []GlossaryInstruction) string {
|
||||
var blocks []string
|
||||
for _, instruction := range instructions {
|
||||
if instruction.Text != "" {
|
||||
blocks = append(blocks, instruction.Collection+":\n"+instruction.Text)
|
||||
}
|
||||
}
|
||||
return strings.Join(blocks, "\n\n")
|
||||
}
|
||||
|
||||
func PrepareGlossaryVariables(entries []glossary.Entry, terms []glossary.Term) (GlossaryVariables, error) {
|
||||
|
||||
@@ -35,6 +35,7 @@ func ExecuteModuleProposal(ctx context.Context, req ModuleProposalRequest) (cont
|
||||
if err != nil {
|
||||
return contracts.ProposalResult{}, err
|
||||
}
|
||||
prepared.InstructionsBlock = promptcontext.GlossaryInstructionsBlock(req.ProposalRequest.RunSpec.GlossaryInstructions)
|
||||
glossary = &prepared
|
||||
}
|
||||
variables, err := buildProposalVariables(
|
||||
|
||||
@@ -15,6 +15,7 @@ func buildProposalVariables(transcript *schema.Transcript, glossary promptcontex
|
||||
return map[string]string{
|
||||
"TranscriptDescriptionBlock": promptcontext.TranscriptDescriptionBlock(transcriptDescription),
|
||||
"GlossaryJSON": glossary.ContextJSON,
|
||||
"GlossaryInstructionsBlock": glossary.InstructionsBlock,
|
||||
"ProtectedTermsJSON": glossary.ProtectionJSON,
|
||||
"SectionJSON": string(sectionJSON),
|
||||
}, nil
|
||||
|
||||
@@ -16,11 +16,12 @@ func TestBuildProposalVariablesPreservesSectionGlossaryAndDescription(t *testing
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prepared.InstructionsBlock = "party: use this guidance"
|
||||
variables, err := buildProposalVariables(transcript, prepared, 0, " Fantasy names ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(variables["SectionJSON"], `"section_index": 0`) || !strings.Contains(variables["SectionJSON"], "gestures") || !strings.Contains(variables["GlossaryJSON"], "Jesters") || !strings.Contains(variables["TranscriptDescriptionBlock"], "Fantasy names") {
|
||||
if !strings.Contains(variables["SectionJSON"], `"section_index": 0`) || !strings.Contains(variables["SectionJSON"], "gestures") || !strings.Contains(variables["GlossaryJSON"], "Jesters") || !strings.Contains(variables["TranscriptDescriptionBlock"], "Fantasy names") || variables["GlossaryInstructionsBlock"] != prepared.InstructionsBlock {
|
||||
t.Fatalf("encoded variables incomplete: %+v", variables)
|
||||
}
|
||||
empty, err := buildProposalVariables(transcript, prepared, 0, " ")
|
||||
|
||||
Reference in New Issue
Block a user