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.
This commit is contained in:
parent
0446dbf7d0
commit
14b890c920
2 changed files with 103 additions and 37 deletions
|
|
@ -1,5 +1,12 @@
|
|||
<?php
|
||||
// Diese Datei zu config.php kopieren und den echten API-Key eintragen.
|
||||
// Diese Datei zu config.php kopieren und die echten API-Keys eintragen.
|
||||
// config.php ist per .gitignore ausgeschlossen und wird nie ins Repo übernommen.
|
||||
|
||||
// Welcher Anbieter für die KI-Fahrzeugschätzung genutzt wird: 'anthropic' oder 'openai'.
|
||||
// Nur der Key des gewählten Anbieters muss ausgefüllt sein.
|
||||
$AI_PROVIDER = 'anthropic';
|
||||
|
||||
$ANTHROPIC_API_KEY = 'sk-ant-...';
|
||||
|
||||
$OPENAI_API_KEY = 'sk-...';
|
||||
$OPENAI_MODEL = 'gpt-4o-mini'; // optional, Standard falls leer
|
||||
|
|
|
|||
131
api/lookup.php
131
api/lookup.php
|
|
@ -1,8 +1,8 @@
|
|||
<?php
|
||||
// Serverseitiger Endpunkt: nimmt eine frei getippte Automarke/Modell entgegen und lässt
|
||||
// Claude plausible Kofferraum-Kenndaten dafür schätzen. Der API-Key bleibt ausschließlich
|
||||
// auf dem Server (aus config.php oder der Umgebungsvariable ANTHROPIC_API_KEY) und wird
|
||||
// nie an den Browser ausgeliefert.
|
||||
// Serverseitiger Endpunkt: nimmt eine frei getippte Automarke/Modell entgegen und lässt eine
|
||||
// KI plausible Kofferraum-Kenndaten dafür schätzen. Welcher Anbieter genutzt wird (Claude oder
|
||||
// OpenAI), legt config.php fest — der jeweilige API-Key bleibt ausschließlich auf dem Server
|
||||
// und wird nie an den Browser ausgeliefert.
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
|
|
@ -13,20 +13,22 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|||
exit;
|
||||
}
|
||||
|
||||
$apiKey = getenv('ANTHROPIC_API_KEY') ?: null;
|
||||
$provider = getenv('AI_PROVIDER') ?: null;
|
||||
$anthropicKey = getenv('ANTHROPIC_API_KEY') ?: null;
|
||||
$openaiKey = getenv('OPENAI_API_KEY') ?: null;
|
||||
$openaiModel = null;
|
||||
|
||||
$configFile = __DIR__ . '/config.php';
|
||||
if (!$apiKey && file_exists($configFile)) {
|
||||
if (file_exists($configFile)) {
|
||||
require $configFile;
|
||||
$apiKey = $ANTHROPIC_API_KEY ?? null;
|
||||
$provider = $provider ?: ($AI_PROVIDER ?? null);
|
||||
$anthropicKey = $anthropicKey ?: ($ANTHROPIC_API_KEY ?? null);
|
||||
$openaiKey = $openaiKey ?: ($OPENAI_API_KEY ?? null);
|
||||
$openaiModel = $OPENAI_MODEL ?? null;
|
||||
}
|
||||
|
||||
if (!$apiKey) {
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
'error' => '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 = <<<SYS
|
||||
Du lieferst Kenndaten für ein 3D-Vorschau-Tool eines Auto-HiFi-Shops, das aus Marke/Modell
|
||||
eine stark vereinfachte, parametrische Kofferraum-Näherung generiert.
|
||||
|
|
@ -65,28 +61,23 @@ Regeln:
|
|||
SYS;
|
||||
|
||||
try {
|
||||
$message = $client->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'] ?? '';
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue