Thisco

ChatGPT API Script

Jan 30th, 2023
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1.  
  2. repeat wait() until game:IsLoaded();
  3.  
  4. -- // SETTINGS \\ --
  5.  
  6. local SECRET_KEY = "secret key here"; --https://beta.openai.com/account/api-keys
  7. local CLOSE_RANGE_ONLY = true;
  8.  
  9. _G.MESSAGE_SETTINGS = {
  10. ["MINIMUM_CHARACTERS"] = 15,
  11. ["MAXIMUM_CHARACTERS"] = 50,
  12. ["MAXIMUM_STUDS"] = 15,
  13. };
  14.  
  15. _G.WHITELISTED = { --Only works if CLOSE_RANGE_ONLY is disabled
  16. ["seem2006"] = true,
  17. };
  18.  
  19. _G.BLACKLISTED = { --Only works if CLOSE_RANGE_ONLY is enabled
  20. ["Builderman"] = true,
  21. };
  22.  
  23. -- // DO NOT CHANGE BELOW \\ --
  24.  
  25. if _G.OpenAI or SECRET_KEY == "secret key here" then return end;
  26.  
  27. _G.OpenAI = true;
  28.  
  29. local ReplicatedStorage = game:GetService("ReplicatedStorage");
  30. local Players = game:GetService("Players");
  31. local HttpService = game:GetService("HttpService");
  32. local LocalPlayer = Players.LocalPlayer;
  33. local SayMessageRequest = ReplicatedStorage:WaitForChild("DefaultChatSystemChatEvents"):WaitForChild("SayMessageRequest");
  34. local OnMessageDoneFiltering = ReplicatedStorage:WaitForChild("DefaultChatSystemChatEvents"):WaitForChild("OnMessageDoneFiltering");
  35. local Debounce = false;
  36.  
  37. local function MakeRequest(Prompt)
  38. return syn.request({
  39. Url = "https://api.openai.com/v1/completions",
  40. Method = "POST",
  41. Headers = {
  42. ["Content-Type"] = "application/json",
  43. ["Authorization"] = "Bearer " .. SECRET_KEY
  44. },
  45. Body = HttpService:JSONEncode({
  46. model = "text-davinci-003",
  47. prompt = Prompt,
  48. temperature = 0.9,
  49. max_tokens = 47, --150
  50. top_p = 1,
  51. frequency_penalty = 0.0,
  52. presence_penalty = 0.6,
  53. stop = {" Human:", " AI:"}
  54. });
  55. });
  56. end
  57.  
  58. OnMessageDoneFiltering.OnClientEvent:Connect(function(Table)
  59. local Message, Instance = Table.Message, Players:FindFirstChild(Table.FromSpeaker);
  60. local Character = Instance and Instance.Character;
  61.  
  62. if Instance == LocalPlayer or string.match(Message, "#") or not Character or not Character:FindFirstChild("Head") or not LocalPlayer.Character or not LocalPlayer.Character:FindFirstChild("Head") then return end;
  63. if Debounce or #Message < _G.MESSAGE_SETTINGS["MINIMUM_CHARACTERS"] or #Message > _G.MESSAGE_SETTINGS["MAXIMUM_CHARACTERS"] then return end;
  64. 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;
  65.  
  66. Debounce = true;
  67.  
  68. local HttpRequest = MakeRequest("Human: " .. Message .. "\n\nAI:");
  69. local Response = Instance.Name .. ": " .. string.sub(HttpService:JSONDecode(HttpRequest["Body"]).choices[1].text, 2);
  70.  
  71. if #Response < 128 then --200
  72. SayMessageRequest:FireServer(Response, "All");
  73. wait(5);
  74. Debounce = false;
  75. else
  76. --warn("Response (> 128): " .. Response);
  77. if #Response - 128 < 128 then
  78. SayMessageRequest:FireServer(string.sub(Response, 1, 128), "All");
  79. delay(3, function()
  80. SayMessageRequest:FireServer(string.sub(Response, 129), "All");
  81. wait(5);
  82. Debounce = false;
  83. end)
  84. else
  85. SayMessageRequest:FireServer("Sorry but the answer was too big, please try again.", "All");
  86. wait(2.5);
  87. Debounce = false;
  88. end
  89. end
  90. end)
  91.  
  92. warn("Script has been executed with success.");
Advertisement
Add Comment
Please, Sign In to add comment