ai icon indicating copy to clipboard operation
ai copied to clipboard

AnthropicStream - example copy/paste errors

Open bcholewa opened this issue 4 months ago • 8 comments

Description

Check the example from: https://sdk.vercel.ai/docs/api-reference/providers/anthropic-stream

Code example

import Anthropic from '@anthropic-ai/sdk'; import { AnthropicStream, StreamingTextResponse } from 'ai'; import { experimental_buildAnthropicPrompt } from 'ai/prompts';

// Create an Anthropic API client (that's edge friendly??) const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY || '', });

// IMPORTANT! Set the runtime to edge export const runtime = 'edge';

export async function POST(req: Request) { // Extract the prompt from the body of the request const { messages } = await req.json();

// Ask Claude for a streaming chat completion given the prompt const response = await anthropic.completions.create({ prompt: experimental_buildAnthropicPrompt(messages), model: 'claude-2', stream: true, max_tokens_to_sample: 300, }); // Convert the response into a friendly text-stream const stream = AnthropicStream(response); // Respond with the stream return new StreamingTextResponse(stream); }

Additional context

Results with Argument of type 'Stream<Completion>' is not assignable to parameter of type 'Response | AsyncIterable<CompletionChunk>'. Type 'Stream<Completion>' is not assignable to type 'AsyncIterable<CompletionChunk>'. The types returned by 'Symbol.asyncIterator.next(...)' are incompatible between these types. Type 'Promise<IteratorResult<Completion, any>>' is not assignable to type 'Promise<IteratorResult<CompletionChunk, any>>'. Type 'IteratorResult<Completion, any>' is not assignable to type 'IteratorResult<CompletionChunk, any>'. Type 'IteratorYieldResult<Completion>' is not assignable to type 'IteratorResult<CompletionChunk, any>'. Type 'IteratorYieldResult<Completion>' is not assignable to type 'IteratorYieldResult<CompletionChunk>'. Type 'Completion' is not assignable to type 'CompletionChunk'.ts(2345)

bcholewa avatar Mar 07 '24 15:03 bcholewa

What is the version of your anthropic SDK? (the docs are for v0.15.0 and newer)

lgrammel avatar Mar 08 '24 18:03 lgrammel

"@anthropic-ai/sdk": "^0.17.1"

bcholewa avatar Mar 08 '24 20:03 bcholewa

also experiencing this issue with "^0.17.1"

GoldinGuy avatar Mar 13 '24 05:03 GoldinGuy

v0.15.0 is what's working for me

Gitnock avatar Mar 13 '24 06:03 Gitnock

I'm still experiencing this issue using v0.15.0. what is the version of ai you're using?

GoldinGuy avatar Mar 14 '24 21:03 GoldinGuy

I'm still experiencing this issue using v0.15.0. what is the version of ai you're using? am using [email protected]

Gitnock avatar Mar 14 '24 21:03 Gitnock

can confirm [email protected] and v0.15.0 for the anthropic SDK fixes this issue. hopefully this is addressed in the next version of ai.

GoldinGuy avatar Mar 14 '24 22:03 GoldinGuy

I just tried to reproduce with v0.18.0 of the anthropic SDK and the build passed for me. Is this still an issue with that version?

lgrammel avatar Mar 15 '24 13:03 lgrammel

"@anthropic-ai/sdk": "^0.18.0" & "ai": "^3.0.12" - it all works now.

bcholewa avatar Mar 19 '24 08:03 bcholewa