OpenAI in React: Integrating GPT-4 with Your React Application

Rate this content

In this talk, attendees will learn how to integrate OpenAI's GPT-4 language model into their React applications, exploring practical use cases and implementation strategies to enhance user experience and create intelligent, interactive applications.

11 min
12 Dec, 2023

AI Generated Video Summary

AI is a revolutionary change that helps businesses solve real problems and make applications smarter. Vectors enable semantic search, allowing us to find contextually relevant information. We'll build an AI-powered documentation site that answers questions, provides contextually relevant information, and offers links for further exploration. To enable vector search with MongoDB, we use the LingChain method to connect to MongoDB, create vector embeddings for user queries, and find related documents using maximal marginal reference. Join the workshop for a complete start-to-finish guide and integrate MongoDB Vector Search into your next React-based AI application.

1. Introduction to AI and React

Short description:

AI is a revolutionary change that helps businesses solve real problems and make applications smarter. We'll explore the demand for intelligent apps, limitations of LLMs, and how to overcome them. Using GPT and RAG, we can augment React apps with smarter capabilities using vectors as building blocks for representing complex data.

Artificial intelligence. It's just a fad, right? It's gonna blow over like a blockchain. Well, actually I don't think so. In fact, AI is far from a fad. It's a revolutionary change. It's helping businesses solve real problems and making employees and individuals more productive. So let's talk about why AI matters now more than ever and how AI can take your react applications to the next level.

I'm Jesse Hall, a senior developer advocate at MongoDB. You might also know me from my YouTube channel, Code Stacker. So throughout this talk, we're going to explore the demand for intelligent apps, practical use cases, limitations of LLMs, how to overcome these limitations, the tech stack that we're going to use to build a smart react app and how to integrate GPT, make it smart and optimize the user experience.

There is a huge demand for building intelligence into our applications in order to make these modern, highly engaging applications and to make differentiating experiences for each of our users. We have something called Generative Pretrained Transformers or GPT. These large language models perform a variety of tasks from natural language processing to content generation, and even some elements of common sense reasoning, and they are the brains that are making our applications smarter. But there is a catch. GPTs are incredible, but they aren't perfect. One of their key limitations is their static knowledge base. They only know what they've been trained on. There are integrations with some models now that can search the Internet for newer information, but how do we know that that information that they're finding on the Internet is accurate? They can hallucinate. Very confidently, I might add. So how can we minimize this? Now they can't access or learn from real-time proprietary data, your data. And that's a big limitation, don't you think? The need for real-time, proprietary and domain specific data is why we can't rely on the LLMs as they are.

Well, this brings us to the focus of our talk today. It's not merely about leveraging the power of GPT in React, it's about taking your React applications to the next level by making them intelligent and context aware. We're going to explore how to augment React apps with smarter capabilities using large language models and boost those capabilities even further with retrieval augmented generation or RAG. Now, what's involved in retrieval augmented generation? First up, vectors. What are vectors? These are the building blocks that allow us to represent complex multidimensional data in a format that's easy to manipulate and understand. Now the simplest explanation is a vector is a numerical representation of data. An array of numbers and these numbers are coordinates in an in dimensional space where in is the array length. So, however, many numbers we have in the array is how many dimensions we have. Now, you'll also hear vectors referred to as vector embeddings or just embeddings.

2. Vectors and Retrieval Augmented Generation

Short description:

Vectors enable semantic search, allowing us to find contextually relevant information. They can be created through an encoder and used in retrieval augmented generation. Private data is converted into embeddings and stored in a vector database. User queries are vectorized and used for vector search to find related information.

So here's a real life example of vectors in use. When you go to a store and you ask a worker where to find something, many times they're going to say go to aisle 30, bay 15. And so that is a two dimensional vector. And we also notice at stores that similar items will be placed near each other for ease of searching and finding.

The light bulbs aren't just scattered all over the store, they're strategically placed to be found easily. And so, again, what makes vectors so special? They enable semantic search. In simpler terms, they let us find information that is contextually relevant, not just a keyword search. And the data source is not just limited to text, it can also be images, video or audio. These can all be converted to vectors.

So how do we go about creating these vectors? Well, this is done through an encoder. The encoder defines how the information is organized in the virtual space. So now let's tie all this back to retrieval augmented generation. So first we take our private data or custom data, whatever it may be, and generate our embeddings using an embedding model and then store those embeddings in a vector database. And once we have our embeddings for our custom data, we can now accept user queries to find relevant information within our custom data. Now to do this, we send the user's natural language query to an LLM, which vectorizes the query, and then we use vector search to find information that is closely related, semantically related to the user's query, and then we return those results.

