Next.js Discord

Discord Forum

Vercel AI SDK - Help

Answered
New Guinea Freshwater Crocodile posted this in #help-forum
Open in Discord
Avatar
New Guinea Freshwater CrocodileOP
I am sing the vercel AI sdk to build a react native app.

This is what I have in the FE to interact with the api endpoint;
\javascript    const { messages, error, handleInputChange, input, handleSubmit } = useChat({
        initialMessages: [
            { id: "1", role: "assistant", content: "Let the cards guide your journey." },
        ],
        fetch: expoFetch as unknown as typeof globalThis.fetch,
        api: generateAPIUrl('/api/chat'),
        onError: (err) => console.error(err, 'ERROR'),
    });


I am using firebase and I want the api endpoint to only respond to authenticated requests. Is there a way built in way for me to send authentication data to the api endpoint where auth can be validated? Because by default, anyone can use the api endpoint.

Another thing; I want the AI to refer to the user by name so I need to be able to provide that data to the LLM.

48 Replies

Avatar
New Guinea Freshwater CrocodileOP
Any help would be super appreciated
Avatar
New Guinea Freshwater CrocodileOP
I cant find anything in the documentation, super stuck 🫠
"Firebase services (like Cloud Firestore, Authentication, Realtime Database, Remote Config, and more) are available to import within individual sub-packages."
Avatar
New Guinea Freshwater CrocodileOP
I meant how can I send that data using the vercel SDK, the vercel ai SDK gives me access to the useChat hook (where I provide the api endpoint /api/chat), how can I send data through the hook? Otherwise I would have to leave my endpoint unauthenticated @gin
Really really appreciate the help
Avatar
New Guinea Freshwater CrocodileOP
I do have an api endpoint already however the problem is passing the data to the API endpoint via the useChat hook provided by the vercel AI sdk
Avatar
New Guinea Freshwater CrocodileOP
I have read everything a few times, there is no e.g of auth on there or passing custom data other than data required to run the LLM
Avatar
wdym by custom data
u have to implement the auth yourself
Avatar
New Guinea Freshwater CrocodileOP
I understand, the problem is;
useChat is a hook provided by vercel that helps you with LLM APIs
It lets you provide an API endpoint to your BE
Avatar
yes? its on the page
Avatar
New Guinea Freshwater CrocodileOP
But lets say if I want to pass in custom data (just as an e.g) the users first name to the BE, I cant find a way
using their hook
Avatar
u usually handle authentication with a cookie
u can read it from the request object
Avatar
New Guinea Freshwater CrocodileOP
Yeah but its a react native hook
react*
I cant pass cookies via hook
Avatar
its available in the request object
Avatar
New Guinea Freshwater CrocodileOP
I am trying to find the key that will let me pass the data using useChat
what is the key called, I am sorry but I cant see it
Maybe im being a really big idiot rn but I appreciate the help
I have read the documentation multiple times, and I dont see a way
"Requests use the global, standard Request object."
Avatar
New Guinea Freshwater CrocodileOP
Maybe I am not communcating this right. The issue is passing the data to the api endpoint. I am aware on how to use the request object. useChat is a FE hook, I need to pass custom data into the useChat hook
I already have that part setup
Avatar
if u want authentication u usually use your auth cookie and read it in your api route
idk what u want to pass in there cause the hook is to simplify the request
Answer
Avatar
there we go
Image
here is your answer
Avatar
New Guinea Freshwater CrocodileOP
Legend!
Thank you so much
Really appreciate the help
u can mark this topic as answered
Image
Avatar
New Guinea Freshwater CrocodileOP
Of course!
Avatar
thank you!