Use prompt-defined structured output contracts
This commit is contained in:
@@ -1,154 +1,14 @@
|
||||
package responseschema
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"embed"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Key identifies one structured response schema used by Audita.
|
||||
type Key string
|
||||
|
||||
const (
|
||||
CorrectionSetKey Key = "correction_set"
|
||||
CorrectionRepairKey Key = "correction_repair"
|
||||
ValidatorDecisionSetKey Key = "validator_decision_set"
|
||||
AssetRoot = "assets"
|
||||
correctionSetSchemaID = "audita.correction_set"
|
||||
correctionRepairSchemaID = "audita.correction_repair"
|
||||
validatorDecisionSchemaID = "audita.validator_decision_set"
|
||||
schemaVersionV1 = "v1"
|
||||
)
|
||||
const AssetRoot = "assets"
|
||||
|
||||
//go:embed assets/*.json
|
||||
var schemaAssets embed.FS
|
||||
|
||||
// Filesystem exposes the canonical embedded schemas to PromptKit.
|
||||
// Filesystem exposes embedded schemas to PromptKit.
|
||||
func Filesystem() fs.FS { return schemaAssets }
|
||||
|
||||
func AssetPath(key Key) (string, bool) {
|
||||
switch key {
|
||||
case CorrectionSetKey:
|
||||
return "correction_set.v1.json", true
|
||||
case CorrectionRepairKey:
|
||||
return "correction_repair.v1.json", true
|
||||
case ValidatorDecisionSetKey:
|
||||
return "validator_decision_set.v1.json", true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
// Schema describes one registered structured response schema.
|
||||
type Schema struct {
|
||||
ID string `json:"id"`
|
||||
Version string `json:"version"`
|
||||
Name string `json:"name"`
|
||||
JSONSchema json.RawMessage `json:"json_schema"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
func (s Schema) DiagnosticsMap() map[string]any {
|
||||
return map[string]any{
|
||||
"id": s.ID,
|
||||
"version": s.Version,
|
||||
"name": s.Name,
|
||||
"sha256": s.SHA256,
|
||||
}
|
||||
}
|
||||
|
||||
var registry = map[Key]Schema{
|
||||
CorrectionRepairKey: mustBuildSchemaFromAsset(correctionRepairSchemaID, schemaVersionV1, "audita_correction_repair_v1", "assets/correction_repair.v1.json"),
|
||||
CorrectionSetKey: mustBuildSchemaFromAsset(
|
||||
correctionSetSchemaID,
|
||||
schemaVersionV1,
|
||||
"audita_correction_set_v1",
|
||||
"assets/correction_set.v1.json",
|
||||
),
|
||||
ValidatorDecisionSetKey: mustBuildSchemaFromAsset(
|
||||
validatorDecisionSchemaID,
|
||||
schemaVersionV1,
|
||||
"audita_validator_decision_set_v1",
|
||||
"assets/validator_decision_set.v1.json",
|
||||
),
|
||||
}
|
||||
|
||||
func Registered() []Schema {
|
||||
keys := make([]string, 0, len(registry))
|
||||
for key := range registry {
|
||||
keys = append(keys, string(key))
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
out := make([]Schema, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
out = append(out, cloneSchema(registry[Key(key)]))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Lookup returns a copy of the registered schema for the provided key.
|
||||
func Lookup(key Key) (Schema, bool) {
|
||||
schema, ok := registry[key]
|
||||
if !ok {
|
||||
return Schema{}, false
|
||||
}
|
||||
return cloneSchema(schema), true
|
||||
}
|
||||
|
||||
// MustLookup returns a copy of the registered schema and panics when missing.
|
||||
func MustLookup(key Key) Schema {
|
||||
schema, ok := Lookup(key)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("unknown structured response schema key %q", key))
|
||||
}
|
||||
return schema
|
||||
}
|
||||
|
||||
func cloneSchema(in Schema) Schema {
|
||||
out := in
|
||||
if in.JSONSchema != nil {
|
||||
out.JSONSchema = append(json.RawMessage(nil), in.JSONSchema...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func mustBuildSchemaFromAsset(id string, version string, name string, assetPath string) Schema {
|
||||
rawSchema, err := schemaAssets.ReadFile(assetPath)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("read response schema asset %q: %v", assetPath, err))
|
||||
}
|
||||
return mustBuildSchema(id, version, name, rawSchema)
|
||||
}
|
||||
|
||||
func mustBuildSchema(id string, version string, name string, rawSchema []byte) Schema {
|
||||
id = strings.TrimSpace(id)
|
||||
version = strings.TrimSpace(version)
|
||||
name = strings.TrimSpace(name)
|
||||
if id == "" {
|
||||
panic("schema id must not be empty")
|
||||
}
|
||||
if version == "" {
|
||||
panic("schema version must not be empty")
|
||||
}
|
||||
if name == "" {
|
||||
panic("schema name must not be empty")
|
||||
}
|
||||
if !json.Valid(rawSchema) {
|
||||
panic(fmt.Sprintf("schema %s:%s is not valid JSON", id, version))
|
||||
}
|
||||
|
||||
hash := sha256.Sum256(rawSchema)
|
||||
return Schema{
|
||||
ID: id,
|
||||
Version: version,
|
||||
Name: name,
|
||||
JSONSchema: append(json.RawMessage(nil), rawSchema...),
|
||||
SHA256: hex.EncodeToString(hash[:]),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,129 +1,20 @@
|
||||
package responseschema
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/fs"
|
||||
"testing"
|
||||
|
||||
jsonschema "github.com/santhosh-tekuri/jsonschema/v6"
|
||||
)
|
||||
|
||||
func TestPromptKitSchemaPathsUseCanonicalAssets(t *testing.T) {
|
||||
for _, key := range []Key{CorrectionSetKey, CorrectionRepairKey, ValidatorDecisionSetKey} {
|
||||
path, ok := AssetPath(key)
|
||||
if !ok {
|
||||
t.Fatalf("missing path for %q", key)
|
||||
}
|
||||
file, err := Filesystem().Open(AssetRoot + "/" + path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := io.ReadAll(file)
|
||||
file.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(data, MustLookup(key).JSONSchema) {
|
||||
t.Fatalf("PromptKit schema differs for %q", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupKnownSchemas(t *testing.T) {
|
||||
correction, ok := Lookup(CorrectionSetKey)
|
||||
if !ok {
|
||||
t.Fatalf("expected correction-set schema to be registered")
|
||||
}
|
||||
if correction.ID != correctionSetSchemaID {
|
||||
t.Fatalf("unexpected correction-set schema id %q", correction.ID)
|
||||
}
|
||||
if correction.Version != schemaVersionV1 {
|
||||
t.Fatalf("unexpected correction-set schema version %q", correction.Version)
|
||||
}
|
||||
if correction.Name != "audita_correction_set_v1" {
|
||||
t.Fatalf("unexpected correction-set schema name %q", correction.Name)
|
||||
}
|
||||
|
||||
validation, ok := Lookup(ValidatorDecisionSetKey)
|
||||
if !ok {
|
||||
t.Fatalf("expected validator-decision schema to be registered")
|
||||
}
|
||||
if validation.ID != validatorDecisionSchemaID {
|
||||
t.Fatalf("unexpected validator-decision schema id %q", validation.ID)
|
||||
}
|
||||
if validation.Version != schemaVersionV1 {
|
||||
t.Fatalf("unexpected validator-decision schema version %q", validation.Version)
|
||||
}
|
||||
if validation.Name != "audita_validator_decision_set_v1" {
|
||||
t.Fatalf("unexpected validator-decision schema name %q", validation.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupUnknownSchema(t *testing.T) {
|
||||
if _, ok := Lookup(Key("missing")); ok {
|
||||
t.Fatalf("expected unknown schema lookup to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaHashesMatchRegisteredJSON(t *testing.T) {
|
||||
for _, key := range []Key{CorrectionSetKey, CorrectionRepairKey, ValidatorDecisionSetKey} {
|
||||
schema, ok := Lookup(key)
|
||||
if !ok {
|
||||
t.Fatalf("missing schema %q", key)
|
||||
}
|
||||
sum := sha256.Sum256(schema.JSONSchema)
|
||||
expected := hex.EncodeToString(sum[:])
|
||||
if schema.SHA256 != expected {
|
||||
t.Fatalf("unexpected hash for %q: got %q want %q", key, schema.SHA256, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupReturnsSchemaCopy(t *testing.T) {
|
||||
schema, ok := Lookup(CorrectionSetKey)
|
||||
if !ok {
|
||||
t.Fatalf("missing correction-set schema")
|
||||
}
|
||||
if len(schema.JSONSchema) == 0 {
|
||||
t.Fatalf("expected non-empty schema payload")
|
||||
}
|
||||
schema.JSONSchema[0] = 'x'
|
||||
|
||||
again, ok := Lookup(CorrectionSetKey)
|
||||
if !ok {
|
||||
t.Fatalf("missing correction-set schema on second lookup")
|
||||
}
|
||||
if len(again.JSONSchema) == 0 {
|
||||
t.Fatalf("unexpected empty schema payload")
|
||||
}
|
||||
if again.JSONSchema[0] != '{' {
|
||||
t.Fatalf("expected lookup to return independent schema copy")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagnosticsMapIncludesStableSchemaMetadataShapeForAllSchemas(t *testing.T) {
|
||||
registered := Registered()
|
||||
if len(registered) == 0 {
|
||||
t.Fatalf("expected registered response schemas")
|
||||
}
|
||||
|
||||
for _, schema := range registered {
|
||||
metadataMap := schema.DiagnosticsMap()
|
||||
if metadataMap["id"] != schema.ID ||
|
||||
metadataMap["version"] != schema.Version ||
|
||||
metadataMap["name"] != schema.Name ||
|
||||
metadataMap["sha256"] != schema.SHA256 {
|
||||
t.Fatalf("unexpected diagnostics metadata map for %q: %+v", schema.ID, metadataMap)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCorrectionRepairSchema(t *testing.T) {
|
||||
var document any
|
||||
if err := json.Unmarshal(MustLookup(CorrectionRepairKey).JSONSchema, &document); err != nil {
|
||||
raw, err := fs.ReadFile(Filesystem(), AssetRoot+"/correction_repair.v1.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(raw, &document); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
compiler := jsonschema.NewCompiler()
|
||||
|
||||
Reference in New Issue
Block a user