anthropics / courses

Anthropic's educational courses
Other
4.52k stars 341 forks source link

In anthropic_api_fundamentals/05.Streaming.ipynb error: unexpecter keyword argument 'event_handler' #28

Open avadon1 opened 2 weeks ago

avadon1 commented 2 weeks ago

Path to course in question: https://github.com/anthropics/courses/blob/master/anthropic_api_fundamentals/05_Streaming.ipynb In the Streaming helpers part of the 05.Streaming segment of the api_fundamentals courses when running the last piece of code I am getting the following error: image I tried downgrading to an earlier version of anthropic (0.33.0) but the problem persists.

antsant commented 1 week ago

This content should be updated to use the new Iterator API: https://github.com/anthropics/anthropic-sdk-python/pull/532

antsant commented 1 week ago

I believe this is the current way to do this:

from anthropic import AsyncAnthropic

client = AsyncAnthropic()

green = '\033[32m'
reset = '\033[0m'

async def streaming_events_demo():
    async with client.messages.stream(
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": "Generate a 5-word poem",
            }
        ],
        model="claude-3-opus-20240229",
    ) as stream:
        async for event in stream:
            if event.type == "text":
                # This runs only on text delta stream messages
                print(green + event.text + reset, flush=True)
            else:
                # This runs on any stream event
                print("on_event fired:", event.type)

await streaming_events_demo()