Model\TiktokenTokenizer.cs (91)
1040private static readonly (string Prefix, ModelEncoding Encoding)[] _modelPrefixToEncoding =
1042( "o1-", ModelEncoding.O200kBase ), // e.g. o1-mini
1043( "o3-", ModelEncoding.O200kBase ), // e.g. o3-mini
1044( "o4-mini-", ModelEncoding.O200kBase ), // e.g. o4-mini
1047( "gpt-5.1-", ModelEncoding.O200kBase ),
1048( "gpt-5-", ModelEncoding.O200kBase ),
1049( "gpt-4.1-", ModelEncoding.O200kBase ), // e.g., gpt-4.1-mini
1050( "gpt-4.5-", ModelEncoding.O200kBase ), // e.g., gpt-4.5
1051( "gpt-4o-", ModelEncoding.O200kBase ), // e.g., gpt-4o-2024-05-13
1052( "chatgpt-4o-", ModelEncoding.O200kBase ),
1053( "gpt-4-", ModelEncoding.Cl100kBase ), // e.g., gpt-4-0314, etc., plus gpt-4-32k
1054( "gpt-3.5-", ModelEncoding.Cl100kBase ), // e.g, gpt-3.5-turbo-0301, -0401, etc.
1055( "gpt-35-", ModelEncoding.Cl100kBase ), // Azure deployment name
1056( "gpt-oss-", ModelEncoding.O200kHarmony ),
1059( "ft:gpt-4o", ModelEncoding.O200kBase ),
1060( "ft:gpt-4", ModelEncoding.Cl100kBase ),
1061( "ft:gpt-3.5-turbo", ModelEncoding.Cl100kBase ),
1062( "ft:davinci-002", ModelEncoding.Cl100kBase ),
1063( "ft:babbage-002", ModelEncoding.Cl100kBase ),
1066private static readonly Dictionary<string, ModelEncoding> _modelToEncoding =
1067new Dictionary<string, ModelEncoding>(StringComparer.OrdinalIgnoreCase)
1070{ "o1", ModelEncoding.O200kBase },
1071{ "o3", ModelEncoding.O200kBase },
1072{ "o4-mini", ModelEncoding.O200kBase },
1075{ "gpt-5.1", ModelEncoding.O200kBase },
1076{ "gpt-5", ModelEncoding.O200kBase },
1077{ "gpt-4.1", ModelEncoding.O200kBase },
1078{ "gpt-4o", ModelEncoding.O200kBase },
1079{ "gpt-4", ModelEncoding.Cl100kBase },
1080{ "gpt-3.5-turbo", ModelEncoding.Cl100kBase },
1081{ "gpt-3.5", ModelEncoding.Cl100kBase },
1082{ "gpt-3.5-turbo-16k", ModelEncoding.Cl100kBase },
1083{ "gpt-35", ModelEncoding.Cl100kBase }, // Azure deployment name
1084{ "gpt-35-turbo", ModelEncoding.Cl100kBase }, // Azure deployment name
1085{ "gpt-35-turbo-16k", ModelEncoding.Cl100kBase }, // Azure deployment name
1088{ "davinci-002", ModelEncoding.Cl100kBase },
1089{ "babbage-002", ModelEncoding.Cl100kBase },
1093{ "text-embedding-ada-002", ModelEncoding.Cl100kBase },
1094{ "text-embedding-3-small", ModelEncoding.Cl100kBase },
1095{ "text-embedding-3-large", ModelEncoding.Cl100kBase },
1099{ "text-davinci-003", ModelEncoding.P50kBase },
1100{ "text-davinci-002", ModelEncoding.P50kBase },
1101{ "text-davinci-001", ModelEncoding.R50kBase },
1102{ "text-curie-001", ModelEncoding.R50kBase },
1103{ "text-babbage-001", ModelEncoding.R50kBase },
1104{ "text-ada-001", ModelEncoding.R50kBase },
1105{ "davinci", ModelEncoding.R50kBase },
1106{ "curie", ModelEncoding.R50kBase },
1107{ "babbage", ModelEncoding.R50kBase },
1108{ "ada", ModelEncoding.R50kBase },
1111{ "code-davinci-002", ModelEncoding.P50kBase },
1112{ "code-davinci-001", ModelEncoding.P50kBase },
1113{ "code-cushman-002", ModelEncoding.P50kBase },
1114{ "code-cushman-001", ModelEncoding.P50kBase },
1115{ "davinci-codex", ModelEncoding.P50kBase },
1116{ "cushman-codex", ModelEncoding.P50kBase },
1119{ "text-davinci-edit-001", ModelEncoding.P50kEdit },
1120{ "code-davinci-edit-001", ModelEncoding.P50kEdit },
1124{ "text-similarity-davinci-001", ModelEncoding.R50kBase },
1125{ "text-similarity-curie-001", ModelEncoding.R50kBase },
1126{ "text-similarity-babbage-001", ModelEncoding.R50kBase },
1127{ "text-similarity-ada-001", ModelEncoding.R50kBase },
1128{ "text-search-davinci-doc-001", ModelEncoding.R50kBase },
1129{ "text-search-curie-doc-001", ModelEncoding.R50kBase },
1130{ "text-search-babbage-doc-001", ModelEncoding.R50kBase },
1131{ "text-search-ada-doc-001", ModelEncoding.R50kBase },
1132{ "code-search-babbage-code-001", ModelEncoding.R50kBase },
1133{ "code-search-ada-code-001", ModelEncoding.R50kBase },
1136{ "gpt2", ModelEncoding.GPT2 },
1137{ "gpt-2", ModelEncoding.GPT2 },
1140{ Phi4ModelName, ModelEncoding.Cl100kBase },
1143private static ModelEncoding GetModelEncoding(string modelName)
1145if (!_modelToEncoding.TryGetValue(modelName, out ModelEncoding encoder))
1147foreach ((string Prefix, ModelEncoding Encoding) in _modelPrefixToEncoding)
1157if (encoder == ModelEncoding.None)
1193private static (Dictionary<string, int> SpecialTokens, Regex Regex, string VocabFile, Type? DataType, string PackageName) GetTiktokenConfigurations(ModelEncoding modelEncoding, string? modelName = null)
1197case ModelEncoding.Cl100kBase:
1206case ModelEncoding.GPT2:
1209case ModelEncoding.O200kBase:
1212case ModelEncoding.P50kBase:
1215case ModelEncoding.P50kEdit:
1219case ModelEncoding.R50kBase:
1222case ModelEncoding.O200kHarmony:
1288ModelEncoding modelEncoding,
1534ModelEncoding modelEncoding;
1537modelEncoding = ModelEncoding.Cl100kBase;
1541modelEncoding = ModelEncoding.O200kBase;
1545modelEncoding = ModelEncoding.O200kHarmony;
1549modelEncoding = ModelEncoding.P50kBase;
1553modelEncoding = ModelEncoding.P50kEdit;
1557modelEncoding = ModelEncoding.R50kBase;