Create an AI Chatbot
Let’s use next-forge to create a simple AI chatbot.
Today we’re going to create an AI chatbot using next-forge and the built-in AI package, powered by Vercel AI SDK.
1. Create a new project
This will create a new project with the name ai-chatbot
and install the necessary dependencies.
2. Configure your environment variables
Follow the guide on Environment Variables to fill in your environment variables.
Specifically, make sure you set an OPENAI_API_KEY
environment variable.
3. Create the chatbot UI
We’re going to start by creating a simple chatbot UI with a text input and a button to send messages.
Create a new file called Chatbot
in the app/components
directory. We’re going to use a few things here:
useChat
from our AI package to handle the chat logic.Button
andInput
components from our Design System to render the form.Thread
andMessage
components from our AI package to render the chat history.handleError
from our Design System to handle errors.SendIcon
fromlucide-react
to create a send icon.
4. Create the chatbot API route
Create a new file called chat
in the app/api
directory. This Next.js route handler will handle the chatbot’s responses.
We’re going to use the streamText
function from our AI package to stream the chatbot’s responses to the client. We’ll also use the provider
function from our AI package to get the OpenAI provider, and the log
function from our Observability package to log the chatbot’s responses.
5. Update the app
Finally, we’ll update the app/page.tsx
file to be a simple entry point that renders the chatbot UI.
6. Run the app
Run the app development server and you should be able to see the chatbot UI at http://localhost:3000.
That’s it! You’ve now created an AI chatbot using next-forge and the built-in AI package. If you have any questions, please reach out to me on Twitter or open an issue on GitHub.