82 lines
3.9 KiB
PHP
82 lines
3.9 KiB
PHP
<?php
|
|
// ============================================================
|
|
// gemini_comment_generator.php
|
|
// Connects to Gemini API to generate natural, driver-like comments
|
|
// ============================================================
|
|
|
|
require_once __DIR__ . '/../../core/bootstrap.php';
|
|
require_once __DIR__ . '/../../functions.php';
|
|
|
|
// The API key should be stored in .env or the database. For now, reading from env.
|
|
$geminiApiKey = getenv('GEMINI_API_KEY');
|
|
|
|
if (!$geminiApiKey) {
|
|
die("Error: GEMINI_API_KEY not set in environment.\n");
|
|
}
|
|
|
|
/**
|
|
* Generate a comment using Gemini API
|
|
*
|
|
* @param string $postContext The content of the post or the discussion
|
|
* @param string $intent What the bot should try to achieve (e.g. "promote_siro", "agree_and_empathize")
|
|
* @return string The generated comment
|
|
*/
|
|
function generateCommentWithGemini($postContext, $intent) {
|
|
global $geminiApiKey;
|
|
|
|
$url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" . $geminiApiKey;
|
|
|
|
$systemInstruction = "أنت سائق سيارة أجرة في الأردن أو السعودية. تتحدث بلهجة عامية بسيطة جداً، وتكتب أحياناً بأسلوب عفوي (ممكن مع أخطاء مطبعية بسيطة لتكون طبيعياً). أنت تستخدم تطبيق 'سيرو' (Siro) للنقل الذكي وتراه التطبيق الأفضل لأنه يوفر دخل ممتاز ولا يظلم السائق. لا تتحدث كمسوق لشركة، بل كزميل ناصح.";
|
|
|
|
$prompt = "السياق/المنشور الذي تقرأه الآن: \n\"" . $postContext . "\"\n\n";
|
|
|
|
if ($intent === 'promote_siro') {
|
|
$prompt .= "المطلوب: اكتب تعليقاً تتعاطف فيه مع المشكلة المذكورة (إن وجدت)، وانصح الزملاء بتجربة تطبيق 'سيرو' كحل واقعي وممتاز لكباتن النقل الذكي. اجعل التعليق قصيراً ومباشراً.";
|
|
} elseif ($intent === 'answer_question') {
|
|
$prompt .= "المطلوب: أجب على السؤال المطروح في المنشور بوضوح بناءً على خبرتك مع تطبيق 'سيرو'.";
|
|
} else {
|
|
$prompt .= "المطلوب: اكتب تعليقاً متفاعلاً بشكل عام مع المنشور بأسلوب السائقين.";
|
|
}
|
|
|
|
$data = [
|
|
"contents" => [
|
|
[
|
|
"parts" => [
|
|
["text" => $systemInstruction . "\n\n" . $prompt]
|
|
]
|
|
]
|
|
],
|
|
"generationConfig" => [
|
|
"temperature" => 0.7, // A bit of creativity for varied responses
|
|
"maxOutputTokens" => 150
|
|
]
|
|
];
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode === 200) {
|
|
$result = json_decode($response, true);
|
|
if (isset($result['candidates'][0]['content']['parts'][0]['text'])) {
|
|
return trim($result['candidates'][0]['content']['parts'][0]['text']);
|
|
}
|
|
}
|
|
|
|
return "والله يا صاحبي جربت تطبيق سيرو وارتحت كثير من المشاكل هذي، جربه ما بتندم."; // Fallback response
|
|
}
|
|
|
|
// Example usage if called directly via CLI:
|
|
if (php_sapi_name() === 'cli') {
|
|
echo "Testing Gemini Generation...\n";
|
|
$testContext = "التطبيقات الثانية بتاخذ عمولة عالية جداً ومش ملحقين بنزين!";
|
|
$generated = generateCommentWithGemini($testContext, 'promote_siro');
|
|
echo "Generated Comment: \n" . $generated . "\n";
|
|
}
|