COMING SOON 🚀

u

[PHP] OpenAI API 호출

31463534_1742957901.3594.jpg

 

 

 

 

 

 

 




[광고] 여러분의 추억을 공유해 주세요! - 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

+
제목 글쓴이 날짜 조회
10개월 전 조회 866
10개월 전 조회 598
10개월 전 조회 851
10개월 전 조회 558
10개월 전 조회 997
10개월 전 조회 557
10개월 전 조회 994
10개월 전 조회 831
10개월 전 조회 513
11개월 전 조회 714
11개월 전 조회 680
11개월 전 조회 709
11개월 전 조회 1,018
11개월 전 조회 625
11개월 전 조회 662
11개월 전 조회 791
11개월 전 조회 786
11개월 전 조회 918
11개월 전 조회 596
11개월 전 조회 764
11개월 전 조회 686
11개월 전 조회 784
11개월 전 조회 773
12개월 전 조회 831
1년 전 조회 682
1년 전 조회 690
1년 전 조회 818
1년 전 조회 736
1년 전 조회 644
1년 전 조회 954