-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai.php
101 lines (74 loc) · 1.99 KB
/
openai.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
<?php
// Set the API endpoint URL
$url = 'https://api.openai.com/v1/completions';
// Set your OpenAI API key
$api_key = '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';
// Check if the user has submitted a question
if(isset($_POST['question'])) {
// Get the question from the form data
$question = $_POST['question'];
// Set up the request data
$data = array(
'prompt' => $question,
'temperature' => 0.5,
'max_tokens' => 100,
'stop' => '.'
);
// Set the HTTP headers
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
);
// Initialize the cURL session
$curl = curl_init();
// Set the cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => $headers
));
// Send the HTTP request
$response = curl_exec($curl);
// Close the cURL session
curl_close($curl);
// Decode the JSON response
$json_response = json_decode($response, true);
// Get the generated text from the response
$answer = $json_response['choices'][0]['text'];
} else {
// Set a default question
$question = 'What is the meaning of life?';
// Set an empty answer
$answer = '';
}
// Set the content type header to application/json
header('Content-Type: application/json');
// Create an array to hold the response data
$response = array(
'question' => $question,
'answer' => $answer
);
// Encode the array as JSON
$json = json_encode($response);
// Output the JSON string
echo $json;
?>
<!DOCTYPE html>
<html>
<head>
<title>OpenAI API Example</title>
</head>
<body>
<h1>Ask a Question</h1>
<form method="POST">
<label for="question">Question:</label><br>
<input type="text" id="question" name="question" value="<?php echo $question; ?>"><br>
<br>
<input type="submit" value="Ask">
</form>
<h2>Answer:</h2>
<p><?php echo $answer; ?></p>
</body>
</html>