-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathworker.js
More file actions
260 lines (230 loc) · 9.26 KB
/
worker.js
File metadata and controls
260 lines (230 loc) · 9.26 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
// ---------------------------- //
// - github.com/Vauth/duckgpt - //
// ---------------------------- //
const MODELS = [
'@cf/meta/llama-3.1-8b-instruct', // Best balanced model (Recommended)
'@cf/meta/llama-3.2-3b-instruct', // Ultra-fast, lightweight
'@cf/meta/llama-3.1-70b-instruct', // High intelligence (Uses more neurons)
'@cf/mistral/mistral-7b-instruct-v0.1', // Reliable alternative to Llama
'@cf/google/gemma-7b-it', // Google's lightweight model
'@cf/qwen/qwen1.5-7b-chat' // Good for multilingual tasks
];
const MAIN_MODEL = '@cf/meta/llama-3.1-8b-instruct';
const ERROR_404 = {"action":"error", "status": 404, "usage": "GET /chat/?prompt=<text>&model=<model>&history=<List[Dict{str, str}]>", "models": MODELS};
const HEAD_JSON = { 'content-type': 'application/json', 'Access-Control-Allow-Origin': "*"};
const HEAD_HTML = { 'content-type': 'text/html', 'Access-Control-Allow-Origin': "*"};
// ---------- Event Listener ---------- //
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const prompt = url.searchParams.get('prompt');
const history = url.searchParams.get('history') || '[]';
const model = url.searchParams.get('model') || MAIN_MODEL;
if (url.pathname === "/") {return new Response(HTML, { headers: HEAD_HTML });}
if (prompt && url.pathname === "/chat/") {
const response = await Chat(prompt, history, model, env);
return new Response(JSON.stringify(response), { headers: HEAD_JSON });
}
return new Response(JSON.stringify(ERROR_404), {status: 404, headers: HEAD_JSON});
}
};
// ---------- Duckgpt Function ---------- //
async function Chat(prompt, history, model, env) {
try {
const messages = JSON.parse(history);
messages.push({ role: "user", content: prompt });
const response = await env.AI.run(model, {
messages: [
{ role: "system", content: "You are a helpful assistant named 'DuckGPT'." },
...messages
]
});
return {"action":"success", "status": 200, "response": response.response, "model": model};
} catch (error) {
return { "action":"error", "status": 403, "response": error.message };
}
}
// ----------------------------- //
// - github.com/zar0x/duck-gui - //
// ----------------------------- //
let CONFIG = {
"scripts": {
"particles": `https://vauth.github.io/duck-gui/scripts/particles.js`,
"script": `https://vauth.github.io/duck-gui/scripts/script.js`
},
"styles": {
"light": `https://vauth.github.io/duck-gui/styles/light.css`,
"styles": `https://vauth.github.io/duck-gui/styles/styles.css`
},
"resources": {
"clear": `https://vauth.github.io/duck-gui/assets/clear.png`,
"developer": `https://vauth.github.io/duck-gui/assets/developer.png`,
"help": `https://vauth.github.io/duck-gui/assets/help.png`,
"load": `https://vauth.github.io/duck-gui/assets/load.gif`,
"photo": `https://vauth.github.io/duck-gui/assets/photo.jpg`,
"send": `https://vauth.github.io/duck-gui/assets/send.png`,
"setting": `https://vauth.github.io/duck-gui/assets/setting.png`,
"source": `https://vauth.github.io/duck-gui/assets/source.png`,
"favicon": `https://vauth.github.io/duck-gui/assets/favicon.png`,
"ogcover": `https://vauth.github.io/duck-gui/assets/ogcover.png`
}
}
// ---------- HTML Website ---------- //
let HTML = `<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" type="image/png" sizes="512x512" href="${CONFIG.resources.favicon}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="DuckGPT unlimited AI assistant." />
<meta name="keywords" content="chatgpt, duckgpt, duckduckgo, AI" />
<meta property="og:image" content="${CONFIG.resources.ogcover}">
<meta name="author" content="execal">
<title>DuckGPT Chat</title>
<meta charset="UTF-8" />
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
}
#container {
position: relative;
}
#loader {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
background: linear-gradient(135deg, #2b2b2b, #121212);
}
.loader {
width: 48px;
height: 48px;
border: 2px solid #e1e1e1;
position: absolute;
left: -24px;
top: -24px;
box-sizing: border-box;
animation: rotation 2s ease-in-out infinite;
}
#sq2 {
border-color: #ff3333;
animation-delay: 1s;
}
@keyframes rotation {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
<script
id="MathJax-script"
async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
</head>
<body>
<div id="loader">
<div id="container">
<div id="sq1" class="loader"></div>
<div id="sq2" class="loader"></div>
</div>
</div>
<script>if (JSON.parse(localStorage.getItem("setting"))['light'])
{document.getElementById('loader').style.background = 'linear-gradient(135deg, #ffffff, #a4a4a4)';
document.getElementById('sq1').style.borderColor = '#1e1e1e';
document.getElementById('sq2').style.borderColor = '#2965ff'}</script>
<div id="dailog" class="dailog"></div>
<div class="header" id="nHead">
<button id="new-chat-button">
<img class="iconImg headerIcon" src="${CONFIG.resources.clear}" alt="clear Chat" />
</button>
</div>
<div class="header" id="sHead">
<button id="setting-button">
<img class="iconImg headerIcon" src="${CONFIG.resources.setting}" alt="Settings" />
</button>
</div>
<div id="popup-overlay" class="hidden">
<div id="popup-content">
<span id="close-popup">×</span>
<div id="clearchat-popup" class="hidden">
<p>
Are you sure you want to clear the current chat and start a new one?
</p>
<button id="yes-button">Yes</button>
<button id="no-button">No</button>
</div>
<div id="setting-popup" class="hidden">
<div class="setting-option">
<label for="particles-toggle">Particle Effects</label>
<label class="switch">
<input type="checkbox" id="particles-toggle"/>
<span class="slider round"></span>
</label>
</div>
<div class="setting-option">
<label for="theme-toggle">Light Theme</label>
<label class="switch">
<input type="checkbox" id="theme-toggle" />
<span class="slider round"></span>
</label>
</div>
<div class="socials">
<a href="https://github.com/ZAR0X/duck-gui" title="GitHub"><i class="fab fa-github"></i></a>
<a href="https://t.me/execal" title="Telegram"><i class="fab fa-telegram"></i></a>
<a href="mailto:qzarox@gmail.com" title="Email"><i class="fas fa-envelope"></i></a>
</div>
</div>
</div>
</div>
<div id="intro-cards-container" class="intro-cards hidden">
<div class="intro-card card-1">
<img src="${CONFIG.resources.source}" alt="Open Source Icon" />
<p>This GUI is an open-source project for <a href="https://github.com/vauth/duckgpt">DuckGPT</a></p>
</div>
<!-- <div class="intro-card card-2">
<img src="${CONFIG.resources.help}" alt="Help Icon" />
<p>Click the "Type a message below to start.</p>
</div> -->
<div class="intro-card card-3">
<img src="${CONFIG.resources.developer}" alt="Developer Icon" />
<p>Front-End: <a href="https://github.com/ZAR0X">Zarox</a><br>Back-End: <a href="https://github.com/VAUTH">Vauth</a></p>
</div>
</div>
<div class="chat-container">
<div id="chat-box" class="chat-box">
<div class="message-container" id="spaceChatBox"></div>
</div>
<div class="input-container">
<div
id="chat-input"
class="editable-div"
contenteditable="true"
role="textbox"
aria-multiline="true"
></div>
<div id="placeholder">Type your message here...</div>
<!-- <input type="text" id="chat-input" placeholder="Type your message here..." autofocus> -->
<button id="send-button">
<img class="iconImg" src="${CONFIG.resources.send}" alt="Send" />
</button>
</div>
</div>
<div id="particles-js"></div>
<script type="module" src="${CONFIG.scripts.particles}"></script>
<script type="module" src="${CONFIG.scripts.script}"></script>
</body>
</html>`