- Kleines Modell (claude-haiku-5-5) extrahiert Fakten mit Belegstelle und Konfidenz; programmatische Prüfung (Gültigkeit, Belegstelle im Original, Widerspruch zum gespeicherten Wert oder in der Nachricht, Plausibilität, Kritikalität nach Feld und Phase). Widerspruch oder fehlgeschlagene Prüfung → Prüfmodell (claude-opus-5-5, serverseitiger Fallback bei Ablehnung) unabhängig von der Konfidenz; solche Fakten nie automatisch. - Nur geprüfte, bisher fehlende Angaben gehen an den Fakteneingang (Quelle „llm“); Zeitraum/Personen nur als Änderungswunsch (M06); Rest als Vorschlag im Posteingang („Vorschläge übernehmen“). - Versionierte Prompts mit Platzhaltern, JSON-Schema mit eigener Validierung und einer Wiederholung; Ausfall/Ablehnung → sicherer Rückfall zur Vermieterin. HTTP-Client, kein SDK (laut Plan). - Dienst „KI: Gästekommunikation“ verdrahtet, standardmäßig aus; Schlüssel je Mandant oder FEWO_AI_API_KEY; Verbrauch in service_usages. - messages:extract alle fünf Minuten; ai:evaluate misst Fehlwerte und übersehene Angaben getrennt (M05). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
2.9 KiB
PHP
67 lines
2.9 KiB
PHP
<?php
|
||
|
||
namespace App\Communication\Extraction;
|
||
|
||
use Illuminate\Http\Client\ConnectionException;
|
||
use Illuminate\Support\Facades\Http;
|
||
|
||
/**
|
||
* The model connection for the extraction (Claude Messages API over HTTP – no SDK dependency, as approved in the
|
||
* v0.11 plan). One call: system prompt, the message as user content, and a JSON schema the answer must follow
|
||
* (structured outputs, `output_config.format`). Provider, endpoint and models come from config('fewo.ai').
|
||
*/
|
||
class LlmClient
|
||
{
|
||
/** Server-side refusal fallback (opt-in default for Claude Opus 5.5). */
|
||
private const FALLBACK_BETA = 'server-side-fallback-2026-07-01';
|
||
|
||
/**
|
||
* @param array<string, mixed> $schema
|
||
* @return array{data: array<string, mixed>, model: string, usage: array<string, mixed>}
|
||
*
|
||
* @throws ExtractionFailed
|
||
*/
|
||
public function structured(string $apiKey, string $model, string $system, string $message, array $schema, string $effort = 'medium'): array
|
||
{
|
||
$body = [
|
||
'model' => $model,
|
||
'max_tokens' => 4096,
|
||
'system' => $system,
|
||
'messages' => [['role' => 'user', 'content' => $message]],
|
||
'output_config' => ['effort' => $effort, 'format' => ['type' => 'json_schema', 'schema' => $schema]],
|
||
];
|
||
$headers = ['x-api-key' => $apiKey, 'anthropic-version' => (string) config('fewo.ai.api_version'), 'content-type' => 'application/json'];
|
||
if (str_starts_with($model, 'claude-opus-')) {
|
||
$body['fallbacks'] = 'default';
|
||
$headers['anthropic-beta'] = self::FALLBACK_BETA;
|
||
}
|
||
|
||
try {
|
||
$response = Http::withHeaders($headers)->timeout((int) config('fewo.ai.timeout', 40))->retry(2, 2000, throw: false)->post((string) config('fewo.ai.endpoint'), $body);
|
||
} catch (ConnectionException $exception) {
|
||
throw new ExtractionFailed('Keine Verbindung zum KI-Dienst: '.$exception->getMessage(), previous: $exception);
|
||
}
|
||
|
||
if (! $response->successful()) {
|
||
throw new ExtractionFailed('KI-Dienst antwortet mit '.$response->status().'.');
|
||
}
|
||
|
||
$payload = (array) $response->json();
|
||
$stop = $payload['stop_reason'] ?? null;
|
||
if ($stop === 'refusal') {
|
||
throw new ExtractionFailed('Der KI-Dienst hat die Auswertung abgelehnt.');
|
||
}
|
||
if ($stop === 'max_tokens') {
|
||
throw new ExtractionFailed('Die Antwort des KI-Dienstes ist unvollständig.');
|
||
}
|
||
|
||
$text = collect((array) ($payload['content'] ?? []))->where('type', 'text')->pluck('text')->implode('');
|
||
$data = json_decode($text, true);
|
||
if (! is_array($data)) {
|
||
throw new ExtractionFailed('Die Antwort des KI-Dienstes ist kein gültiges JSON.');
|
||
}
|
||
|
||
return ['data' => $data, 'model' => (string) ($payload['model'] ?? $model), 'usage' => (array) ($payload['usage'] ?? [])];
|
||
}
|
||
}
|