How To Build AI Chatbots Using React JS in Minutes
Tags: react, openai, javascript, news
Published: 2023-11-08 14:00:33

Conversational AI is taking off, and chatbots powered by large language models (LLMs) like ChatGPT are rapidly becoming a must-have for many apps and websites. As a React developer, you want a simple way to build a great chat interface backed by these incredible LLMs. **That’s where [NLUX](https://nlux.ai), — the open-source Javascript / React JS library, comes in**.

## Straight To The Point 👇

Here is all what you need to build a custom AI chatbot using NLUX:

```sh
npm install @nlux/react @nlux/openai-react
```
```jsx
import {AiChat} from '@nlux/react';
import {useAdapter} from '@nlux/openai-react';
 
const MyAiChatComp = () => {
    const chatGptAdapter = useAdapter({
        apiKey: 'YOUR_OPEN_AI_API_KEY',
        initialSystemMessage:
            'I want you to act Elon Musk, with a twist of humor, teaching ' +
            'a web developer about how to make the most out of ' +
            'generative AI. Give answers of around 1 paragraph.'
    });
 
    return (
        <AiChat
            adapter={chatGptAdapter}
            promptBoxOptions={{placeholder: 'Hi, Elon here! How can I help?'}}
        />
    );
}
```

In this example, we’re instructing ChatGPT to “act as” Elon Musk, and advise a web developer about how to make the most out of generative AI. All the interactions with the bot will be influenced by that instruction.

You can enter any “act as” instruction! Your imagination is the limit!

## The Result 💬 🤖

And now you have added to your web page an AI chat component, with a prompt box, and a What’s-App-like UI, connected to OpenAI’s ChatGPT API, and actually behaving like Elon Musk!

![How to build Conversational AI with NLUX](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blcrxr6f99r7u5jthxap.png)

**And this how the conversation happens:**

{% embed https://codesandbox.io/embed/23xh6f?view=Editor+%2B+Preview&module=%2Fapp.tsx&hidenavigation=1"
     style="width:100%; height: 500px; border:0; border-radius: 4px; overflow:hidden;"
     title="pop-haibt-23xh6f"
     allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
     sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts %}

---

## So What Is NLUX ? 👨‍💻

[NLUX](https://nlux.ai) (for Natural Language User eXperience) is a new open-source Javascript / React JS library that makes it super simple to integrate powerful large language models (LLMs) like ChatGPT into your web app or website.

As you can see in the example above, with just a few lines of code, you can add conversational AI capabilities and interact with your favourite LLM.

Over the past few weeks, I have dedicated significant time and effort into this project. Moving forward, I am excited to commit even more time and energy toward building a free, open-source, and features-rich JS library to make it super simple for web developers to build AI-powered apps.

> Your contributions are welcome. **_If you have an idea for a new feature or want to fix a bug, feel free to open an issue or submit a pull request._** I’ll be very excited to see what you can build with NLUX!

## Learn More

* NPM Packages for [React JS here](https://www.npmjs.com/package/@nlux/react) and for [Vanilla JS here](https://www.npmjs.com/package/@nlux/core).
* GitHub Repository: [github.com/nluxai/nlux](https://github.com/nluxai/nlux) - You can ask questions or get help in the GitHub Discussions section.
* Website for documentation and updates: [nlux.ai](https://nlux.ai)
 
And if you like the idea, **[please star ⭐️ the repo on GitHub](https://github.com/nluxai/nlux)** to show your support! 🙏️ 🙂 🧡

Thank you so much!
Salmen