첫 라마ai를 활용한 자료를 올려봅니다.
groq에서 제공하는 가이드를 클로드한테 줘서 만들어달라 했네요.
[code]
<?php
// 게시글 정보 가져오기
$post_content = strip_tags($view['wr_content']); // HTML 태그 제거
$post_title = $view['wr_subject'];
$post_author = $view['wr_name'];
// API에 전송할 프롬프트 구성
$prompt = "다음 게시글이 스팸인지 분석해주세요. 제목: {$post_title}, 작성자: {$post_author}, 내용: {$post_content}";
$prompt .= "\n\n스팸이면 '스팸', 정상글이면 '정상'이라고만 답변해주세요.";
// API 요청 데이터 준비
$data = array(
'messages' => array(
array(
'role' => 'user',
'content' => $prompt
)
),
'model' => 'llama-3.3-70b-versatile',
'temperature' => 0.1 // 일관된 결과를 위해 낮은 temperature 설정
);
// API 설정
$url = 'https://api.groq.com/openai/v1/chat/completions';
$api_key = 'groq_api키를 삽입';
// cURL 초기화 및 설정
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json'
)
));
// API 요청 실행
$response = curl_exec($ch);
// 응답 처리
if (!curl_errno($ch)) {
$result = json_decode($response, true);
// API 응답에서 판단 결과 추출
if (isset($result['choices'][0]['message']['content'])) {
$ai_response = strtolower(trim($result['choices'][0]['message']['content']));
// 스팸으로 판단된 경우 경고 메시지 출력
if ($ai_response === '스팸') {
echo '<div class="alert alert-danger" style="padding: 15px; margin: 20px 0; border: 1px solid #d9534f; border-radius: 4px; color: #d9534f; background-color: #f2dede;">';
echo '<strong>주의!</strong> 이 게시글은 스팸으로 의심됩니다.';
echo '</div>';
}
}
}
// cURL 세션 종료
curl_close($ch);
?>
[/code]
이 코드를 view.skin.php 원하는 위치에 추가하시면 됩니다, 좋아요 근처에 추가하는게 제일 이쁜 것 같아요.
아래 사진처럼 표시됩니다.

다만, "투자, 마사지" 등의 특정 단어가 들어가는 게시글이 떳떳한 게시글인데도 불구하고 스팸 표시되는 경우가 많은 것 같습니다.
스팸 지수 버전:
[code]
<?php
// 게시글 정보 가져오기
$post_content = strip_tags($view['wr_content']); // HTML 태그 제거
$post_title = $view['wr_subject'];
$post_author = $view['wr_name'];
// API에 전송할 프롬프트 구성
$prompt = "다음 게시글이 스팸인지 분석해주세요. 제목: {$post_title}, 작성자: {$post_author}, 내용: {$post_content}
다음 형식으로 답변해주세요:
1줄: 스팸 지수(0-100 사이의 숫자만)
2줄: '스팸' 또는 '정상'";
// API 요청 데이터 준비
$data = array(
'messages' => array(
array(
'role' => 'user',
'content' => $prompt
)
),
'model' => 'llama-3.3-70b-versatile',
'temperature' => 0.1
);
// API 설정
$url = 'https://api.groq.com/openai/v1/chat/completions';
$api_key = 'groq_api키';
// cURL 초기화 및 설정
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json'
)
));
// API 요청 실행
$response = curl_exec($ch);
// 응답 처리
if (!curl_errno($ch)) {
$result = json_decode($response, true);
// API 응답에서 판단 결과 추출
if (isset($result['choices'][0]['message']['content'])) {
$ai_response = explode("\n", trim($result['choices'][0]['message']['content']));
$spam_score = intval($ai_response[0]);
$is_spam = strtolower(trim($ai_response[1])) === '스팸';
// 스팸 지수에 따른 색상 설정
$score_color = '';
if ($spam_score < 30) {
$score_color = '#28a745'; // 초록색 (안전)
} elseif ($spam_score < 70) {
$score_color = '#ffc107'; // 노란색 (주의)
} else {
$score_color = '#dc3545'; // 빨간색 (위험)
}
// 스팸 지수 표시 HTML
echo '<div class="spam-score-container" style="margin: 20px 0; padding: 15px; border-radius: 4px; background-color: #f8f9fa;">';
echo '<div style="margin-bottom: 10px;">스팸 지수:</div>';
echo '<div style="position: relative; height: 30px; background-color: #e9ecef; border-radius: 15px; overflow: hidden;">';
echo "<div style='position: absolute; width: {$spam_score}%; height: 100%; background-color: {$score_color}; transition: width 0.5s ease-in-out;'></div>";
echo "<div style='position: absolute; width: 100%; height: 100%; text-align: center; line-height: 30px; color: " . ($spam_score > 50 ? 'white' : 'black') . ";'>{$spam_score}%</div>";
echo '</div>';
// 스팸으로 판단된 경우 경고 메시지
if ($is_spam) {
echo '<div class="alert alert-danger" style="margin-top: 15px; padding: 15px; border: 1px solid #d9534f; border-radius: 4px; color: #d9534f; background-color: #f2dede;">';
echo '<strong>주의!</strong> 이 게시글은 스팸으로 의심됩니다.';
echo '</div>';
// 관리자에게 알림 기능 (선택적)
if ($is_admin) {
// 스팸 로그 기록
$spam_log = G5_DATA_PATH . '/spam_log.txt';
$log_content = date('Y-m-d H:i:s') . " | {$post_title} | {$post_author} | 스팸지수: {$spam_score}%\n";
file_put_contents($spam_log, $log_content, FILE_APPEND);
}
}
}
}
// cURL 세션 종료
curl_close($ch);
// CSS 스타일 추가
echo '<style>
.spam-score-container {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
.spam-score-container div {
font-size: 14px;
}
</style>';
?>
[/code]
api 발급:
[api이용료 무료]
댓글 7개
게시글 목록
| 번호 | 제목 |
|---|---|
| 13281 | |
| 13259 | |
| 13250 | |
| 13228 | |
| 13204 | |
| 13193 | |
| 13176 | |
| 13175 | |
| 13170 | |
| 13159 | |
| 13145 | |
| 13144 |

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