char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
acecd5603
char/aliceai-t5-moe
1#pragma once 2 3#include "json.h" 4 5#include <cstdint> 6#include <initializer_list> 7#include <map> 8#include <memory> 9#include <string> 10#include <vector> 11 12// JSON schema, covering the subset that json_schema_to_grammar() can convert. 13 14struct common_chat_schema { 15enum node_kind { 16KIND_ANY , 17KIND_REF , 18KIND_ANY_OF , 19KIND_ALL_OF , 20KIND_CONST , 21KIND_ENUM , 22KIND_NULL , 23KIND_BOOLEAN , 24KIND_NUMBER , 25KIND_INTEGER , 26KIND_STRING , 27KIND_ARRAY , 28KIND_TUPLE , 29KIND_OBJECT , 30 }; 31 32enum value_type { 33TYPE_NULL , 34TYPE_BOOLEAN , 35TYPE_NUMBER , 36TYPE_INTEGER , 37TYPE_STRING , 38TYPE_ARRAY , 39TYPE_OBJECT , 40 }; 41 42enum string_format { 43FORMAT_NONE , 44FORMAT_UUID ,// uuid, uuid1 .. uuid5 45FORMAT_DATE , 46FORMAT_TIME , 47FORMAT_DATE_TIME , 48 }; 49 50class type_set { 51uint32_t mask_ = 0 ; 52 53public : 54type_set () = default ; 55type_set ( std :: initializer_list < value_type > types ) { 56for (auto type : types) { 57add( type ); 58} 59} 60 61static type_set all () { 62return { TYPE_NULL , TYPE_BOOLEAN , TYPE_NUMBER , TYPE_INTEGER , TYPE_STRING , TYPE_ARRAY , TYPE_OBJECT }; 63} 64 65void add ( value_type type ) { mask_ |= 1u << type ; } 66 67bool has ( value_type type ) const { return ( mask_ & ( 1u << type )) != 0 ; } 68bool is_only ( value_type type ) const { return mask_ == ( 1u << type ); } 69bool empty () const { return mask_ == 0 ; } 70 71type_set & operator |=( const type_set & other ) { mask_ |= other .mask_; return * this ; } 72type_set & operator &=( const type_set & other ) { mask_ &= other .mask_; return * this ; } 73 74bool operator == ( const type_set & other ) const { return mask_ == other .mask_; } 75bool operator != ( const type_set & other ) const { return mask_ != other .mask_; } 76}; 77 78virtual ~ common_chat_schema () = default ; 79virtual node_kind kind () const = 0 ; 80 81type_set value_types () const ; 82 83// Whether a value matching the schema may be a string, through any branch of it. 84bool may_be_string () const ; 85 86static const char * kind_name ( node_kind kind ); 87static const char * type_name ( value_type type ); 88}; 89 90using common_chat_schema_ptr = std :: unique_ptr < common_chat_schema > ; 91 92struct common_chat_schema_any : common_chat_schema { 93node_kind kind () const override { return KIND_ANY ; } 94}; 95 96// {"$ref": "#/..."}, only references into the same document are supported 97struct common_chat_schema_ref : common_chat_schema { 98std :: string ref ; 99const common_chat_schema * target = nullptr ; // owned by common_chat_schema_document::refs 100 101explicit common_chat_schema_ref ( std :: string ref ) : ref ( std :: move ( ref )) {} 102 103node_kind kind () const override { return KIND_REF ; } 104}; 105 106// oneOf / anyOf, or a "type" array expanded to one alternative per type 107struct common_chat_schema_any_of : common_chat_schema { 108std :: vector < common_chat_schema_ptr > children ; 109 110node_kind kind () const override { return KIND_ANY_OF ; } 111}; 112 113struct common_chat_schema_all_of : common_chat_schema { 114std :: vector < common_chat_schema_ptr > children ; 115 116node_kind kind () const override { return KIND_ALL_OF ; } 117}; 118 119struct common_chat_schema_const : common_chat_schema { 120common_json value ; 121 122explicit common_chat_schema_const ( common_json value ) : value ( std :: move ( value )) {} 123 124node_kind kind () const override { return KIND_CONST ; } 125}; 126 127struct common_chat_schema_enum : common_chat_schema { 128std :: vector < common_json > values ; 129 130node_kind kind () const override { return KIND_ENUM ; } 131}; 132 133struct common_chat_schema_null : common_chat_schema { 134node_kind kind () const override { return KIND_NULL ; } 135}; 136 137struct common_chat_schema_boolean : common_chat_schema { 138node_kind kind () const override { return KIND_BOOLEAN ; } 139}; 140 141struct common_chat_schema_number : common_chat_schema { 142node_kind kind () const override { return KIND_NUMBER ; } 143}; 144 145// bounds are inclusive, exclusiveMinimum / exclusiveMaximum are folded in 146struct common_chat_schema_integer : common_chat_schema { 147int64_t minimum = INT64_MIN ; // INT64_MIN for unbounded 148int64_t maximum = INT64_MAX ; // INT64_MAX for unbounded 149 150node_kind kind () const override { return KIND_INTEGER ; } 151}; 152 153struct common_chat_schema_string : common_chat_schema { 154std :: string pattern ; // empty when absent 155string_format format = FORMAT_NONE ; 156int min_length = 0 ; 157int max_length = -1 ; // -1 for unbounded 158 159node_kind kind () const override { return KIND_STRING ; } 160}; 161 162struct common_chat_schema_array : common_chat_schema { 163common_chat_schema_ptr items ; // a common_chat_schema_any when "items" is absent 164int min_items = 0 ; 165int max_items = -1 ; // -1 for unbounded 166 167node_kind kind () const override { return KIND_ARRAY ; } 168}; 169 170struct common_chat_schema_tuple : common_chat_schema { 171std :: vector < common_chat_schema_ptr > items ; 172 173node_kind kind () const override { return KIND_TUPLE ; } 174}; 175 176struct common_chat_schema_property { 177std ::string name; 178common_chat_schema_ptr schema; 179bool required = false; 180}; 181 182struct common_chat_schema_object : common_chat_schema { 183std :: vector < common_chat_schema_property > properties ; // in schema order 184common_chat_schema_ptr additional_properties ; // null when not allowed 185 186node_kind kind () const override { return KIND_OBJECT ; } 187}; 188 189struct common_chat_schema_document { 190common_chat_schema_ptr root; 191std ::map < std:: string , common_chat_schema_ptr > refs ; 192}; 193 194// A document shared by the PEG parsers built from its nodes, which it keeps alive 195using common_chat_schema_document_ptr = std :: shared_ptr < const common_chat_schema_document > ; 196 197// Throws std::runtime_error when the schema falls outside the supported subset. 198common_chat_schema_document common_chat_schema_from_json ( const common_json & schema );