Path: blob/main/userscripts/kaipreset_you_bias.lua
473 views
-- You bias1-- Makes the word "You" less (or more) common in character references2-- , optionally also between double quotes.3-- Only works with models with a tokenizer based on GPT-2, such as GPT-2,4-- GPT-Neo and GPT-J.56-- This file is part of KoboldAI.7--8-- KoboldAI is free software: you can redistribute it and/or modify9-- it under the terms of the GNU Affero General Public License as published by10-- the Free Software Foundation, either version 3 of the License, or11-- (at your option) any later version.12--13-- This program is distributed in the hope that it will be useful,14-- but WITHOUT ANY WARRANTY; without even the implied warranty of15-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16-- GNU Affero General Public License for more details.17--18-- You should have received a copy of the GNU Affero General Public License19-- along with this program. If not, see <https://www.gnu.org/licenses/>.2021kobold = require("bridge")() -- This line is optional and is only for EmmyLua type annotations22local userscript = {} ---@class KoboldUserScript232425local example_config = [[;-- You bias26;--27return {28bias = -7.0, -- Negative numbers make it less likely, positive numbers more, and -math.huge impossible29only_if_outside_double_quotes = true,30}31]]3233-- If config file is empty, write example config34local f = kobold.get_config_file()35f:seek("set")36if f:read(1) == nil then37f:write(example_config)38end39f:seek("set")40example_config = nil4142-- Read config43local cfg, err = load(f:read("a"))44f:close()45if err ~= nil then46error(err)47end48cfg = cfg()49if type(cfg.bias) ~= "number" then50error("`bias` must be a number")51elseif cfg.bias ~= cfg.bias or cfg.bias == math.huge then52error("`bias` can't be `nan` or `math.huge`")53end545556---@type table<integer, integer>57local you_tokens <const> = {345, 921, 1639, 5832, 7013, 36981}5859local genmod_run = false6061function userscript.genmod()62genmod_run = true63local context64if cfg.only_if_outside_double_quotes then65context = " " .. kobold.worldinfo:compute_context(kobold.submission, {})66end6768for i, generated_row in ipairs(kobold.generated) do69local should_bias = true7071if cfg.only_if_outside_double_quotes then72local str = context .. kobold.decode(generated_row)73local last_open_quote = 074local last_close_quote = 075local i = 076local j = 077while true do78i, j = str:find('"', j+1)79if i == nil then80break81end82if str:sub(i-1, i-1) == " " or str:sub(i-1, i-1) == "\n" then83last_open_quote = j84else85last_close_quote = j86end87end88if last_open_quote > last_close_quote then89should_bias = false90end91end9293if should_bias then94for k, v in ipairs(you_tokens) do95kobold.logits[i][v+1] = kobold.logits[i][v+1] + cfg.bias96end97end98end99end100101function userscript.outmod()102if not genmod_run then103warn("WARNING: Generation modifier was not executed, so this script has had no effect")104end105end106107return userscript108109110