Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
KoboldAI
GitHub Repository: KoboldAI/KoboldAI-Client
Path: blob/main/userscripts/kaipreset_you_bias.lua
473 views
1
-- You bias
2
-- Makes the word "You" less (or more) common in character references
3
-- , optionally also between double quotes.
4
-- Only works with models with a tokenizer based on GPT-2, such as GPT-2,
5
-- GPT-Neo and GPT-J.
6
7
-- This file is part of KoboldAI.
8
--
9
-- KoboldAI is free software: you can redistribute it and/or modify
10
-- it under the terms of the GNU Affero General Public License as published by
11
-- the Free Software Foundation, either version 3 of the License, or
12
-- (at your option) any later version.
13
--
14
-- This program is distributed in the hope that it will be useful,
15
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
16
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
-- GNU Affero General Public License for more details.
18
--
19
-- You should have received a copy of the GNU Affero General Public License
20
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22
kobold = require("bridge")() -- This line is optional and is only for EmmyLua type annotations
23
local userscript = {} ---@class KoboldUserScript
24
25
26
local example_config = [[;-- You bias
27
;--
28
return {
29
bias = -7.0, -- Negative numbers make it less likely, positive numbers more, and -math.huge impossible
30
only_if_outside_double_quotes = true,
31
}
32
]]
33
34
-- If config file is empty, write example config
35
local f = kobold.get_config_file()
36
f:seek("set")
37
if f:read(1) == nil then
38
f:write(example_config)
39
end
40
f:seek("set")
41
example_config = nil
42
43
-- Read config
44
local cfg, err = load(f:read("a"))
45
f:close()
46
if err ~= nil then
47
error(err)
48
end
49
cfg = cfg()
50
if type(cfg.bias) ~= "number" then
51
error("`bias` must be a number")
52
elseif cfg.bias ~= cfg.bias or cfg.bias == math.huge then
53
error("`bias` can't be `nan` or `math.huge`")
54
end
55
56
57
---@type table<integer, integer>
58
local you_tokens <const> = {345, 921, 1639, 5832, 7013, 36981}
59
60
local genmod_run = false
61
62
function userscript.genmod()
63
genmod_run = true
64
local context
65
if cfg.only_if_outside_double_quotes then
66
context = " " .. kobold.worldinfo:compute_context(kobold.submission, {})
67
end
68
69
for i, generated_row in ipairs(kobold.generated) do
70
local should_bias = true
71
72
if cfg.only_if_outside_double_quotes then
73
local str = context .. kobold.decode(generated_row)
74
local last_open_quote = 0
75
local last_close_quote = 0
76
local i = 0
77
local j = 0
78
while true do
79
i, j = str:find('"', j+1)
80
if i == nil then
81
break
82
end
83
if str:sub(i-1, i-1) == " " or str:sub(i-1, i-1) == "\n" then
84
last_open_quote = j
85
else
86
last_close_quote = j
87
end
88
end
89
if last_open_quote > last_close_quote then
90
should_bias = false
91
end
92
end
93
94
if should_bias then
95
for k, v in ipairs(you_tokens) do
96
kobold.logits[i][v+1] = kobold.logits[i][v+1] + cfg.bias
97
end
98
end
99
end
100
end
101
102
function userscript.outmod()
103
if not genmod_run then
104
warn("WARNING: Generation modifier was not executed, so this script has had no effect")
105
end
106
end
107
108
return userscript
109
110