Backup #1863

ID1863
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/AI/ScoringService.php
Version2
Typ modified
Größe2.4 KB
Hasha616b35b1c1f4dedc5d82233579efec8a2c6c5b57065c3a50b853dfdfad56498
Datum2025-12-27 23:48:28
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Infrastructure\AI;

// @responsibility: Berechnet gewichtete Scores aus Vector-Similarity, Recency und Authority

use DateTimeInterface;
use Domain\Constants;

/**
 * Calculates weighted scores for RAG search results.
 *
 * Combines vector similarity with recency and authority factors
 * to produce a more nuanced ranking of search results.
 */
final readonly class ScoringService
{
    /** Weight for vector similarity score (cosine) */
    private const WEIGHT_SIMILARITY = 0.7;

    /** Weight for document recency */
    private const WEIGHT_RECENCY = 0.1;

    /** Weight for source authority */
    private const WEIGHT_AUTHORITY = 0.2;

    /**
     * Calculate weighted score combining similarity, recency, and authority.
     *
     * @param float             $vectorScore    The cosine similarity score from vector search (0.0-1.0)
     * @param DateTimeInterface $documentDate   The document's processed/created date
     * @param float             $authorityScore The source authority score (0.0-1.0, default: 0.5)
     *
     * @return float The weighted score (0.0-1.0)
     */
    public function calculateScore(
        float $vectorScore,
        DateTimeInterface $documentDate,
        float $authorityScore = 0.5
    ): float {
        $recencyScore = $this->calculateRecency($documentDate);

        return (self::WEIGHT_SIMILARITY * $vectorScore)
             + (self::WEIGHT_RECENCY * $recencyScore)
             + (self::WEIGHT_AUTHORITY * $authorityScore);
    }

    /**
     * Calculate recency score based on document age.
     *
     * Returns 1.0 for today, linearly decreasing to 0.0 at 365 days old.
     *
     * @param DateTimeInterface $date The document date
     *
     * @return float Recency score (0.0-1.0)
     */
    private function calculateRecency(DateTimeInterface $date): float
    {
        $now = new \DateTime();
        $interval = $now->diff($date);
        $daysOld = $interval->days;

        // Documents older than 1 year get minimum recency score
        return max(0.0, 1.0 - ($daysOld / 365));
    }

    /**
     * Get the weight configuration for transparency.
     *
     * @return array{similarity: float, recency: float, authority: float}
     */
    public function getWeights(): array
    {
        return [
            'similarity' => self::WEIGHT_SIMILARITY,
            'recency' => self::WEIGHT_RECENCY,
            'authority' => self::WEIGHT_AUTHORITY,
        ];
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1863 2 modified 2.4 KB 2025-12-27 23:48
1859 1 modified 2.4 KB 2025-12-27 23:47

← Zurück zur Übersicht