$schema * @return array{data: array, model: string, usage: array} * * @throws AccessRejected when the provider rejects the access * @throws ExtractionFailed for every other unusable outcome */ 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 $schema * @return array{0: array, 1: array} */ 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, 'system' => $system, 'messages' => [['role' => 'user', 'content' => $message]], 'output_config' => ['effort' => $effort, 'format' => ['type' => 'json_schema', 'schema' => $schema]], ]; if (str_starts_with($model, 'claude-opus-')) { $body['fallbacks'] = 'default'; $headers['anthropic-beta'] = self::ANTHROPIC_FALLBACK_BETA; } return [$headers, $body]; } /** * @param array $schema * @return array{0: array, 1: array} */ 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], ], ]; } /** * @param array $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, }; return collect((array) ($payload['content'] ?? []))->where('type', 'text')->pluck('text')->implode(''); } /** * @param array $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'; } }