Node.js Quickstart
In this quickstart tutorial, you'll build a simple AI chatbot with a streaming user interface. Along the way, you'll learn key concepts and techniques that are fundamental to using the SDK in your own projects.
If you are unfamiliar with the concepts of Prompt Engineering and HTTP Streaming, you can optionally read these documents first.
Prerequisites
To follow this quickstart, you'll need:
- Node.js 18+ and pnpm installed on your local development machine.
- An OpenAI API key.
If you haven't obtained your OpenAI API key, you can do so by signing up on the OpenAI website.
Vercel AI SDK is designed to be a unified interface to interact with any large language model. This means that you can change model and providers with just one line of code! Learn more about available providers and building custom providers in the providers section.
Setup Your Application
Start by creating a new directory using the mkdir
command. Change into your new directory and then run the pnpm init
command. This will create a a package.json
in your new directory.
mkdir my-ai-appcd my-ai-apppnpm init
Install dependencies
In your new directory, install the following dependencies:
pnpm add ai @ai-sdk/openai zod dotenvpnpm add -D @types/node tsx typescript
Make sure you are using ai
version 3.1 or higher.
The ai
and @ai-sdk/openai
packages contain the Vercel AI SDK and the Vercel AI SDK OpenAI provider, respectively. You will use zod
to define type-safe schemas that you will pass to the large language model (LLM). You will use dotenv
to access environment variables (your OpenAI key) within your application. There are also three development dependencies, installed with the -D
flag, that are necessary to run your Typescript code.
Configure OpenAI API key
Create a .env
file in your project root and add your OpenAI API Key. This key is used to authenticate your application with the OpenAI service.
touch .env
OPENAI_API_KEY=xxxxxxxxx
Replace xxxxxxxxx
with your actual OpenAI API key.
Vercel AI SDK's OpenAI Provider will default to using the OPENAI_API_KEY
environment variable.
Build Your Application
Now that your project is configured, it’s time to start building.
In this quickstart guide, you will explore the fundamental concepts behind Vercel AI SDK by building a Node.js chatbot that will run in your terminal.
First, create an index.ts
file in the root of your project and add the following code:
import { openai } from '@ai-sdk/openai';import { CoreMessage, streamText } from 'ai';import dotenv from 'dotenv';import * as readline from 'node:readline/promises';
dotenv.config();
const terminal = readline.createInterface({ input: process.stdin, output: process.stdout,});
const messages: CoreMessage[] = [];
async function main() { while (true) { const userInput = await terminal.question('You: ');
messages.push({ role: 'user', content: userInput });
const result = await streamText({ model: openai('gpt-4-turbo'), system: `You are a helpful, respectful and honest assistant.`, messages, });
let fullResponse = ''; process.stdout.write('\nAssistant: '); for await (const delta of result.textStream) { fullResponse += delta; process.stdout.write(delta); } process.stdout.write('\n\n');
messages.push({ role: 'assistant', content: fullResponse }); }}
main().catch(console.error);
Let's take a closer look at the code:
- Import Libraries and Configure Environment:
- Import necessary libraries for handling the OpenAI SDK, messaging, and environment variables.
- You call
dotenv.config();
to load environment variables, ensuring secure access to your OpenAI API key.
- Setup Command Line Interface:
- Set up a readline interface for taking input from the terminal, enabling interactive sessions directly from the command line.
- Initialize Your Message Storage:
- Define an array called
messages
to store the history of your conversation. This history is crucial for the AI to maintain context in ongoing dialogues.
- Define an array called
- Define the Main Function:
- In your
main
function, which is asynchronous, manage the chat process:- Prompt for and capture user input, storing it in
userInput
. - Add user input to the
messages
array as a user message. - Call the
streamText
function with the model, system instructions, and the accumulated messages to generate an AI response. - Iterate over the text stream returned by the
streamText
function (result.textStream
) and print the contents of the stream to the terminal
- Prompt for and capture user input, storing it in
- In your
- Handle Errors and Execute:
- Invoke the
main
function with error handling to catch and log any issues, allowing your program to either recover or exit gracefully when errors occur.
- Invoke the
Head back to the terminal and run the following command:
pnpm tsx src/index.ts
Type in a message and hit enter. You should see the model's response stream in as it’s being generated, just like a typewriter.
Where to Next?
You've built an AI chatbot using the Vercel AI SDK! Experiment and extend the functionality of this application further by exploring tool calling or generating structured data.
If you are looking to leverage the broader capabilities of LLMs, Vercel AI SDK Core provides a comprehensive set of lower-level tools and APIs that will help you unlock a wider range of AI functionalities beyond the chatbot paradigm.