3. Building a REACT Application with AI and Next.js

Short description:

We'll build an AI-powered documentation site that answers questions, provides contextually relevant information, and offers links for further exploration. To get started, we create embeddings for our custom data written in markdown files. We use the Lang chain text splitter, MongoDB Atlas vector store, open AI embeddings, MongoDB client, and .env. After preparing the MongoDB connection and processing the documentation files, we store the embeddings in our MongoDB collection. We set up a search index on the collection using a JSON configuration. Finally, we set up the Next.js app, utilizing the Vercel AI SDK, LingChain, and OpenAI chat models.

So let's take a look at how to build a REACT application with these technologies. And this is what we're going to build, an AI-powered documentation site. Now, this site will not only answer questions but also provide contextually relevant information, summarize answers, and provide links to relevant pages to dig deeper.

Now, the first thing that we need to do is create embeddings for our custom data. Now, since this chatbot is going to reference our custom documentation, we'll assume it's written in markdown files. And so this embedding function is just a node app, a single file with less than 60 lines of code, and we're going to build our Next.js app. We have the Lang chain text splitter, the MongoDB Atlas vector store, open AI embeddings, MongoDB client, and then .env.

Below that, we'll prepare our MongoDB connection using our connection string and get the collection that we want to use. And then we need to process our documentation files. We have to split them into chunks that the encoder can use. So we'll create a splitter using the recursive character text splitter from lang chain to split the Markdown files that we're fetching. And then we'll create our output, awaiting the splitter, create documents function, passing it our documents. And lastly, we'll use lang chain to store these embeddings in our MongoDB collection. We're passing to MongoDB Atlas vector search the output, creating a new AI embeddings, and then giving it the metadata for the collection, index name, text key, and embedding key. These keys are the fields that contain the original text and the embedding vectors.

And this is an example of what ends up in our database. We have our original text and Markdown format, our vector embeddings and metadata that identifies the portion of the document that this text came from. And one last thing that we have to do to prepare the data for searching is to set up a search index on our collection in MongoDB. You can do that through the Atlas UI using this JSON configuration. So here we're specifying the dimensions of the encoder model that we're using. In this case, it's 1536. And then you can also define the similarity and type to use.

Okay, so now the fun part, let's set up the Next.js app. If you'd like, you can use the Next.js LingChain starter, which already has everything set up except for the MongoDB driver, so you'll also need to npm install MongoDB. Next, add your OpenAI API key to the environment variable file. Now this is the default chat route provided with the Next.js LingChain template. We can see that it utilizes the Vercel AI SDK along with LingChain, OpenAI chat models and some LingChain schema helpers. Now, further down this route handler, we are creating an OpenAI chat, and here we can specify which model we'd like to use. We're also setting streaming to true because we want the response to start populating in the UI as fast as possible and stream to the user, and then it returns the streaming response. And so in this route is where we need to inject our own custom data.

4. Using MongoDB Vector Search with LingChain

Short description:

To enable vector search with MongoDB, we use the LingChain method to connect to MongoDB, create vector embeddings for user queries, and find related documents using maximal marginal reference. We include the search results, user's question, and context in the LLM to receive a response. The question, 'How do I get started with MongoDB and Node.js?' is answered with a summary and links to relevant documentation pages. Join the workshop for a complete start-to-finish guide and integrate MongoDB Vector Search into your next React-based AI application.

To do that, we're going to use the LingChain MongoDB vector search method. In this route, we're going to connect to MongoDB just like we did before, and we're going to pass to this the question that the user asked. And then we'll use the MongoDB Atlas vector search LingChain method to create vector embeddings for the user's question.

And then when we do the search, we're using something called maximal marginal reference to find the related documents. We can specify the number of results to fetch and how many of the top results to return. This allows us to refine how accurate we want to be.

Now back in our chat route, we're going to get the last message from the user and pass it to the new vector search route that we just created. And now we can include the results of that search and pass everything to the standard LLM. We'll include some context, the vector search results, and the user's question. And finally, we replace the last user's message with our updated message that includes our custom information and context. That then gets passed to the LLM and we receive our response back. And these are the results.

The question is, how do I get started with MongoDB and Node.js? It answers with a summary and links to specific pages in the documentation for further help.

Now if you want to build something like this from start to finish, be sure to attend my workshop. You can get more details on how to sign up for that on the conference website. Now remember, the future is not just about smarter AI, but also about how well it's integrated into user-centric platforms, like your next React-based project. So be sure to give MongoDB Vector Search a try in your next AI application.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

