tonyljx / TextWordExplain

汉语新解
https://texthuman.ai
243 stars 56 forks source link

Claude API报错 messages: first message must use the "user" role #2

Open mazhiyuam opened 2 months ago

mazhiyuam commented 2 months ago

相关代码

const response = await anthropic.messages.create({
      model: "claude-3-5-sonnet-20240620",
      max_tokens: 1024,
      messages: [
        { role: "assistant", content: systemPrompt },
        {
          role: "user",
          content: `(汉语新解 ${prompt}) 输出要求: 要输出svg内容`,
        },
      ],
    });

报错信息

Error in chat API: BadRequestError: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages: first message must use the \"user\" role"}}
{
  status: 400,
  headers: {
    'access-control-allow-headers': '*',
    'access-control-allow-methods': 'POST, GET, OPTIONS, PUT, PATCH, HEAD, DELETE',
    'access-control-allow-origin': '*',
    'access-control-max-age': '2592000',
    'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
    'content-length': '120',
    'content-type': 'application/json',
    date: 'Fri, 13 Sep 2024 02:24:02 GMT',
    via: '1.1 google',
    'x-ratelimit-limit': '200',
    'x-ratelimit-remaining': '199',
    'x-ratelimit-reset': '1726194302',
    'x-request-id': '49353562-c4d1-4e79-af40-3f49da512a60'
  },
  request_id: undefined,
  error: {
    type: 'error',
    error: {
      type: 'invalid_request_error',
      message: 'messages: first message must use the "user" role'
    }
  }
}
mazhiyuam commented 2 months ago

本地改了一下就好了, assistant不能是第一个message

messages: [
        { role: "user", content: "汉语新解 (Chinese Reinterpretation)"},
        { role: "assistant", content: systemPrompt },
        {
          role: "user",
          content: `(汉语新解 ${prompt}) 输出要求: 要输出svg内容`,
        },
      ]
tonyljx commented 2 months ago

比较奇怪,我没遇到这个问题

mazhiyuam commented 2 months ago

比较奇怪,我没遇到这个问题

搜到 aws上说 The first message must always use the user role.

https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html#model-parameters-anthropic-claude-messages-overview

xianminx commented 2 months ago

确实, 我试过第一个如果是 system, 不会报错, 但是没法生成 svg 代码