PHP ChatGPT API 예제 > 자료실

회원로그인

오늘 본 게시물

오늘 본 페이지

없음

오늘 본 C.m.A Util

없음

자료실

자료실

PHP ChatGPT API 예제

페이지 정보

본문

첨부해 드린 gpt-form.php 와 gpt-run.php 를 웹서버의 동일한 폴더에 업로드 합니다.
gpt-run.php 의 api_key 에 openai api key 를 발행하여 입력합니다.



<?php
$api_key = "affdsfsfssfdfsddffdsdsfsdsdfdfs";

function translate($content, $source='ko', $target='en') {
	$handle = curl_init();
	curl_setopt($handle, CURLOPT_URL,'https://www.googleapis.com/language/translate/v2');
	curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
	$data = array('key' => "API Key",
					'q' => $content,
					'source' => $source,
					'target' => $target);
	curl_setopt($handle, CURLOPT_POSTFIELDS, preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', http_build_query($data)));
	curl_setopt($handle,CURLOPT_HTTPHEADER,array('X-HTTP-Method-Override: GET'));
	$response = curl_exec($handle);
	$responseDecoded = json_decode($response, true);
	$responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
	curl_close($handle);
	return  $responseDecoded['data']['translations'][0]['translatedText'];
}

function ko2en($content) {
	return translate($content, 'ko', 'en');
}

function en2ko($content) {
	return translate($content, 'en', 'ko');
}

$url = "https://api.openai.com/v1/completions";

// "What is the capital of France?"
$prompt = filter_var($_POST["prompt"], FILTER_SANITIZE_STRING);;
$data = array(
	"model" => "text-davinci-003",
	"prompt" => $prompt,
	"max_tokens" => 3000,
	"temperature" => 0.5,
);

$data_string = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	"Content-Type: application/json",
	"Authorization: Bearer $api_key",
	"Content-Length: " . strlen($data_string))
);

$output = curl_exec($ch);
curl_close($ch);
// print_r($output);

$output_json = json_decode($output, true);
$response = $output_json["choices"][0]["text"];
// echo $response;
?>

<form method="post" id="gpt-run" action="gpt-form.php" onsubmit="return true;" style="display:none;">   <textarea id="response" name="response"><?php echo trim($response); ?></textarea>   <textarea id="prompt" name="prompt"><?php echo $prompt; ?></textarea>   <input type="submit" value="Submit"> </form>

<script> document.getElementById("gpt-run").submit(); </script>

추천 0 비추천 0

첨부파일

첨부파일 2

  • 회사 : Cginjs
  • 대표 : Cginjs
  • 주소 :
  • 메일 : admin@mysample.com
  • 사업자 등록번호 :
Copyright © Cginjs All rights reserved.
notice
Warning: Undefined variable $HTTP_ACCEPT_LANGUAGE in /cginjs/www/bbs/visit_insert.inc.php on line 131