The Knowledge Base API manages content for Retrieval-Augmented Generation (RAG) used by the AI chatbot. Content is stored with vector embeddings for semantic search.
RAG Architecture: Documents are stored with metadata and optionally vector embeddings for similarity search. The current implementation stores text content; embeddings can be generated via a background worker or OpenAI API integration.
if (req.method === 'GET') { // Fetch knowledge without the massive vector embeddings to save bandwidth const items = await prisma.$queryRaw` SELECT id, content, metadata, "createdAt" FROM knowledge_base ORDER BY "createdAt" DESC `; return res.status(200).json(items);}
Vector embeddings are excluded from GET requests to reduce bandwidth. Embeddings are stored in the database but only used internally for semantic search.
[ { "id": "660e8400-e29b-41d4-a716-446655440001", "content": "KAIU Natural Living ofrece envíos gratis en compras superiores a $150.000 COP en todo Colombia. Los tiempos de entrega son de 3-5 días hábiles en ciudades principales y 5-8 días en zonas rurales.", "metadata": { "title": "Política de Envíos", "type": "Política" }, "createdAt": "2026-03-01T10:00:00Z" }, { "id": "770e8400-e29b-41d4-a716-446655440002", "content": "Todos nuestros productos son elaborados con ingredientes naturales certificados orgánicos. No utilizamos parabenos, sulfatos ni fragancias sintéticas.", "metadata": { "title": "Certificaciones de Productos", "type": "Documento" }, "createdAt": "2026-02-28T14:30:00Z" }]
else if (req.method === 'POST') { const { content, title, type } = req.body; if (!content) return res.status(400).json({ error: 'Content is required' }); // Idealmente, aquí también llamaríamos al modelo de Embeddings (ej. OpenAI) // para generar el vector y guardarlo. Por ahora, guardamos el texto plano // que puede ser usado como RAG estático o indexado luego por un worker. const newKnowledge = await prisma.knowledgeBase.create({ data: { content, metadata: { title: title || 'Sin Título', type: type || 'Documento' } } }); return res.status(201).json({ success: true, id: newKnowledge.id });}
Future Enhancement: Vector embeddings can be generated using OpenAI’s text-embedding-ada-002 model or similar. The current implementation stores plain text for static RAG or deferred embedding generation.
const response = await fetch('https://api.kaiucol.com/api/admin/knowledge', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...' }, body: JSON.stringify({ content: `Nuestra política de devoluciones permite cambios o reembolsos dentro de los primeros 30 días de la compra. El producto debe estar sin abrir y en su empaque original. El cliente cubre el costo del envío de devolución.Para iniciar una devolución:1. Contacta servicio al cliente con tu número de orden2. Espera el código de autorización3. Envía el producto a nuestra bodega en Bogotá4. Recibirás el reembolso en 5-10 días hábiles `.trim(), title: 'Política de Devoluciones', type: 'Política' })});const data = await response.json();// { success: true, id: "880e8400-e29b-41d4-a716-446655440003" }