BillsTheGod

Untitled

Jul 1st, 2022 (edited)
1,374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. -- made by BillsTheGod
  2. if _G.OpenAI then return end;
  3.  
  4. _G.OpenAI = true;
  5.  
  6. repeat wait() until game:IsLoaded();
  7.  
  8. -- // SETTINGS \\ --
  9.  
  10. local SECRET_KEY = "secret key here"; --https://beta.openai.com/account/api-keys
  11. local MESSAGE_SETTINGS = {
  12.     ["MINIMUM_CHARACTERS"] = 15,
  13.     ["MAXIMUM_CHARACTERS"] = 50,
  14. };
  15.  
  16. _G.WHITELISTED = {
  17.     ["seem2006"] = true,
  18. };
  19.  
  20. -- // DO NOT CHANGE BELOW \\ --
  21.  
  22. local ReplicatedStorage = game:GetService("ReplicatedStorage");
  23. local HttpService = game:GetService("HttpService");
  24. local Players = game:GetService("Players");
  25. local SayMessageRequest = ReplicatedStorage:WaitForChild("DefaultChatSystemChatEvents"):WaitForChild("SayMessageRequest");
  26. local Debounce = false;
  27.  
  28. local function MakeRequest(Prompt)
  29.     return syn.request({
  30.         Url = "https://api.openai.com/v1/completions",
  31.         Method = "POST",
  32.         Headers = {
  33.             ["Content-Type"] = "application/json",
  34.             ["Authorization"] =  "Bearer " .. SECRET_KEY
  35.         },
  36.         Body = HttpService:JSONEncode({
  37.             model = "text-davinci-002",
  38.             prompt = Prompt,
  39.             temperature = 0.9,
  40.             max_tokens = 47, --150
  41.             top_p = 1,
  42.             frequency_penalty = 0.0,
  43.             presence_penalty = 0.6,
  44.             stop = {" Human:", " AI:"}
  45.         });
  46.     });
  47. end
  48.  
  49. local function ConnectFunction(Instance)
  50.     Instance.Chatted:Connect(function(Message)
  51.         if not _G.WHITELISTED[Instance.Name] or Debounce or #Message < MESSAGE_SETTINGS["MINIMUM_CHARACTERS"] or #Message > MESSAGE_SETTINGS["MAXIMUM_CHARACTERS"] then return end;
  52.            
  53.         Debounce = true;
  54.  
  55.         local HttpRequest = MakeRequest("Human: " .. Message .. "\n\nAI:");
  56.         local Response = string.gsub(string.sub(HttpService:JSONDecode(HttpRequest["Body"]).choices[1].text, 2), "[%p%c]", "");
  57.  
  58.         if #Response < 200 then
  59.             SayMessageRequest:FireServer(Response, "All");
  60.             wait(5);
  61.             Debounce = false;
  62.         else
  63.             warn("Response (> 200): " .. Response);
  64.             SayMessageRequest:FireServer("Sorry I didn't understand your question very well.", "All");
  65.             wait(2.5);
  66.             Debounce = false;
  67.         end
  68.     end)
  69. end
  70.  
  71. for i,v in pairs(Players:GetPlayers()) do
  72.     if v.Name ~= Players.LocalPlayer.Name then
  73.         ConnectFunction(v);
  74.     end
  75. end
  76.  
  77. Players.PlayerAdded:Connect(function(Player)
  78.     ConnectFunction(Player);
  79. end)
  80.  
  81. warn("Script has been executed with success.");
Add Comment
Please, Sign In to add comment