I built a *streaming* AI chat app

Fireship · Beginner ·📰 AI News & Updates ·3y ago

Key Takeaways

The video demonstrates building a streaming AI chat app using the Vercel AI SDK with APIs like OpenAI, Anthropic, HuggingFace, LangChain, and more.

Full Transcript

a few days ago Purcell jumped on the AI hype train and released their brand new AI SDK an open source tool that makes it really easy to build apps with large language models like gpt4 and others and it handles text streaming on the server allowing you to recreate the typing effect that you see in apps like chat GPT in today's video we'll build an AI chat bot from scratch and spell kit to get an idea of what this tool is all about because it is incredibly useful if you plan on integrating llms into your app this video is not sponsored but I do want to take a second to show this demo that versal made that allows you to compare multiple llms side by side just by itself it's a very useful tool for comparing responses between different models under the hood it's using xjs along with the aisdk that we're about to look at to get started let's go ahead and create a new spell kit project and by the way if you're new to spell kit I've been working on a full course for the last couple months that will be out by the end of June I'll leave a discount below if you want to get access with fireship Pro now inside the project let's first install the AI SDK then we'll need to decide which AI platform we want to integrate like anthropic cohere hugging vase or open AI I'm going to go with openai here and install the openai edge Library this is an alternative to the main openai package which is based on axios whereas The Edge package is based on the browser native fetch API that's mostly important if you plan on deploying this to Edge functions like those on Purcell but that's completely optional and it's important to point out that you don't need to use resell to take advantage of this Library the next step is to go to the openai dashboard and get an API key let's go ahead and copy this API key into a DOT EnV file in the root of the spell kit project now in order to interact with this API we need a server endpoint install Kit we can easily create an API endpoint by right clicking on the routes directory then use the extension to create a new server.ts file that'll give us an HTTP endpoint in the chat directory that's initially configured to handle get requests in our case though we want to change that to a post request which is accomplished by renaming the function the code in this function will only run on the server which means we can safely import openai and the environment variable with the API key a cool thing about spell kit is that instead of using process.env we import environment variables from the dollar sign EnV namespace which provides intellisense to prevent you from using a variable that doesn't exist now we can pass that API key to the configuration object to initialize the openai SDK now when sending a post request we'll include a body of messages to give the AI model the context of the chat from there we can use the create chat completion method to specify an openai model like GPT 3.5 turbo and then we'll make sure to set the stream option to True when streaming the API will send back small chunks of the response instead of waiting for the entire response to process and this is the point where versel's AI SDK becomes useful it has a helper function to First convert the response to a stream and then a class called streaming text response to then stream it from your own server endpoint which might also be running on the edge that's pretty cool and it also makes it possible to run callbacks as the stream is being processed like for example on start you might want to save the proof to the database or on completion you might want to save the rest response from the AI to a database and that's all it takes to build a streaming API on the back end now that that's done let's head over to the page.svelt file to code up the front end on the front end we'll go ahead and import the use chat helper from AI spell use chat as a function that can configure settings and returns a bunch of helper functions and stores to help you manage that state of the chat by default it's looking for a post endpoint with a URL of API chat on the same origin however my endpoint is on the chat URL so I'll go ahead and update that in the settings one thing it returns is a messages store that we can subscribe to by putting a dollar sign in front of it its value is an array so we'll Loop over it with each which will display and update every message in the chat in real time next we'll create an HTML form so the user can submit a new message to the chat the library provides a function to automatically handle the submit event and also an input store to bind the proper form input to the chat prompt congratulations you just built a streaming artificial intelligence app that runs on the edge now run npm run Dev to run it locally when you submit the form it will automatically make a post request to your API endpoint to communicate securely with the AI on the back end then stream the response and update the UI token by token on the front end thanks for watching and I will see you in the next one

Original Description

Learn how to use the new Vercel AI SDK to quickly build streaming AI chat apps with APIs like OpenAI, Anthropic, HuggingFace, LangChain, and more. Upgrade to get the full SvelteKit Course at https://fireship.io/pro Use promo code FKIT to save 35% Vercel AI Announcement https://vercel.com/blog/introducing-the-vercel-ai-sdk
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Beyond Fireship · Beyond Fireship · 29 of 42

