Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message Interruption When Using useChat with @ai-sdk/anthropic and Asynchronous Tools, While Normal Chat Works Fine #3709

Open
Abdurihim opened this issue Nov 15, 2024 · 0 comments
Labels
ai/provider ai/ui bug Something isn't working

Comments

@Abdurihim
Copy link

Abdurihim commented Nov 15, 2024

Description

I'm encountering an issue when using the useChat function along with @ai-sdk/anthropic in my project. The normal chat functionality seems to work without any problems. However, when attempting to use asynchronous tools within the chat, the message flow gets interrupted.

Code example

`

const result = await streamText({
  system: systemPrompt,
  model: model.model,
  temperature: 0,
  messages,
  maxTokens: model.maxTokens,
  tools,
  toolChoice: "auto",
  experimental_toolCallStreaming: true,
  maxRetries: 3,
  abortSignal: req.signal,

  onFinish(event) {
    console.log("对话结束原因:", event.finishReason);
    console.log("对话使用信息:", event.usage);
  },
  onStepFinish(event) {
    console.log("对话结束原因:", event.finishReason);
  },
});







  const {
messages,
input,
handleInputChange,
handleSubmit,
isLoading,
stop,
addToolResult,

} = useChat({
api: "/api/chat",
body: {
customData: {
developmentEnv: envSelection.developmentEnv,
selectedStack: envSelection.selectedStack,
languageModel: envSelection.languageModel,
},
},
maxSteps: 100,
async onToolCall({ toolCall }) {
console.log("onToolCall", toolCall);

  // 根据环境选择对应的工具处理器
  const handlers =
    envSelection.developmentEnv === DevelopmentEnv.ReactLive
      ? toolHandlers
      : envSelection.developmentEnv === DevelopmentEnv.Local
      ? previewToolHandlers
      : null;

  if (handlers) {
    const handler = handlers[
      toolCall.toolName as keyof typeof handlers
    ] as (args: any) => Promise<any>;
    if (handler) {
      return await handler(toolCall.args);
    }
  }
},
onError: (error) => {
  console.log("Chat error:", error);
  toast({
    variant: "destructive",
    title: "错误提示",
    description: error.message || "发生未知错误,请稍后重试",
  });
},
// 添加onFinish回调
onFinish: async (message) => {},

});
`

AI provider

@ai-sdk/anthropi ^0.0.54

Additional context

Chat error: Error
at processDataProtocolResponse (index.mjs:736:1)
at async callChatApi (index.mjs:1019:1)
at async getStreamedResponse (index.mjs:226:1)
at async processChatStream (index.mjs:1231:1)
at async eval (index.mjs:347:1)
at async eval (index.mjs:398:1)
at async eval (index.mjs:398:1)
at async eval (index.mjs:398:1)
at async eval (index.mjs:398:1)
at async eval (index.mjs:398:1)
at async eval (index.mjs:398:1)

@Abdurihim Abdurihim added the bug Something isn't working label Nov 15, 2024
@Abdurihim Abdurihim changed the title @ai-sdk/anthropic "^0.0.54" - Chat Error Occurring While @ai-sdk/openai Works Fine Message Interruption When Using useChat with @ai-sdk/anthropic and Asynchronous Tools, While Normal Chat Works Fine Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/provider ai/ui bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants