Is there a way around to call actions from non functional components?
I am trying to build a chatbot with Vercel AI Sdk for streaming the response from OpenAI.
I was wondering if there is a way to call actions in my route.ts? I do not want to call the action unless my POST function is called, so
(In my action, I am doing vector search)
And also I was thinking to move this from
route.ts
to httpAction
and then use that to do the vector search, but will it be efficient? Another reason to refrain from using httpAction
is that it will be exposed and it just doesn't make any sense to use it like that.6 Replies
httpAction
works here but fetchAction described below is better. I'd just add a { secret: process.env.MY_SECRET_KEY, ...}
to the args that you check in the function implementation so that this action isn't exposed to anyone who wants to use your OpenAI key
Another option is using a Convex HTTP endpoint for this POST (edit: you'll still need a process.env.SECRET or similar)You can call an action from a Route handler, check out the new simplified nextjs entrypoint:
https://docs.convex.dev/client/react/nextjs/server-rendering#server-actions-and-route-handlers
You can use
fetchAction
for thisNext.js Server Rendering | Convex Developer Hub
Next.js automatically renders both Client and Server Components on the server
As an aside, http actions aren't any more open than regular actions. If you're worried about bad actors using your API, you should do the
process.env.MY_SECRET_KEY
thing in regular actions or http.Thanks guys,
fetchAction
did the work for me.
And also, I am storing my key as a secret key in my regular actions.@Rayy you might also be interested that we now support http response streaming: https://discord.com/channels/1019350475847499849/1019372556693815418/1240087869914218586
Example code streaming back AI response and writing it to the DB periodically: https://github.com/sshader/streaming-chat-gpt/blob/sshader-streaming/convex/http.ts
GitHub
streaming-chat-gpt/convex/http.ts at sshader-streaming · sshader/st...
An example of streaming ChatGPT via the OpenAI v4.0 node SDK. - sshader/streaming-chat-gpt
That’s so amazing. 🔥