Overview
With the AI SDK installed, you can easily create a chatbot using the useChat
hook. This hook provides an easy way to interact with AI models and display conversations in streaming mode.
Demo AI Chat
Setup
As an example, let's use the OpenAI API to create an AI assistant. Here are the steps to set up the project:
-
Create OpenAI API key
Please create an account, top up credits, and get the API key here.
-
Install the
@ai-sdk/openai
package:bash
-
Add the following environment variables to .env file with the API key:
code
API Endpoint
Create an API endpoint to handle the chatbot conversation in the /api/demo/ai/route.ts
file:
route.ts
You can also use Gemini by installing @ai-sdk/google
and add GOOGLE_GENERATIVE_AI_API_KEY
to your environment variables. Here is an example of using the gemini-1.5-pro-latest
model:
route.ts