vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.65k stars 1.42k forks source link

Unable to ask question that uses two tools with streamText and AWS Bedrock #3068

Closed andreasdurietztibber closed 2 weeks ago

andreasdurietztibber commented 2 weeks ago

Description

We ran into two unexpected things that both work well when using generateText but not with streamText.

  1. We have tools that don't really have parameters. So we would put parameters: z.object({ }),. Doing so makes the streamText simply stop after the first tool call, without any additional information.
  2. When we put dummy params like parameters: z.object({ query: z.string() }), and ask a question that would require both tools if fails with an error.

Error message

AI_InvalidToolArgumentsError: Invalid arguments for tool sun: JSON parsing failed: Text: {"query": "Where does the user live?"}{"query": "What color is the sun?"}.
Error message: JSON Parse error: Unable to parse JSON string

We can get around that by putting parameters: z.record(z.string(), z.string()), as parameter but it seems like a strange workaround when we don't even want a parameter.

Code example

import { streamText, tool } from "ai";
import { z } from "zod";
import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock";
import { defaultProvider } from "@aws-sdk/credential-provider-node";

(async () => {
  console.log("Debugging the bot");
  const bedrock = createAmazonBedrock({
    bedrockOptions: { credentials: defaultProvider(), region: "eu-central-1" },
  });
  const model = bedrock("anthropic.claude-3-5-sonnet-20240620-v1:0");

  const result = await streamText({
    model,
    tools: {
      house: tool({
        description: "Has information about where the user lives",
        parameters: z.object({ query: z.string() }),
        execute: async () => {
          return { address: "10 downing street" };
        },
      }),
      sun: tool({
        description: "Knows things about the sun",
        parameters: z.object({ query: z.string() }),
        execute: async () => {
          return "The sun is yellow and really hot";
        },
      }),
    },
    prompt: "Where do I live and what color is the sun?",
    maxToolRoundtrips: 4,
  });

  for await (const textPart of result.textStream) {
    console.log(textPart);
  }
})();

Additional context

The problem with empty params stopping the streamText seem to be present for both bedrock & anthropic provider. However the problem with params failing the JSON parsing only seem to be present with @ai-sdk/amazon-bedrock but not with @ai-sdk/anthropic.

lgrammel commented 2 weeks ago

Reproduced the JSON parse issue

lgrammel commented 2 weeks ago

https://github.com/vercel/ai/pull/3071

lgrammel commented 2 weeks ago

Reproduced the empty argument issue

lgrammel commented 2 weeks ago

@ai-sdk/amazon-bedrock@0.0.26 is being released with the parallel tool call fix

lgrammel commented 2 weeks ago

https://github.com/vercel/ai/pull/3073

lgrammel commented 2 weeks ago

ai@3.3.43 is being released with the empty arguments fix

andreasdurietztibber commented 2 weeks ago

I have to say that's pretty impressive! 😄 From bug report to fixes with new releases in two packages, within 1-2 hours. I can confirm it works for our use case now. Big thanks! 🥳