Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
KoboldAI
GitHub Repository: KoboldAI/KoboldAI-Client
Path: blob/main/userscripts/kaipreset_substitution.lua
473 views
1
-- Word substitution
2
-- Performs a search-and-replace on the AI's output.
3
4
-- This file is part of KoboldAI.
5
--
6
-- KoboldAI is free software: you can redistribute it and/or modify
7
-- it under the terms of the GNU Affero General Public License as published by
8
-- the Free Software Foundation, either version 3 of the License, or
9
-- (at your option) any later version.
10
--
11
-- This program is distributed in the hope that it will be useful,
12
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
-- GNU Affero General Public License for more details.
15
--
16
-- You should have received a copy of the GNU Affero General Public License
17
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19
kobold = require("bridge")() -- This line is optional and is only for EmmyLua type annotations
20
local userscript = {} ---@class KoboldUserScript
21
22
23
local example_config = [[;-- Substitution
24
;--
25
;-- This example config causes all occurrences of "Hello," (without the double
26
;-- quotes) to be replaced with "Goodbye," (without the double quotes) and
27
;-- all occurrences of "test" to be replaced with "****".
28
;--
29
;-- The strings are parsed as Lua strings, so the standard escape sequences \",
30
;-- \n, \\, and so on apply here as well.
31
;--
32
return {
33
{"Hello,", "Goodbye,"},
34
{"test", "****"},
35
}
36
]]
37
38
-- If config file is empty, write example config
39
local f = kobold.get_config_file()
40
f:seek("set")
41
if f:read(1) == nil then
42
f:write(example_config)
43
end
44
f:seek("set")
45
example_config = nil
46
47
-- Read config
48
local cfg, err = load(f:read("a"))
49
f:close()
50
if err ~= nil then
51
error(err)
52
end
53
cfg = cfg()
54
55
56
function userscript.outmod()
57
for i, output in ipairs(kobold.outputs) do
58
for j, row in ipairs(cfg) do
59
output = output:gsub(row[1], row[2])
60
end
61
kobold.outputs[i] = output
62
end
63
end
64
65
return userscript
66
67