mirror of
https://github.com/ellite/Wallos.git
synced 2025-12-30 17:59:54 -06:00
add openrouter endpoint (#922)
This commit is contained in:
@@ -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)) {
|
||||
|
||||
@@ -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'])) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -975,6 +975,7 @@ $userData['currency_symbol'] = $currencies[$main_currency]['symbol'];
|
||||
<select id="ai_type" name="ai_type" onchange="toggleAiInputs()">
|
||||
<option value="chatgpt" <?= (isset($aiSettings['type']) && $aiSettings['type'] == 'chatgpt') ? 'selected' : '' ?>>ChatGPT</option>
|
||||
<option value="gemini" <?= (isset($aiSettings['type']) && $aiSettings['type'] == 'gemini') ? 'selected' : '' ?>>Gemini</option>
|
||||
<option value="openrouter" <?= (isset($aiSettings['type']) && $aiSettings['type'] == 'openrouter') ? 'selected' : '' ?>>OpenRouter</option>
|
||||
<option value="ollama" <?= (isset($aiSettings['type']) && $aiSettings['type'] == 'ollama') ? 'selected' : '' ?>>Local Ollama</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user