option('path') ?: resource_path('prompts/extraction/evaluation/samples.json')); $key = (string) ($this->option('key') ?: config('fewo.ai.api_key')); if ($key === '') { $this->error('Kein API-Schlüssel (FEWO_AI_API_KEY oder --key).'); return self::FAILURE; } $samples = (array) json_decode((string) file_get_contents($path), true); $property = Property::query()->withoutGlobalScopes()->first() ?? new Property(['name' => 'Ferienwohnung', 'max_guests' => 4]); $totals = ['expected' => 0, 'found' => 0, 'missed' => 0, 'wrong' => 0, 'extra' => 0, 'changes_missed' => 0, 'failed' => 0]; foreach ($samples as $sample) { $booking = new Booking(['arrival' => now()->addDays(14)->toDateString(), 'departure' => now()->addDays(17)->toDateString(), 'guests' => 2, 'status' => 'reserved']); $booking->setRelation('property', $property); $booking->setRelation('guest', null); $message = new InboundMessage(['subject' => $sample['subject'], 'body' => $sample['body']]); try { $result = $extractor->extract($key, $message, $booking)['data']; } catch (ExtractionFailed $exception) { $totals['failed']++; $this->warn("{$sample['id']}: {$exception->getMessage()}"); continue; } $found = collect($result['facts'])->mapWithKeys(fn (array $fact): array => [$fact['field'] => mb_strtolower(trim($fact['value']))])->all(); foreach ($sample['expected'] as $field => $value) { $totals['expected']++; match (true) { ! isset($found[$field]) => $totals['missed']++, $found[$field] !== mb_strtolower($value) => $totals['wrong']++, default => $totals['found']++, }; } $totals['extra'] += count(array_diff_key($found, $sample['expected'])); $changes = collect($result['change_requests'])->pluck('field')->all(); $totals['changes_missed'] += count(array_diff($sample['expected_changes'], $changes)); $this->line("{$sample['id']}: ".json_encode(['found' => $found, 'changes' => $changes], JSON_UNESCAPED_UNICODE)); } $this->table(array_keys($totals), [array_values($totals)]); $rate = $totals['expected'] > 0 ? round(100 * $totals['missed'] / $totals['expected'], 1) : 0; $this->info("Übersehene Angaben: {$rate} % · Prompt-Version ".MessageExtractor::promptVersion()); return self::SUCCESS; } }