-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStructs.cs
More file actions
469 lines (442 loc) · 11.6 KB
/
Structs.cs
File metadata and controls
469 lines (442 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using net.novelai.util;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace net.novelai.api
{
/// <summary>
/// <see href="https://text.novelai.net/docs/index.html"></see>
/// </summary>
[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
public class Structs
{
[Obsolete]
public static string API_ENDPOINT = "https://api.novelai.net/";
public static string TEXT_ENDPOINT = "https://text.novelai.net/";
public static string IMAGE_ENDPOINT = "https://image.novelai.net/";
#region authentication
public struct AuthConfig
{
[JsonInclude]
public string Username;
[JsonInclude]
public string Password;
[JsonInclude]
public string AccessKey;
[JsonInclude]
public string AccessToken;
[JsonInclude]
public string EncryptionKey;
[JsonInclude]
public string APIKey;
}
public struct NaiKeys
{
public byte[] EncryptionKey;
public string AccessKey;
public string AccessToken;
public string APIKey;
public Dictionary<string, byte[]> keystore;
}
#endregion
#region generate
public struct NaiGenerateHTTPRespRaw
{
public string output;
}
public struct NaiGenerateHTTPResp
{
public string output;
public string Error;
public int StatusCode;
public string Message;
}
public struct NaiModel
{
public int created;
public string id;
public string @object;
public string owned_by;
}
public struct BiasParams
{
public uint[] sequence;
public double bias;
public bool ensure_sequence_finish;
public bool generate_once;
}
public struct NaiGenerateParams
{
public string label;
/// <summary>
/// AI base model. Available values (Default: "glm-4-6"):<br/>
/// <ul>
/// <li>glm-4-6<br/></li>
/// <li>kayra-v1<br/></li>
/// <li>clio-v1<br/></li>
/// <li>euterpe-v2<br/></li>
/// <li>6B-v4 (Sigurd)<br/></li>
/// <li>genji-jp-6b-v2<br/></li>
/// </ul>
/// </summary>
public string model;
public string promptFilename;
/// <summary>
/// range [0.1, 100]
/// </summary>
public double temperature;
/// <summary>
/// range [1, 2048]
/// </summary>
public uint max_length;
/// <summary>
/// range [1, 2048]
/// </summary>
public uint min_length;
public uint top_k;
public double top_p;
public double top_a;
/// <summary>
/// range [0, 65536]
/// </summary>
public double top_g;
/// <summary>
/// range [0, 1]
/// </summary>
public double min_p;
public double typical_p;
/// <summary>
/// range [0,1]
/// </summary>
public double tail_free_sampling;
public double repetition_penalty;
/// <summary>
/// range [0, 8192]
/// </summary>
public uint repetition_penalty_range;
public double repetition_penalty_slope;
/// <summary>
/// range [-16, 16]
/// </summary>
public double repetition_penalty_frequency;
/// <summary>
/// range [-16, 16]
/// </summary>
public double repetition_penalty_presence;
/// <summary>
/// range [0, 10]
/// </summary>
/// <summary>
/// [off, very_light, light, medium, aggressive, very_aggressive]
/// </summary>
public string phrase_rep_pen;
public uint[][] bad_words_ids;
public uint[][] stop_sequences;
public uint[] repetition_penalty_whitelist;
public bool generate_until_sentence;
public bool use_cache;
public bool use_string;
public bool return_full_text;
/// <summary>
/// AI modules, modifies AI behavior. Available values (default/no module "special_openings"):<br/>
/// <ul>
/// <li>special_openings<br/></li>
/// <li>special_instruct<br/></li>
/// <li>special_proseaugmenter<br/></li>
/// <li>theme_textadventure<br/></li>
/// <li>theme_christmas<br/></li>
/// </ul>
/// </summary>
public string prefix;
public BiasParams[] logit_bias_exp;
/// <summary>
/// range [0, 30]
/// </summary>
public uint num_logprobs;
public uint[] order;
public bool bracket_ban;
}
public struct PermutationsSpec
{
public string[] Model;
public string[] Prefix;
public string[] PromptFilename;
public double[] Temperature;
public uint[] MaxLength;
public uint[] MinLength;
public uint[] TopK;
public double[] TopP;
public double[] TailFreeSampling;
public double[] RepetitionPenalty;
public uint[] RepetitionPenaltyRange;
public double[] RepetitionPenaltySlope;
}
public struct NaiGenerateResp
{
public string EncodedRequest;
public string EncodedResponse;
public string Response;
public Exception Error;
}
public struct NaiGenerateKayra
{
public string input;
public string model;
public NaiGenerateParams parameters;
}
public struct NaiGenerateGLM
{
public string model;
/// <summary>
/// range [1, 2048]
/// </summary>
public uint max_tokens;
/// <summary>
/// range [0.1, 100]
/// </summary>
public double temperature;
public double top_p;
public uint top_k;
/// <summary>
/// range [-16, 16]
/// </summary>
public double frequency_penalty;
/// <summary>
/// range [-16, 16]
/// </summary>
public double presence_penalty;
public double unified_linear;
public double unified_quadratic;
public double unified_increase_linear_with_entropy;
public double unified_cubic;
public int[] stop;
public Dictionary<int, double> logit_bias;
public uint[] prompt;
public int logprobs;
public bool stream;
}
public struct NaiGenerateVoice
{
public string text; // The text to speak. Limited to 2000 characters.
public string seed; // Seed is used when voice is set to -1
/*
* Use the following seeds for the predefined voices:
* Voice Type Seed to use
* -------- ------- -----------
* Aini Female Aini
* Orea Female Orea
* Claea Female Claea
* Lim Female Lim
* Aurae Female Aurae
* Naia Female Naia
* -------- ------- -----------
* Aulon Male Aulon
* Elei Male Elei
* Ogma Male Ogma
* Raid Male Raid
* Pega Male Pega
* Lam Male Lam
* -------- ------- -----------
* Ligeia Unisex Anananan
*/
public int voice; // Voice is used by v1, use -1 to use seed
/*
* Use the following values for the predefined voice
* Voice Type Voice to use
* -------- ------- ------------
* Cyllene Female 17
* Leucosia Female 95
* Crina Female 44
* Hespe Female 80
* Ida Female 106
* -------- ------- ------------
* Alseid Male 6
* Daphnis Male 10
* Echo Male 16
* Thel Male 41
* Nomios Male 77
* -------- ------- ------------
* Custom Seed -1
*/
public bool opus;
public string version; // Version should be either v1 or v2
}
public struct NaiByteArrayResponse
{
public byte[] output;
public string ContentType;
public string Error;
public int StatusCode;
public string Message;
}
#endregion
#region adventure
public struct ScenarioSettings
{
//public NaiGenerateParams Parameters;
public bool TrimResponses;
public bool BanBrackets;
public OutputTrimType TrimType;
}
public struct LorebookEntry
{
public int LoreId;
public string Text;
public ContextConfig ContextCfg;
public int LastUpdatedAt;
public string DisplayName;
public string[] Keys;
public int SearchRange;
public bool Enabled;
public bool ForceActivation;
public bool KeyRelative;
public bool NonStoryActivatable;
public uint[] Tokens;
public Regex[] KeysRegex;
public static LorebookEntry FromEditable(LorebookEntryEditable from)
{
gpt_bpe.GPTEncoder encoder = gpt_bpe.NewEncoder();
uint[] tokens = encoder.Encode(from.Text);
Regex[] regexKeys = new Regex[from.Keys.Length];
for (int i = 0; i < from.Keys.Length; i++)
{
string key = from.Keys[i];
regexKeys[i] = new Regex(string.Format(@"(?i)(^|\W)({0})($|\W)", key));
}
return new LorebookEntry
{
LoreId = from.LoreId,
DisplayName = from.Keys[0],
Text = from.Text,
ContextCfg = from.ContextCfg,
Keys = from.Keys,
SearchRange = from.SearchRange,
Enabled = from.Enabled,
ForceActivation = from.ForceActivation,
KeysRegex = regexKeys,
Tokens = tokens,
};
}
}
public struct LorebookEntryEditable
{
public int LoreId;
public string Text;
public ContextConfig ContextCfg;
public string[] Keys;
public int SearchRange;
public bool Enabled;
public bool ForceActivation;
public static LorebookEntryEditable FromLore(LorebookEntry from)
{
return new LorebookEntryEditable
{
LoreId = from.LoreId,
Text = from.Text,
ContextCfg = from.ContextCfg,
Keys = from.Keys,
SearchRange = from.SearchRange,
Enabled = from.Enabled,
ForceActivation = from.ForceActivation,
};
}
}
public struct ContextConfig
{
public string Prefix;
public string Suffix;
public int TokenBudget;
public int ReservedTokens;
public int BudgetPriority;
public TrimDirection TrimDirection;
public InsertionType InsertionType;
public MaxTrimType MaximumTrimType;
public int InsertionPosition;
}
public struct ContextEntry
{
public string Text;
public ContextConfig ContextCfg;
public uint[] Tokens;
public string Label;
public Dictionary<string, int[][]>[] MatchIndexes;
public uint Index;
public uint[] ResolveTrim(ITokenizer tokenizer, int budget)
{
int trimSize = 0;
int numTokens = Tokens.Length;
int projected = budget - numTokens + ContextCfg.ReservedTokens;
if (projected > ContextCfg.TokenBudget)
{
trimSize = ContextCfg.TokenBudget;
}
else if (projected >= 0)
{
// We have enough to fit this into the budget.
trimSize = numTokens;
}
else
{
if (numTokens * 0.3 <= budget)
{
trimSize = budget;
}
else
{
trimSize = 0;
}
}
uint[] trimmedTokens = tokenizer.TrimNewlines(Tokens, ContextCfg.TrimDirection, trimSize);
if (trimmedTokens.Length == 0 && ContextCfg.MaximumTrimType >= MaxTrimType.SENTENCES)
{
trimmedTokens = tokenizer.TrimSentences(Tokens, ContextCfg.TrimDirection, trimSize);
}
if (trimmedTokens.Length == 0 && ContextCfg.MaximumTrimType == MaxTrimType.TOKENS)
{
switch (ContextCfg.TrimDirection)
{
case TrimDirection.TOP:
trimmedTokens = new uint[trimSize];
Array.Copy(Tokens, numTokens - trimSize, trimmedTokens, 0, trimSize);
break;
case TrimDirection.BOTTOM:
trimmedTokens = new uint[trimSize];
Array.Copy(Tokens, 0, trimmedTokens, 0, trimSize);
break;
default:
trimmedTokens = Tokens;
break;
}
}
return trimmedTokens;
}
}
[JsonObject]
public struct RemoteStoryMeta
{
[JsonProperty("id")]
public string storyID;
public string type;
public string metaID;
public StoryMeta meta;
}
[JsonObject]
public struct StoryMeta
{
public string id;
public string remoteId;
public string remoteStoryId;
public string title;
public string description;
public string textPreview;
public bool favorite;
public string[] tags;
[JsonProperty("createdAt")]
public long created;
public long lastUpdatedAt;
}
#endregion
}
}