Next.js Discord

Discord Forum

Vercel AI SDK - onCompletion callback not running

Unanswered
Song Sparrow posted this in #help-forum
Open in Discord
Song SparrowOP
Been wracking my head for a couple days with this. Using NextJS 13 with the app directory. console.log('onCompletion', completion); never runs, including when the message is over.

import { LangChainStream, StreamingTextResponse } from 'ai';
import { ChatOpenAI } from 'langchain/chat_models/openai';
import { AIMessage, ChatMessage, HumanMessage } from 'langchain/schema';

import { auth } from '@/auth';

export const runtime = 'edge';

export async function POST(req: Request) {
  const json = await req.json();
  const { messages }: {messages: ChatMessage[]} = json;

  const { stream, handlers } = LangChainStream({
    async onStart() {
      console.log('onStart');
    },
    async onCompletion(completion) {
      console.log('onCompletion', completion);
      return;
    },
  });

  const llm = new ChatOpenAI({
    streaming: true,
  });

  llm
    .call(
      messages.map((m: ChatMessage) => {
        console.log('m', m)
        return m.role == 'user'
          ? new HumanMessage(m.content)
          : new AIMessage(m.content)
      }),
      {},
      [handlers]
    )
    .catch(console.error);

  return new StreamingTextResponse(stream);
}

1 Reply

you might want to use Vercel version OpenAI to invoke OpenAI APIs, which works on Edge Runtime.
here is my poc project that uses Vercel AI SDK.
https://github.com/tfutada/zenn-vercel-ai-sdk/tree/main/app