테스트 사이트 - 개발 중인 베타 버전입니다

제미나이 ai extend 님 소스 질문 채택완료

뉴마커스 10개월 전 조회 2,291

내용이 부실할때 내용을 채워서 등록해주는 제미나이 ai extend

https://sir.kr/g5_plugin/12600?sfl=wr_subject&stx=ai

 

리자님 올려주신 AI 글쓰기를 적용해봤는데요. 특정 게시판에 적용하게 하려면 어찌해야할까요??

 

 

 gemini-content-expander.extend.php</strong></p>

<p><strong><?php

if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가</strong></p>

<p><strong>// Gemini API를 사용하여 텍스트를 확장하는 함수

function extend_text_with_gemini($text) {

    if (!defined('G5_GEMINI_API_KEY') || empty(G5_GEMINI_API_KEY)) {

        return $text; // API 키가 설정되지 않은 경우 원본 텍스트 반환

    }</strong></p>

<p><strong>    $api_key = G5_GEMINI_API_KEY;

    $url = '<a href="https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key='" target="_blank" rel="noopener noreferrer">https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key='</a> . $api_key;</strong></p>

<p><strong>    $data = [

        'contents' => [

            ['parts' => [['text' => "Please expand on the following text, maintaining the original context and tone: $text"]]],

        ],

        'generationConfig' => [

            'temperature' => 0.7,

            'topK' => 40,

            'topP' => 0.95,

            'maxOutputTokens' => 1024,

        ],

    ];</strong></p>

<p><strong>    $options = [

        'http' => [

            'method'  => 'POST',

            'header'  => 'Content-Type: application/json',

            'content' => json_encode($data)

        ]

    ];</strong></p>

<p><strong>    $context  = stream_context_create($options);

    $result = file_get_contents($url, false, $context);</strong></p>

<p><strong>    if ($result === FALSE) {

        return $text; // API 호출 실패 시 원본 텍스트 반환

    }</strong></p>

<p><strong>    $response = json_decode($result, true);

    

    if (isset($response['candidates'][0]['content']['parts'][0]['text'])) {

        return $response['candidates'][0]['content']['parts'][0]['text'];

    }</strong></p>

<p><strong>    return $text; // 응답에서 텍스트를 찾을 수 없는 경우 원본 텍스트 반환

}</strong></p>

<p><strong>// 게시글 작성 시 내용 확장

add_event('write_update_before', 'extend_content_before_update', 10);

function extend_content_before_update($board = null, $wr_id = null, $w = '', $qstr = '', $redirect_url = '') {

    global $wr_content;</strong></p>

<p><strong>    // 내용이 짧은 경우에만 확장

    if (isset($wr_content) && mb_strlen($wr_content) < 100) {

        $wr_content = extend_text_with_gemini($wr_content);

    }

}</strong></p>

<p><strong>

댓글을 작성하려면 로그인이 필요합니다.

답변 1개

채택된 답변
+20 포인트
10개월 전

해당 소스는 extend 에 넣어서 항상 실행하도록 되어있는데요

특정 게시판에서만 할거라면 펑션 부분에 조건문을 추가해주면됩니다.

 

</p>

<p> </p>

<p>if($_GET['bo_table'] == 'free'){ // 보드ID 가 free 인 게시판에서만 적용</p>

<p>    // 게시글 작성 시 내용 확장

    add_event('write_update_before', 'extend_content_before_update', 10);

    function extend_content_before_update($board = null, $wr_id = null, $w = '', $qstr = '', $redirect_url = '') {

        global $wr_content;

        // 내용이 짧은 경우에만 확장

        if (isset($wr_content) && mb_strlen($wr_content) < 100) {

            $wr_content = extend_text_with_gemini($wr_content);

        }

    }</p>

<p>}</p>

<p>

 

위 코드를 특정 게시판의 보드ID 로 조건문을 거시면 해당 보드 내에서만 작동합니다.

보드 ID 는

 

 

해당 게시판 URL 뒤 bo_table= 에 할당된 ID 를 말합니다.

 

if($_GET['bo_table'] != 'free'){ // 보드ID 가 free 인 게시판 제외

if($_GET['bo_table'] == 'free' || $_GET['bo_table'] == 'test'){ // free 보드와 test 보드에서 적용

 

이렇게 복수 지정도 가능합니다.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

뉴마커스
10개월 전
저의 수준에서 자세히 알려주셔서 감사합니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인