In this article, we will guide you through the process of building a full-stack application that leverages Retrieval Augmented Generation (RAG) to facilitate a chatbot that delivers accurate and contextually relevant responses. We will utilize Pinecone to manage our knowledge base, and Vercel’s AI SDK will help streamline our chatbot’s workflow. Let’s dive into creating an effective and engaging user experience!
Step 1: Setting Up Your Next.js Application
To kick things off, we will create a new Next.js application. Next.js is favored for its performance and server-side rendering capabilities. We can create our app with minimal hassle by running the following command:
npx create-next-app chatbot
After creating the app, we should add the AI package using:
npm install ai
For a complete list of dependencies, refer to the full list.
Step 2: Create the Chatbot
Next, we’ll create the chatbot by establishing both the backend and frontend components in our Next.js application.
Chatbot Frontend Component
We start with the frontend of our chatbot where we will construct the user interface for interaction. Here’s an analogy: think of your chatbot like a movie theater. The front end is the glamorous entrance where ticket holders enter, whereas the backend is the projection room that shows the movies (in this case, the messages). The following code will set up the Chat component to render our chat interface:
import React, { FormEvent, ChangeEvent } from 'react';
import Messages from './Messages';
import { Message } from 'ai-react';
interface Chat {
input: string;
handleInputChange: (e: ChangeEvent) => void;
handleMessageSubmit: (e: FormEvent) => Promise;
messages: Message[];
}
const Chat: React.FC = ({ input, handleInputChange, handleMessageSubmit, messages }) => {
return (
);
};
export default Chat;
In this code, we set up our chat interface to display messages and capture user input effectively.
Chatbot API Endpoint
Next, we need to set up our API endpoint to handle requests and responses. Think of this API as a delivery service, transporting requests from the user to the backend, and delivering responses back. Here’s how to create that API:
import { Configuration, OpenAIApi } from 'openai-edge';
import { Message, OpenAIStream, StreamingTextResponse } from 'ai';
const config = new Configuration({ apiKey: process.env.OPENAI_API_KEY });
const openai = new OpenAIApi(config);
export const runtime = 'edge';
export async function POST(req: Request) {
try {
const messages = await req.json();
const prompt = [ /* Prompt setup */ ];
const response = await openai.createChatCompletion({ model: 'gpt-3.5-turbo', stream: true, messages: // Your message logic here });
const stream = OpenAIStream(response);
return new StreamingTextResponse(stream);
} catch (e) {
throw e;
}
};
This code allows us to process user messages and obtain a coherent chatbot response.
Step 3: Adding Context
Understanding context is crucial for creating a natural conversation with our chatbot. The next step involves seeding our knowledge base, which is akin to planting a garden where our chatbot can gather the necessary information to respond.
Seeding the Knowledge Base
In this step, we’ll create a crawler that will gather data from various online sources. It’s like having a diligent worker who collects vegetables for our garden. Below is a portion of the crawler code:
class Crawler {
private seen = new Set();
private queue: { url: string; depth: number }[] = [];
async crawl(startUrl: string): Promise {
this.addToQueue(startUrl);
while (this.shouldContinueCrawling()) {
const { url, depth } = this.queue.shift()!;
if (this.isTooDeep(depth) || this.isAlreadySeen(url)) continue;
this.seen.add(url);
const html = await this.fetchPage(url);
this.pages.push({ url, content: this.parseHtml(html) });
this.addNewUrlsToQueue(this.extractUrls(html, url), depth);
}
return this.pages;
}
};
Troubleshooting
While you work through these steps, you may encounter some challenges. Here are a few troubleshooting tips:
- Problem: Chatbot does not respond correctly.
- Solution: Ensure that the prompts and messages sent to the OpenAI API are well-structured and contextually accurate.
- Problem: The crawler is not pulling data.
- Solution: Check the maximum depth and number of pages to ensure you’re not exceeding the limits!
- Problem: Environment variables like
OPENAI_API_KEYnot set. - Solution: Ensure you have defined all required environment variables in your application.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
By following the steps outlined above, you will create a powerful context-aware chatbot that can interact effectively with users, providing accurate responses without hallucination. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Running Tests
Always ensure your implementation is bug-free by performing end-to-end tests. This project uses Playwright for testing. You can run the tests by executing:
npm run test:e2e
After running the tests, view the HTML report generated to fix any errors that may arise.

