From dabde9abafb073a08f8619a689b2cc83439e7888 Mon Sep 17 00:00:00 2001 From: hajimi Date: Fri, 12 Jun 2026 11:21:53 +0800 Subject: [PATCH] fix: restore kb retrieve fallback --- server/app/common/service/ai/KbService.php | 63 +++++++++++++++++++--- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/server/app/common/service/ai/KbService.php b/server/app/common/service/ai/KbService.php index f78bd89..ba2f680 100644 --- a/server/app/common/service/ai/KbService.php +++ b/server/app/common/service/ai/KbService.php @@ -23,6 +23,28 @@ class KbService { public const COLLECTION_GLOBAL = 'global'; + private const QUERY_TERM_ALIASES = [ + '旧澳六合' => ['旧澳六合', '老澳门六合彩'], + '新澳六合' => ['新澳六合', '新澳门六合彩'], + ]; + + private const QUERY_COMMON_TERMS = [ + '分析线索', + '走势分析', + '开奖记录', + '开奖结果', + '冷热号', + '波色', + '生肖', + '特码', + '号码', + '彩票', + '比赛', + '新闻', + '马刺', + 'NBA', + ]; + public static function upsertDocument(string $domain, string $subtype, int $sourceId): array { $transactionStarted = false; @@ -320,11 +342,18 @@ class KbService if (empty($candidates)) { $keywordParts = self::splitQueryTerms($queryText); $fallback = clone $query; - foreach (array_slice($keywordParts, 0, 3) as $term) { - $fallback->where(function ($subQuery) use ($term) { - $subQuery->whereLike('c.chunk_text', '%' . $term . '%') - ->whereOrLike('d.title', '%' . $term . '%') - ->whereOrLike('d.keywords', '%' . $term . '%'); + if (!empty($keywordParts)) { + $fallback->where(function ($keywordQuery) use ($keywordParts) { + foreach (array_slice($keywordParts, 0, 8) as $index => $term) { + $like = '%' . $term . '%'; + $method = $index === 0 ? 'where' : 'whereOr'; + $keywordQuery->{$method}(function ($subQuery) use ($like) { + $subQuery->whereLike('c.chunk_text', $like) + ->whereOr('d.title', 'like', $like) + ->whereOr('d.summary', 'like', $like) + ->whereOr('d.keywords', 'like', $like); + }); + } }); } $candidates = $fallback->order('d.source_updated_at', 'desc')->limit($limit)->select()->toArray(); @@ -800,9 +829,27 @@ class KbService return []; } - $terms = preg_split('/[\s,,。!?;、::\/\\\\]+/u', $queryText) ?: []; - $terms = array_values(array_filter(array_map('trim', $terms), static fn(string $term) => mb_strlen($term) >= 2)); - return array_unique($terms); + $terms = []; + foreach (self::QUERY_TERM_ALIASES as $trigger => $aliases) { + if (mb_strpos($queryText, $trigger) !== false) { + array_push($terms, ...$aliases); + } + } + + foreach (self::QUERY_COMMON_TERMS as $term) { + if (mb_strpos($queryText, $term) !== false) { + $terms[] = $term; + } + } + + $parts = preg_split('/[\s,,。!?;、::\/\\\\]+/u', $queryText) ?: []; + array_push($terms, ...$parts); + + $terms = array_values(array_filter(array_map('trim', $terms), static function (string $term) { + return mb_strlen($term) >= 2; + })); + + return array_values(array_unique($terms)); } private static function normalizeHtmlText(string $html): string