-
Notifications
You must be signed in to change notification settings - Fork 9
/
scenario.go
850 lines (776 loc) · 26.4 KB
/
scenario.go
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
package scenario
import (
"encoding/json"
"github.com/wbrown/gpt_bpe"
"github.com/wbrown/novelai-research-tool/aimodules"
novelai_api "github.com/wbrown/novelai-research-tool/novelai-api"
"github.com/wbrown/novelai-research-tool/structs"
"io/ioutil"
"log"
"reflect"
"regexp"
"sort"
"strings"
)
type ContextConfig struct {
Prefix *string `json:"prefix,omitempty" yaml:"prefix"`
Suffix *string `json:"suffix,omitempty" yaml:"suffix"`
TokenBudget *int `json:"tokenBudget,omitempty" yaml:"tokenBudget"`
ReservedTokens *int `json:"reservedTokens,omitempty" yaml:"reservedTokens"`
BudgetPriority *int `json:"budgetPriority,omitempty" yaml:"budgetPriority"`
TrimDirection *string `json:"trimDirection,omitempty" yaml:"trimDirection"`
InsertionType *string `json:"insertionType,omitempty" yaml:"insertionType"`
MaximumTrimType *string `json:"maximumTrimType,omitempty" yaml:"maximumTrimType"`
InsertionPosition *int `json:"insertionPosition,omitempty" yaml:"insertionPosition"`
AllowInnerInsertion *bool `json:"allowInnerInsertion,omitempty" yaml:"allowInnerInsertion"`
AllowInsertionInside *bool `json:"allowInsertionInside,omitempty" yaml:"allowInsertionInside"`
Force *bool `json:"forced,omitempty" yaml:"forced"`
}
type ContextEntry struct {
Text *string `json:"text,omitempty" yaml:"text"`
ContextCfg *ContextConfig `json:"contextConfig,omitempty" yaml:"config"`
Tokens *gpt_bpe.Tokens `json:"-" yaml:"-"`
Label string `json:"-" yaml:"-"`
MatchIndexes []map[string][][]int `json:"-" yaml:"-"`
Index uint `json:"-" yaml:"-"`
}
type ContextEntries []ContextEntry
func (contexts ContextEntries) Len() int {
return len(contexts)
}
func (contexts ContextEntries) Swap(i, j int) {
contexts[i], contexts[j] = contexts[j], contexts[i]
}
func (contexts ContextEntries) Less(i, j int) bool {
if *contexts[i].ContextCfg.BudgetPriority <
*contexts[j].ContextCfg.BudgetPriority {
return true
} else if *contexts[i].ContextCfg.BudgetPriority ==
*contexts[j].ContextCfg.BudgetPriority {
return contexts[i].Index > contexts[j].Index
}
return false
}
type MaxTrimType uint
const (
TrimSentences MaxTrimType = iota
TrimNewlines MaxTrimType = iota
TrimTokens MaxTrimType = iota
)
type LorebookEntry struct {
Text *string `json:"text,omitempty" yaml:"text"`
ContextCfg *ContextConfig `json:"contextConfig,omitempty" yaml:"contextConfig"`
LastUpdatedAt *int `json:"lastUpdatedAt,omitempty" yaml:"lastUpdatedAt"`
DisplayName *string `json:"displayName,omitempty" yaml:"displayName"`
Keys *[]string `json:"keys,omitempty" yaml:"keys"`
SearchRange *int `json:"searchRange,omitempty" yaml:"searchRange"`
Enabled *bool `json:"enabled,omitempty" yaml:"enabled"`
ForceActivation *bool `json:"forceActivation,omitempty" yaml:"forceActivation"`
KeyRelative *bool `json:"keyRelative,omitempty" yaml:"keyRelative"`
NonStoryActivatable *bool `json:"nonStoryActivatable,omitempty" yaml:"nonStoryActivatable"`
CategoryId *string `json:"category,omitempty" yaml:"categoryId"`
LoreBiasGroups *structs.BiasGroups `json:"loreBiasGroups,omitempty" yaml:"loreBiasGroups"`
KeysRegex []*regexp.Regexp `json:"-" yaml:"-"`
}
type Category struct {
Name *string `json:"name,omitempty" yaml:"name"`
Id *string `json:"id,omitempty" yaml:"id"`
Enabled *bool `json:"enabled,omitempty" yaml:"enabled"`
CreateSubcontext *bool `json:"createSubcontext,omitempty" yaml:"createSubcontext"`
SubcontextSettings *LorebookEntry `json:"subcontextSettings,omitempty" yaml:"subcontextSettings"`
UseCategoryDefaults *bool `json:"useCategoryDefaults,omitempty" yaml:"useCategoryDefaults"`
CategoryDefaults *LorebookEntry `json:"categoryDefaults,omitempty" yaml:"categoryDefaults"`
CategoryBiasGroups *structs.BiasGroups `json:"categoryBiasGroups,omitempty" yaml:"categoryBiasGroups"`
}
type LorebookSettings struct {
OrderByKeyLocations bool `json:"orderByKeyLocations" yaml:"orderByKeyLocations"`
}
type Lorebook struct {
Version int `json:"lorebookVersion"`
Entries []LorebookEntry `json:"entries"`
Settings LorebookSettings `json:"settings"`
Categories []Category `json:"categories"`
}
func (lorebook *Lorebook) ToPlaintext() string {
entryStrings := make([]string, 0)
for entryIdx := range lorebook.Entries {
entry := lorebook.Entries[entryIdx]
if entry.Text != nil && (entry.Enabled == nil || *entry.Enabled) {
normalizedDisplayName := strings.Replace(*entry.DisplayName,
":", " -", -1)
entryStrings = append(entryStrings,
strings.Join([]string{normalizedDisplayName, *entry.Text},
":\n"))
}
}
return strings.Join(entryStrings, "\n***\n")
}
func (lorebook *Lorebook) ToPlaintextFile(path string) {
output := lorebook.ToPlaintext()
if err := ioutil.WriteFile(path, []byte(output), 0755); err != nil {
log.Fatal(err)
}
}
func (lorebook *Lorebook) ToFile(path string) {
var err error
outputBytes, err := json.MarshalIndent(lorebook, "", " ")
if err != nil {
log.Fatal(err)
}
if ioutil.WriteFile(path, outputBytes, 0755) != nil {
log.Fatal(err)
}
}
func (defaults *LorebookEntry) RealizeDefaults(entry *LorebookEntry) {
fields := reflect.TypeOf(*defaults)
for field := 0; field < fields.NumField(); field++ {
fieldValues := reflect.ValueOf(defaults).Elem().Field(field)
if fieldValues.IsNil() {
continue
}
entryValue := reflect.ValueOf(entry).Elem().Field(field)
if entryValue.IsNil() {
entryValue.Set(fieldValues)
}
}
}
type ScenarioAIModule struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
RemoteID string `json:"remoteId"`
}
type ScenarioSettings struct {
Parameters *novelai_api.NaiGenerateParams `json:"parameters,omitempty"`
TrimResponses *bool `json:"trimResponses,omitempty"`
BanBrackets *bool `json:"banBrackets,omitempty"`
Prefix *string `json:"prefix,omitempty"`
ScenarioAIModule *ScenarioAIModule `json:"-,omitempty"`
Model *string `json:"model,omitempty"`
}
type Scenario struct {
ScenarioVersion int `json:"scenarioVersion"`
Title string `json:"title"`
Author string `json:"author"`
Description string `json:"description"`
Prompt string `json:"prompt"`
Tags []string `json:"tags,omitempty"`
Context ContextEntries `json:"context,omitempty"`
Settings ScenarioSettings `json:"settings,omitempty"`
Lorebook Lorebook `json:"lorebook,omitempty"`
Placeholders []Placeholder `json:"placeholders,omitempty"`
StoryContextConfig *ContextConfig `json:"storyContextConfig,omitempty"`
Biases *structs.BiasGroups `json:"-" yaml:"biases"`
AIModule *aimodules.AIModule `json:"-"`
PlaceholderMap Placeholders `json:"-"`
Encoder *gpt_bpe.GPTEncoder `json:"-"`
}
type ContextReportEntry struct {
Label string `json:"label"`
InsertionPos int `json:"insertion_pos"`
TokenCount int `json:"token_count"`
TokensInserted int `json:"tokens_inserted"`
BudgetRemaining int `json:"budget_remaining"`
ReservedRemaining int `json:"reserved_remaining"`
MatchIndexes []map[string][][]int `json:"matches"`
Forced bool `json:"forced"`
}
type ContextReport []ContextReportEntry
func createLorebookRegexp(key string) *regexp.Regexp {
keyRegex, err := regexp.Compile("(?i)(^|\\W)(" + key + ")($|\\W)")
if err != nil {
log.Fatal(err)
}
return keyRegex
}
func (lorebook *Lorebook) ResolveContexts(placeholders *Placeholders,
contexts *ContextEntries) (entries ContextEntries) {
beginIdx := len(*contexts)
for loreIdx := range lorebook.Entries {
lorebookEntry := lorebook.Entries[loreIdx]
if !*lorebookEntry.Enabled {
continue
}
keys := lorebookEntry.Keys
keysRegex := lorebookEntry.KeysRegex
indexes := make([]map[string][][]int, 0)
searchRange := lorebookEntry.SearchRange
for keyIdx := range keysRegex {
var keyRegex *regexp.Regexp
resolvedKey := placeholders.ReplacePlaceholders(
(*keys)[keyIdx])
if resolvedKey != (*keys)[keyIdx] {
(*keys)[keyIdx] = resolvedKey
keyRegex = createLorebookRegexp(resolvedKey)
} else {
keyRegex = keysRegex[keyIdx]
}
for ctxIdx := range *contexts {
searchText := *(*contexts)[ctxIdx].Text
searchLen := len(searchText) - *searchRange
if searchLen > 0 {
searchText = searchText[searchLen:]
}
ctxMatches := keyRegex.FindAllStringIndex(searchText, -1)
keyMatches := make(map[string][][]int, 0)
if searchLen > 0 {
for ctxMatchIdx := range ctxMatches {
ctxMatches[ctxMatchIdx][0] =
ctxMatches[ctxMatchIdx][0] + searchLen
ctxMatches[ctxMatchIdx][1] =
ctxMatches[ctxMatchIdx][1] + searchLen
}
}
if len(ctxMatches) > 0 {
keyMatches[(*keys)[keyIdx]] = append(
keyMatches[(*keys)[keyIdx]], ctxMatches...)
}
if len(keyMatches) > 0 {
indexes = append(indexes, keyMatches)
}
}
}
resolvedText := placeholders.ReplacePlaceholders(
*lorebookEntry.Text)
label := placeholders.ReplacePlaceholders(
*lorebookEntry.DisplayName)
if len(indexes) > 0 || *lorebookEntry.ForceActivation {
entry := ContextEntry{
Text: &resolvedText,
ContextCfg: lorebookEntry.ContextCfg,
Label: label,
MatchIndexes: indexes,
Index: uint(beginIdx + loreIdx),
}
entries = append(entries, entry)
}
}
return entries
}
/*
Create context list
Add story to context list
Add story context array to list
Add active lorebook entries to the context list
Add active ephemeral entries to the context list
Add cascading lorebook entries to the context list
Determine token lengths of each entry
Determine reserved tokens for each entry
Sort context list by insertion order
For each entry in the context list
trim entry
insert entry
reduce reserved tokens
*/
func CreateDefaultContextConfig() ContextConfig {
reservedTokens := 512
insertionPosition := -1
tokenBudget := 2048
budgetPriority := 0
trimDirection := "trimTop"
insertionType := "newline"
maximumTrimType := "sentence"
suffix := "\n"
force := false
return ContextConfig{
Prefix: nil,
Suffix: &suffix,
ReservedTokens: &reservedTokens,
InsertionPosition: &insertionPosition,
TokenBudget: &tokenBudget,
BudgetPriority: &budgetPriority,
TrimDirection: &trimDirection,
InsertionType: &insertionType,
MaximumTrimType: &maximumTrimType,
Force: &force,
}
}
func (scenario Scenario) createStoryContext(story string) ContextEntry {
var storyCfg ContextConfig
if scenario.StoryContextConfig != nil {
force := true
storyCfg = *scenario.StoryContextConfig
storyCfg.Force = &force
} else {
storyCfg = CreateDefaultContextConfig()
*storyCfg.Force = true
}
return ContextEntry{
Text: &story,
ContextCfg: &storyCfg,
Label: "Story",
Index: 0,
}
}
func (contexts *ContextEntries) getReserved() (
reserved ContextEntries) {
for ctxIdx := range *contexts {
ctx := (*contexts)[ctxIdx]
if *ctx.ContextCfg.ReservedTokens > 0 {
reserved = append(reserved, ctx)
}
}
sort.Sort(sort.Reverse(reserved))
return reserved
}
func (contexts *ContextEntries) ApplyTokenizer(tokenizer *gpt_bpe.GPTEncoder) {
for idx := range *contexts {
ctx := (*contexts)[idx]
prefix := ""
if ctx.ContextCfg.Prefix != nil {
prefix = *ctx.ContextCfg.Prefix
}
suffix := ""
if ctx.ContextCfg.Suffix != nil {
prefix = *ctx.ContextCfg.Suffix
}
bracketedText := prefix + *ctx.Text + suffix
ctx.Tokens = tokenizer.Encode(&bracketedText)
(*contexts)[idx] = ctx
}
}
func (context *ContextEntry) Clone() ContextEntry {
text := *context.Text
config := *context.ContextCfg
return ContextEntry{
Text: &text,
ContextCfg: &config,
Index: context.Index,
Label: context.Label,
MatchIndexes: context.MatchIndexes,
}
}
func (context *ContextEntry) getTrimDirection() gpt_bpe.TrimDirection {
switch *context.ContextCfg.TrimDirection {
case "trimTop":
return gpt_bpe.TrimTop
case "trimBottom":
return gpt_bpe.TrimBottom
default:
return gpt_bpe.TrimNone
}
}
func (context *ContextEntry) getMaxTrimType() MaxTrimType {
if context.ContextCfg.MaximumTrimType == nil {
return TrimNewlines
}
switch *context.ContextCfg.MaximumTrimType {
case "sentence":
return TrimSentences
case "newline":
return TrimNewlines
case "token":
return TrimTokens
default:
return TrimNewlines
}
}
func assertThresholdOrEmpty(tokens *gpt_bpe.Tokens, ratio float32, target int) {
if float32(len(*tokens))*ratio >= float32(target) {
*tokens = make(gpt_bpe.Tokens, 0)
}
}
func (context *ContextEntry) ResolveTrim(encoder *gpt_bpe.GPTEncoder, budget int) (
trimmedTokens *gpt_bpe.Tokens) {
target := 0
tokens := encoder.Encode(context.Text)
numTokens := len(*tokens)
projected := budget - numTokens
if projected > *context.ContextCfg.TokenBudget {
target = *context.ContextCfg.TokenBudget
} else if projected >= 0 {
// We have enough to fit this into the budget.
target = numTokens
} else {
target = budget
}
trimDirection := context.getTrimDirection()
maxTrimType := context.getMaxTrimType()
// First, try newline trimming.
trimmedTokens, _ = encoder.TrimNewlines(tokens, trimDirection, uint(target))
assertThresholdOrEmpty(trimmedTokens, 0.3, target)
// If that fails, try trimming the sentences.
if len(*trimmedTokens) == 0 && maxTrimType >= TrimSentences {
trimmedTokens, _ = encoder.TrimSentences(context.Tokens,
trimDirection, uint(target))
}
// And if that also fails, trim tokens as a last resort.
assertThresholdOrEmpty(trimmedTokens, 0.3, target)
if len(*trimmedTokens) == 0 && maxTrimType == TrimTokens {
toTrim := *context.Tokens
switch trimDirection {
case gpt_bpe.TrimTop:
toTrim = toTrim[numTokens-target:]
case gpt_bpe.TrimBottom:
toTrim = toTrim[:target]
default:
toTrim = *trimmedTokens
}
trimmedTokens = &toTrim
}
return trimmedTokens
}
type ContextBuilder struct {
Encoder *gpt_bpe.GPTEncoder
Contexts ContextEntries
Placeholders *Placeholders
}
func NewContextBuilder(encoder *gpt_bpe.GPTEncoder) *ContextBuilder {
contexts := make(ContextEntries, 0)
placeholders := make(Placeholders, 0)
return &ContextBuilder{
Encoder: encoder,
Contexts: contexts,
Placeholders: &placeholders,
}
}
func (cb *ContextBuilder) PrependContext(context *ContextEntry) {
cb.Contexts = append(ContextEntries{context.Clone()}, cb.Contexts...)
}
func (cb *ContextBuilder) AppendContext(context *ContextEntry) {
cb.Contexts = append(cb.Contexts, context.Clone())
}
func (cb *ContextBuilder) AppendContexts(contexts *ContextEntries) {
for idx := range *contexts {
contextToAppend := (*contexts)[idx]
cb.AppendContext(&contextToAppend)
}
}
func (cb *ContextBuilder) ResolvePlaceholder(context *ContextEntry) {
resolved := cb.Placeholders.ReplacePlaceholders(*context.Text)
if resolved != *context.Text {
context.Text = &resolved
}
}
func (cb *ContextBuilder) ResolvePlaceholders() {
}
func (cb *ContextBuilder) Realize(budget int) (string, ContextReport) {
cb.Contexts.ApplyTokenizer(cb.Encoder)
reservations := 0
reservedContexts := cb.Contexts.getReserved()
for ctxIdx := range reservedContexts {
ctx := reservedContexts[ctxIdx]
reservedTokens := ctx.ContextCfg.ReservedTokens
szTokens := len(*ctx.Tokens)
if szTokens < *reservedTokens {
budget -= szTokens
reservations += szTokens
} else {
budget -= *reservedTokens
reservations += *reservedTokens
}
}
sort.Sort(sort.Reverse(cb.Contexts))
contextReport := make(ContextReport, 0)
newContexts := make([]string, 0)
for ctxIdx := range cb.Contexts {
ctx := cb.Contexts[ctxIdx]
reserved := 0
if *ctx.ContextCfg.ReservedTokens > 0 {
if len(*ctx.Tokens) > *ctx.ContextCfg.ReservedTokens {
reserved = *ctx.ContextCfg.ReservedTokens
} else {
reserved = len(*ctx.Tokens)
}
}
trimmedTokens := ctx.ResolveTrim(cb.Encoder, budget+reserved)
numTokens := len(*trimmedTokens)
budget -= numTokens - reserved
reservations -= reserved
contextText := strings.Split(cb.Encoder.Decode(trimmedTokens),
"\n")
ctxInsertion := ctx.ContextCfg.InsertionPosition
if numTokens == 0 {
continue
} else {
contextReport = append(contextReport, ContextReportEntry{
Label: ctx.Label,
InsertionPos: *ctx.ContextCfg.InsertionPosition,
TokenCount: len(*ctx.Tokens),
TokensInserted: numTokens,
BudgetRemaining: budget,
ReservedRemaining: reservations,
MatchIndexes: ctx.MatchIndexes,
Forced: *ctx.ContextCfg.Force,
})
}
var before []string
var after []string
if *ctxInsertion < 0 {
*ctxInsertion += 1
if len(newContexts)+*ctxInsertion >= 0 {
before = newContexts[0 : len(newContexts)+*ctxInsertion]
after = newContexts[len(newContexts)+*ctxInsertion:]
} else {
before = []string{}
after = newContexts[0:]
}
} else {
before = newContexts[0:*ctxInsertion]
after = newContexts[*ctxInsertion:]
}
newContexts = make([]string, 0)
for bIdx := range before {
newContexts = append(newContexts, before[bIdx])
}
for cIdx := range contextText {
newContexts = append(newContexts, contextText[cIdx])
}
for aIdx := range after {
newContexts = append(newContexts, after[aIdx])
}
/* fmt.Printf("PRIORITY: %4v RESERVATIONS: %4v, RESERVED: %4v ACTUAL: %4v TRIMMED: %4v LEFT: %4v LABEL: %15v INSERTION_POS: %4v TRIM_TYPE: %8v TRIM_DIRECTION: %10v\n",
contexts[ctxIdx].ContextCfg.BudgetPriority,
reservations,
reserved,
len(*contexts[ctxIdx].Tokens),
numTokens,
budget,
contexts[ctxIdx].Label,
contexts[ctxIdx].ContextCfg.InsertionPosition,
contexts[ctxIdx].ContextCfg.MaximumTrimType,
contexts[ctxIdx].ContextCfg.TrimDirection)
for ctxTextIdx := range(newContexts) {
fmt.Printf("resolvedText: %v\n", newContexts[ctxTextIdx])
} */
}
return strings.Join(newContexts, "\n"), contextReport
}
func (scenario Scenario) GenerateContext(story string, budget int) (
newContext string,
ctxReport ContextReport) {
cb := NewContextBuilder(scenario.Encoder)
cb.Placeholders = &scenario.PlaceholderMap
storyEntry := scenario.createStoryContext(story)
contexts := ContextEntries{storyEntry}
lorebookContexts := scenario.Lorebook.ResolveContexts(cb.Placeholders,
&contexts)
for ctxIdx := range contexts {
resolved := scenario.PlaceholderMap.ReplacePlaceholders(
*contexts[ctxIdx].Text)
contexts[ctxIdx].Text = &resolved
}
cb.AppendContexts(&contexts)
cb.AppendContexts(&scenario.Context)
cb.AppendContexts(&lorebookContexts)
budget -= int(*scenario.Settings.Parameters.MaxLength)
// Reserve 20 tokens if we're using an AI module.
if scenario.Settings.Parameters.Prefix == nil ||
*scenario.Settings.Parameters.Prefix != "vanilla" {
budget -= 20
}
// Reserve 20 tokens if we're completing sentences.
if scenario.Settings.Parameters.GenerateUntilSentence == nil ||
*scenario.Settings.Parameters.GenerateUntilSentence {
budget -= 20
}
return cb.Realize(budget)
}
var placeholderDefRegex = regexp.MustCompile(
"\\$\\{(?P<var>[\\p{L}|0-9|_|\\-|#|(|)]+)\\[(?P<default>[^\\]]+)\\]:(?P<description>[^\\}]+)\\}")
var placeholderTableRegex = regexp.MustCompile(
"(?P<var>[\\p{L}|0-9|#|_|\\-|(|)]+)\\[(?P<default>[^\\]]+)\\]:(?P<description>[^\\n]+)\n")
var placeholderVarRegex = regexp.MustCompile(
"\\$\\{(?P<var>[\\p{L}|0-9|#|_|\\-|(|)]+)(\\}|\\[[^\\}]+\\})")
type Placeholder struct {
Variable string `json:"key" yaml:"key"`
Defaults string `json:"defaultValue" yaml:"default"`
Description string `json:"description" yaml:"description"`
LongDescription string `json:"longDescription" yaml:"longDescription"`
Value string `json:"-" yaml:"value"`
}
type Placeholders map[string]*Placeholder
func getPlaceholderTable(text string) (trimmed string, tableBlock string) {
trimmed = text
if len(text) > 6 && text[0:3] == "%{\n" {
blockEnd := strings.Index(text, "\n}\n")
if blockEnd != -1 {
tableBlock = text[3 : blockEnd+1]
trimmed = text[blockEnd+3:]
}
}
return trimmed, tableBlock
}
func extractPlaceholderDefs(rgx *regexp.Regexp, text string) (variables Placeholders) {
variables = make(Placeholders, 0)
defs := rgx.FindAllString(text, -1)
for defIdx := range defs {
fields := rgx.FindStringSubmatch(defs[defIdx])
placeholder := Placeholder{Variable: fields[1],
Defaults: fields[2],
Description: fields[3],
Value: fields[2]}
if len(fields) == 5 {
placeholder.LongDescription = fields[4]
}
variables[fields[1]] = &placeholder
}
return variables
}
func (target *Placeholders) UpdateValues(kvs map[string]string) {
for k, v := range kvs {
if entry, ok := (*target)[k]; ok {
entry.Value = v
(*target)[k] = entry
} else {
(*target)[k] = &Placeholder{
Variable: k,
Value: v,
}
}
}
}
func DiscoverPlaceholderTable(text string) Placeholders {
_, block := getPlaceholderTable(text)
return extractPlaceholderDefs(placeholderTableRegex, block)
}
func (variables Placeholders) ReplacePlaceholders(text string) (replaced string) {
text, _ = getPlaceholderTable(text)
for {
match := placeholderVarRegex.FindStringIndex(text)
if match == nil {
break
}
key := placeholderVarRegex.FindStringSubmatch(text[match[0]:match[1]])
if placeholder, ok := variables[key[1]]; ok {
replaced += text[:match[0]] + placeholder.Value
} else {
replaced += text[:match[1]]
}
text = text[match[1]:]
}
replaced += text
return replaced
}
func DiscoverPlaceholderDefs(text string) Placeholders {
return extractPlaceholderDefs(placeholderDefRegex, text)
}
func (accPlaceholders Placeholders) Realize() {
for varName := range accPlaceholders {
accPlaceholders[varName].Variable = varName
}
}
func (accPlaceholders Placeholders) Add(new Placeholders) {
for varName := range new {
new.Realize()
if _, exists := accPlaceholders[varName]; exists {
log.Printf("WARNING: Previous placeholder definition for"+
" %s exists. Overwriting!", varName)
}
accPlaceholders[varName] = new[varName]
}
}
func (target *Placeholders) merge(source Placeholders) {
for k, v := range source {
(*target)[k] = v
}
}
func (scenario *Scenario) GetPlaceholderDefs() (defs Placeholders) {
defs = make(Placeholders, 0)
for placeholderIdx := range scenario.Placeholders {
placeholder := scenario.Placeholders[placeholderIdx]
placeholder.Value = placeholder.Defaults
defs[placeholder.Variable] = &placeholder
}
defs.merge(DiscoverPlaceholderTable(scenario.Prompt))
defs.merge(DiscoverPlaceholderDefs(scenario.Prompt))
for ctxIdx := range scenario.Context {
defs.merge(DiscoverPlaceholderDefs(*scenario.Context[ctxIdx].Text))
}
for lbkIdx := range scenario.Lorebook.Entries {
defs.merge(DiscoverPlaceholderDefs(
*scenario.Lorebook.Entries[lbkIdx].Text))
}
return defs
}
func (scenario *Scenario) SetMemory(memory string) {
scenario.Context[0].Text = &memory
scenario.Context[0].Tokens = scenario.Encoder.Encode(&memory)
}
func (scenario *Scenario) SetAuthorsNote(an string) {
scenario.Context[1].Text = &an
scenario.Context[1].Tokens = scenario.Encoder.Encode(&an)
}
func (scenario *Scenario) GetEncoder() *gpt_bpe.GPTEncoder {
if scenario.Settings.Model != nil {
return novelai_api.GetEncoderByModel(*scenario.Settings.Model)
} else {
return &gpt_bpe.GPT2Encoder
}
}
func ScenarioFromSpec(prompt string, memory string, an string,
model string) (scenario Scenario) {
memoryCfg := CreateDefaultContextConfig()
*memoryCfg.BudgetPriority = 800
*memoryCfg.InsertionPosition = 0
*memoryCfg.Force = true
anCfg := CreateDefaultContextConfig()
*anCfg.ReservedTokens = 2048
*anCfg.BudgetPriority = -400
*anCfg.InsertionPosition = -4
*anCfg.Force = true
scenario.Prompt = prompt
scenario.Encoder = novelai_api.GetEncoderByModel(model)
scenario.Context = ContextEntries{
{Text: &memory,
ContextCfg: &memoryCfg,
Tokens: scenario.Encoder.Encode(&memory),
Label: "Memory",
Index: 1},
{Text: &an,
ContextCfg: &anCfg,
Tokens: scenario.Encoder.Encode(&an),
Label: "A/N",
Index: 2}}
scenario.PlaceholderMap = scenario.GetPlaceholderDefs()
scenario.Settings.Parameters = &novelai_api.NaiGenerateParams{}
return scenario
}
func ScenarioFromFile(path string) (scenario Scenario,
err error) {
scenarioBytes, err := ioutil.ReadFile(path)
if err != nil {
return scenario, err
}
err = json.Unmarshal(scenarioBytes, &scenario)
if err != nil {
return scenario, err
}
scenario.Encoder = scenario.GetEncoder()
for ctxIdx := range scenario.Context {
ctx := scenario.Context[ctxIdx]
toEncode := *ctx.ContextCfg.Prefix +
*ctx.Text
ctx.Tokens = scenario.Encoder.Encode(&toEncode)
scenario.Context[ctxIdx] = ctx
}
ctxCfgForce := true
scenario.Context[0].Label = "Memory"
scenario.Context[0].Index = 1
scenario.Context[0].ContextCfg.Force = &ctxCfgForce
scenario.Context[1].Label = "A/N"
scenario.Context[1].Index = 2
scenario.Context[1].ContextCfg.Force = &ctxCfgForce
for loreIdx := range scenario.Lorebook.Entries {
loreEntry := scenario.Lorebook.Entries[loreIdx]
loreEntry.ContextCfg.Force = loreEntry.ForceActivation
for keyIdx := range *loreEntry.Keys {
key := (*loreEntry.Keys)[keyIdx]
keyRegex := createLorebookRegexp(key)
loreEntry.KeysRegex = append(loreEntry.KeysRegex, keyRegex)
}
scenario.Lorebook.Entries[loreIdx] = loreEntry
}
if scenario.Settings.ScenarioAIModule != nil {
aimodule := aimodules.AIModuleFromArgs(
scenario.Settings.ScenarioAIModule.Id,
scenario.Settings.ScenarioAIModule.Name,
scenario.Settings.ScenarioAIModule.Description)
scenario.AIModule = &aimodule
}
scenario.Settings.Parameters.CoerceDefaults()
scenario.Settings.Parameters.Prefix = scenario.Settings.Prefix
scenario.Settings.Parameters.BanBrackets = scenario.Settings.BanBrackets
scenario.PlaceholderMap = scenario.GetPlaceholderDefs()
return scenario, err
}