-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
118 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,60 @@ | ||
<?php | ||
|
||
include "openai_api.php"; // API | ||
// or | ||
//include "openai_chat.php"; // chat.openai.com (GPT-3.5) | ||
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" | ||
> | ||
|
||
<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://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> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h2 class="text-center">ChatGPT</h2> | ||
<title>ChatGPT</title> | ||
</head> | ||
|
||
<body> | ||
<div class="container"> | ||
<h2 class="text-center">ChatGPT <small class="h5 font-weight-normal"><a href="/?type=chat">chat.openai.com</a> | <a href="/">API</a></small></h2> | ||
<form method="post"> | ||
<div class="row"> | ||
<div class="form-group col-12"> | ||
<input type="text" class="form-control" id="conversationId" placeholder="set chat id or leave blank to start a new chat" <?=!function_exists('openai_chat')?'style="display:none"':''?> value=""> | ||
</div> | ||
<div class="messages col-12" id="messages"> | ||
<!-- Messages will be displayed here --> | ||
</div> | ||
<div class="row"> | ||
<div class="form-group col-12"> | ||
<input type="text" class="form-control" id="conversationId" placeholder="set chat id or leave blank to start a new chat" <?= $_GET['type']=='chat' ? '' : 'style="display:none"' ?> value=""> | ||
</div> | ||
<div class="messages col-12" id="messages"> | ||
<!-- Messages will be displayed here --> | ||
</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 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> | ||
</div> | ||
</form> | ||
</div> | ||
</body> | ||
<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 = ''; | ||
|
||
conversationId.value = getCookie("conversation_id"); | ||
|
||
messageInput.focus(); | ||
</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 = ''; | ||
|
||
// Send message when submit button is clicked | ||
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; | ||
|
@@ -74,7 +68,7 @@ function sendMessageHandler(event) { | |
} | ||
|
||
const conversation_id = conversationId.value; | ||
|
||
sendMessage.innerText = 'loading...'; | ||
conversationId.disabled = true; | ||
sendMessage.disabled = true; | ||
|
@@ -92,99 +86,104 @@ function sendMessageHandler(event) { | |
|
||
// Send the message to the server | ||
fetch('', { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
message: message, | ||
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"); | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
<?= $_GET['type']=='chat' ? "openai_type: 'chat'," : '' ?> | ||
message: message, | ||
conversation_id: conversation_id, | ||
parent_message_id: parent_message_id | ||
}), | ||
}) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
|
||
if(data.hasOwnProperty('error')){ | ||
p.innerHTML = 'Server: ' + data.error.msg; | ||
} | ||
else if (data.hasOwnProperty('message')) { | ||
p.innerHTML = 'Server: ' + marked(data.message); | ||
conversationId.value = data.conversation_id; // set conversation_id | ||
parent_message_id = data.parent_message_id; // set parent_message_id | ||
setCookie("conversation_id",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.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 = 'Server: ' + data.error.msg; | ||
} else if (data.hasOwnProperty('message')) { | ||
p.innerHTML = 'Server: ' + 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.style.height = (messageInput.scrollHeight + 2) + "px"; | ||
} | ||
messageInput.addEventListener("input", messageInputResize); | ||
|
||
function replaceHTML(str) { | ||
const jsEntities = [ | ||
['&', '&'], | ||
['<', '<'], | ||
['>', '>'], | ||
['\'', '''], | ||
['"', '"'], | ||
['\n', '<br>'], | ||
['\t', ' '] | ||
['&', '&'], | ||
['<', '<'], | ||
['>', '>'], | ||
['\'', '''], | ||
['"', '"'], | ||
['\n', '<br>'], | ||
['\t', ' '] | ||
]; | ||
for (let i = 0; i < jsEntities.length; i++) { | ||
str = str.replace(new RegExp(jsEntities[i][0], 'g'), jsEntities[i][1]); | ||
str = str.replace(new RegExp(jsEntities[i][0], 'g'), jsEntities[i][1]); | ||
} | ||
return str; | ||
} | ||
function setCookie(name,value,days){ | ||
|
||
function setCookie(name, value, days) { | ||
let expires = ""; | ||
if (days){ | ||
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++) { | ||
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); | ||
while (c.charAt(0) == ' ') c = c.substring(1, c.length); | ||
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); | ||
} | ||
return ''; | ||
return null; | ||
} | ||
</script> | ||
</script> | ||
|
||
</body> | ||
</html> |