Skip to main content
Version: v2.x

Get Started ― NLUX And ChatGPT API From Browser (unsafe)

OpenAI provides very powerful APIs to access their GPT chat completion models.
In this guide, we will use NLUX and ChatGPT APIs to create a simple chatbot in the browser.

warning

The adapters used in this getting started page are simple to use but they should only be used for prototyping and testing purposes. As they connect to OpenAI API directly from the browser, exposing the API key in the client-side code. This approach is unsafe and not recommended for production use cases.

Some safe alternatives for production

NLUX is available as a React JS component and hooks, or as a JavaScript library.
The features are identical for both platforms. Use the version that best suits your needs.


NLUX + ChatGPT + React JS

This guide will walk you through the steps to add NLUX conversational capabilities to a React JS app.
It assumes that you already have a React JS app set up.

If you don't have a React JS app set up yet, and you are looking for a quick way to get started, you can use Vite's react-ts template to quickly set up a React JS app.

Set up a React JS project with vite

Use the following npm commands to set up a React JS app with TypeScript using Vite's react-ts template:

npm create vite@latest my-ai-chat-app -- --template react-ts
cd my-ai-chat-app
npm install
npm run dev

The last command will start the development server and open the app in your default browser.


1. Get Your OpenAI API Key

Start by getting a new API key from OpenAI.

  1. If you don't have an account, go to the OpenAI signup page and create an account.
  2. Go to the API keys page
  3. Click the Creat new secret key button
  4. Give your API key a name and click Create secret key
  5. Copy the API key and save it in a safe place. You will need it to configure the OpenAI NLUX adapter.

2. Install NLUX Packages

You can start by adding NLUX to your React JS app using your favorite package manager. At the root of your project, run the following command:

npm install @nlux/react @nlux/openai-react

This will install the @nlux/react and @nlux/openai-react packages.


3. Import Component And Hook

Import the useUnsafeChatAdapter hook and the AiChat component in your JSX file:

import {AiChat} from '@nlux/react';
import {useUnsafeChatAdapter} from '@nlux/openai-react';

The AiChat component is the main chat component that you will use to display the chat UI.
The useUnsafeChatAdapter hook is used to create an adapter for the OpenAI Chat Completion API.


4. Create ChatGPT Adapter

You can use the useUnsafeChatAdapter hook to create an OpenAI adapter.
You can optionally import ChatAdapterOptions from @nlux/openai-react to define the type of the options object.

const adapterOptions: ChatAdapterOptions = {
apiKey: 'your-openai-api-key-here',
model: 'gpt-3.5-turbo',
systemMessage: 'Act as a helpful assistant and be funny and engaging.',
};

export const App = () => {
const openAiAdapter = useUnsafeChatAdapter(adapterOptions);
}

The useUnsafeChatAdapter hook takes config parameters and returns an adapter object. Please refer to the reference documentation for more information on the available options.


5. Create Chat Component

Now that we have the OpenAI adapter, we will create the chat component and pass the adapter to it.

import {AiChat} from '@nlux/react';
import {useUnsafeChatAdapter, ChatAdapterOptions} from '@nlux/openai-react';

const adapterOptions: ChatAdapterOptions = {
apiKey: 'your-openai-api-key-here',
model: 'gpt-3.5-turbo',
systemMessage: 'Act as a helpful assistant and be funny and engaging.',
};

export const App = () => {
const openAiAdapter = useUnsafeChatAdapter(adapterOptions);

return (
<AiChat
adapter={openAiAdapter}
composerOptions={{
placeholder: 'How can I help you today?'
}}
/>
);
};

The AiChat component can take several parameters:

  • The first parameter adapter is the only required parameter, and it is the adapter that we created earlier.
  • The second parameter that we provide here is an object that contains the composer options. In this case, we are passing a placeholder text placeholder to customize the composer.

For full documentation on how to customize the AiChat component, please refer to the AiChat documentation.


6. Add CSS Styles

NLUX comes with a default CSS theme that you can use to style the chat UI. There are 2 ways to import the stylesheet, depending on your setup.

Using JSX Bundler

You can import it in your JSX component file by installing the @nlux/themes package:

npm install @nlux/themes

Then import the default theme nova.css in your React component:

import '@nlux/themes/nova.css';

This will require a CSS bundler such as Vite, or Webpack that's configured to handle CSS imports for global styles. Most modern bundlers are configured to handle CSS imports.

Alternatively, you can include the CSS stylesheet in your HTML file.
We provide a CDN link that you can use to include the stylesheet in your HTML file:

<link rel="stylesheet" href="https://content.nlkit.com/nlux/themes/nova.css" />

This CDN link is not meant for production use, and it is only provided for convenience. Make sure you replace it with the latest version of the stylesheet before deploying your app to production.


7. Run Your App

Once you have configured all of the above, your code will look like this:

import {AiChat} from '@nlux/react';
import {useUnsafeChatAdapter, ChatAdapterOptions} from '@nlux/openai-react';
import '@nlux/themes/nova.css';

const adapterOptions: ChatAdapterOptions = {
apiKey: 'your-openai-api-key-here',
model: 'gpt-3.5-turbo',
systemMessage: 'Act as a helpful assistant and be funny and engaging.',
};

export const App = () => {
const openAiAdapter = useUnsafeChatAdapter(adapterOptions);

return (
<AiChat
adapter={openAiAdapter}
composerOptions={{
placeholder: 'How can I help you today?'
}}
/>
);
};

You can now run your app and test the chatbot.
The result is a fully functional chatbot UI:

And NLUX is handling all the UI interactions and the communication with the OpenAI API.