33
44import asyncio
55import os
6+
67from dotenv import load_dotenv
78
89from strands_nova import NovaModel
1112async def basic_streaming ():
1213 """Demonstrate basic streaming."""
1314 print ("=== Basic Streaming Example ===\n " )
14-
15- model = NovaModel (
16- model = "nova-premier-v1" ,
17- temperature = 0.7 ,
18- max_tokens = 500
19- )
20-
15+
16+ model = NovaModel (model = "nova-premier-v1" , temperature = 0.7 , max_tokens = 500 )
17+
2118 print ("Question: Write a short story about a robot learning to paint.\n " )
2219 print ("Response: " , end = "" , flush = True )
23-
20+
2421 async for event in model .stream ("Write a short story about a robot learning to paint." ):
2522 if "contentBlockDelta" in event :
2623 delta = event ["contentBlockDelta" ]["delta" ]
2724 if "text" in delta :
2825 print (delta ["text" ], end = "" , flush = True )
29-
26+
3027 print ("\n " )
3128
3229
3330async def streaming_with_system_prompt ():
3431 """Demonstrate streaming with system prompt."""
3532 print ("\n === Streaming with System Prompt ===\n " )
36-
37- model = NovaModel (
38- model = "nova-premier-v1" ,
39- temperature = 0.3
40- )
41-
33+
34+ model = NovaModel (model = "nova-premier-v1" , temperature = 0.3 )
35+
4236 system_prompt = "You are a helpful coding assistant. Provide concise, well-commented code examples."
43-
37+
4438 print ("Question: Write a Python function to merge two sorted lists.\n " )
4539 print ("Response: " , end = "" , flush = True )
46-
47- async for event in model .stream (
48- "Write a Python function to merge two sorted lists." ,
49- system_prompt = system_prompt
50- ):
40+
41+ async for event in model .stream ("Write a Python function to merge two sorted lists." , system_prompt = system_prompt ):
5142 if "contentBlockDelta" in event :
5243 delta = event ["contentBlockDelta" ]["delta" ]
5344 if "text" in delta :
5445 print (delta ["text" ], end = "" , flush = True )
55-
46+
5647 print ("\n " )
5748
5849
@@ -70,15 +61,13 @@ async def reasoning_model_example():
7061 model = NovaModel (
7162 model = "nova-premier-v1" , # Placeholder until reasoning model is available
7263 # reasoning_effort="medium", # Currently not supported, will raise error
73- temperature = 0.5
64+ temperature = 0.5 ,
7465 )
7566
7667 print ("Question: What is the sum of all prime numbers between 1 and 100?\n " )
7768 print ("Response: " , end = "" , flush = True )
7869
79- async for event in model .stream (
80- "What is the sum of all prime numbers between 1 and 100?"
81- ):
70+ async for event in model .stream ("What is the sum of all prime numbers between 1 and 100?" ):
8271 if "contentBlockDelta" in event :
8372 delta = event ["contentBlockDelta" ]["delta" ]
8473 if "text" in delta :
@@ -90,50 +79,46 @@ async def reasoning_model_example():
9079async def web_search_example ():
9180 """Demonstrate web search capabilities."""
9281 print ("\n === Web Search Example ===\n " )
93-
94- model = NovaModel (
95- model = "nova-premier-v1" ,
96- web_search_options = {"search_context_size" : "low" }
97- )
98-
82+
83+ model = NovaModel (model = "nova-premier-v1" , web_search_options = {"search_context_size" : "low" })
84+
9985 print ("Question: What is the current Amazon stock price?\n " )
10086 print ("Response: " , end = "" , flush = True )
101-
87+
10288 async for event in model .stream (
103- "What is the current Amazon stock price?" ,
104- system_prompt = "You are a helpful financial assistant."
89+ "What is the current Amazon stock price?" , system_prompt = "You are a helpful financial assistant."
10590 ):
10691 if "contentBlockDelta" in event :
10792 delta = event ["contentBlockDelta" ]["delta" ]
10893 if "text" in delta :
10994 print (delta ["text" ], end = "" , flush = True )
110-
95+
11196 print ("\n " )
11297
11398
11499async def main ():
115100 """Run all streaming examples."""
116101 # Load environment variables
117102 load_dotenv ()
118-
103+
119104 # Check for API key
120105 if not os .getenv ("NOVA_API_KEY" ):
121106 print ("Error: NOVA_API_KEY not found in environment variables." )
122107 print ("Please set: export NOVA_API_KEY='your-api-key'" )
123108 print ("Get your API key from: https://nova.amazon.com/apis" )
124109 return
125-
110+
126111 # Run examples
127112 await basic_streaming ()
128113 await streaming_with_system_prompt ()
129-
114+
130115 # Optional: Run reasoning and web search examples
131116 # Note: These require specific model access
132117 try :
133118 await reasoning_model_example ()
134119 except Exception as e :
135120 print (f"\n Reasoning model example skipped: { e } \n " )
136-
121+
137122 try :
138123 await web_search_example ()
139124 except Exception as e :
0 commit comments