Skip to content

Commit e41dd4b

Browse files
authored
Add files via upload
1 parent ddbe94f commit e41dd4b

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

config.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
// config file
3+
define('OPENAI_API_KEY', 'sk-.................................');
4+
define('MODEL', 'text-davinci-003');
5+
define('TEMPERATURE', 0.9);
6+
define('MAX_TOKENS', 1000);
7+
define('TOP_P', 1);
8+
define('FREQUENCY_PENALTY', 0.0);
9+
define('PRESENCE_PENALTY', 0.0);

gptchat.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
header('Content-Type: application/json');
3+
header('Access-Control-Allow-Origin: *');
4+
header('Access-Control-Allow-Methods: POST');
5+
header('Access-Control-Allow-Headers: Content-Type, Authorization');
6+
7+
require_once 'config.php';
8+
9+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
10+
$input = json_decode(file_get_contents('php://input'), true);
11+
$message = urlencode($input['message']);
12+
13+
$ch = curl_init();
14+
15+
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/engines/" . MODEL . "/completions");
16+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
17+
curl_setopt($ch, CURLOPT_POST, 1);
18+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
19+
"prompt" => $message,
20+
"max_tokens" => MAX_TOKENS,
21+
"temperature" => TEMPERATURE,
22+
"top_p" => TOP_P,
23+
"frequency_penalty" => FREQUENCY_PENALTY,
24+
"presence_penalty" => PRESENCE_PENALTY
25+
)));
26+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
27+
"Content-Type: application/json",
28+
"Authorization: Bearer " . OPENAI_API_KEY
29+
));
30+
31+
$response = curl_exec($ch);
32+
echo $response;
33+
34+
curl_close($ch);
35+
} else {
36+
http_response_code(405);
37+
echo json_encode(['error' => 'Method not allowed']);
38+
}

script.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const chatForm = document.getElementById("chat-form");
2+
const chatInput = document.getElementById("chat-input");
3+
const chatOutput = document.getElementById("chat-output");
4+
5+
chatForm.addEventListener("submit", async (e) => {
6+
e.preventDefault();
7+
const message = chatInput.value.trim();
8+
if (!message) return;
9+
10+
chatOutput.innerHTML += `<p class="user-message">${message}</p>`;
11+
chatInput.value = "";
12+
chatOutput.scrollTop = chatOutput.scrollHeight;
13+
14+
const response = await fetch("gptchat.php", {
15+
method: "POST",
16+
headers: {
17+
"Content-Type": "application/json",
18+
},
19+
body: JSON.stringify({ message }),
20+
});
21+
22+
if (response.ok) {
23+
const data = await response.json();
24+
if (data.choices && data.choices[0] && data.choices[0].text) {
25+
chatOutput.innerHTML += `<p class="bot-message">${data.choices[0].text}</p>`;
26+
} else {
27+
console.error("Error: Unexpected response format", data);
28+
}
29+
chatOutput.scrollTop = chatOutput.scrollHeight;
30+
} else {
31+
console.error("Error communicating with GPTChat API");
32+
}
33+
});

0 commit comments

Comments
 (0)