1.1.2 • Published 4 months ago

react-llm-chatbot v1.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

React Easy Chatbot

A customizable React chatbot component that can be easily integrated into any React application.

Features

  • 🚀 Simple to integrate with any React app
  • ⚙️ Highly customizable appearance
  • 🌙 Light and dark themes
  • 📱 Mobile-responsive design
  • 🔄 Easy API integration
  • 🔒 Token-based authentication

Installation

npm install react-llm-chatbot
# or
yarn add react-llm-chatbot

Quick Start

import React from 'react';
import Chatbot from 'react-llm-chatbot';

const App = () => {
  // You need to obtain this token from your backend service
  const token = "your-api-access-token";

  return (
    <div className="App">
      <Chatbot 
        apiBaseURL="https://your-chatbot-api.com/api"
        token={token}
        theme="light"
        onError={(error) => console.error('Chatbot error:', error)}
      />
    </div>
  );
};

export default App;

Configuration Options

PropTypeDefaultDescription
apiBaseURLstring-Base URL for the chatbot API
tokenstring-Required Authentication token for API access
themestring'light'Theme ('light' or 'dark')
positionstring'bottom-right'Position of the chatbot ('bottom-right', 'bottom-left', 'top-right', 'top-left')
widthnumber/string350Width of the chat window
heightnumber/string500Height of the chat window
titlestring'Chat Assistant'Title shown in the chat header
placeholderstring'Type a message...'Placeholder for the input field
showHeaderbooleantrueWhether to show the chat header
showFooterbooleantrueWhether to show the chat footer
customStylesobject{}Custom CSS variables to override default styles
onErrorfunction() => {}Callback when an error occurs

API Integration

This chatbot component is designed to work with a backend API that provides the following endpoints:

1. /chat

  • Method: POST
  • Description: Sends a message to the chatbot
  • Headers: Authorization: Bearer <token>
  • Request Body:
    {
      "message": "user_message"
    }
  • Response:
    {
      "status": "success",
      "response": "chatbot_response"
    }

2. /test

  • Method: GET
  • Description: Tests the API connection
  • Response:
    {
      "status": "ok",
      "message": "API is working!",
      "active_tokens": [number_of_active_tokens]
    }

Advanced Usage

Using the ChatbotProvider

For more control, you can use the ChatbotProvider to manage the chatbot state in your application:

import React from 'react';
import { ChatbotProvider } from 'react-llm-chatbot';

const App = () => {
  return (
    <ChatbotProvider
      apiBaseURL="https://your-chatbot-api.com/api"
      token="your-api-token"
    >
      {/* Your components can now use the useChatbot hook */}
      <YourCustomChat />
    </ChatbotProvider>
  );
};

Using the useChatbot Hook

import React from 'react';
import { useChatbot } from 'react-llm-chatbot';

const YourCustomChat = () => {
  const { 
    messages, 
    sendMessage, 
    isLoading, 
    clearMessages 
  } = useChatbot();

  // Custom chat UI implementation
  return (
    <div>
      {/* Your custom UI */}
    </div>
  );
};

License

MIT

1.1.2

4 months ago

1.1.1

4 months ago

1.1.0

4 months ago

1.0.5

4 months ago

1.0.4

4 months ago

1.0.3

4 months ago

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago