RayyR
Convex Community2y ago
8 replies
Rayy

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)

export async function POST(req: Request) {
  const { messages, chatId } = await req.json();
  const previousMessage: string = messages[messages.length - 1].content;

  const response = await openai.chat.completions.create({
    model: "gpt-3.5-turbo",
    stream: true,
    messages,
  });

  // Convert the response into a friendly text-stream
  const stream = OpenAIStream(response);

  // Respond with the stream
  return new StreamingTextResponse(stream);
}


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.
Was this page helpful?