Merhaba forum üyeleri,
Bir süredir denemelerini yaptığım kodu beraber geliştirmek , öneri almak ve herkese açık isteyenin istediği şekilde kullanabilmesi için paylaşıyorum.
Bu sayede ihtiyacı olan arkadaşlar kodu kullanabilir. Bu konuda deneyimli olan arkadaşlar yardımcı olup kodu geliştirebilir. Prompt yazma konusunda uzmanlaşmış kişiler de denediği promptları ekleyebilirler.
Ayrıca kodu chatgptye yazdırdım çalışmayan bölümlerini düzenledim, bunu da belirtmek isterim.
Kodu kullanmak için chat gpt api keye ihtiyacımız var. Gpt apikeyi APİ KEY GİRİN bölümüne yazmanız gerekiyor. ÖRNEK KONU yazan bölüme sitenizin ana konusunu yazmanız gerekiyor.
Daha sonra bu dosyayı cpanelden cronjob olarak istediğiniz zaman çalıştırdığınızda otomatik olarak içerik paylaşılacaktır. Tabi isterseniz manuel de çalıştırabilirsiniz.
Kod önce ana konudan kategori üretiyor, üretilen kategoriye başlık üretiyor, başlığa içerik üretiyor, içeriğin uzun olması için önceki içeriği baz alarak devamını getiriyor ve paylaşıyor.
******************************
Versiyon 2 geldi, sunucuda yük oluşturmamak için ajax mantığı ile çalışıyor. Ek olarak promptlarla sayfalar arası yeni sayfalar ekleyerek soru cevap şeklinde makale uzunluğu ayarlanabilir. Versiyon 3'de bunu da yapacağım. Versiyon 4'de öneçıkarılan görsel oluşturma ve oluşturulan kategorileri , içeriği post ederken kategorileştirmeyi deneyeceğim.
Versiyon 2:
Dosyaları açtığınız bir klasör içerisinde sunucuya atın. örnek olarak x dosyası diyelim. X dosyasının içerisine bu dosyaları oluşturun ve kodları kendinize göre düzenleyin. X klasörünü manuel olaak ya da cronjob ile çalıştırın. Kodlardaki api key girin ve konuyu girin bölümlerini değiştirmeyi unutmayın.
index.php : <!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div id="kategoriSonuc"></div>
<div id="baslikSonuc"></div>
<div id="icerikSonuc"></div>
</body>
</html>script.js : $(document).ready(function() {
$.ajax({
url: 'kategori.php',
success: function(kategoriResponse) {
$('#kategoriSonuc').html("Kategori: " + kategoriResponse);
// İlk adım başarılı olduktan sonra ikinci adımı başlat
$.ajax({
url: 'baslik.php?kategori=' + kategoriResponse,
success: function(baslikResponse) {
$('#baslikSonuc').html("Başlık: " + baslikResponse);
// İkinci adım başarılı olduktan sonra üçüncü adımı başlat
$.ajax({
url: 'icerik.php?baslik=' + baslikResponse,
success: function(icerikResponse) {
$('#icerikSonuc').html("İçerik: " + icerikResponse);
},
error: function() {
alert("İçerik alınamadı");
}
});
},
error: function() {
alert("Başlık alınamadı");
}
});
},
error: function() {
alert("Kategori alınamadı");
}
});
});kategori.php: <?php
$openai_api_key ='APİ-KEY-GİRİN';
$x = "KONUYU GİRİN";
// Kategori için prompt
$kategori_prompt = $x;
$kategori_response = sendToOpenAI($kategori_prompt, $openai_api_key, 20); // Örnek token sınırı
$kategori = json_decode($kategori_response, true)['choices'][0]['message']['content'];
echo $kategori;
function sendToOpenAI($prompt, $api_key, $max_tokens) {
$url = 'https://api.openai.com/v1/chat/completions';
$data = json_encode([
"model" => "gpt-3.5-turbo",
"messages" => [
["role" => "system", "content" => "Behave like a category generator for a website. Generate one category for the given main topic. There should be no words other than the category text, only provide the category text as the answer. Respond in Turkish."],
["role" => "user", "content" => $prompt]
],
"max_tokens" => $max_tokens
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'CURL Hatası: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
?>baslik.php : <?php
$openai_api_key = 'APİ-KEY-GİRİN'';
if (isset($_GET['kategori'])) {
$kategori = $_GET['kategori'];
$baslik_response = sendToOpenAI2($kategori, $openai_api_key, 50);
$baslik = json_decode($baslik_response, true)['choices'][0]['message']['content'];
echo $baslik;
}
function sendToOpenAI2($prompt, $api_key, $max_tokens) {
$url = 'https://api.openai.com/v1/chat/completions';
$data = json_encode([
"model" => "gpt-3.5-turbo",
"messages" => [
["role" => "system", "content" => "Act like a blog post title generator. I want you to generate a title that is SEO-compliant, relevant to the given category, engaging, niche-specific, and includes keywords about the category. When given a category, respond with only the title, without sending any other text content. Generate one title. Respond in Turkish."],
["role" => "user", "content" => $prompt]
],
"max_tokens" => $max_tokens
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'CURL Hatası: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
?>icerik.php: <?php
require_once('../wp-load.php');
$openai_api_key = 'APİ-KEY-GİRİN';
if (isset($_GET['baslik'])) {
$baslik = $_GET['baslik'];
$icerik_response = sendToOpenAI3($baslik, $openai_api_key, 1500);
$icerikk = json_decode($icerik_response, true)['choices'][0]['message']['content'];
addContentToWordPress($baslik, $icerikk);
echo $icerikk;
// İçerik işlemleri burada gerçekleştirildikten sonra
}
function sendToOpenAI3($prompt, $api_key, $max_tokens) {
$url = 'https://api.openai.com/v1/chat/completions';
$data = json_encode([
"model" => "gpt-3.5-turbo",
"messages" => [
["role" => "system", "content" => "You are an article writer. You write articles for your website. You write the articles in a way that is suitable for SEO, using headings such as h1, h2, and addressing the readers. The texts you write consist of at least 750 words and meaningful sentences that do not repeat each other. You write your texts using the WordPress platform. You are creating excellent content. You write articles that match the provided titles. Respond in Turkish. Use a human and fun language. Write in a conversational tone as if you're explaining it to a friend."],
["role" => "user", "content" => $prompt]
],
"max_tokens" => $max_tokens
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'CURL Hatası: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
// İşlemler tamamlandıktan sonra başlık ve içerik eklemek için bu işlevi kullanabilirsiniz
function addContentToWordPress($baslik, $icerik) {
if (!empty($baslik) && !empty($icerik)) {
$new_post = array(
'post_title' => $baslik,
'post_content' => $icerik,
'post_status' => 'publish',
);
$post_id = wp_insert_post($new_post);
if ($post_id) {
echo '<br>';
echo 'İçerik başarıyla eklendi. Gönderi ID: ' . $post_id;
echo '<br>';
} else {
echo '<br>';
echo 'İçerik eklerken bir hata oluştu.';
echo '<br>';
}
} else {
echo '<br>';
echo 'Başlık veya içerik eksik.';
echo '<br>';
}
}
?>Versiyon 1:
<?php
// WordPress yükleme dosyasını dahil edin
require_once('wp-load.php');
// OpenAI API bilgileri
$openai_api_key = 'APİ KEY GİRİN';
// Konu
$x = "ÖRNEK KONU";
// Kategori için prompt
$kategori_prompt = $x;
// OpenAI'den kategori üretin
$kategori_response = sendToOpenAI($kategori_prompt, $openai_api_key, 20); // Örnek token sınırı
$kategori = json_decode($kategori_response, true)['choices'][0]['message']['content'];
// OpenAI'den başlık üretin
$baslik_response = sendToOpenAI2($kategori, $openai_api_key, 50); // Örnek token sınırı
$baslik = json_decode($baslik_response, true)['choices'][0]['message']['content'];
// OpenAI'den içerik üretin
$icerik_response = sendToOpenAI3($baslik, $openai_api_key, 1500); // İçerik için daha uzun bir token sınırı
$icerik = json_decode($icerik_response, true)['choices'][0]['message']['content'];
$icerik_response2 = sendToOpenAI4($icerik, $openai_api_key, 1500); // İçerik için daha uzun bir token sınırı
$icerik2 = json_decode($icerik_response2, true)['choices'][0]['message']['content'];
// Dönen veriyi görüntüle
echo "Kategori: " . $kategori;
echo "<br>";
echo "<br>";
echo "Başlık: " . $baslik;
echo "<br>";
echo "<br>";
echo "İçerik: " . $icerik;
echo "<br>";
echo "<br>";
echo "İçerik: " . $icerik2;
echo "<br>";
echo "<br>";
$birlesikDegisken = $icerik . " " . $icerik2;
echo "İçerik birleşik: " . $birlesikDegisken;
echo "<br>";
echo "<br>";
// WordPress'e içerik ekleyin
addContentToWordPress($baslik, $birlesikDegisken);
function sendToOpenAI($prompt, $api_key, $max_tokens) {
// Örnek olarak, CURL ile API'ye bir POST isteği göndermek:
$url = 'https://api.openai.com/v1/chat/completions';
$data = json_encode([
"model" => "gpt-3.5-turbo",
"messages" => [
["role" => "system", "content" => "Behave like a category generator for a website. Generate one category for the given main topic. There should be no words other than the category text, only provide the category text as the answer.Respond in Turkish."],
["role" => "user", "content" => $prompt]
],
"max_tokens" => $max_tokens // Token sınırını belirtin
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'CURL Hatası: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
function sendToOpenAI2($prompt, $api_key, $max_tokens) {
// Örnek olarak, CURL ile API'ye bir POST isteği göndermek:
$url = 'https://api.openai.com/v1/chat/completions';
$data = json_encode([
"model" => "gpt-3.5-turbo",
"messages" => [
["role" => "system", "content" => "Act like a blog post title generator. I want you to generate a title that is SEO-compliant, relevant to the given category, engaging, niche-specific, and includes keywords about the category. When given a category, respond with only the title, without sending any other text content. Generate one title.Respond in Turkish."],
["role" => "user", "content" => $prompt]
],
"max_tokens" => $max_tokens // Token sınırını belirtin
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'CURL Hatası: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
function sendToOpenAI3($prompt, $api_key, $max_tokens) {
// Örnek olarak, CURL ile API'ye bir POST isteği göndermek:
$url = 'https://api.openai.com/v1/chat/completions';
$data = json_encode([
"model" => "gpt-3.5-turbo",
"messages" => [
["role" => "system", "content" => "You are an article writer. You write articles for your website. You write the articles in a way that is suitable for SEO, using headings such as h1, h2, and addressing the readers. The texts you write consist of at least 750 words and meaningful sentences that do not repeat each other. You write your texts using the WordPress platform. You are creating excellent content. You write articles that match the provided titles.Respond in Turkish. Use a human and fun language. Write in a conversational tone as if you're explaining it to a friend."],
["role" => "user", "content" => $prompt]
],
"max_tokens" => $max_tokens // Token sınırını belirtin
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'CURL Hatası: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
function sendToOpenAI4($prompt, $api_key, $max_tokens) {
// Örnek olarak, CURL ile API'ye bir POST isteği göndermek:
$url = 'https://api.openai.com/v1/chat/completions';
$data = json_encode([
"model" => "gpt-3.5-turbo",
"messages" => [
["role" => "system", "content" => "Bring me the rest of the article. Based on the article I sent you, write about different topics, and avoid repeating the same sentences. Respond in Turkish. Use a human and fun language. Write in a conversational tone as if you're explaining it to a friend. Based on the article I sent you, write about different topics, and avoid repeating the same sentences."],
["role" => "user", "content" => $prompt]
],
"max_tokens" => $max_tokens // Token sınırını belirtin
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'CURL Hatası: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
function addContentToWordPress($title, $content) {
if (!empty($title) && !empty($content)) {
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
);
$post_id = wp_insert_post($new_post);
if ($post_id) {
echo 'İçerik başarıyla eklendi. Gönderi ID: ' . $post_id;
} else {
echo 'İçerik eklerken bir hata oluştu.';
}
} else {
echo 'OpenAI API den boş bir yanıt aldınız.';
}
}
?> Düzeltme : İçeriği uzatmak için girilen prompt düzenlendi.
Ek olarak yardımcı olamak isteyen arkadaşlar, dall-e kullanarak öne çıkarılan görsel eklemeyi de koda eklerse çok makbule geçer.
İyi forumlar!