OpenAI chat bot
Key RequiredUniversal

OpenAI chat bot

Universal ScriptSebySeby 24.2K views 586 executesUpdated Jul 7, 2022
5.0/ 5(4 votes)
40100% Positive
Lua Script•101 lines (3159 chars)
repeat wait() until game:IsLoaded();

-- // SETTINGS \\ --

local SECRET_KEY = "secret key here"; --https://beta.openai.com/account/api-keys
local CLOSE_RANGE_ONLY = false;

_G.MESSAGE_SETTINGS = {
	["MINIMUM_CHARACTERS"] = 15,
	["MAXIMUM_CHARACTERS"] = 50,
	["MAXIMUM_STUDS"] = 15,
};

_G.WHITELISTED = { --Only works if CLOSE_RANGE_ONLY is disabled
	["seem2006"] = true,
};

_G.BLACKLISTED = { --Only works if CLOSE_RANGE_ONLY is enabled
	["Builderman"] = true,
};

-- // DO NOT CHANGE BELOW \\ --

if _G.OpenAI or SECRET_KEY == "secret key here" then return end;

_G.OpenAI = true;

local ReplicatedStorage = game:GetService("ReplicatedStorage");
local Players = game:GetService("Players");
local HttpService = game:GetService("HttpService");
local LocalPlayer = Players.LocalPlayer;
local SayMessageRequest = ReplicatedStorage:WaitForChild("DefaultChatSystemChatEvents"):WaitForChild("SayMessageRequest");
local Debounce = false;

local function MakeRequest(Prompt)
	return syn.request({
		Url = "https://api.openai.com/v1/completions", 
		Method = "POST",
		Headers = {
			["Content-Type"] = "application/json",
			["Authorization"] =  "Bearer " .. SECRET_KEY
		},
		Body = HttpService:JSONEncode({
			model = "text-davinci-002",
			prompt = Prompt,
			temperature = 0.9,
  			max_tokens = 47, --150
  			top_p = 1,
  			frequency_penalty = 0.0,
  			presence_penalty = 0.6,
  			stop = {" Human:", " AI:"}
		});
	});
end

local function ConnectFunction(Instance)
	Instance.Chatted:Connect(function(Message)
		local Character = Instance.Character;

		if not Character or not Character:FindFirstChild("Head") or not LocalPlayer.Character or not LocalPlayer.Character:FindFirstChild("Head") then return end;
		if Debounce or #Message < _G.MESSAGE_SETTINGS["MINIMUM_CHARACTERS"] or #Message > _G.MESSAGE_SETTINGS["MAXIMUM_CHARACTERS"] then return end;
		if CLOSE_RANGE_ONLY then if _G.BLACKLISTED[Instance.Name] or (Character.Head.Position - LocalPlayer.Character.Head.Position).Magnitude > _G.MESSAGE_SETTINGS["MAXIMUM_STUDS"] then return end elseif not _G.WHITELISTED[Instance.Name] then return end;

		Debounce = true;

		local HttpRequest = MakeRequest("Human: " .. Message .. "\n\nAI:");
		local Response = Instance.Name .. ": " .. string.gsub(string.sub(HttpService:JSONDecode(HttpRequest["Body"]).choices[1].text, 2), "[%p%c]", "");

		if #Response < 128 then --200
			SayMessageRequest:FireServer(Response, "All");
			wait(5);
			Debounce = false;
		else
			warn("Response (> 128): " .. Response);
			if #Response - 128 < 128 then
				SayMessageRequest:FireServer(string.sub(Response, 1, 128), "All");
				delay(3, function()
					SayMessageRequest:FireServer(string.sub(Response, 129), "All");
					wait(5);
					Debounce = false;
				end)	
			else
				SayMessageRequest:FireServer("Sorry but the answer was too big, please try again.", "All");
				wait(2.5);
				Debounce = false;
			end
		end
	end)
end

for i,v in pairs(Players:GetPlayers()) do
	if v.Name ~= Players.LocalPlayer.Name then
		ConnectFunction(v);
	end
end

Players.PlayerAdded:Connect(function(Player)
	ConnectFunction(Player);
end)

warn("Script has been executed with success.");

Features & Information

how to use: - create an account in https://openai.com/api/ - go to Manage Account -> API Key (https://beta.openai.com/account/api-keys), copy the api key and paste it inside the SECRET_KEY variable - write the name of people you want the bot to reply to, inside the WHITELISTED table (or simply enable the CLOSE_RANGE_ONLY variable if you want it to answer everyone around you) - enjoy notes: - the bot will only reply to texts that has > 15 characters and < 50 characters (you can change these settings in the script, inside the MESSAGE_SETTINGS table) - there's a cooldown between replies in order to not spam the API, 5 seconds - if the response is > 200 characters (roblox chat limit) then the text will be cut in 2 parts Showcase: https://medal.tv/games/roblox/clips/qCW0QQDYnfptO/d1337r0YtsZw?invite=cr-MSw3ckssMTY4NjEyMzcs Credits to BillsTheGod on v3rmillion Original thread: https://v3rmillion.net/showthread.php?tid=1176455 If you have a v3rmillion account, consider liking the thread post so that the user gets the 500 likes badge
#ai#chat#bot#openai#fe

Community Comments (4)

OlimPatherOlimPatherJul 7, 2022(edited)

No way! This is awesome!

AlperSocialAlperSocialJul 10, 2022(edited)

fire

upseguestupseguestJul 15, 2022(edited)

i get kicked with 'Player service requires player disconnect" why?????

alejosdaniel98alejosdaniel98Sep 8, 2022(edited)

Does this still work?! I saw someone using an AI Bot earlier in PLS DONATE but this won't work. They told me to look up "OPEN AI" so that led me here.

More Scripts for Universal Scripts