-- the thing to fix the windos cmd thing --
os.execute ("chcp 65001 >nul")
-- json fix taken from selene and the others--
local function escape_json(str)
if not str then return "" end
str = str:gsub('\\', '\\\\')
str = str:gsub('"', '\\"')
str = str:gsub('\r', '')
str = str:gsub('\n', '\\n')
return str
end
-- we need to make a stop start thing for lua llike used in selene for later --
local function wait(sec)
local c = os.clock()
while os.clock() - c < sec do end
end
-- do not fuck with this part its coyed from selene --
local function lu_talk(chat_history)
local parts = {}
for i, msg in ipairs(chat_history) do
local safe_content = escape_json(msg.content)
parts[i] = string.format('{"role": "%s", "content": "%s"}', msg.role, safe_content)
end
local messages_string = table.concat(parts, ",")
return string.format('{"model": "gpt-oss:20b", "stream": false, "messages": [%s]}', messages_string)
end
-- tell it how to talk
local personality = [[ put it here]]
-- something --
local chat_history = {
{ role = "system", content = personality }
}
--taken from selene--
print ("type 'exit' to quit.\n")
-- the core of talking to ollama i dont know whats gooing on there--
while true do
io.write("> ")
io.flush()
local user_input = io.read()
if not user_input or user_input == "exit" then break end
if user_input ~= "" then
table.insert(chat_history, { role = "user", content = user_input })
local payload = lu_talk(chat_history)
local file = io.open("payload.json", "w")
file:write(payload)
file:close()
local handle = io.popen('curl.exe -s -X POST http://localhost:11434/v1/chat/completions -H "Content-Type: application/json" -d @payload.json')
local result = handle:read("*a")
handle:close()
local reply = result:match('"content"%s*:%s*"(.-)"')
if reply then
reply = reply:gsub('\\n', '\n'):gsub('\\"', '"'):gsub('\\\\', '\\')
print("\nLu > " .. reply .. "\n")
table.insert(chat_history, { role = "assistant", content = reply })
-- Typewriter Effect not by me
io.write("\n")
for i = 1, #reply do
io.write(reply:sub(i, i))
io.flush()
wait(0.025) -- Controls typing speed (lower = faster)
end
io.write("\n\n")
table.insert(chat_history, { role = "assistant", content = reply })
else
print("\n[error getting response - check if Ollama is running]\n")
end
end
end
To embed this project on your website, copy the following code and paste it into your website's HTML: