Nexai.js library

Github Repository ->

The Nexai AI Support Chat supports GPT-3.5-Turbo, GPT-4, Mistral and many other LLMs.

nexai.js is a nodejs library can be used in client side with React or JS or in server side nodejs.

nexai.js allows you to chat with the nexai ai chatbot and keep a conversation history on the server.

nexai.js also allows you to add document sources that the nexai ai will index and include in it's knowledgebase.

Install nexai.js in your typescript, nodejs or js project

This is the low level library used by the React AI Chat component nexai.js/chat-bubble and nexai.js/ai-search React Component.

For existing projects install the nexai.js library into your project dependencies.

npm i --save nexai.js

Full AI Chat Boilerplate Code

You will need your project API key from Dashboard -> Project -> Settings

For new projects you can clone the git repository directly. It is a good starting point for build full AI powered apps.

https://github.com/nexai-cloud/nexai.js

git clone https://github.com/nexai-cloud/nexai.js
cd nexai.js
npm install
npm run start

Adding documents to the AI knowledgebase

You will need your project APIKey and SecretKey from Dashboard -> Project -> Settings

The code will add a whole website to your AI knowledgebase.

The folliwng can only be run server-side as your secretKey must NEVER be revealed.

import { Nexai } from "nexai.js"

const apiKey = 'your-api-key'
// secretKey is only needed for adding documents
// it is not needed AI queries or chat
// @important do not reveal your secret key
const secretKey = process.env.SECRET_KEY

const nexai = Nexai({ apiKey, secretKey })

async function start() {
  // change do your website url
  const resp1 = await nexai.add('https://nexai.site') 
  console.log('add website response', resp1)
  
  const resp2 = await nexai.chat('How do I install nexai.js?')
  console.log('chat response', resp2)
}

start()

Save the file as add-website.ts

Run it with typescript tsx

SECRET_KEY=your-secret-key npx tsx add-website.ts

Simple Query to the AI with your knowledgebase as context

The following code will send some queries to the chat AI. The AI responds specifically to the context of documents in your website.

You will need your project APIKey from Dashboard -> Project -> Settings

import { Nexai } from "nexai.js"

const apiKey = 'your-api-key'

const nexai = Nexai({ apiKey })

async function start() {

  // assuming you already added https://nexai.site to your knowledgebase

  const resp1 = await nexai.chat('Tell me about nexai.')
  console.log('nexai resp 1', resp1)
  
  const resp2 = await nexai.chat('How do I install it?')
  console.log('nexai resp 2', resp2)
}

start()

Save the file as ai-chat.ts

Run it with typescript tsx

npx tsx ai-chat.ts

React AI Chat Example

You can use nexai directly in your React project.

import { Nexai } from "nexai.js"

const apiKey = 'your-api-key'

const nexai = Nexai({ apiKey })

export const SimpleChatExample = () => {
  const [msgs, setMsgs] = useState<string[]>([])
  const inputRef = useRef<HTMLInputElement>(null)
  const handleChat = async () => {
    const { value } = inputRef.current
    setMsgs([
      ...msgs,
      value
    ])
    const resp = await nexai.chat(value)
    setMsgs([
      ...msgs,
      resp.response
    ])
  }
  return (
    <div className="nexai-chat">
      <div className="chat-msgs">
        {
          msgs.map(msg => (
            <div className="chat-msg"></div>
          ))
        }
      </div>
      <input ref={inputRef} />
      <button onClick={handleChat}>Send</button>
    </div>
  )
}

Use in a component like this:

export const SampleComponent = () => {
  return (
    <div id="sample-component">
      <h1>Chat</h1>
      <div>This is an example chat component</div>
      <SimpleChatExample />
    </div>
  )
}

For a full react ai chatbot library use nexai.js/chat-bubble library.

You are good to go!

Go to the Nexai Dashboard