I'm using an addEventListener click event to trigger a new GPT3 request which takes the latest database post as a prompt for text completion.
The data is sent to the database through a hidden html form, with the action of the form triggering a php script.
The newly generated text is then sent to the database, which becomes the prompt for the next GPT3 request etc etc...
The problem is the event only fires once, and I would like to be able to continue to generate new text within the loading page whilst the other data heavy files within the page loads.
<form name="GPT3commentForm" id="GPT3commentForm" display="none" action="/insert_GPT3.php" method="post" autocomplete="off">
<input type="hidden" name="GPT3_field1" id="GPT3Text"/></form>
<?php
require_once './includes/Openai.php';
$openai = New Openai();
//Engine, prompt and max tokens - takes the last entry of the database array as the prompt
$openai->request("davinci", end($stack), 100);
$latestEntry = end($stack);
$latestEntry = JSON_encode($latestEntry);
?>
<script>
var data = <?php echo json_encode($response, JSON_HEX_TAG); ?>; // Don't forget the extra semicolon!
//turn the string into an object
const dataObj = JSON.parse(data);
console.log(typeof dataObj);
//access only the text completion section...
console.log(dataObj.choices[0].text);
GPT3Array = [];
newData = [];
newDataObj = [];
currentGPT3Text = 0;
GPT3Array[currentGPT3Text] = dataObj.choices[0].text;
document.getElementById('GPT3Text').value = GPT3Array[currentGPT3Text];
const GPT3symbolSpan = document.createElement("span");
const GPT3link = document.createElement("a");
GPT3link.textContent = GPT3Array[currentGPT3Text];
GPT3symbolSpan.appendChild(GPT3link);
GPT3LoadingText.appendChild(GPT3symbolSpan);
document.getElementById('GPT3commentForm').submit();
var generateText = function (event) {
currentGPT3Text += 1;
<?php
require_once './includes/Openai.php';
$new_openai = New Openai();
//Engine, prompt and max tokens - takes the last entry of the database array as the prompt
$new_openai->request("davinci", end($stack), 100);
$latestEntry = end($stack);
$latestEntry = JSON_encode($latestEntry);
//unset($new_openai);
//unset($latestEntry);
?>
//make newData and newDataObj an array to make this work...
newData[currentGPT3Text] = <?php echo json_encode($response, JSON_HEX_TAG); ?>;
newDataObj[currentGPT3Text] = JSON.parse(newData[currentGPT3Text]);
GPT3Array[currentGPT3Text] = newDataObj[currentGPT3Text].choices[0].text;
document.getElementById('GPT3Text').value = GPT3Array[currentGPT3Text];
document.getElementById('GPT3commentForm').submit();
GPT3link.textContent = GPT3Array[currentGPT3Text];
console.log(currentGPT3Text);
console.log(GPT3Array[currentGPT3Text]);
}
// GPT3LoadingText.onclick = generateText;
document.getElementById('loading-screen').addEventListener('click', generateText, {once: false});
</script>
Here is a link to the site showing the issue: https://surfacecollider.net/ (Ignore the other errors in the dev console, they're relating to separate issues that I'm yet to sort out with GLTF loaders which resolve on load).
And here's the php linked to the form action:
<?php
$username = "************";
$password = "************";
$database = "************";
$mysqli = new mysqli("************", $username, $password, $database);
// Don't forget to properly escape your values before you send them to DB
// to prevent SQL injection attacks.
$GPT3_field2 = $mysqli->real_escape_string($_POST['GPT3_field1']);
$query = "INSERT INTO comments (comment)
VALUES ('{$GPT3_field2}')";
$mysqli->query($query);
?>
<?php
$query = $mysqli->query("SELECT * FROM comments");
$query = "SELECT * FROM comments";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$field1name = $row["comments"];
}
/* free result set */
$result->free();
}?>
<?php
$username = "************";
$password = "************";
$database = "************";
$mysqli = new mysqli("***********", $username, $password, $database);
$query = "SELECT * FROM comments";
echo '<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td> <font face="Arial">Username</font> </td>
<td> <font face="Arial">GPT3_field1</font> </td>
</tr>';
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$field1name = $row["GPT3_field1"];
echo '<tr>
<td>'.$field1name.'</td>
</tr>';
}
$result->free();
}
$mysqli->close();?>
Any ideas why this is happening please?