0.5.6 • Published 9 months ago

react-ai-chatbot v0.5.6

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

React AI ChatBot

Simple React AI Chatbot based on GPT-3 or GPT-4 OpenAI models to chat with your data. This component provides hooks for chatbot interaction - sending and receiving messages. UI Styling is completely up to you!

Chatlas.ai is used to set discussion context and settings!

It's recommended to have your own Open AI Api Key to use React AI ChatBot! You can get your key here.

Install

npm install react-ai-chatbot

or

yarn install react-ai-chatbot

Usage instructions:

  1. Register or login at OpenAI platform and set your API Key here.

  2. Singup at chatlas.ai and add your OpenAI Key.

  3. Start discussion either from documents or webpages.

  4. Enable discussion sharing (at right top side). Copy discussion endpoint.

  5. Example implementation (update <your_discussion_endpoint> accordingly):

import { ReactNode, useState, ChangeEvent } from 'react';
import { ChatBotProvider, useChatBot, usePending } from 'react-ai-chatbot';

const ChatBot = ({ children } : { children: ReactNode }) => {

  const [ message, setMessage ] = useState<string>('');

  const { messages, sendMessage, isLoading, isError, error } = useChatBot();

  const messageChangeHandler = (e:ChangeEvent<HTMLInputElement>) => setMessage(e.target.value);

  const sendMessageHandler = () => {

    sendMessage(message);
    setMessage('');
  }

  return <div>
    {isLoading ? <div>loading...</div> : ''}
    {isError ? <div>{error}</div> : ''}
    <div>
      {messages.map((m, i)=><div key={i}>{m.type}: {m.message}</div>)}
      <div>{children}</div>
    </div>
    <div>
      <input type="text" value={message} onChange={messageChangeHandler} />
    </div>
    <button onClick={sendMessageHandler}>Send</button>
  </div>
}

const PendingMessage = () => {

  const { pending } = usePending();

  return <>{ pending }</>;
}

const App = () => {

  return <ChatBotProvider endpoint='<your_discussion_endpoint>'>
    <ChatBot>
      <PendingMessage />
    </ChatBot>
  </ChatBotProvider>
}

Documentation

useChatBot

  • messages: <{type: 'bot' | 'user', message: string}> - Discussion message are stored in this array
  • sendMessage: <(message: string)> - function used to send message
  • isLoading: boolean - true while ai generates the message
  • isError: boolean - when error occurs
  • error: string - error message
  • clearMessages() - clear all chat messages
  • setEndpoint(endpoint:string) - set chat endpoint. Can be used to switch to a different chat

usePending

  • pending: string - use to get message on-the-fly while it's being generated, chatgpt style.