Use prompt-defined structured output contracts
This commit is contained in:
@@ -1,52 +1,36 @@
|
||||
package prompts
|
||||
package prompts_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/responseschema"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/appconfig"
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/prompts"
|
||||
ck "go.maximumdirect.net/configkit"
|
||||
pk "go.maximumdirect.net/promptkit"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func TestEmbeddedDefinitionsPrepareThroughPromptKit(t *testing.T) {
|
||||
promptInputs, err := pk.FilesystemInputs("embedded prompts", Filesystem(), AssetRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
endpoint := "https://example.invalid/v1"
|
||||
model := "fixture-model"
|
||||
sources := pk.ConfigurationSources{
|
||||
Underlays: []ck.Underlay{
|
||||
{Inputs: promptInputs},
|
||||
{Inputs: []ck.Input{{Source: pk.ProfileSource("registry fixture profile", []pk.ProfileConfig{
|
||||
{ID: "fixture", Endpoint: &endpoint, Model: &model},
|
||||
})}}},
|
||||
},
|
||||
}
|
||||
engine, err := pk.NewEngine(pk.Config{Sources: sources},
|
||||
pk.WithSchemaFS(responseschema.Filesystem(), responseschema.AssetRoot),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
engine := fixtureEngine(t)
|
||||
vars := map[string]string{
|
||||
"HardeningPolicy": HardeningText(),
|
||||
"HardeningPolicy": prompts.HardeningText(),
|
||||
"TranscriptDescriptionBlock": "Transcript description:\nfixture-description\n\n",
|
||||
"GlossaryJSON": "{\"name\":\"fixture-glossary\"}",
|
||||
"SectionJSON": "{\"text\":\"fixture-section\"}",
|
||||
"PayloadJSON": "[{\"text\":\"fixture-correction\"}]",
|
||||
}
|
||||
vars = WithSharedVariables(vars)
|
||||
vars = prompts.WithSharedVariables(vars)
|
||||
for _, tc := range []struct{ id, schema, payload string }{
|
||||
{PromptIDModuleGlossaryProposal, "correction_set.v1.json", "fixture-section"},
|
||||
{PromptIDModuleHomophonesProposal, "correction_set.v1.json", "fixture-section"},
|
||||
{PromptIDModuleSpokenWordProposal, "correction_set.v1.json", "fixture-section"},
|
||||
{PromptIDModuleGrammarProposal, "correction_set.v1.json", "fixture-section"},
|
||||
{PromptIDValidatorSpokenFormPlausibility, "validator_decision_set.v1.json", "fixture-correction"},
|
||||
{PromptIDValidatorMeaningReversalReview, "validator_decision_set.v1.json", "fixture-correction"},
|
||||
{PromptIDValidatorEditorialReview, "validator_decision_set.v1.json", "fixture-correction"},
|
||||
{prompts.PromptIDModuleGlossaryProposal, "correction_set.v1.json", "fixture-section"},
|
||||
{prompts.PromptIDModuleHomophonesProposal, "correction_set.v1.json", "fixture-section"},
|
||||
{prompts.PromptIDModuleSpokenWordProposal, "correction_set.v1.json", "fixture-section"},
|
||||
{prompts.PromptIDModuleGrammarProposal, "correction_set.v1.json", "fixture-section"},
|
||||
{prompts.PromptIDValidatorSpokenFormPlausibility, "validator_decision_set.v1.json", "fixture-correction"},
|
||||
{prompts.PromptIDValidatorMeaningReversalReview, "validator_decision_set.v1.json", "fixture-correction"},
|
||||
{prompts.PromptIDValidatorEditorialReview, "validator_decision_set.v1.json", "fixture-correction"},
|
||||
} {
|
||||
t.Run(tc.id, func(t *testing.T) {
|
||||
prepared, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: tc.id, ProfileID: "fixture", Vars: vars})
|
||||
@@ -56,6 +40,13 @@ func TestEmbeddedDefinitionsPrepareThroughPromptKit(t *testing.T) {
|
||||
if prepared.PromptID != tc.id || prepared.OutputContract.SchemaPath != tc.schema || prepared.StructuredOutput == nil {
|
||||
t.Fatalf("selection/schema: %+v", prepared)
|
||||
}
|
||||
inspection, err := engine.InspectPrompt(context.Background(), tc.id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if prepared.OutputContract.RepairAttempts != inspection.OutputContract.RepairAttempts {
|
||||
t.Fatalf("prepared budget = %d, inspected budget = %d", prepared.OutputContract.RepairAttempts, inspection.OutputContract.RepairAttempts)
|
||||
}
|
||||
if len(prepared.Messages) != 2 {
|
||||
t.Fatalf("message count: %d", len(prepared.Messages))
|
||||
}
|
||||
@@ -68,7 +59,7 @@ func TestEmbeddedDefinitionsPrepareThroughPromptKit(t *testing.T) {
|
||||
if strings.HasPrefix(tc.id, "audita/modules.") && !strings.Contains(rendered, "fixture-glossary") {
|
||||
t.Error("missing glossary context")
|
||||
}
|
||||
missing := map[string]string{"HardeningPolicy": HardeningText()}
|
||||
missing := map[string]string{"HardeningPolicy": prompts.HardeningText()}
|
||||
if _, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: tc.id, ProfileID: "fixture", Vars: missing}); err == nil {
|
||||
t.Error("missing variables accepted")
|
||||
}
|
||||
@@ -86,32 +77,24 @@ func TestRegistryMetadataIdentifiesRequiredPrompts(t *testing.T) {
|
||||
"audita/validators.spoken_form_plausibility",
|
||||
}
|
||||
for _, id := range required {
|
||||
metadata, ok := LookupMetadata(id)
|
||||
if !ok || metadata.PromptID != id || metadata.PromptSource != SourceBuiltin ||
|
||||
metadata.EmbeddedPath != AssetRoot+"/"+strings.TrimPrefix(id, "audita/")+".yaml" {
|
||||
metadata, ok := prompts.LookupMetadata(id)
|
||||
if !ok || metadata.PromptID != id || metadata.PromptSource != prompts.SourceBuiltin ||
|
||||
metadata.EmbeddedPath != prompts.AssetRoot+"/"+strings.TrimPrefix(id, "audita/")+".yaml" {
|
||||
t.Fatalf("required prompt metadata for %q: %+v", id, metadata)
|
||||
}
|
||||
}
|
||||
all := RegisteredMetadata()
|
||||
all := prompts.RegisteredMetadata()
|
||||
all[0].PromptID = "changed"
|
||||
if again := RegisteredMetadata(); again[0].PromptID == "changed" {
|
||||
if again := prompts.RegisteredMetadata(); again[0].PromptID == "changed" {
|
||||
t.Fatal("metadata enumeration shares mutable state")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepairDefinitionsPrepareThroughPromptKit(t *testing.T) {
|
||||
inputs, err := pk.FilesystemInputs("embedded prompts", Filesystem(), AssetRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
endpoint, model := "https://example.invalid/v1", "fixture"
|
||||
engine, err := pk.NewEngine(pk.Config{Sources: pk.ConfigurationSources{Underlays: []ck.Underlay{{Inputs: inputs}, {Inputs: []ck.Input{{Source: pk.ProfileSource("fixture", []pk.ProfileConfig{{ID: "fixture", Endpoint: &endpoint, Model: &model}})}}}}}}, pk.WithSchemaFS(responseschema.Filesystem(), responseschema.AssetRoot))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, id := range []string{PromptIDModuleGlossaryRepair, PromptIDModuleHomophonesRepair, PromptIDModuleSpokenWordRepair, PromptIDModuleGrammarRepair} {
|
||||
engine := fixtureEngine(t)
|
||||
for _, id := range []string{prompts.PromptIDModuleGlossaryRepair, prompts.PromptIDModuleHomophonesRepair, prompts.PromptIDModuleSpokenWordRepair, prompts.PromptIDModuleGrammarRepair} {
|
||||
t.Run(id, func(t *testing.T) {
|
||||
prepared, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: id, ProfileID: "fixture", Vars: WithSharedVariables(map[string]string{"RepairRequestJSON": `{"proposal_ref":2,"correction":{"id":1,"original_text":" ","corrected_text":""},"findings":[{"validator":"shape","reason":"example"}]}`})})
|
||||
prepared, err := engine.Prepare(context.Background(), pk.RunRequest{PromptID: id, ProfileID: "fixture", Vars: prompts.WithSharedVariables(map[string]string{"RepairRequestJSON": `{"proposal_ref":2,"correction":{"id":1,"original_text":" ","corrected_text":""},"findings":[{"validator":"shape","reason":"example"}]}`})})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -126,3 +109,21 @@ func TestRepairDefinitionsPrepareThroughPromptKit(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func fixtureEngine(t *testing.T) *pk.Engine {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
if err := os.WriteFile(main, []byte("version: 3\npromptkit:\n profiles:\n - id: fixture\n endpoint: https://example.invalid/v1\n model: fixture-model\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, _, err := appconfig.Load(context.Background(), ck.Input{Source: ck.File(main)}, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
engine, err := pk.NewFromConfig(loaded, pk.RuntimeConfig{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return engine
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user