jsonSchema static method

Map<String, Object?> jsonSchema({
  1. bool steerableOnly = true,
})

A JSON Schema (draft 2020-12) for model output, generated from vocabulary. With steerableOnly, families offers only steerableFamilies.

Implementation

static Map<String, Object?> jsonSchema({bool steerableOnly = true}) {
  List<String> words(IntentField f) => [
    for (final v in f.values)
      if (!steerableOnly || v.steerable) v.value,
  ];
  return {
    r'$schema': 'https://json-schema.org/draft/2020-12/schema',
    'type': 'object',
    'additionalProperties': false,
    'properties': {
      for (final f in vocabulary)
        f.name: switch (f.kind) {
          IntentFieldKind.oneOf => {
            'description': f.description,
            'type': 'string',
            'enum': words(f),
          },
          IntentFieldKind.listOf => {
            'description': f.description,
            'type': 'array',
            'items': {'type': 'string', 'enum': words(f)},
            'maxItems': f.maxItems,
            'uniqueItems': true,
          },
          IntentFieldKind.text => {
            'description': '${f.description}, at most ${f.maxWords} words',
            'type': 'string',
            'maxLength': maxNameLength,
          },
        },
    },
  };
}