apps/web/src/lib/aiRetrieval.ts
1/**
2 * Knowledge files occasionally contain placeholder answers written in the
3 * agent's voice ("I can't find a public phone number in the available
4 * sources…", "The closest place to look is…"). Retrieved verbatim they
5 * outrank real answers and teach the model to refuse and to cite "the
6 * sources". Such chunks are dropped at retrieval time; the system prompt owns
7 * all refusal wording.
8 */
9const REFUSAL_TEMPLATE =
10 /\b(?:I can(?:no|')t (?:find|confirm|locate)|I (?:do not|don't) have (?:a source|any (?:source|information))|(?:in|from) the (?:available )?sources(?: I have)?|the closest (?:relevant )?(?:page|place) (?:is|to look))\b/i
11
12export function isRefusalTemplate(text: string): boolean {
13 return REFUSAL_TEMPLATE.test(text)
14}
15
16/**
17 * unleash.ai has no contact form, but the FAQ files still say "please use the
18 * contact form on the website". The model copies whatever route the passage
19 * names, so swap that route for the real one before it sees the text.
20 */
21const CONTACT_FORM_ROUTE =
22 /\b(?:please\s+)?(?:use|fill\s+in|fill\s+out|complete|submit)\s+(?:the|our)\s+contact\s+form(?:\s+on\s+(?:the|our)\s+(?:website|site))?/gi
23
24export function normalizeContactRoute(text: string): string {
25 return text.replace(CONTACT_FORM_ROUTE, 'email support@unleash.ai')
26}
27