Код отправляет запрос к ChatGPT API для генерации ответа в стиле Шекспира на вопрос о компьютерах и выводит полученный ответ:
Код |
---|
<?php
$apiKey = 'YOUR_API_KEY';
$url = 'https://api.openai.com/v1/chat/completions';
$data = [
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'system', 'content' => 'You are Shakespeare. Respond in Shakespearean English.'],
['role' => 'user', 'content' => 'What are computers?']
],
'temperature' => 0.7
];
$options = [
'http' => [
'header' => "Content-Type: application/json\r\nAuthorization: Bearer $apiKey\r\n",
'method' => 'POST',
'content' => json_encode($data)
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$result = json_decode($response, true);
echo $result['choices'][0]['message']['content'];
?> |