From 14b890c920b44e80c795571f2fc3f7f7e4cfe2b9 Mon Sep 17 00:00:00 2001 From: Maaxxs <61059039+MaaxxsDev@users.noreply.github.com> Date: Mon, 31 Aug 2026 10:18:46 +0200 Subject: [PATCH] Support switching the AI lookup between Claude and OpenAI api/lookup.php now branches on \$AI_PROVIDER in config.php ('anthropic' or 'openai'). Claude uses the existing Anthropic SDK path; OpenAI is called via a plain cURL request (Chat Completions, JSON response_format) to avoid a second SDK dependency. Only the selected provider's key needs to be set. --- api/config.example.php | 9 ++- api/lookup.php | 131 ++++++++++++++++++++++++++++++----------- 2 files changed, 103 insertions(+), 37 deletions(-) diff --git a/api/config.example.php b/api/config.example.php index f1be060..0cd5cbb 100644 --- a/api/config.example.php +++ b/api/config.example.php @@ -1,5 +1,12 @@ 'Kein Anthropic API-Key konfiguriert. api/config.example.php nach api/config.php kopieren und Key eintragen.', - ]); - exit; -} +$provider = $provider ?: 'anthropic'; +$openaiModel = $openaiModel ?: 'gpt-4o-mini'; $body = json_decode(file_get_contents('php://input'), true); $query = trim((string)($body['query'] ?? '')); @@ -37,12 +39,6 @@ if ($query === '' || mb_strlen($query) > 80) { exit; } -require __DIR__ . '/vendor/autoload.php'; - -use Anthropic\Client; - -$client = new Client(apiKey: $apiKey); - $systemPrompt = <<messages->create( - model: 'claude-opus-5', - maxTokens: 1024, - outputConfig: ['effort' => 'low'], - system: $systemPrompt, - messages: [ - ['role' => 'user', 'content' => $query], - ], - ); + if ($provider === 'openai') { + if (!$openaiKey) { + throw new RuntimeException('Kein OpenAI API-Key konfiguriert. api/config.example.php nach api/config.php kopieren, AI_PROVIDER auf "openai" setzen und OPENAI_API_KEY eintragen.'); + } + $text = lookupViaOpenAI($query, $systemPrompt, $openaiKey, $openaiModel); + } else { + if (!$anthropicKey) { + throw new RuntimeException('Kein Anthropic API-Key konfiguriert. api/config.example.php nach api/config.php kopieren und ANTHROPIC_API_KEY eintragen.'); + } + $text = lookupViaClaude($query, $systemPrompt, $anthropicKey); + } } catch (\Throwable $e) { http_response_code(502); echo json_encode(['error' => 'KI-Anfrage fehlgeschlagen: ' . $e->getMessage()]); exit; } -$text = ''; -foreach ($message->content as $block) { - if ($block->type === 'text') { - $text .= $block->text; - } -} - if (!preg_match('/\{.*\}/s', $text, $m)) { http_response_code(502); echo json_encode(['error' => 'Antwort konnte nicht gelesen werden.']); @@ -101,3 +92,71 @@ if (!is_array($data)) { } echo json_encode($data); + +// ---------- Anbieter ---------- + +function lookupViaClaude(string $query, string $systemPrompt, string $apiKey): string +{ + require __DIR__ . '/vendor/autoload.php'; + + $client = new \Anthropic\Client(apiKey: $apiKey); + + $message = $client->messages->create( + model: 'claude-opus-5', + maxTokens: 1024, + outputConfig: ['effort' => 'low'], + system: $systemPrompt, + messages: [ + ['role' => 'user', 'content' => $query], + ], + ); + + $text = ''; + foreach ($message->content as $block) { + if ($block->type === 'text') { + $text .= $block->text; + } + } + return $text; +} + +function lookupViaOpenAI(string $query, string $systemPrompt, string $apiKey, string $model): string +{ + $payload = [ + 'model' => $model, + 'response_format' => ['type' => 'json_object'], + 'max_tokens' => 1024, + 'messages' => [ + ['role' => 'system', 'content' => $systemPrompt], + ['role' => 'user', 'content' => $query], + ], + ]; + + $ch = curl_init('https://api.openai.com/v1/chat/completions'); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + 'Authorization: Bearer ' . $apiKey, + ], + CURLOPT_POSTFIELDS => json_encode($payload), + CURLOPT_TIMEOUT => 30, + ]); + $raw = curl_exec($ch); + $curlError = curl_error($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($raw === false) { + throw new RuntimeException('OpenAI-Verbindung fehlgeschlagen: ' . $curlError); + } + + $decoded = json_decode($raw, true); + if ($httpCode >= 400) { + $msg = $decoded['error']['message'] ?? ('HTTP ' . $httpCode); + throw new RuntimeException('OpenAI-Fehler: ' . $msg); + } + + return $decoded['choices'][0]['message']['content'] ?? ''; +}