class Api_AI { private $apiKey; private $baseUrl; public function __construct() { // کلید لیارا را قرار بده $this->apiKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiI2OTMxYWNlNmI5MzNjZWNmMmExZjcwNDAiLCJ0eXBlIjoiYWlfa2V5IiwiaWF0IjoxNzY0ODYzMjA2fQ.DHQSU-LeBjj1qAgBPFS1u2tfr1a8OWFlORYHVHBMxM0'; // آدرس جدید مثل نمونه JS $this->baseUrl = 'https://ai.liara.ir/api/6931ac59d829a317c2a477cd/v1/chat/completions'; } /** * ارسال پیام به مدل‌های Liara (OpenAI Compatible) */ public function generateContent($prompt, $model = "google/gemini-2.0-flash-001", $temperature = 0.7, $maxTokens = 256) { $data = [ "model" => $model, "messages" => [ [ "role" => "user", "content" => $prompt ] ], "temperature" => $temperature, "max_tokens" => $maxTokens ]; $jsonData = json_encode($data); $ch = curl_init($this->baseUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $this->apiKey, ]); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); $response = curl_exec($ch); if ($response === false) { $error = 'خطای CURL: ' . curl_error($ch); curl_close($ch); return $error; } curl_close($ch); $responseData = json_decode($response, true); if (isset($responseData['error'])) { return "خطا از Liara AI:\n" . print_r($responseData['error'], true); } if (isset($responseData['choices'][0]['message']['content'])) { return $responseData['choices'][0]['message']['content']; } return "پاسخ نامعتبر:\n" . $response; } }