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.
OpenAI in React: Integrating GPT-4 with Your React Application
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
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
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
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
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.
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.