[PHP] OpenAI API 호출

[광고] 여러분의 추억을 공유해 주세요! - https://김타자.kr
function callOpenAI($messages) {
$api_key = "sk-"; //OpenAI API 키 입력
$model = "gpt-3.5-turbo";
// OpenAI API URL
$url = "https://api.openai.com/v1/chat/completions";
// 요청 데이터 배열 생성
$data = array(
"model" => $model,
"messages" => array(
array("role" => "user", "content" => $messages)
),
"temperature" => 0.7,
"max_tokens" => 500
);
// JSON 변환
$data_json = json_encode($data);
// cURL 초기화
$ch = curl_init();
// cURL 옵션 설정
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Bearer " . $api_key
));
// API 호출 및 응답 저장
$response = curl_exec($ch);
// 오류 체크
if (curl_errno($ch)) {
return "cURL Error: " . curl_error($ch);
}
// cURL 닫기
curl_close($ch);
// JSON 디코딩하여 반환
return json_decode($response, true);
}
댓글 1개
게시판 목록
AI
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 100 |
|
7개월 전 | 215 | |
| 99 | 8개월 전 | 423 | ||
| 98 | 8개월 전 | 386 | ||
| 97 | 8개월 전 | 346 | ||
| 96 |
nekoieye
|
8개월 전 | 630 | |
| 95 | 8개월 전 | 326 | ||
| 94 |
untitled
|
8개월 전 | 369 | |
| 93 | 8개월 전 | 490 | ||
| 92 |
|
8개월 전 | 511 | |
| 91 |
|
8개월 전 | 635 | |
| 90 |
|
8개월 전 | 283 | |
| 89 |
|
8개월 전 | 383 | |
| 88 | 8개월 전 | 350 | ||
| 87 | 8개월 전 | 324 | ||
| 86 | 8개월 전 | 447 | ||
| 85 | 9개월 전 | 516 | ||
| 84 | 9개월 전 | 338 | ||
| 83 |
|
9개월 전 | 352 | |
| 82 | 9개월 전 | 432 | ||
| 81 | 9개월 전 | 423 | ||
| 80 | 9개월 전 | 354 | ||
| 79 | 9개월 전 | 600 | ||
| 78 | 9개월 전 | 561 | ||
| 77 | 9개월 전 | 501 | ||
| 76 | 9개월 전 | 446 | ||
| 75 | 9개월 전 | 499 | ||
| 74 | 9개월 전 | 269 | ||
| 73 | 9개월 전 | 936 | ||
| 72 | 9개월 전 | 452 | ||
| 71 | 9개월 전 | 446 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기