I have a functional code in php using the GPT-3 API.
For now, I store the user’s question plus the GPT answer then when the user asks a new question, I use the last question + the last GPT answer + the new question to formulate my prompt, but it doesn’t work!!
A help?
Here is my code to get an answer :
private function getResponseFromGPT3($question) {
// Connect to the database
try {
$db = new PDO('mysql:host=localhost;dbname=c1626908c_bot', 'c1626908c_bot', 'Canada1995');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
// Retrieve conversation context
try {
$stmt = $db->prepare("SELECT conversation_context FROM conversations WHERE user_id = :user_id ORDER BY created_at DESC LIMIT 1");
$stmt->bindValue(':user_id', $this->chatId);
$stmt->execute();
$conversation_context = $stmt->fetchColumn();
} catch (PDOException $e) {
echo 'Retrieve failed: ' . $e->getMessage();
}
// Retrieve last question
try {
$stmt = $db->prepare("SELECT question FROM conversations WHERE user_id = :user_id ORDER BY created_at DESC LIMIT 1");
$stmt->bindValue(':user_id', $this->chatId);
$stmt->execute();
$last_question_from_user = $stmt->fetchColumn();
} catch (PDOException $e) {
echo 'Retrieve failed: ' . $e->getMessage();
}
// Use conversation context, last question and current question to construct question
$question = "Moi : " . $last_question_from_user . PHP_EOL . "Bot : " . $conversation_context . PHP_EOL . "Moi : " . $question . PHP_EOL . "Bot : ";
$model = "text-davinci-003";
$url = "https://api.openai.com/v1/completions";
$data = array(
"model" => $model,
"prompt" => $question,
"max_tokens" => 4000
);