v0.11 U6: Bring your own key für die KI-Auswertung (Anthropic, OpenRouter)
Deploy FeWo mit PipelineManager / deploy (push) Successful in 5m11s
Deploy FeWo mit PipelineManager / deploy (push) Successful in 5m11s
- Jeder Mandant verbindet unter „System › KI-Anbindung“ seinen eigenen Zugang: Anbieter (Anthropic oder OpenRouter), API-Schlüssel, Modell für die erste Auswertung und für die Prüfung – keine Modellkennung im Code, kein zentraler Betreiberschlüssel (FEWO_AI_API_KEY entfällt). - Gespeichert im bestehenden TenantService (ai_guest_mail): Schlüssel verschlüsselt, nie wieder angezeigt, nie in Meldungen; Anbieter/Modelle in settings, Prüfergebnis in check_status/check_message/checked_at. - Feste Endpunkte je Anbieter (AiProvider), nicht konfigurierbar. OpenRouter mit response_format json_schema (strict) und provider.require_parameters. - „Verbindung prüfen“ testet beide Modelle. Ein abgelehnter Zugang (401/402/403) schaltet die Auswertung für den Mandanten ab, meldet es im Postfach der Verwaltung; Nachrichten bleiben zur Prüfung im Posteingang. - Zweistufige Extraktion und ihre Sicherheitsregeln unverändert. - Zielarchitektur §3.8, Umsetzungsplan U6, Bericht, Drehbuch, Status. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6f9aa46943
commit
6862dec201
20 files changed
+752
-95
No files matched your search
@@ -3,26 +3,70 @@
|
||||
namespace App\Communication\Extraction;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\RequestException;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 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').
|
||||
* The model connection of the extraction with the landlord's own access (bring your own key, v0.11): Anthropic's
|
||||
* Messages API or OpenRouter's chat completions, both over HTTP to the provider's fixed endpoint (AiProvider), no SDK
|
||||
* dependency. One call: system prompt, the message, and a JSON schema the answer must follow (structured outputs).
|
||||
* The key travels only in the request header; it is never part of an exception, a log line or a stored result.
|
||||
*/
|
||||
class LlmClient
|
||||
{
|
||||
/** Server-side refusal fallback (opt-in default for Claude Opus 5.5). */
|
||||
private const FALLBACK_BETA = 'server-side-fallback-2026-07-01';
|
||||
/** Server-side refusal fallback on Anthropic (opt-in default for the Claude Opus family). */
|
||||
private const ANTHROPIC_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
|
||||
* @throws AccessRejected when the provider rejects the access
|
||||
* @throws ExtractionFailed for every other unusable outcome
|
||||
*/
|
||||
public function structured(string $apiKey, string $model, string $system, string $message, array $schema, string $effort = 'medium'): array
|
||||
public function structured(AiAccess $access, string $model, string $system, string $message, array $schema, string $effort = 'medium'): array
|
||||
{
|
||||
[$headers, $body] = match ($access->provider) {
|
||||
AiProvider::Anthropic => $this->anthropic($access, $model, $system, $message, $schema, $effort),
|
||||
AiProvider::OpenRouter => $this->openRouter($access, $model, $system, $message, $schema),
|
||||
};
|
||||
|
||||
try {
|
||||
$response = Http::withHeaders($headers)
|
||||
->timeout((int) config('fewo.ai.timeout', 40))
|
||||
// Only passing problems are repeated; a rejected key or a bad request is not.
|
||||
->retry(2, 2000, fn (Throwable $exception): bool => $exception instanceof ConnectionException || ($exception instanceof RequestException && in_array($exception->response->status(), [429, 500, 502, 503, 529], true)), throw: false)
|
||||
->post($access->provider->endpoint(), $body);
|
||||
} catch (ConnectionException) {
|
||||
throw new ExtractionFailed('Keine Verbindung zu '.$access->provider->getLabel().'.');
|
||||
}
|
||||
|
||||
if (in_array($response->status(), [401, 402, 403], true)) {
|
||||
throw new AccessRejected($access->provider->getLabel().' lehnt den Zugang ab ('.$response->status().': '.$this->providerMessage($response, $access).').');
|
||||
}
|
||||
if (! $response->successful()) {
|
||||
throw new ExtractionFailed($access->provider->getLabel().' antwortet mit '.$response->status().' ('.$this->providerMessage($response, $access).').');
|
||||
}
|
||||
|
||||
$payload = (array) $response->json();
|
||||
$text = $access->provider === AiProvider::Anthropic ? $this->anthropicText($payload) : $this->openRouterText($payload);
|
||||
$data = json_decode($text, true);
|
||||
if (! is_array($data)) {
|
||||
throw new ExtractionFailed('Die Antwort ist kein gültiges JSON.');
|
||||
}
|
||||
|
||||
return ['data' => $data, 'model' => (string) ($payload['model'] ?? $model), 'usage' => (array) ($payload['usage'] ?? [])];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $schema
|
||||
* @return array{0: array<string, string>, 1: array<string, mixed>}
|
||||
*/
|
||||
private function anthropic(AiAccess $access, string $model, string $system, string $message, array $schema, string $effort): array
|
||||
{
|
||||
$headers = ['x-api-key' => $access->key, 'anthropic-version' => (string) config('fewo.ai.anthropic_version'), 'content-type' => 'application/json'];
|
||||
$body = [
|
||||
'model' => $model,
|
||||
'max_tokens' => 4096,
|
||||
@@ -30,37 +74,70 @@ public function structured(string $apiKey, string $model, string $system, string
|
||||
'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;
|
||||
$headers['anthropic-beta'] = self::ANTHROPIC_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);
|
||||
}
|
||||
return [$headers, $body];
|
||||
}
|
||||
|
||||
if (! $response->successful()) {
|
||||
throw new ExtractionFailed('KI-Dienst antwortet mit '.$response->status().'.');
|
||||
}
|
||||
/**
|
||||
* @param array<string, mixed> $schema
|
||||
* @return array{0: array<string, string>, 1: array<string, mixed>}
|
||||
*/
|
||||
private function openRouter(AiAccess $access, string $model, string $system, string $message, array $schema): array
|
||||
{
|
||||
return [
|
||||
['Authorization' => 'Bearer '.$access->key, 'content-type' => 'application/json', 'HTTP-Referer' => (string) config('app.url'), 'X-OpenRouter-Title' => 'FewoPortal'],
|
||||
[
|
||||
'model' => $model,
|
||||
'max_tokens' => 4096,
|
||||
'messages' => [['role' => 'system', 'content' => $system], ['role' => 'user', 'content' => $message]],
|
||||
'response_format' => ['type' => 'json_schema', 'json_schema' => ['name' => 'extraction', 'strict' => true, 'schema' => $schema]],
|
||||
// Only providers that honour the schema; otherwise it would merely be a hint.
|
||||
'provider' => ['require_parameters' => true],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$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.');
|
||||
}
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
private function anthropicText(array $payload): string
|
||||
{
|
||||
match ($payload['stop_reason'] ?? null) {
|
||||
'refusal' => throw new ExtractionFailed('Der Anbieter hat die Auswertung abgelehnt.'),
|
||||
'max_tokens' => throw new ExtractionFailed('Die Antwort ist unvollständig.'),
|
||||
default => null,
|
||||
};
|
||||
|
||||
$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 collect((array) ($payload['content'] ?? []))->where('type', 'text')->pluck('text')->implode('');
|
||||
}
|
||||
|
||||
return ['data' => $data, 'model' => (string) ($payload['model'] ?? $model), 'usage' => (array) ($payload['usage'] ?? [])];
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
private function openRouterText(array $payload): string
|
||||
{
|
||||
$choice = (array) ($payload['choices'][0] ?? []);
|
||||
match ($choice['finish_reason'] ?? null) {
|
||||
'content_filter' => throw new ExtractionFailed('Der Anbieter hat die Auswertung abgelehnt.'),
|
||||
'length' => throw new ExtractionFailed('Die Antwort ist unvollständig.'),
|
||||
'error' => throw new ExtractionFailed('Der Anbieter meldet einen Fehler.'),
|
||||
default => null,
|
||||
};
|
||||
|
||||
return (string) ($choice['message']['content'] ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
* The provider's own short message, cut and without anything that looks like a key.
|
||||
*/
|
||||
private function providerMessage(Response $response, AiAccess $access): string
|
||||
{
|
||||
$message = str_replace($access->key, '***', (string) ($response->json('error.message') ?? $response->json('error.type') ?? ''));
|
||||
|
||||
return mb_substr((string) preg_replace('/(sk-[A-Za-z0-9_\-]{6})[A-Za-z0-9_\-]+/', '$1…', $message), 0, 200) ?: 'ohne Angabe';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user