Skip to content

Knowledge Base

Knowledge sources give the AI relevant context for each query, grounding responses in your actual content.

Best for structured question-answer pairs:

import { FAQKnowledgeSource } from '@chatcops/core/knowledge';
const faq = new FAQKnowledgeSource([
{ question: 'What are your business hours?', answer: '9am-5pm EST, Monday to Friday.' },
{ question: 'Do you offer refunds?', answer: 'Yes, full refunds within 30 days.' },
{ question: 'How do I contact support?', answer: 'Email support@example.com or use this chat!' },
{ question: 'What payment methods do you accept?', answer: 'Visa, Mastercard, PayPal, and wire transfer.' },
]);
  1. User’s query is tokenized into words
  2. Each FAQ pair is scored by word frequency overlap (question + answer)
  3. Top 5 matching FAQs are returned as context
  4. Context is formatted as Q: ...\nA: ... and injected into the AI prompt

Best for unstructured content (documentation, product descriptions, policies):

import { TextKnowledgeSource } from '@chatcops/core/knowledge';
const docs = new TextKnowledgeSource(
`Our company was founded in 2020 by Jane Doe.
We specialize in AI-powered customer support solutions.
Our flagship product is ChatCops, an embeddable chatbot widget.
We serve over 500 businesses worldwide.
Our headquarters is in San Francisco, CA.`,
{ chunkSize: 500 } // Optional: characters per chunk
);
  1. Source text is split into chunks at initialization
  2. User’s query words are matched against each chunk
  3. Top 3 relevant chunks are returned
  4. Context is prefixed with "Relevant context:\n"
interface KnowledgeSource {
type: string;
getContext(query: string): Promise<string>;
}

Combine multiple knowledge sources — all contexts are concatenated:

chatcopsMiddleware({
provider: { type: 'claude', apiKey: '...' },
systemPrompt: 'Answer using the provided context. If unsure, say so.',
knowledge: [
new FAQKnowledgeSource(faqs),
new TextKnowledgeSource(productDocs),
new TextKnowledgeSource(policyDocs),
],
});

Implement KnowledgeSource for database-backed or vector search:

class VectorKnowledgeSource implements KnowledgeSource {
type = 'vector';
async getContext(query: string): Promise<string> {
const results = await vectorDB.search(query, { topK: 5 });
return results.map(r => r.text).join('\n\n');
}
}