Next.js Discord

Discord Forum

vercel ai sdk very slow

Unanswered
Ghost ant posted this in #help-forum
Open in Discord
Ghost antOP
The streaming of the chatbot is extremely slow for some reason. The web search is quick, but the streamText is way too slow.

export async function POST(req: Request) {
  const { messages }: { messages: CoreMessage[] } = await req.json();
  const topic = messages[messages.length - 1].content;
  const data = new StreamData();

  try {
    // Perform search queries and get results concurrently
    const [searchQueries, searchResults] = await Promise.all([
      generateSearchQueries(topic as string, 3),
      getSearchResults(await generateSearchQueries(topic as string, 3))
    ]);

    data.append({ results: searchResults });

    const inputData = searchResults.map((item: any) => 
      `--START ITEM--\nURL: ${item.url}\nCONTENT: ${item.text.slice(0, 750)}\n--END ITEM--\n`
    ).join('');

    console.log(inputData)
    const result = await streamText({
      model: openai('gpt-4o-mini'),
      system: 'You are a helpful research assistant. Write a report according to the user\'s instructions. In the end write some potential questions that the user could search about',
      prompt: `Input Data:\n${inputData}Write a two paragraph research report about ${topic} based on the provided information. Include as many sources as possible. Provide citations in the text using footnote notation ([#]). First provide the report, followed by a single "References" section that lists all the URLs used, in the format [#] <url>.`
    });

    return result.toDataStreamResponse({ data });
  } catch (error) {
    console.error('Error in POST function:', error);
    return new Response('An error occurred', { status: 500 });
  } finally {
    data.close();
  }
}

1 Reply

Ghost antOP
neither it flows smoothly