File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ import anthropic
2+
3+ client = anthropic .Anthropic ()
4+
5+ response = client .messages .create (
6+ model = "claude-3-7-sonnet-20250219" ,
7+ max_tokens = 3200 ,
8+ thinking = {"type" : "enabled" , "budget_tokens" : 1600 },
9+ messages = [{"role" : "user" , "content" : "Create a haiku about Anthropic." }],
10+ )
11+
12+ for block in response .content :
13+ if block .type == "thinking" :
14+ print (f"Thinking: { block .thinking } " )
15+ elif block .type == "text" :
16+ print (f"Text: { block .text } " )
Original file line number Diff line number Diff line change 1+ import anthropic
2+
3+ client = anthropic .Anthropic ()
4+
5+ with client .messages .stream (
6+ model = "claude-3-7-sonnet-20250219" ,
7+ max_tokens = 3200 ,
8+ thinking = {"type" : "enabled" , "budget_tokens" : 1600 },
9+ messages = [{"role" : "user" , "content" : "Create a haiku about Anthropic." }],
10+ ) as stream :
11+ thinking = "not-started"
12+
13+ for event in stream :
14+ if event .type == "thinking" :
15+ if thinking == "not-started" :
16+ print ("Thinking:\n ---------" )
17+ thinking = "started"
18+
19+ print (event .thinking , end = "" , flush = True )
20+ elif event .type == "text" :
21+ if thinking != "finished" :
22+ print ("\n \n Text:\n -----" )
23+ thinking = "finished"
24+
25+ print (event .text , end = "" , flush = True )
You can’t perform that action at this time.
0 commit comments