sakhaglobal-chatbot-rag-sdk
v0.3.4
Published
RAG Chatbot SDK for embedding AI chat into websites
Downloads
72
Readme
RagChat SDK
A lightweight JavaScript/TypeScript SDK for integrating RAG (Retrieval-Augmented Generation) chatbots into web applications. Supports chat, PDF knowledge ingestion, and document management.
✨ Features
- 🤖 RAG-based chat interface
- 📄 Upload, list, and delete PDFs
- 🔐 API-key–based authentication
- ⚡ Works in React, Next.js, and vanilla JS
- 🛡️ Safe handling of empty backend responses
📦 Installation
npm install sakhaglobal-chatbot-rag-sdkor
yarn add sakhaglobal-chatbot-rag-sdk🚀 Quick Start
1️⃣ Import and initialize
import { RagChat } from "sakhaglobal-chatbot-rag-sdk";
const rag = new RagChat({
apiBaseUrl: "http://localhost:8000",
apiKey: "pk_live_xxx",
});2️⃣ (Optional) Initialize LLM providers
Call this once during app startup.
await rag.init({
geminiKey: "GEMINI_API_KEY",
groqKey: "GROQ_API_KEY",
});ℹ️ You can enable one or both providers depending on your backend setup.
3️⃣ Chat with your knowledge base
const response = await rag.chat("Hello!", "session_123");
console.log(response.answer);Response format:
{
answer: string;
sources?: string[];
}📄 PDF Management
Upload a PDF
const file = document.querySelector("input[type=file]").files[0];
await rag.uploadPdf(file);List uploaded PDFs
const res = await rag.listPdfs();
console.log(res.pdfs);
/*
[
{
filename: "document.pdf",
size: "1.2 MB",
date: "2025-01-01"
}
]
*/Delete a PDF
await rag.deletePdf("document.pdf");✅ Safe even if backend returns 204 No Content
🧪 Example (React)
import { useState } from "react";
import { RagChat } from "@sakhaglobal/ragchat";
const rag = new RagChat({
apiBaseUrl: "http://localhost:8000",
apiKey: "pk_live_xxx",
});
export default function Chat() {
const [msg, setMsg] = useState("");
const [reply, setReply] = useState("");
const send = async () => {
const res = await rag.chat(msg, "session_123");
setReply(res.answer);
};
return (
<>
<input value={msg} onChange={(e) => setMsg(e.target.value)} />
<button onClick={send}>Send</button>
<p>{reply}</p>
</>
);
}⚠️ Error Handling
All SDK methods throw a RagChatError on failure.
try {
await rag.deletePdf("file.pdf");
} catch (err) {
console.error("Operation failed:", err.message);
}