-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChatBot.lua
More file actions
379 lines (321 loc) · 15.1 KB
/
Copy pathChatBot.lua
File metadata and controls
379 lines (321 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
local ui = game:GetService("CoreGui"):FindFirstChild("AI")
if ui then ui:Destroy() end
-- This chatbot is powered by https://pollinations.ai
local uiElements = {
["AI"] = Instance.new("ScreenGui"),
["Container"] = Instance.new("Frame"),
["UICorner"] = Instance.new("UICorner"),
["Chat"] = Instance.new("Frame"),
["Messages"] = Instance.new("ScrollingFrame"),
["UserTemplate"] = Instance.new("Frame"),
["UICorner_1"] = Instance.new("UICorner"),
["Message"] = Instance.new("TextLabel"),
["UIPadding"] = Instance.new("UIPadding"),
["UISizeConstraint"] = Instance.new("UISizeConstraint"),
["UIPadding_1"] = Instance.new("UIPadding"),
["SysTemplate"] = Instance.new("Frame"),
["UICorner_2"] = Instance.new("UICorner"),
["Message_1"] = Instance.new("TextLabel"),
["UIPadding_2"] = Instance.new("UIPadding"),
["UISizeConstraint_1"] = Instance.new("UISizeConstraint"),
["Buttons"] = Instance.new("Frame"),
["UIListLayout"] = Instance.new("UIListLayout"),
["UIPadding_3"] = Instance.new("UIPadding"),
["Copy"] = Instance.new("ImageButton"),
["Header"] = Instance.new("Frame"),
["Icon"] = Instance.new("ImageLabel"),
["Icon1"] = Instance.new("ImageLabel"),
["UICorner_3"] = Instance.new("UICorner"),
["UIStroke"] = Instance.new("UIStroke"),
["InputBar"] = Instance.new("Frame"),
["Bar"] = Instance.new("TextBox"),
["UIPadding_4"] = Instance.new("UIPadding"),
["UICorner_4"] = Instance.new("UICorner"),
["UIStroke_1"] = Instance.new("UIStroke"),
["LocalScript"] = Instance.new("LocalScript")
}
uiElements["AI"].Parent = game:GetService("CoreGui")
uiElements["AI"].Name = "AI"
uiElements["Container"].Parent = uiElements["AI"]
uiElements["Container"].Position = UDim2.new(0.5, 0, 0.5, 0)
uiElements["Container"].Size = UDim2.new(0.58423912525177, 0, 0.6565656661987305, 0)
uiElements["Container"].BackgroundColor3 = Color3.fromRGB(15, 15, 15)
uiElements["Container"].AnchorPoint = Vector2.new(0.5, 0.5)
uiElements["UICorner"].Parent = uiElements["Container"]
uiElements["UICorner"].CornerRadius = UDim.new(0, 14)
uiElements["Chat"].Parent = uiElements["Container"]
uiElements["Chat"].Size = UDim2.new(1, 0, 0.9788461327552795, -64)
uiElements["Chat"].BackgroundTransparency = 1
uiElements["Messages"].Parent = uiElements["Chat"]
uiElements["Messages"].Position = UDim2.new(0, 0, 0.10786513239145279, 0)
uiElements["Messages"].Size = UDim2.new(1, 0, 0.9932584762573242, 0)
uiElements["Messages"].BorderSizePixel = 0
uiElements["Messages"].BackgroundTransparency = 1
uiElements["Messages"].AutomaticCanvasSize = Enum.AutomaticSize.Y
uiElements["Messages"].CanvasSize = UDim2.new(0, 0, 0, 68)
uiElements["Messages"].ScrollBarImageColor3 = Color3.fromRGB(0, 0, 0)
uiElements["Messages"].ScrollBarThickness = 8
uiElements["UserTemplate"].Parent = uiElements["Messages"]
uiElements["UserTemplate"].Position = UDim2.new(1, 0, 0, 0)
uiElements["UserTemplate"].Size = UDim2.new(0, 100, 0, 30)
uiElements["UserTemplate"].BackgroundColor3 = Color3.fromRGB(23, 23, 23)
uiElements["UserTemplate"].AnchorPoint = Vector2.new(1, 0)
uiElements["UserTemplate"].AutomaticSize = Enum.AutomaticSize.XY
uiElements["UserTemplate"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["UserTemplate"].BorderSizePixel = 0
uiElements["UserTemplate"].Visible = false
uiElements["UICorner_1"].Parent = uiElements["UserTemplate"]
uiElements["UICorner_1"].CornerRadius = UDim.new(1, 0)
uiElements["Message"].Parent = uiElements["UserTemplate"]
uiElements["Message"].Size = UDim2.new(1, 0, 1, 0)
uiElements["Message"].BackgroundColor3 = Color3.fromRGB(255, 255, 255)
uiElements["Message"].AutomaticSize = Enum.AutomaticSize.XY
uiElements["Message"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["Message"].BorderSizePixel = 0
uiElements["Message"].BackgroundTransparency = 1
uiElements["Message"].Font = Enum.Font.GothamMedium
uiElements["Message"].TextColor3 = Color3.fromRGB(190, 190, 190)
uiElements["Message"].TextSize = 18
uiElements["Message"].RichText = true
uiElements["Message"].Text = "X"
uiElements["Message"].TextWrapped = true
uiElements["UIPadding"].Parent = uiElements["Message"]
uiElements["UIPadding"].PaddingTop = UDim.new(0, 3)
uiElements["UIPadding"].PaddingBottom = UDim.new(0, 3)
uiElements["UIPadding"].PaddingLeft = UDim.new(0, 8)
uiElements["UIPadding"].PaddingRight = UDim.new(0, 8)
uiElements["UISizeConstraint"].Parent = uiElements["UserTemplate"]
uiElements["UIPadding_1"].Parent = uiElements["Messages"]
uiElements["UIPadding_1"].PaddingTop = UDim.new(0, 8)
uiElements["UIPadding_1"].PaddingRight = UDim.new(0, 8)
uiElements["SysTemplate"].Parent = uiElements["Messages"]
uiElements["SysTemplate"].Size = UDim2.new(0, 100, 0, 30)
uiElements["SysTemplate"].BackgroundColor3 = Color3.fromRGB(23, 23, 23)
uiElements["SysTemplate"].AutomaticSize = Enum.AutomaticSize.XY
uiElements["SysTemplate"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["SysTemplate"].BorderSizePixel = 0
uiElements["SysTemplate"].Visible = false
uiElements["SysTemplate"].BackgroundTransparency = 1
uiElements["UICorner_2"].Parent = uiElements["SysTemplate"]
uiElements["UICorner_2"].CornerRadius = UDim.new(1, 0)
uiElements["Message_1"].Parent = uiElements["SysTemplate"]
uiElements["Message_1"].Size = UDim2.new(1, 0, 1, 0)
uiElements["Message_1"].BackgroundColor3 = Color3.fromRGB(255, 255, 255)
uiElements["Message_1"].AutomaticSize = Enum.AutomaticSize.XY
uiElements["Message_1"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["Message_1"].BorderSizePixel = 0
uiElements["Message_1"].BackgroundTransparency = 1
uiElements["Message_1"].Font = Enum.Font.GothamMedium
uiElements["Message_1"].TextColor3 = Color3.fromRGB(190, 190, 190)
uiElements["Message_1"].TextSize = 18
uiElements["Message_1"].RichText = true
uiElements["Message_1"].Text = "Y"
uiElements["Message_1"].TextWrapped = true
uiElements["Message_1"].TextXAlignment = Enum.TextXAlignment.Left
uiElements["UIPadding_2"].Parent = uiElements["Message_1"]
uiElements["UIPadding_2"].PaddingTop = UDim.new(0, 3)
uiElements["UIPadding_2"].PaddingBottom = UDim.new(0, 3)
uiElements["UIPadding_2"].PaddingLeft = UDim.new(0, 8)
uiElements["UIPadding_2"].PaddingRight = UDim.new(0, 8)
uiElements["UISizeConstraint_1"].Parent = uiElements["SysTemplate"]
uiElements["Buttons"].Parent = uiElements["SysTemplate"]
uiElements["Buttons"].Position = UDim2.new(0, 0, 1, 0)
uiElements["Buttons"].Size = UDim2.new(1, 0, 0, 15)
uiElements["Buttons"].BackgroundColor3 = Color3.fromRGB(255, 255, 255)
uiElements["Buttons"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["Buttons"].BorderSizePixel = 0
uiElements["Buttons"].BackgroundTransparency = 1
uiElements["UIListLayout"].Parent = uiElements["Buttons"]
uiElements["UIListLayout"].Padding = UDim.new(0, 4)
uiElements["UIListLayout"].FillDirection = Enum.FillDirection.Horizontal
uiElements["UIListLayout"].SortOrder = Enum.SortOrder.LayoutOrder
uiElements["UIPadding_3"].Parent = uiElements["Buttons"]
uiElements["UIPadding_3"].PaddingLeft = UDim.new(0, 4)
uiElements["Copy"].Parent = uiElements["Buttons"]
uiElements["Copy"].Size = UDim2.new(0, 15, 1, 0)
uiElements["Copy"].BackgroundColor3 = Color3.fromRGB(255, 255, 255)
uiElements["Copy"].Active = false
uiElements["Copy"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["Copy"].BorderSizePixel = 0
uiElements["Copy"].BackgroundTransparency = 1
uiElements["Copy"].Image = "rbxassetid://93531807477279"
uiElements["Header"].Parent = uiElements["Chat"]
uiElements["Header"].Size = UDim2.new(1, 0, -0.017977528274059296, 50)
uiElements["Header"].BackgroundColor3 = Color3.fromRGB(20, 20, 20)
uiElements["Header"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["Header"].BorderSizePixel = 0
uiElements["Header"].ZIndex = 0
uiElements["Icon"].Parent = uiElements["Header"]
uiElements["Icon"].Position = UDim2.new(0, 8, 0, 8)
uiElements["Icon"].Size = UDim2.new(0, 30, 0, 30)
uiElements["Icon"].BackgroundColor3 = Color3.fromRGB(255, 255, 255)
uiElements["Icon"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["Icon"].BorderSizePixel = 0
uiElements["Icon"].BackgroundTransparency = 1
uiElements["Icon"].Image = "rbxassetid://125966901198850"
uiElements["Icon1"].Parent = uiElements["Header"]
uiElements["Icon1"].Position = UDim2.new(1, -8, 0, 8)
uiElements["Icon1"].Size = UDim2.new(0, 30, 0, 30)
uiElements["Icon1"].BackgroundColor3 = Color3.fromRGB(255, 255, 255)
uiElements["Icon1"].AnchorPoint = Vector2.new(1, 0)
uiElements["Icon1"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["Icon1"].BorderSizePixel = 0
uiElements["Icon1"].BackgroundTransparency = 1
uiElements["Icon1"].Image = "rbxassetid://73985599900390"
uiElements["UICorner_3"].Parent = uiElements["Header"]
uiElements["UICorner_3"].CornerRadius = UDim.new(1, 0)
uiElements["UIStroke"].Parent = uiElements["Container"]
uiElements["UIStroke"].Color = Color3.fromRGB(40, 40, 40)
uiElements["InputBar"].Parent = uiElements["Container"]
uiElements["InputBar"].Position = UDim2.new(0, 0, 1, 0)
uiElements["InputBar"].Size = UDim2.new(1, 0, 0, 0)
uiElements["InputBar"].BackgroundColor3 = Color3.fromRGB(255, 255, 255)
uiElements["InputBar"].AnchorPoint = Vector2.new(0, 1)
uiElements["InputBar"].AutomaticSize = Enum.AutomaticSize.Y
uiElements["InputBar"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["InputBar"].BorderSizePixel = 0
uiElements["InputBar"].BackgroundTransparency = 1
uiElements["Bar"].Parent = uiElements["InputBar"]
uiElements["Bar"].Position = UDim2.new(0.5, 0, 1, -4)
uiElements["Bar"].Size = UDim2.new(1, -16, 1, 0)
uiElements["Bar"].BackgroundColor3 = Color3.fromRGB(23, 23, 23)
uiElements["Bar"].AnchorPoint = Vector2.new(0.5, 1)
uiElements["Bar"].AutomaticSize = Enum.AutomaticSize.Y
uiElements["Bar"].BorderColor3 = Color3.fromRGB(0, 0, 0)
uiElements["Bar"].BorderSizePixel = 0
uiElements["Bar"].Font = Enum.Font.GothamMedium
uiElements["Bar"].TextColor3 = Color3.fromRGB(220, 220, 220)
uiElements["Bar"].TextSize = 18
uiElements["Bar"].Text = ""
uiElements["Bar"].PlaceholderText = "Ask anything"
uiElements["Bar"].TextWrapped = true
uiElements["Bar"].TextXAlignment = Enum.TextXAlignment.Left
uiElements["UIPadding_4"].Parent = uiElements["Bar"]
uiElements["UIPadding_4"].PaddingTop = UDim.new(0, 4)
uiElements["UIPadding_4"].PaddingBottom = UDim.new(0, 4)
uiElements["UIPadding_4"].PaddingLeft = UDim.new(0, 16)
uiElements["UIPadding_4"].PaddingRight = UDim.new(0, 16)
uiElements["UICorner_4"].Parent = uiElements["Bar"]
uiElements["UICorner_4"].CornerRadius = UDim.new(1, 0)
uiElements["UIStroke_1"].Parent = uiElements["Bar"]
uiElements["UIStroke_1"].ApplyStrokeMode = Enum.ApplyStrokeMode.Border
uiElements["UIStroke_1"].Color = Color3.fromRGB(40, 40, 40)
uiElements["UIStroke_1"].Thickness = 0.6000000238418579
-- since this ui to lua is horrible, i have to do this
for name, i in next, uiElements do i.Name = name:gsub("_%d+", "") end
local script = Instance.new("LocalScript", uiElements["Bar"])
local user = game:GetService("UserInputService")
local box = script.Parent
local messagesList = uiElements["Messages"]
local userTemplate, sysTemplate = uiElements.UserTemplate, uiElements.SysTemplate
local lastBox, lastFocusReleased, isGenerating;
local isStudio = game:GetService("RunService"):IsStudio()
local currentOffset = 0
local http_func = request or http and http.request or http_request or syn and syn.request
local REQUIRED_PROMPT = "\n" -- Change to whatever you want
user.TextBoxFocusReleased:Connect(function(Box)
lastBox, lastFocusReleased = Box, tick()
end)
local messages = {
{
role = "system",
content = "You are a helpful AI assistant." .. REQUIRED_PROMPT
}
}
local function richText(txt) --> turn stuff like **x** into <b>x</b> since .RichText is enabled
txt = txt:gsub("%*%*([^\n%*]+)%*%*", "<b>%1</b>")-- removes bolds
txt = txt:gsub("~~([^\n~]+)~~", "<strike>%1</strike>")
txt = txt:gsub("^(#+)([^\n]+)", function(h, t) if #h > 6 then return end return `<font size = "{25 - #h * 2}">{t}</font>` end) -- probably doesn't work really well
return txt
end
local function copy(b)
if isStudio then
print("Cannot copy on studio.") -- you can just make a textbox,set it's text to b.Text & wait for user to copy after :CaptureFocus(), but thats too annoying to implement
else
setclipboard(b.Text); -- executors have access to this
end
end
local function createMessage(IsUser, Message)
local Clone = IsUser and userTemplate:Clone() or sysTemplate:Clone()
if IsUser then
Clone.AnchorPoint = Vector2.new(1, 0)
else
Clone.AnchorPoint = Vector2.new(0, 0)
Clone.Buttons.Copy.MouseButton1Click:Connect(function()
copy(Clone.Message)
end)
end
Clone.Message.Text = richText(Message)
Clone.Visible = true
Clone.Parent = messagesList
Clone.Size = UDim2.new(Clone.Size.X.Scale, Clone.Size.X.Offset, 0, Clone.Message.TextBounds.Y + 2)
local yOffset = 0
for _, child in ipairs(messagesList:GetChildren()) do
if child:IsA("Frame") and child.Visible then
yOffset += child.AbsoluteSize.Y
end
end
Clone.Position = UDim2.new(Clone.AnchorPoint.X, 0, 0, yOffset + 10) -- + 10 to make padding
messagesList.CanvasSize = UDim2.new(0, 0, 0, yOffset + Clone.AbsoluteSize.Y)
messagesList.CanvasPosition = Vector2.new(0, yOffset + Clone.AbsoluteSize.Y)
return Clone
end
user.InputBegan:Connect(function(Input, GPE)
local Code = Input.KeyCode
if Code == Enum.KeyCode.Return then -- I know I can use user:GetFocusedTextBox() but if they pressed enter, that means it's no longer focused, so check if the time between the unfocus & focus is less than 0.1 seconds
local IsShifting = user:IsKeyDown(Enum.KeyCode.LeftShift) or user:IsKeyDown(Enum.KeyCode.RightShift)
if lastBox == box and IsShifting and tick() - lastFocusReleased < 0.01 then -- User meant a newline.
box.Text ..= "\n"
box.CursorPosition = #box.Text + 1
box:CaptureFocus()
else -- Just send the message
if isGenerating or lastBox ~= box then return end
local Prompt = box.Text
box.Text = ""
currentOffset += createMessage(true, Prompt).AbsoluteSize.Y
table.insert(messages, {
role = "user",
content = Prompt
})
isGenerating = true
local Response = createMessage(false, "Thinking...")
task.spawn(function()
local Dots = 3
while task.wait(.33) do
if not isGenerating then return end
Dots = (Dots % 3) + 1
Response.Message.Text = `Thinking{string.rep(".", Dots)}`
end
end)
local Data = {
Url = "https://text.pollinations.ai/openai",
Method = "POST",
Headers = {
["Content-Type"] = "application/json"
},
Body = {
messages = messages
}
}
local Result;
if isStudio then -- Since roblox studio doesn't have direct access to HTTP requests from the client, make the server send the http request & then return it via a RemoteFunction (Super cool approach)
Result = game.ReplicatedStorage.HTTP:InvokeServer(Data)
else
if not http_func then
return createMessage(false, "No http function available!")
end
Data.Body = game:GetService("HttpService"):JSONEncode(Data.Body)
Result = game:GetService("HttpService"):JSONDecode(http_func(Data).Body);
end
isGenerating = false
local Msg = (Result.choices and Result.choices[1] or { message = { content = "Unable to fetch reply :(" } }).message.content or "No content???"
table.insert(messages, {
role = "system",
content = Msg
})
Response.Message.Text = richText(Msg);
currentOffset += Response.AbsoluteSize.Y
messagesList.CanvasSize += Response.Size
end
end
end)