Next.js Discord

Discord Forum

Using the langchain calculator with Vercel AI SDK.

Unanswered
Peterbald posted this in #help-forum
Open in Discord
PeterbaldOP
Hello,

I'm unable to figure out how to get the values from the langchain calculator. I simply want to track API usage but the documentation is pretty limited.

Here is my API route's code. The onCompletion callback only returns a string, same thing for the onToken callback.

I've created a new Calculator() and I'm passing it to the tools field but from there I'm stuck. Should I pass something to the constructor or something?

Any help would be greatly appreciated.

import { kv } from "@vercel/kv";
import { LangChainStream, StreamingTextResponse, type Message } from "ai";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { AIMessage, HumanMessage } from "langchain/schema";
import { Calculator } from "langchain/tools/calculator";
import { getCurrentSession } from "@/lib/session";
import { nanoid } from "@/lib/utils";

export const runtime = "edge";

export async function POST(req: Request) {
  const userId = (await getCurrentSession())?.user.id;

  if (!userId) {
    return new Response("Unauthorized", {
      status: 401,
    });
  }

  const { id, messages, previewToken } = await req.json();

  const { stream, handlers } = LangChainStream({
    async onCompletion(completion) {
      const title = messages[0].content.substring(0, 100);
      const chatId = id ?? nanoid();
      const createdAt = Date.now();
      const path = `/chat/${chatId}`;
      const payload = {
        id: chatId,
        title,
        userId,
        createdAt,
        path,
        messages: [
          ...messages,
          {
            content: completion,
            role: "assistant",
          },
        ],
      };
      await kv.hmset(`chat:${id}`, payload);
      await kv.zadd(`user:chat:${userId}`, {
        score: createdAt,
        member: `chat:${id}`,
      });
    },
    onToken(token) {
      console.log({ token });
    },
  });

  const llm = new ChatOpenAI({
    streaming: true,
    modelName: "gpt-3.5-turbo",
    temperature: 0,
    configuration: {
      apiKey: previewToken || process.env.OPENAI_API_KEY,
    },
  });

  llm
    .call(
      (messages as Message[]).map((m) =>
        m.role == "user"
          ? new HumanMessage(m.content)
          : new AIMessage(m.content)
      ),
      {
        tools: [new Calculator()],
      },
      [handlers]
    )
    .catch(console.error);

  return new StreamingTextResponse(stream);
}

6 Replies

European sprat
I don't know for sure but I think tools are used with langchain agents
you need Vercel version OpenAI SDK to get it run in Edge Runtime.
PeterbaldOP
@tafutada777 the chat completions are running fine at the edge, using the vercel ai SDK Langchain adapter. The question is about getting the needed data out of the calculator tool.
oh sorry. just keep in mind that Edge is not Node.js but V8 Isolates which is subset of Node.js so some libraries won’t work in Edge
and cpu cap is 50ms so cpu intensive tasks won’t work
only i/o bound tasks are acceptable