-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
220 lines (160 loc) · 5.65 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ChatGPT Playground (starting up)</title>
<style>
.byline {
font-family: Verdana;
font-size: 80%;
padding-bottom: 0.7rem;
}
pre {
margin-bottom: 3rem;
}
.message {
margin-bottom: 10px;
}
textarea {
width: 100%;
height: 60px;
margin-bottom: 10px;
}
button {
display: block;
margin-top: 10px;
}
</style>
</head>
<body>
<div id="app"></div>
<!-- https://unpkg.com/browse/vue/dist/ -->
<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>
<script src="https://unpkg.com/[email protected]/lib/index.iife.js"></script>
<script src="https://unpkg.com/[email protected]/dist/pinia.iife.js"></script>
<script defer>
let elPrompt, elSend;
const PROCESSING = "Processing...";
const setPlaceholder = processing => {
elPrompt.setAttribute( "placeholder", processing ? PROCESSING : elPrompt.dataset.placeholder );
elSend.disabled = !!processing;
setTimeout( () => elPrompt.focus(), 40 );
};
const q = (sel, root) => (root || document).querySelector(sel ?? null);
const { createApp, ref, onMounted } = Vue;
const { createPinia, defineStore } = Pinia;
const ChatGPTPlayground = {
template: `
<div>
<h1>
VueJS+Pinia chatbot Playground (minimal) via OpenAI API direct access</h1>
<p class="byline">By <a href="https://github.com/DarrenSem/Vue-ChatGPT">Darren Semotiuk</a></p>
<div v-for="(msg, index) in chat.messages" :key="index" class="message">
<p><b>{{ msg.role.toUpperCase() }}:</b></p>
<pre>{{ msg.content }}</pre>
</div>
<textarea
data-prompt="prompt" v-model="chat.userMessage"
@keydown="chat.handleCTRLkey"
data-placeholder="Enter user message...
[CTRL]+[UP/DOWN] for prompt history"
></textarea>
<button title="[CTRL]+[ENTER] to send" data-send="send" @click="chat.sendMessage">Send</button>
</div>
`,
setup() {
const chat = useChatStore();
onMounted(() => {
chat.loadApiKey();
document.title = q("h1").innerText;
elPrompt = q('[data-prompt="prompt"]');
elSend = q('[data-send="send"]');
setPlaceholder(!"processing");
});
return { chat };
}
};
const useChatStore = defineStore('chat', {
state: () => ({
model: "gpt-4o", // "gpt-3.5-turbo";
messages: [
{ role: "system", content: "You are an expert at XYZ (context defined later).\nLet's think step by step.\n\nXYZ context is defined as" }
],
userMessage: "",
historyIndex: null,
}),
actions: {
async loadApiKey() {
let apiKey = localStorage.getItem("apiKey");
if (!apiKey) {
apiKey = prompt("Please enter your OpenAI API key: (will be kept in localStorage only)");
if (apiKey) {
localStorage.setItem("apiKey", apiKey);
}
}
return apiKey;
},
async callChatApi(message) {
const apiKey = await this.loadApiKey();
if (!apiKey) return;
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({
model: this.model,
messages: message
})
});
if (!response.ok) {
localStorage.removeItem("apiKey");
alert("API call failed; please verify your API key is correct.");
return;
};
const data = await response.json();
return data.choices[0].message;
},
async sendMessage() {
if (!this.userMessage.trim()) return;
this.messages.push({ role: "user", content: this.userMessage });
this.userMessage = "";
this.historyIndex = null;
setPlaceholder(!!"processing");
const botReply = await this.callChatApi(this.messages);
setPlaceholder(!"processing");
if (botReply) {
this.messages.push(botReply);
}
},
handleCTRLkey(event) {
if (!event.ctrlKey) return;
// if (elPrompt.getAttribute("placeholder") === PROCESSING) return;
if (event.key === "Enter") {
event.preventDefault();
elSend.click();
};
if (event.key === "ArrowUp" || event.key === "ArrowDown") {
event.preventDefault();
const rotateHistory = offset => {
const userPrompts = this.messages.reduce( (acc, el, i) => ( i % 2 && acc.push(el), acc ), [] );
const L = userPrompts.length;
if(!L) return;
const prev = this.historyIndex ?? (offset < 0 ? L : -1);
const next = ( prev + offset + L ) % L;
const userPrompt = userPrompts[next];
this.userMessage = userPrompt.content;
this.historyIndex = next;
};
rotateHistory( event.key === "ArrowUp" ? -1 : 1 );
};
}
} // actions: {
}); // const useChatStore = defineStore('chat', {
const pinia = createPinia();
createApp(ChatGPTPlayground).use(pinia).mount("#app");
</script>
</body>
</html>