JSNation 2023JSNation 2023
24 min
AI and Web Development: Hype or Reality
In this talk, we'll take a look at the growing intersection of AI and web development. There's a lot of buzz around the potential uses of AI in writing, understanding, and debugging code, and integrating it into our applications is becoming easier and more affordable. But there are also questions about the future of AI in app development, and whether it will make us more productive or take our jobs.
There's a lot of excitement, skepticism, and concern about the rise of AI in web development. We'll explore the real potential for AI in creating new web development frameworks, and separate fact from fiction.
So if you're interested in the future of web development and the role of AI in it, this talk is for you. Oh, and this talk abstract was written by AI after I gave it several of my unstructured thoughts.
React Summit US 2023React Summit US 2023
30 min
The Rise of the AI Engineer
We are observing a once in a generation “shift right” of applied AI, fueled by the emergent capabilities and open source/API availability of Foundation Models. A wide range of AI tasks that used to take 5 years and a research team to accomplish in 2013, now just require API docs and a spare afternoon in 2023. Emergent capabilities are creating an emerging title: to wield them, we'll have to go beyond the Prompt Engineer and write *software*. Let's explore the wide array of new opportunities in the age of Software 3.0!
JS GameDev Summit 2023JS GameDev Summit 2023
37 min
Building the AI for Athena Crisis
This talk will dive into how to build an AI for a turn based strategy game from scratch. When I started building Athena Crisis, I had no idea how to build an AI. All the available resources were too complex or confusing, so I just started building it based on how I would play the game. If you would like to learn how to build an AI, check out this talk!
TestJS Summit 2023TestJS Summit 2023
8 min
Code coverage with AI
In this lightning demo I will showcase how Codium, a cutting-edge generative AI tool, is revolutionizing code integrity. We will demonstrate Codium's ability to generate useful Mocha tests, taken from a public repository and highlight the seamless integration. You can see Codium as it transforms complex test scenarios into actionable insights, propelling code coverage forward. Join us for an insightful peek into the future of automated testing where speed meets quality!
React Summit 2023React Summit 2023
29 min
Improving Developer Happiness with AI
GitHub Copilot is an AI pair programmer that can help you write code faster and spend less time writing repetitive code.This session will cover some interesting use cases for Copilot that could shine a light on its possibilities. This ranges from prompting Copilot to suggest a function based on a comment, learning how to use a new framework, tackling a security or accessibility bug, better documenting your code, translating  code from one language to another, etc.Agenda:
Introduction to CoPilot
- What is Copilot
- How can you use it
- How it can help you write code faster
- Copilot Labs experimental features I will pick examples from the React ecosystem and show how we can fix Security Vulnerabilities and Accessibility issues in some components.

Workshops on related topic

DevOps.js Conf 2024DevOps.js Conf 2024
163 min
AI on Demand: Serverless AI
Featured WorkshopFree
In this workshop, we discuss the merits of serverless architecture and how it can be applied to the AI space. We'll explore options around building serverless RAG applications for a more lambda-esque approach to AI. Next, we'll get hands on and build a sample CRUD app that allows you to store information and query it using an LLM with Workers AI, Vectorize, D1, and Cloudflare Workers.
React Advanced Conference 2023React Advanced Conference 2023
98 min
Working With OpenAI and Prompt Engineering for React Developers
In this workshop we'll take a tour of applied AI from the perspective of front end developers, zooming in on the emerging best practices when it comes to working with LLMs to build great products. This workshop is based on learnings from working with the OpenAI API from its debut last November to build out a working MVP which became PowerModeAI (A customer facing ideation and slide creation tool).
In the workshop they'll be a mix of presentation and hands on exercises to cover topics including:
- GPT fundamentals- Pitfalls of LLMs- Prompt engineering best practices and techniques- Using the playground effectively- Installing and configuring the OpenAI SDK- Approaches to working with the API and prompt management- Implementing the API to build an AI powered customer facing application- Fine tuning and embeddings- Emerging best practice on LLMOps
JSNation Live 2021JSNation Live 2021
81 min
Intro to AI for JavaScript Developers with Tensorflow.js
Have you wanted to explore AI, but didn't want to learn Python to do it? Tensorflow.js lets you use AI and deep learning in javascript – no python required!
We'll take a look at the different tasks AI can help solve, and how to use Tensorflow.js to solve them. You don't need to know any AI to get started - we'll start with the basics, but we'll still be able to see some neat demos, because Tensorflow.js has a bunch of functionality and pre-built models that you can use on the server or in the browser.
After this workshop, you should be able to set up and run pre-built Tensorflow.js models, or begin to write and train your own models on your own data.