Skip to content

Commit 25a0972

Browse files
ouiliameclaude
andcommitted
docs: apply similarity threshold to English Ask AI retrieval
English vector search returned the nearest chunks with no minimum score, so off-topic queries got grounded on weak matches. Require cosine similarity >= 0.6 (mirrors the site search route) so uncovered questions return nothing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 14f9fcb commit 25a0972

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

apps/docs/app/api/chat/route.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const SEARCH_LIMIT = 6
1717
/** Candidates pulled before locale filtering, so a locale still yields SEARCH_LIMIT results. */
1818
const SEARCH_CANDIDATES = SEARCH_LIMIT * 4
1919

20+
/** Minimum cosine similarity for an English vector match (mirrors the site search route). */
21+
const SIMILARITY_THRESHOLD = 0.6
22+
2023
/** Locales the docs are published in (mirrors the site search route). */
2124
const KNOWN_LOCALES = ['en', 'es', 'fr', 'de', 'ja', 'zh']
2225
const DEFAULT_LOCALE = 'en'
@@ -207,7 +210,9 @@ async function searchDocs(query: string, locale: string) {
207210
rows = await db
208211
.select(SEARCH_COLUMNS)
209212
.from(docsEmbeddings)
210-
.where(localeFilter(locale))
213+
.where(
214+
sql`1 - (${docsEmbeddings.embedding} <=> ${vectorLiteral}::vector) >= ${SIMILARITY_THRESHOLD} and ${localeFilter(locale)}`
215+
)
211216
.orderBy(sql`${docsEmbeddings.embedding} <=> ${vectorLiteral}::vector`)
212217
.limit(SEARCH_CANDIDATES)
213218
} else {

0 commit comments

Comments
 (0)