1 Social Media Style Number Formatting in JS
Social Media Style Number Formatting in JS
Beyond Fireship
2 I used 3 different File System APIs in Node.js
I used 3 different File System APIs in Node.js
Beyond Fireship
3 Time is Relative, even in JavaScript
Time is Relative, even in JavaScript
Beyond Fireship
4 How to NOT Screw Up Firebase Environment Variables
How to NOT Screw Up Firebase Environment Variables
Beyond Fireship
5 How to make your JavaScript Bundle Smaller
How to make your JavaScript Bundle Smaller
Beyond Fireship
6 Subtle, yet Beautiful Scroll Animations
Subtle, yet Beautiful Scroll Animations
Beyond Fireship
7 Beyond Surreal? A closer look at NewSQL Relational Data
Beyond Surreal? A closer look at NewSQL Relational Data
Beyond Fireship
8 How to Build a Discord Bot
How to Build a Discord Bot
Beyond Fireship
9 How to make Eyeballs that Follow You Around
How to make Eyeballs that Follow You Around
Beyond Fireship
10 Reverse Engineer Google’s NASA Dart Easter Egg with CSS
Reverse Engineer Google’s NASA Dart Easter Egg with CSS
Beyond Fireship
11 Generate Images Programmatically on the Edge
Generate Images Programmatically on the Edge
Beyond Fireship
12 WTF are all these config files for?
WTF are all these config files for?
Beyond Fireship
13 NEW Firebase Features Just Dropped
NEW Firebase Features Just Dropped
Beyond Fireship
14 Next.js 13 - The Basics
Next.js 13 - The Basics
Beyond Fireship
15 Make Crazy Art with the NEW OpenAI Dall-e API
Make Crazy Art with the NEW OpenAI Dall-e API
Beyond Fireship
16 How to Setup Node.js with TypeScript in 2023
How to Setup Node.js with TypeScript in 2023
Beyond Fireship
17 Dramatically improve website speed with Partytown
Dramatically improve website speed with Partytown
Beyond Fireship
18 The easiest realtime app I’ve ever built
The easiest realtime app I’ve ever built
Beyond Fireship
19 10 Rendering Patterns for Web Apps
10 Rendering Patterns for Web Apps
Beyond Fireship
20 You don't need Node to use NPM packages
You don't need Node to use NPM packages
Beyond Fireship
21 Sorting Algorithms Explained Visually
Sorting Algorithms Explained Visually
Beyond Fireship
22 ChatGPT Official API First Look
ChatGPT Official API First Look
Beyond Fireship
23 I built an image search engine
I built an image search engine
Beyond Fireship
24 Industrial-scale Web Scraping with AI & Proxy Networks
Industrial-scale Web Scraping with AI & Proxy Networks
Beyond Fireship
25 Next.js Server Actions...  5 awesome things you can do
Next.js Server Actions... 5 awesome things you can do
Beyond Fireship
26 The ultimate guide to web performance
The ultimate guide to web performance
Beyond Fireship
27 I built a fullstack PaLM AI app in just 2 minutes
I built a fullstack PaLM AI app in just 2 minutes
Beyond Fireship
28 I tried 8 different Postgres ORMs
I tried 8 different Postgres ORMs
Fireship
I built a *streaming* AI chat app
I built a *streaming* AI chat app
Fireship
30 React VS Svelte...10 Examples
React VS Svelte...10 Examples
Fireship
31 How GitHub Actions 10x my productivity
How GitHub Actions 10x my productivity
Fireship
32 PROOF JavaScript is a Multi-Threaded language
PROOF JavaScript is a Multi-Threaded language
Fireship
33 Mind-blowing page animations are easy now... View Transitions API first look
Mind-blowing page animations are easy now... View Transitions API first look
Fireship
34 I built an Apple Vision Pro app... visionOS tutorial
I built an Apple Vision Pro app... visionOS tutorial
Fireship
35 This UI component library is mind-blowing
This UI component library is mind-blowing
Fireship
36 How I deploy serverless containers for free
How I deploy serverless containers for free
Fireship
37 GitHub Copilot now controls your command line...
GitHub Copilot now controls your command line...
Fireship
38 Build better payment forms using new “embedded” Stripe Checkout
Build better payment forms using new “embedded” Stripe Checkout
Fireship
39 Does Deno 2 really uncomplicate JavaScript?
Does Deno 2 really uncomplicate JavaScript?
Fireship
40 JavaScript performance is weird... Write scientifically faster code with benchmarking
JavaScript performance is weird... Write scientifically faster code with benchmarking
Fireship
41 Is Next.js 15 any good? "use cache" API first look
Is Next.js 15 any good? "use cache" API first look
Beyond Fireship
42 I built a DeepSeek R1 powered VS Code extension…
I built a DeepSeek R1 powered VS Code extension…
Fireship

Learn how to build a streaming AI chat app using the Vercel AI SDK and integrate it with popular AI APIs. This video provides a step-by-step guide on how to get started with building AI-powered chat apps.

Key Takeaways
  1. Set up Vercel AI SDK
  2. Choose AI APIs to integrate
  3. Build the chat app
  4. Test the app
  5. Deploy the app
💡 The Vercel AI SDK provides a convenient way to build streaming AI chat apps with popular AI APIs.

Related AI Lessons

When AI Asks for More Electricity Than a Country Can Imagine
AI's increasing power consumption is causing concerns, learn why it matters for data centers and energy supply
Medium · AI
You Are Not Behind. The World Is.
You're not behind, the world is still adapting to AI, and it's okay to take your time to learn and grow
Medium · AI
Career choice with the advent of AI - pure Computer Science or learn software with a background of core engineering area
Learn how to choose between a Computer Science and Engineering career path or combining programming with a core engineering background in the age of AI
Dev.to AI
The AI Hype Cycle: Calm Before the Next Breakthrough?
Understand the AI hype cycle to anticipate the next breakthrough and make informed decisions
Medium · Programming
Up next
Motorist saved by human chain | 9 News Australia
9 News Australia
Watch →