Frag das Portal durchsucht alles, neue Demo-Inhalte (0.66.0)
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.3 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.4 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.4 (pull_request) Has been skipped
CI / Browser-Tests (pull_request) Successful in 8m33s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 9m8s
CI / PHP 8.3 / mysql:8.4-1 (pull_request) Successful in 17m55s
CI / Build-Artefakte aktuell (pull_request) Successful in 9m50s
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.3 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.4 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.4 (pull_request) Has been skipped
CI / Browser-Tests (pull_request) Successful in 8m33s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 9m8s
CI / PHP 8.3 / mysql:8.4-1 (pull_request) Successful in 17m55s
CI / Build-Artefakte aktuell (pull_request) Successful in 9m50s
- Wissensquellen: Termine, Downloads, Formulare, Stellen, Ideen, Umfragen, Personen, eigene IT-Tickets/Geräte (OpenNIT, je Anfrage zwischengespeichert) - Ranking mit IDF, Abfrage je Begriff, Abkürzungen als ganze Wörter, LIKE unabhängig von Groß-/Kleinschreibung (JSON-Spalten), Datum im Prompt - Demo-Inhalte für einen Träger der Eingliederungshilfe (DemoData), mehrseitige PDFs, vorhandene Inhalte je Bereich entfernen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
1656cd8090
commit
3addc09c38
165 changed files with 2258 additions and 814 deletions
|
|
@ -85,9 +85,11 @@ class AskPortal
|
|||
}
|
||||
|
||||
/**
|
||||
* Treffer nach Begriffen gewichten: im Titel dreifach, im Text je Begriff höchstens
|
||||
* fünf Fundstellen. Wer nur einen von mehreren Begriffen enthält, fällt hinter die zurück,
|
||||
* die mehrere enthalten – und ganz heraus, wenn es bessere gibt.
|
||||
* Treffer nach Begriffen gewichten: im Titel dreifach, im Text je Begriff höchstens fünf
|
||||
* Fundstellen. Seltene Begriffe zählen mehr als häufige (wie bei Suchmaschinen): Bei
|
||||
* „Sammelplatz bei Feuer in der Werkstatt“ entscheidet „Sammelplatz“, nicht „Werkstatt“.
|
||||
* Wer nur einen von mehreren Begriffen enthält, fällt hinter die zurück, die mehrere
|
||||
* enthalten – und ganz heraus, wenn es bessere gibt.
|
||||
*
|
||||
* @param list<array<string, mixed>> $documents
|
||||
* @param list<string> $terms
|
||||
|
|
@ -95,29 +97,44 @@ class AskPortal
|
|||
*/
|
||||
private function rank(array $documents, array $terms): array
|
||||
{
|
||||
if ($terms === []) {
|
||||
if ($terms === [] || $documents === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($documents as &$doc) {
|
||||
$hits = [];
|
||||
$df = array_fill_keys($terms, 0);
|
||||
|
||||
foreach ($documents as $i => $doc) {
|
||||
$title = mb_strtolower($doc['title']);
|
||||
$text = mb_strtolower($doc['text']);
|
||||
$distinct = 0;
|
||||
$score = 0;
|
||||
|
||||
foreach ($terms as $term) {
|
||||
$inTitle = mb_substr_count($title, $term);
|
||||
$inText = min(5, mb_substr_count($text, $term));
|
||||
$hits[$i][$term] = [Knowledge::occurrences($title, $term), min(5, Knowledge::occurrences($text, $term))];
|
||||
|
||||
if (array_sum($hits[$i][$term]) > 0) {
|
||||
$df[$term]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$count = count($documents);
|
||||
$weight = array_map(fn (int $n) => 1 + log(($count + 1) / ($n + 1)), $df);
|
||||
|
||||
foreach ($documents as $i => &$doc) {
|
||||
$distinct = 0;
|
||||
$score = 0.0;
|
||||
|
||||
foreach ($terms as $term) {
|
||||
[$inTitle, $inText] = $hits[$i][$term];
|
||||
|
||||
if ($inTitle + $inText > 0) {
|
||||
$distinct++;
|
||||
$score += $weight[$term] * ($inTitle * 3 + $inText + 4);
|
||||
}
|
||||
|
||||
$score += $inTitle * 3 + $inText;
|
||||
}
|
||||
|
||||
$doc['distinct'] = $distinct;
|
||||
$doc['score'] = $score + $distinct * 4;
|
||||
$doc['score'] = round($score, 1);
|
||||
}
|
||||
unset($doc);
|
||||
|
||||
|
|
@ -172,7 +189,7 @@ class AskPortal
|
|||
|
||||
foreach ($chunks as $i => $chunk) {
|
||||
$lower = mb_strtolower($chunk);
|
||||
$scored[$i] = count(array_filter($terms, fn (string $t) => str_contains($lower, $t))) * 10 - $i * 0.01;
|
||||
$scored[$i] = count(array_filter($terms, fn (string $t) => Knowledge::occurrences($lower, $t) > 0)) * 10 - $i * 0.01;
|
||||
}
|
||||
|
||||
arsort($scored);
|
||||
|
|
@ -200,6 +217,7 @@ class AskPortal
|
|||
|
||||
Regeln:
|
||||
- Antworte kurz und konkret auf Deutsch: höchstens vier Sätze oder eine kurze Liste mit „- “.
|
||||
- Bei Terminen nenne Datum, Uhrzeit und Ort. Vergangene Termine nur, wenn danach gefragt ist.
|
||||
- Belege jede Aussage mit der Nummer des Auszugs in eckigen Klammern, z. B. [2].
|
||||
- Nutze nur, was in den Auszügen steht. Erfinde nichts, ergänze kein Allgemeinwissen.
|
||||
- Beantworten die Auszüge die Frage nicht, antworte nur mit: NICHTS_GEFUNDEN
|
||||
|
|
@ -221,7 +239,10 @@ class AskPortal
|
|||
$blocks[] = $head."\n".self::excerpt($doc['text'], $terms);
|
||||
}
|
||||
|
||||
return "Auszüge aus dem Intranet:\n\n".implode("\n\n", $blocks)."\n\nFrage: ".$question;
|
||||
// Datum für Fragen wie „nächste Woche“ oder „morgen“
|
||||
$today = now()->locale('de')->translatedFormat('l, j. F Y');
|
||||
|
||||
return "Heute ist {$today}.\n\nAuszüge aus dem Intranet:\n\n".implode("\n\n", $blocks)."\n\nFrage: ".$question;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ class Keywords
|
|||
'wieviel', 'wieviele', 'viel', 'viele', 'wir', 'wird', 'wo', 'woher', 'wohin', 'wann', 'warum', 'weshalb', 'zu', 'zum', 'zur',
|
||||
'gerne', 'genau', 'eigentlich', 'immer', 'mal', 'mehr', 'sehr', 'einfach', 'bekomme', 'bekommen', 'machen', 'mache', 'finde',
|
||||
'finden', 'gehen', 'geht', 'brauche', 'brauchen', 'stelle', 'wissen', 'frage', 'fragen', 'hallo', 'danke', 'portal',
|
||||
'bedeutet', 'bedeutung', 'heißt', 'tun', 'nächste', 'nächsten', 'nächster', 'letzte', 'letzten', 'sagen', 'erkläre', 'erklären',
|
||||
'kriege', 'kriegen', 'eigentlich', 'irgendwie', 'jemand', 'jemanden', 'etwa', 'gerade', 'heute', 'morgen',
|
||||
];
|
||||
|
||||
/** Endungen, die abgeschnitten werden (längste zuerst), solange mindestens vier Zeichen bleiben */
|
||||
|
|
@ -32,13 +34,16 @@ class Keywords
|
|||
*/
|
||||
public static function from(string $question): array
|
||||
{
|
||||
$words = preg_split('/[^\p{L}\p{N}-]+/u', mb_strtolower($question), -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
||||
$original = preg_split('/[^\p{L}\p{N}-]+/u', $question, -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
||||
$terms = [];
|
||||
|
||||
foreach ($words as $word) {
|
||||
$word = trim($word, '-');
|
||||
foreach ($original as $raw) {
|
||||
$word = mb_strtolower(trim($raw, '-'));
|
||||
|
||||
if (mb_strlen($word) < 3 || in_array($word, self::STOPWORDS, true)) {
|
||||
// Kurze Abkürzungen in Großbuchstaben zählen („IT“, „QM“, „BW“) – sonst erst ab drei Zeichen
|
||||
$abbreviation = mb_strlen($word) === 2 && preg_match('/^\p{Lu}{2}$/u', trim($raw, '-'));
|
||||
|
||||
if ((mb_strlen($word) < 3 && ! $abbreviation) || in_array($word, self::STOPWORDS, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -46,8 +51,9 @@ class Keywords
|
|||
|
||||
// „MAV-Wahl“ findet auch „Wahl der MAV“
|
||||
if (str_contains($word, '-')) {
|
||||
foreach (explode('-', $word) as $part) {
|
||||
if (mb_strlen($part) >= 3 && ! in_array($part, self::STOPWORDS, true)) {
|
||||
foreach (explode('-', trim($raw, '-')) as $part) {
|
||||
if ((mb_strlen($part) >= 3 || preg_match('/^\p{Lu}{2}$/u', $part)) && ! in_array(mb_strtolower($part), self::STOPWORDS, true)) {
|
||||
$part = mb_strtolower($part);
|
||||
$terms[] = self::stem($part);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use App\Models\User;
|
|||
use App\Plugins\PluginAccess;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
|
|
@ -67,7 +68,24 @@ class Knowledge
|
|||
}
|
||||
|
||||
try {
|
||||
foreach (array_slice(($source['search'])($user, $terms, $perSource), 0, $perSource) as $hit) {
|
||||
// Einmal mit allen Begriffen, dazu je Begriff ein paar Treffer – sonst verdrängt ein
|
||||
// häufiges Wort („Werkstatt“) die wenigen Einträge mit dem entscheidenden („Sammelplatz“)
|
||||
$hits = array_slice(($source['search'])($user, $terms, $perSource), 0, $perSource);
|
||||
|
||||
if (count($terms) > 1) {
|
||||
foreach ($terms as $term) {
|
||||
$hits = [...$hits, ...array_slice(($source['search'])($user, [$term], 3), 0, 3)];
|
||||
}
|
||||
}
|
||||
|
||||
$seen = [];
|
||||
|
||||
foreach ($hits as $hit) {
|
||||
if (isset($seen[$hit['url'].'|'.$hit['title']])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$seen[$hit['url'].'|'.$hit['title']] = true;
|
||||
$documents[] = [
|
||||
'source' => $key,
|
||||
'label' => $source['label'],
|
||||
|
|
@ -100,15 +118,48 @@ class Knowledge
|
|||
{
|
||||
return $query->where(function ($q) use ($columns, $terms) {
|
||||
foreach ($terms as $term) {
|
||||
// Kurze Begriffe („it“, „qm“) nur als ganzes Wort – sonst fände „it“ jedes „mit“
|
||||
if (self::isShort($term)) {
|
||||
$pattern = '(^|[^[:alnum:]])'.preg_quote($term).'([^[:alnum:]]|$)';
|
||||
|
||||
foreach ($columns as $column) {
|
||||
$q->orWhereRaw(self::lowered($column).' REGEXP ?', [$pattern]);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$like = '%'.str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $term).'%';
|
||||
|
||||
foreach ($columns as $column) {
|
||||
$q->orWhere($column, 'like', $like);
|
||||
$q->orWhereRaw(self::lowered($column).' LIKE ?', [$like]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Spalte klein geschrieben als Text – JSON-Spalten (z. B. Inhalte aus dem Baukasten) haben eine
|
||||
* binäre Sortierung, dort wäre LIKE sonst groß-/kleinschreibungsabhängig.
|
||||
*/
|
||||
private static function lowered(string $column): string
|
||||
{
|
||||
return 'LOWER(CAST('.DB::getQueryGrammar()->wrap($column).' AS CHAR))';
|
||||
}
|
||||
|
||||
public static function isShort(string $term): bool
|
||||
{
|
||||
return mb_strlen($term) <= 3;
|
||||
}
|
||||
|
||||
/** Wie oft kommt der Begriff vor? Kurze Begriffe nur als ganzes Wort. */
|
||||
public static function occurrences(string $haystack, string $term): int
|
||||
{
|
||||
return self::isShort($term)
|
||||
? (int) preg_match_all('/(?<![\p{L}\p{N}])'.preg_quote($term, '/').'(?![\p{L}\p{N}])/u', $haystack)
|
||||
: mb_substr_count($haystack, $term);
|
||||
}
|
||||
|
||||
/** HTML in lesbaren Klartext (Absätze bleiben als Zeilenumbrüche erhalten) */
|
||||
public static function plain(?string $html): string
|
||||
{
|
||||
|
|
|
|||
43
app/Ask/PeopleKnowledge.php
Normal file
43
app/Ask/PeopleKnowledge.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\Ask;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Support\ProfileFields;
|
||||
|
||||
/**
|
||||
* Wissensquelle für „Frag das Portal“: das Personenverzeichnis – „Wer ist für die IT
|
||||
* zuständig?“, „Wie erreiche ich die Personalabteilung?“. Nur, was das Verzeichnis
|
||||
* ohnehin zeigt; eingeschränkte Profilfelder nur nach den Regeln des Profils.
|
||||
*/
|
||||
class PeopleKnowledge
|
||||
{
|
||||
public function __construct(private readonly ProfileFields $fields) {}
|
||||
|
||||
/**
|
||||
* @param list<string> $terms
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public function __invoke(User $user, array $terms, int $limit): array
|
||||
{
|
||||
$query = User::query()->active();
|
||||
Knowledge::whereAny($query, ['first_name', 'last_name', 'job_title', 'department', 'location', 'about'], $terms);
|
||||
|
||||
return $query->orderBy('last_name')->limit($limit)->get()->map(function (User $person) use ($user) {
|
||||
$extra = collect($this->fields->visibleValues($person, $user))->map(fn (array $f) => $f['label'].': '.$f['value'])->implode('. ');
|
||||
|
||||
return [
|
||||
'title' => $person->name.($person->job_title ? ' – '.$person->job_title : ''),
|
||||
'url' => route('people.show', $person),
|
||||
'text' => implode(' ', array_filter([
|
||||
$person->name.' arbeitet'.($person->job_title ? ' als '.$person->job_title : '').($person->department ? ' im Bereich '.$person->department : '').($person->location ? ', Standort '.$person->location : '').'.',
|
||||
$person->phone ? 'Telefon: '.$person->phone.'.' : null,
|
||||
$person->mobile ? 'Mobil: '.$person->mobile.'.' : null,
|
||||
'E-Mail: '.$person->email.'.',
|
||||
$extra ? $extra.'.' : null,
|
||||
]))."\n".(string) $person->about,
|
||||
'meta' => collect([$person->department, $person->location])->filter()->implode(', ') ?: null,
|
||||
];
|
||||
})->all();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue