vercel / ai

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

3+ Continue Steps do not work on bedrock #3462

Closed IdoPesok closed 3 weeks ago

IdoPesok commented 4 weeks ago

Description

Continue steps seems to break > 2 messages, or if you pass your own assistant message as the last message.

"ai": "^3.4.32" "@ai-sdk/amazon-bedrock": "^0.0.34"

Code example

import { streamText } from "ai";
import { bedrock } from "@ai-sdk/amazon-bedrock";

const main = async () => {
  try {
    const { textStream } = await streamText({
      model: bedrock("anthropic.claude-3-5-sonnet-20240620-v1:0"),
      maxTokens: 10,
      experimental_continueSteps: true,
      maxSteps: 4,
      messages: [
        {
          role: "user",
          content: "write a poem",
        },
        {
          role: "assistant",
          content: "okay, here is a long poem. on a cold December",
        },
      ],
    });

    for await (const v of textStream) {
      console.log(v);
    }
  } catch (e) {
    console.log("got error", e);
  }
};

main();

Additional context

got error ValidationException: A conversation must alternate between user and assistant roles. Make sure the conversation alternates between user and assistant roles and try again. at de_ValidationExceptionRes

minpeter commented 4 weeks ago

This is the intention of the Claude Chat template. This can be solved by changing the last message in the input to User. (or changing the order of the messages between the assistant and the user)

minpeter commented 4 weeks ago
import { streamText } from "ai";
import { bedrock } from "@ai-sdk/amazon-bedrock";

const main = async () => {
  try {
    const { textStream } = await streamText({
      model: bedrock("anthropic.claude-3-5-sonnet-20240620-v1:0"),
      maxTokens: 10,
      experimental_continueSteps: true,
      maxSteps: 4,
      messages: [
        {
          role: "assistant",
          content: "okay, here is a long poem. on a cold December",
        },
        {
          role: "user",
          content: "write a poem",
        },
      ],
    });

    for await (const v of textStream) {
      console.log(v);
    }
  } catch (e) {
    console.log("got error", e);
  }
};

main();
lgrammel commented 3 weeks ago

Also Anthropic: trailing whitespace issue.

lgrammel commented 3 weeks ago

Same for Bedrock.