OpenAI
PHPでOpenAIのチャットボットを作る。
$prompt = isset($_POST[‘prompt’]) ? $_POST[‘prompt’] : ”;
	$API_KEY = ‘xx OpenAIからAPI_KEY xxx’;
	$headers = array(
		‘Content-Type: application/json’,
		‘Authorization: Bearer ‘ . $API_KEY
	);
	$data = array(
		‘model’ => ‘gpt-3.5-turbo’,
		‘messages’ => array([“role”=>”user”, “content”=>$prompt]),
		“max_tokens” => 4050,
		“temperature” => 0.9,
		“top_p” => 0.95,
		“frequency_penalty” => 0.0,
		“presence_penalty” => 0.0,
		“stop” => array(” Human:”, ” AI:”)
	);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, ‘https://api.openai.com/v1/chat/completions’);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($ch);
	curl_close($ch);
	$response_data = json_decode($response, true);
	echo nl2br(htmlspecialchars(trim($response_data[‘choices’][0][‘message’][‘content’])));


