Next.js Discord

Discord Forum

Trying to use tools with Vercel AI SDK

Unanswered
Yellow croaker posted this in #help-forum
Open in Discord
Yellow croakerOP
For some reason the frontend doesn't render the content returned from the tool. Any ideas why? Thanks in advance!

import { openai } from "@ai-sdk/openai";
import { StreamingTextResponse, streamText, tool } from "ai";
import { fetchHabits } from "@/utils/habitsApi";
import { z } from "zod";

export async function POST(request) {
  const { messages } = await request.json();

  const result = await streamText({
    model: openai("gpt-4o"),
    tools: {
      weather: tool({
        description: "Get the weather in a location",
        parameters: z.object({
          location: z.string().describe("The location to get the weather for"),
        }),
        execute: async ({ location }) => {
          const temperature = 72 + Math.floor(Math.random() * 21) - 10;
          return `The current temperature in ${location} is ${temperature}°F.`;
        },
      }),
    },
    messages,
    role: "assistant",
  });

  console.log(messages);

  return new StreamingTextResponse(result.toAIStream());
}

0 Replies