22 lines
1.0 KiB
Go
22 lines
1.0 KiB
Go
package ai
|
|
|
|
import "encoding/json"
|
|
|
|
// Native schema generation and local validation are complementary: count and
|
|
// index uniqueness are always checked locally, even after constrained decoding.
|
|
func titleScoreSchema(count int) json.RawMessage {
|
|
index := map[string]any{"type": "integer", "minimum": 0, "maximum": count - 1}
|
|
row := func(withScore bool) map[string]any {
|
|
properties := map[string]any{"index": index}
|
|
required := []string{"index"}
|
|
if withScore {
|
|
properties["score"] = map[string]any{"type": "number", "minimum": 0, "maximum": 100}
|
|
required = append(required, "score")
|
|
}
|
|
return map[string]any{"type": "object", "properties": properties, "required": required, "additionalProperties": false}
|
|
}
|
|
schema := map[string]any{"type": "object", "properties": map[string]any{"scores": map[string]any{"type": "array", "minItems": count, "maxItems": count, "items": map[string]any{"anyOf": []any{row(false), row(true)}}}}, "required": []string{"scores"}, "additionalProperties": false}
|
|
raw, _ := json.Marshal(schema)
|
|
return raw
|
|
}
|