/**
* @fileoverview Basic usage example for the RAG Chatbot Library
*/
"use client";
import React, { useState } from "react";
import {
ChatbotProvider,
ChatbotWidget,
useChatbot,
useErrorHandler,
} from "../index";
/**
* Basic chatbot setup example
*/
export function BasicChatbotExample() {
const config = {
llm: {
provider: "openai" as const,
model: "gpt-3.5-turbo",
apiKey: process.env.NEXT_PUBLIC_OPENAI_API_KEY || "",
maxTokens: 1000,
temperature: 0.7,
},
vectorStore: {
provider: "memory" as const,
dimensions: 1536,
},
storage: {
provider: "local" as const,
},
};
return (
Basic Chatbot Example
This example shows the most basic setup of the RAG chatbot.
{/* The chatbot widget will appear as a floating button */}
);
}
/**
* Custom chat interface example
*/
export function CustomChatInterface() {
const { isInitialized, sendMessage, currentConversation, isProcessing } =
useChatbot();
const { error, hasError, clearError } = useErrorHandler();
const [inputValue, setInputValue] = useState("");
const handleSendMessage = async () => {
if (!inputValue.trim() || isProcessing) return;
try {
await sendMessage(inputValue);
setInputValue("");
} catch (err) {
console.error("Failed to send message:", err);
}
};
const handleKeyPress = (e: React.KeyboardEvent) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSendMessage();
}
};
if (!isInitialized) {
return (
);
}
return (
{/* Chat Header */}
AI Assistant
Ask questions about uploaded documents
{/* Error Display */}
{hasError && (
)}
{/* Messages */}
{currentConversation?.messages.map((message) => (
{message.content}
{message.timestamp.toLocaleTimeString()}
))}
{isProcessing && (
)}
{/* Input */}
);
}
/**
* Full featured example with document management
*/
export function FullFeaturedExample() {
return (
Full Featured Chatbot
{/* Document Management Panel */}
{/* Chat Interface */}
);
}
/**
* Document management panel component
*/
function DocumentManagementPanel() {
const { uploadDocument, documents } = useChatbot();
const [isUploading, setIsUploading] = useState(false);
const handleFileUpload = async (
event: React.ChangeEvent
) => {
const file = event.target.files?.[0];
if (!file) return;
setIsUploading(true);
try {
await uploadDocument(file, {
title: file.name,
uploadedAt: new Date(),
});
} catch (error) {
console.error("Upload failed:", error);
} finally {
setIsUploading(false);
}
};
return (
Knowledge Base
{/* Upload Section */}
{isUploading && (
Uploading...
)}
{/* Documents List */}
Documents ({documents.length})
{documents.map((doc) => (
{doc.metadata.title || doc.metadata.filename}
{doc.metadata.fileType} • {doc.metadata.fileSize} bytes
{doc.metadata.uploadedAt?.toLocaleDateString()}
))}
{documents.length === 0 && (
No documents uploaded yet
)}
);
}