AbhishekA
Convex Community2y ago
28 replies
Abhishek

Anthropic SDK with Convex | Claude AI

I was trying to run the anthropic SDK in action and trying to get a streaming response. When I am trying to run the action from the dashboard then the response is not at all coming up.

Here is my action code :
export const generatePostGeneration = action({
  args: {
    userInput: v.string(),
  },
  async handler(ctx, args) {
    const client = new Anthropic({
      apiKey:""       
    });
    let body = "";
    try {
      await client.messages
        .stream({
          messages: [{ role: "user", content: "Hello" }],
          model: "claude-3-sonnet-20240229",
          max_tokens: 1024,
        })
        .on("text", (text: any) => {
          body += text;
          console.log("Stream:", text);
        });
    } catch (error) {
      console.log("Error:", error);
    }
    console.log("Body:", body);
 return body;
  },
});


This is only happening for streaming for normal messages I am getting the response.

anthropic docs: https://docs.anthropic.com/claude/reference/messages-streaming
Claude
When creating a Message, you can set "stream": true to incrementally stream the response using server-sent events (SSE).Streaming with SDKsOur Python and Typescript SDKs offer multiple ways of streaming. The Python SDK allows both sync and async streams. See the documentation in each SDK for details...
Was this page helpful?