Description
A Next.js starter chatbot using Vercel's AI SDK and implements the Retrieval-Augmented Generation (RAG) pattern with Pinecone
npx boilerapp pinecone-vercel-aiDocumentation
In this example, we'll build a full-stack application that uses Retrieval Augmented Generation (RAG) powered by Pinecone to deliver accurate and contextually relevant responses in a chatbot.
RAG is a powerful tool that combines the benefits of retrieval-based models and generative models. Unlike traditional chatbots that can struggle with maintaining up-to-date information or accessing domain-specific knowledge, a RAG-based chatbot uses a knowledge base created from crawled URLs to provide contextually relevant responses.
Incorporating Vercel's AI SDK into our application will allow us easily set up the chatbot workflow and utilize streaming more efficiently, enhancing the responsiveness and performance of our chatbot.
By the end of this tutorial, you'll have a context-aware chatbot that provides accurate responses without hallucination, ensuring a more effective and engaging user experience. Let's get started on building this powerful tool (Full code listing).
Step 1: Setting Up Your Next.js Application
Next.js is a powerful JavaScript framework that enables us to build server-side rendered and static web applications using React. It's a great choice for our project due to its ease of setup, excellent performance, and built-in features such as routing and API routes.
To create a new Next.js app, run the following command:
npx
npx create-next-app chatbot
Next, we'll add the ai package and the OpenAI AI SDK Provider:
npm install ai @ai-sdk/openai
You can use the full list of dependencies if you'd like to build along with the tutorial.
Step 2: Create the Chatbot
In this step, we're going to use the Vercel SDK to establish the backend and frontend of our chatbot within the Next.js application. By the end of this step, our basic chatbot will be up and running, ready for us to add context-aware capabilities in the following stages. Let's get started.
Chatbot frontend component
Now, let's focus on the frontend component of our chatbot. We're going to build the user-facing elements of our bot, creating the interface through which users will interact with our application. This will involve crafting the design and functionality of the chat interface within our Next.js application.
First, we'll create the Chat component, that will render the chat interface.
import React, { FormEvent, ChangeEvent } from "react";
import Messages from "./Messages";
import { Message } from "ai/react";
interface Chat {
input: string;
handleInputChange: (e: ChangeEvent<HTMLInputElement>) => void;
handleMessageSubmit: (e: FormEvent<HTMLFormElement>) => Promise<void>;
messages: Message[];
}
const Chat: React.FC<Chat> = ({
input,
handleInputChange,
handleMessageSubmit,
messages,
}) => {
return (
<div id="chat" className="...">
<Messages messages={messages} />
<>
<form onSubmit={handleMessageSubmit} className="...">
<input
type="text"
className="..."
value={input}
onChange={handleInputChange}
/>
<span className="...">Press ⮐ to send</span>
</form>
</>
</div>
);
};
export default Chat;
This component will display the list of messages and the input form for the user to send messages. The Messages component to render the chat messages:
import { Message } from "ai";
import { useRef } from "react";
export default function Messages({ messages }: { messages: Message[] }) {
const messagesEndRef = useRef<HTMLDivElement | null>(null);
return (
<div className="...">
{messages.map((msg, index) => (
<div
key={index}
className={`${
msg.role === "assistant" ? "text-green-300" : "text-blue-300"
} ... `}
>
<div className="...">{msg.role === "assistant" ? "🤖" : "🧑💻"}</div>
<div className="...">{msg.content}</div>
</div>
))}
<div ref={messagesEndRef} />
</div>
);
}
Our main Page component will manage the state for the messages displayed in the Chat component:
"use client";
import Header from "@/components/Header";
import Chat from "@/components/Chat";
import { useChat } from "ai/react";
const Page: React.FC = () => {
const [context, setContext] = useState<string[] | null>(null);
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div className="...">
<Header className="..." />
<div className="...">
<Chat
input={input}
handleInputChange={handleInputChange}
handleMessageSubmit={handleSubmit}
messages={messages}
/>
</div>
</div>
);
};
export default Page;
The useful useChat hook will manage the state for the messages displayed in the Chat component. It will:
- Send the user's message to the backend
- Update the state with the response from the backend
- Handle any internal state changes (e.g. when the user types a message)
Chatbot API endpoint
Next, we'll set up the Chatbot API endpoint. This is the server-side component that will handle requests and responses for our chatbot. We'll create a new file called api/chat/route.ts and add the following dependencies:
import { Message, streamText } from "ai";
import { openai } from "@ai-sdk/openai";
import { getContext } from "@/utils/context";
Next, we'll define the endpoint handler:
export async function POST(req: Request) {
try {
const { messages } = await req.json();
const prompt = [
{
role: "system\
Prix
Gratuit