-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.php
204 lines (183 loc) · 7.01 KB
/
index.php
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
<?php
include "openai_chat_api.php";
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/marked.min.js"></script>
<title>ChatGPT</title>
<style>
.selected{
font-weight: bold
}
</style>
</head>
<body>
<div class="container">
<h4 class="text-center">
<small class="h5 font-weight-normal"><a href="/" <?= empty($_GET['type']) ? 'class="selected"' : '' ?>>API GPT-3</a> | <a href="/?type=turbo" <?= $_GET['type']=='turbo' ? 'class="selected"' : '' ?>>API GPT-3.5 turbo</a> | <a href="/?type=chat"<?= $_GET['type']=='chat' ? 'class="selected"' : '' ?>>Not an API (chat.openai.com)</a></small>
</h4>
<form method="post">
<div class="row form-group" <?= $_GET['type']=='chat' ? '' : 'style="display:none"' ?>>
<div class="col-9">
<input type="text" class="form-control" id="conversationId" placeholder="set chat id or leave blank to start a new chat" value="">
</div>
<div class="col-3">
<select class="form-control" id="chatModel">
<option value="text-davinci-002-render-sha">Default (GPT-3.5)</option>
<option value="text-davinci-002-render-paid">Legacy (GPT-3.5)</option>
<option value="gpt-4">GPT-4</option>
</select>
</div>
</div>
<div class="row">
<div class="messages col-12" id="messages">
<!-- Messages will be displayed here -->
</div>
</div>
<div class="row">
<div class="form-group col-9">
<textarea rows="1" class="form-control" id="messageInput" placeholder="Enter a message"></textarea>
</div>
<div class="col-3">
<button type="submit" class="btn btn-primary w-100" id="sendMessage">
Send
</button>
</div>
</div>
</form>
</div>
<script>
// Get the input field and submit button
const conversationId = document.getElementById("conversationId");
const messageInput = document.getElementById("messageInput");
const sendMessage = document.getElementById("sendMessage");
const messages = document.getElementById("messages");
var parent_message_id = '';
let сookie = getCookie('conversation_id');
conversationId.value = сookie ?? '';
messageInput.focus();
// Send message when submit button is clicked
function sendMessageHandler(event) {
if (event.type != "click" && !(event.type == "keydown" && event.ctrlKey && event.keyCode == 13))
return;
event.preventDefault();
// Get the message from the input field
const message = messageInput.value;
if (!message.trim()) {
messageInput.focus();
return;
}
const conversation_id = conversationId.value;
sendMessage.innerText = 'loading...';
conversationId.disabled = true;
sendMessage.disabled = true;
messageInput.disabled = true;
// Create a new message element
const messageElement = document.createElement("div");
messageElement.innerHTML = '<p><b>You</b>:<br>' + replaceHTML(message) + '</p>';
messages.appendChild(document.createElement("hr"));
messages.appendChild(messageElement);
// Clear the input field
messageInput.value = "";
messageInputResize();
// Send the message to the server
fetch('', {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
<?= $_GET['type']=='chat' ? "openai_type: 'chat'," : '' ?>
<?= $_GET['type']=='turbo' ? "openai_type: 'turbo'," : '' ?>
message: message,
chat_model: document.getElementById("chatModel").value,
conversation_id: conversation_id,
parent_message_id: parent_message_id
}),
})
.then((response) => response.json())
.then((data) => {
sendMessage.innerText = 'Send';
sendMessage.disabled = false;
messageInput.disabled = false;
messageInput.focus();
// Display the response from the server
const messageElement = document.createElement("div");
const p = document.createElement("p");
if (data.hasOwnProperty('error')) {
p.innerHTML = '<b>AI</b>: ' + data.error.msg;
} else if (data.hasOwnProperty('message')) {
p.innerHTML = '<b>AI</b>: ' + marked(data.message);
if (data.hasOwnProperty('conversation_id')) {
parent_message_id = data.parent_message_id; // set parent_message_id
conversationId.value = data.conversation_id; // set conversation_id
setCookie("conversation_id", data.conversation_id, 365);
}
}
messageElement.appendChild(p);
messages.appendChild(document.createElement("hr"));
messages.appendChild(messageElement);
document.querySelectorAll("pre code").forEach((block) => {
hljs.highlightBlock(block);
});
window.scrollTo(0, document.body.scrollHeight);
})
.catch(error => {
console.error(error);
sendMessage.innerText = 'Send';
sendMessage.disabled = false;
messageInput.disabled = false;
messageInput.focus();
});
}
sendMessage.addEventListener("click", sendMessageHandler);
document.addEventListener("keydown", sendMessageHandler);
/*--------------------*/
function messageInputResize() {
messageInput.style.height = "auto";
messageInput.style.height = (messageInput.scrollHeight + 2) + "px";
}
messageInput.addEventListener("input", messageInputResize);
function replaceHTML(str) {
const jsEntities = [
['&', '&'],
['<', '<'],
['>', '>'],
['\'', '''],
['"', '"'],
['\n', '<br>'],
['\t', ' ']
];
for (let i = 0; i < jsEntities.length; i++) {
str = str.replace(new RegExp(jsEntities[i][0], 'g'), jsEntities[i][1]);
}
return str;
}
function setCookie(name, value, days) {
let expires = "";
if (days) {
let date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
let nameEQ = name + "=";
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
</script>
</body>
</html>