Path: blob/main/userscripts/examples/various_examples.lua
935 views
-- Example script1-- Description goes on2--[[subsequent lines including3in multiline comments]]45kobold = require("bridge")() -- This line is optional and is only for EmmyLua type annotations67-- You can import libraries that are in extern/lualibs/8local inspect = require("inspect")9local mt19937ar = require("mt19937ar")101112---@class KoboldUserScript13local userscript = {}141516local twister = mt19937ar.new()17local seed = math.random(0, 2147483647)1819local token_num = 020local lifetime_token_num = 02122-- This gets run when user submits a string to the AI (right after the input23-- formatting is applied but before the string is actually sent to the AI)24function userscript.inmod()25warn("\nINPUT MODIFIER")26token_num = 027twister:init_genrand(seed)28print("Submitted text: " .. kobold.submission) -- You can also write to kobold.submission to alter the user's input29print("top-p sampling value: " .. kobold.settings.settopp)30end3132-- This gets run every time the AI generates a token (before the token is33-- actually sampled, so this is where you can make certain tokens more likely34-- to appear than others)35function userscript.genmod()36warn("\nGENERATION MODIFIER")3738print("Tokens generated in the current generation: " .. token_num)39print("Tokens generated since this script started up: " .. lifetime_token_num)4041local r = twister:genrand_real3()42print("Setting top-p sampling value to " .. r)43kobold.settings.settopp = r4445local generated = {}46for sequence_number, tokens in ipairs(kobold.generated) do47generated[sequence_number] = kobold.decode(tokens)48end49print("Current generated strings: " .. inspect(generated))5051if token_num == math.floor(kobold.settings.genamt/2) then52print("\n\n\n\n\n\nMaking all subsequent tokens more likely to be exclamation marks...")53end54if token_num >= math.floor(kobold.settings.genamt/2) then55for i = 1, kobold.settings.numseqs do56kobold.logits[i][1] = 13.3757end58end5960token_num = token_num + 161lifetime_token_num = lifetime_token_num + 162end6364-- This gets run right before the output formatting is applied after generation65-- is finished66function userscript.outmod()67warn("\nOUTPUT MODIFIER")68for chunk in kobold.story:reverse_iter() do69print(chunk.num, chunk.content)70end71print("Wrapping first output in brackets")72kobold.outputs[1] = "[" .. kobold.outputs[1] .. "]"73end7475return userscript767778