diff --git a/endpoints/ai/fetch_models.php b/endpoints/ai/fetch_models.php index 2447828..36bcd0d 100644 --- a/endpoints/ai/fetch_models.php +++ b/endpoints/ai/fetch_models.php @@ -4,6 +4,7 @@ require_once '../../includes/connect_endpoint.php'; $chatgptModelsApiUrl = 'https://api.openai.com/v1/models'; $geminiModelsApiUrl = 'https://generativelanguage.googleapis.com/v1beta/models'; +$openrouterModelsApiUrl = 'https://openrouter.ai/api/v1/models'; if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) { if ($_SERVER["REQUEST_METHOD"] === "POST") { @@ -15,7 +16,7 @@ if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) { $aiOllamaHost = isset($data["ollama_host"]) ? trim($data["ollama_host"]) : ''; // Validate ai-type - if (!in_array($aiType, ['chatgpt', 'gemini', 'ollama'])) { + if (!in_array($aiType, ['chatgpt', 'gemini', 'openrouter', 'ollama'])) { $response = [ "success" => false, "message" => translate('error', $i18n) @@ -24,8 +25,8 @@ if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) { exit; } - // Validate ai-api-key and fetch models if ai-type is chatgpt or gemini - if ($aiType === 'chatgpt' || $aiType === 'gemini') { + // Validate ai-api-key and fetch models if ai-type is chatgpt, gemini or openrouter + if ($aiType === 'chatgpt' || $aiType === 'gemini' || $aiType === 'openrouter') { if (empty($aiApiKey)) { $response = [ "success" => false, @@ -45,7 +46,11 @@ if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) { $apiUrl = $chatgptModelsApiUrl; } elseif ($aiType === 'gemini') { $apiUrl = $geminiModelsApiUrl . '?key=' . urlencode($aiApiKey); - } else { + } elseif ($aiType === 'openrouter') { + $headers[] = 'Authorization: Bearer ' . $aiApiKey; + $apiUrl = $openrouterModelsApiUrl; + } + else { // For ollama, no API key is needed // Check for ollama host if (empty($aiOllamaHost)) { diff --git a/endpoints/ai/generate_recommendations.php b/endpoints/ai/generate_recommendations.php index 1b592c1..5fba4e5 100644 --- a/endpoints/ai/generate_recommendations.php +++ b/endpoints/ai/generate_recommendations.php @@ -62,7 +62,7 @@ if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) { $model = isset($aiSettings['model']) ? $aiSettings['model'] : ''; $host = ""; $apiKey = ""; - if (!in_array($type, ['chatgpt', 'gemini', 'ollama']) || !$enabled || empty($model)) { + if (!in_array($type, ['chatgpt', 'gemini', 'openrouter', 'ollama']) || !$enabled || empty($model)) { $response = [ "success" => false, "message" => translate('error', $i18n) @@ -243,6 +243,13 @@ if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) { ] ] ])); + } elseif ($type === 'openrouter') { + $headers[] = 'Authorization: Bearer ' . $apiKey; + curl_setopt($ch, CURLOPT_URL, 'https://openrouter.ai/api/v1/chat/completions'); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ + 'model' => $model, + 'messages' => [['role' => 'user', 'content' => $prompt]] + ])); } curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); @@ -269,7 +276,7 @@ if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) { // Try to decode the AI's JSON reply $replyData = json_decode($reply, true); // decode into array - if ($type === 'chatgpt' && isset($replyData['choices'][0]['message']['content'])) { + if (($type === 'chatgpt' || $type === 'openrouter') && isset($replyData['choices'][0]['message']['content'])) { $recommendationsJson = $replyData['choices'][0]['message']['content']; $recommendations = json_decode($recommendationsJson, true); } elseif ($type === 'gemini' && isset($replyData['candidates'][0]['content']['parts'][0]['text'])) { diff --git a/endpoints/ai/save_settings.php b/endpoints/ai/save_settings.php index ca49df6..af8c8d7 100644 --- a/endpoints/ai/save_settings.php +++ b/endpoints/ai/save_settings.php @@ -12,7 +12,7 @@ if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) { $aiOllamaHost = isset($data['ollama_host']) ? trim($data['ollama_host']) : ''; $aiModel = isset($data['model']) ? trim($data['model']) : ''; - if (empty($aiType) || !in_array($aiType, ['chatgpt', 'gemini', 'ollama'])) { + if (empty($aiType) || !in_array($aiType, ['chatgpt', 'gemini', 'openrouter', 'ollama'])) { $response = [ "success" => false, "message" => translate('error', $i18n) @@ -21,7 +21,7 @@ if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) { exit; } - if (($aiType === 'chatgpt' || $aiType === 'gemini') && empty($aiApiKey)) { + if (($aiType === 'chatgpt' || $aiType === 'gemini' || $aiType === 'openrouter') && empty($aiApiKey)) { $response = [ "success" => false, "message" => translate('invalid_api_key', $i18n) diff --git a/settings.php b/settings.php index ccb812e..962e00b 100644 --- a/settings.php +++ b/settings.php @@ -975,6 +975,7 @@ $userData['currency_symbol'] = $currencies[$main_currency]['symbol'];