Path: blob/main/userscripts/kaipreset_substitution.lua
473 views
-- Word substitution1-- Performs a search-and-replace on the AI's output.23-- This file is part of KoboldAI.4--5-- KoboldAI is free software: you can redistribute it and/or modify6-- it under the terms of the GNU Affero General Public License as published by7-- the Free Software Foundation, either version 3 of the License, or8-- (at your option) any later version.9--10-- This program is distributed in the hope that it will be useful,11-- but WITHOUT ANY WARRANTY; without even the implied warranty of12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13-- GNU Affero General Public License for more details.14--15-- You should have received a copy of the GNU Affero General Public License16-- along with this program. If not, see <https://www.gnu.org/licenses/>.1718kobold = require("bridge")() -- This line is optional and is only for EmmyLua type annotations19local userscript = {} ---@class KoboldUserScript202122local example_config = [[;-- Substitution23;--24;-- This example config causes all occurrences of "Hello," (without the double25;-- quotes) to be replaced with "Goodbye," (without the double quotes) and26;-- all occurrences of "test" to be replaced with "****".27;--28;-- The strings are parsed as Lua strings, so the standard escape sequences \",29;-- \n, \\, and so on apply here as well.30;--31return {32{"Hello,", "Goodbye,"},33{"test", "****"},34}35]]3637-- If config file is empty, write example config38local f = kobold.get_config_file()39f:seek("set")40if f:read(1) == nil then41f:write(example_config)42end43f:seek("set")44example_config = nil4546-- Read config47local cfg, err = load(f:read("a"))48f:close()49if err ~= nil then50error(err)51end52cfg = cfg()535455function userscript.outmod()56for i, output in ipairs(kobold.outputs) do57for j, row in ipairs(cfg) do58output = output:gsub(row[1], row[2])59end60kobold.outputs[i] = output61end62end6364return userscript656667