Right now, I'm using the Vercel ai SDK's useChat hook because it makes chat state management much easier. However, this requires that I have an API route (e.g., app/api/chat/route.ts).
I would like to be able to use the useChat hook client-side with a client-provided API key instead of an API route. This would make it possible to easily use the AI SDK in static sites (frontend-only).
What do you think? 😃
Vercel Alum
Thank you for sharing this detailed feedback, ! I've forwarded to the AI SDK team.
Vercel Staff
Hey! Unfortunately, this isn't currently possible but it's been raised before. Could you file an issue as that's the best place for feature requests? Thanks!
Something to note - all the AI SDK Core functions (generateText, streamText etc.) work client-side. But you would have to manage the message state yourself.
Excited to see what Vercel does with the AI SDK next! ❤️
reply to #4
Leaderboard GOAT
Note to people in this topic:
I did succeed in doing this in my fork of the AI SDK. I would make a PR, but right now I only have it to the point where you provide a model to useChat and it waits for the response to finish. I don't have streaming responses figured out. 🥲
Leaderboard GOAT(edited)
And I just got streaming working, client side! My usage is something like this:
1"use client";2import { createGoogleGenerativeAI } from "@ai-sdk/google";3import { useChat } from "@ai-sdk/react";45// Existing function, I made no changes here6const google = createGoogleGenerativeAI({7 apiKey: "(pass the key from somewhere)",8});910function ChatComponent() {11 const { messages, append, input, setInput, handleSubmit } = useChat({12 model: google("gemini-2.0-flash-001"), // Pass the model instance - this is the change I made13 });1415 return (16 <div>17 {/* Some UI */}18 </div>19 );20}2122export default ChatComponent;
And, it streams! No backend at all!
I would share a video here, but it appears I can't.
Would you guys be interested in a PR for this, or does the team have something else in mind? I'd be happy to contribute once I get everything polished up. 😃
Leaderboard GOAT
I've got a type-safe working useChat hook with streaming now. You can see my branch here:
I only have the React useChat hook done. I'm not going to migrate the Solid JS one unless I get a go-ahead from you guys; I don't want to spend a lot of work on it and then discover the team has it done 😆
That said, I would love to contribute ❤️
reply to #4
Leaderboard GOAT
Just opened a PR — I'm sure there are plenty of ways you can criticize it, let me know what I should change 👀
My PR has been open for a week now… is there any feedback? I have to keep maintaining the PR (fixing merge conflicts so it will be ready to merge), so if the team isn't planning this it would be nice to be able to just close it 😃
Leaderboard GOAT
It looks like Vercel has something else in mind for this, hopefully they add it soon! 🥲