Knowledge Base
Knowledge sources give the AI relevant context for each query, grounding responses in your actual content.
FAQ Knowledge Source
Section titled “FAQ Knowledge Source”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.' },]);How FAQ Matching Works
Section titled “How FAQ Matching Works”- User’s query is tokenized into words
- Each FAQ pair is scored by word frequency overlap (question + answer)
- Top 5 matching FAQs are returned as context
- Context is formatted as
Q: ...\nA: ...and injected into the AI prompt
Text Knowledge Source
Section titled “Text Knowledge Source”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);How Text Matching Works
Section titled “How Text Matching Works”- Source text is split into chunks at initialization
- User’s query words are matched against each chunk
- Top 3 relevant chunks are returned
- Context is prefixed with
"Relevant context:\n"
KnowledgeSource Interface
Section titled “KnowledgeSource Interface”interface KnowledgeSource { type: string; getContext(query: string): Promise<string>;}Multiple Sources
Section titled “Multiple Sources”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), ],});Custom Knowledge Source
Section titled “Custom Knowledge Source”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'); }}