diff --git a/README.md b/README.md index 7270f6e6..cbcf6edc 100644 --- a/README.md +++ b/README.md @@ -334,14 +334,14 @@ async def main(): # Stream the content async for chunk in response.output: - async for chunk in response.output: - if isinstance(chunk, AgentStreamResponse): - print(chunk.text, end='', flush=True) - else: - print(f"Received unexpected chunk type: {type(chunk)}", file=sys.stderr) + if isinstance(chunk, AgentStreamResponse): + print(chunk.text, end='', flush=True) + else: + print(f"Received unexpected chunk type: {type(chunk)}", file=sys.stderr) + print() else: - # Handle non-streaming response (AgentProcessingResult) + # Handle non-streaming response (ConversationMessage) print("\n** RESPONSE ** \n") print(f"> Agent ID: {response.metadata.agent_id}") print(f"> Agent Name: {response.metadata.agent_name}") diff --git a/docs/src/content/docs/cookbook/examples/python-local-demo.md b/docs/src/content/docs/cookbook/examples/python-local-demo.md index aceb431a..4768fa10 100644 --- a/docs/src/content/docs/cookbook/examples/python-local-demo.md +++ b/docs/src/content/docs/cookbook/examples/python-local-demo.md @@ -34,7 +34,8 @@ from agent_squad.orchestrator import AgentSquad, AgentSquadConfig from agent_squad.agents import (BedrockLLMAgent, BedrockLLMAgentOptions, AgentResponse, - AgentCallbacks) + AgentCallbacks, + AgentStreamResponse) from agent_squad.types import ConversationMessage, ParticipantRole orchestrator = AgentSquad(options=AgentSquadConfig( @@ -71,11 +72,20 @@ orchestrator.add_agent(tech_agent) 4. Implement the main logic: ```python async def handle_request(_orchestrator: AgentSquad, _user_input: str, _user_id: str, _session_id: str): - response: AgentResponse = await _orchestrator.route_request(_user_input, _user_id, _session_id) + response: AgentResponse = await _orchestrator.route_request( + _user_input, + _user_id, + _session_id, + stream_response=True, + ) print("\nMetadata:") print(f"Selected Agent: {response.metadata.agent_name}") if response.streaming: - print('Response:', response.output.content[0]['text']) + print('Response:', end=' ') + async for chunk in response.output: + if isinstance(chunk, AgentStreamResponse): + print(chunk.text, end='', flush=True) + print() else: print('Response:', response.output.content[0]['text']) diff --git a/docs/src/content/docs/general/quickstart.mdx b/docs/src/content/docs/general/quickstart.mdx index 74362b54..ab2faaad 100644 --- a/docs/src/content/docs/general/quickstart.mdx +++ b/docs/src/content/docs/general/quickstart.mdx @@ -137,7 +137,8 @@ Ensure you have [requested access](https://docs.aws.amazon.com/bedrock/latest/us from agent_squad.agents import (BedrockLLMAgent, BedrockLLMAgentOptions, AgentResponse, - AgentCallbacks) + AgentCallbacks, + AgentStreamResponse) from agent_squad.types import ConversationMessage, ParticipantRole orchestrator = AgentSquad(options=AgentSquadConfig( @@ -248,12 +249,21 @@ Ensure you have [requested access](https://docs.aws.amazon.com/bedrock/latest/us ```python async def handle_request(_orchestrator: AgentSquad, _user_input: str, _user_id: str, _session_id: str): - response: AgentResponse = await _orchestrator.route_request(_user_input, _user_id, _session_id) + response: AgentResponse = await _orchestrator.route_request( + _user_input, + _user_id, + _session_id, + stream_response=True, + ) # Print metadata print("\nMetadata:") print(f"Selected Agent: {response.metadata.agent_name}") if response.streaming: - print('Response:', response.output.content[0]['text']) + print('Response:', end=' ') + async for chunk in response.output: + if isinstance(chunk, AgentStreamResponse): + print(chunk.text, end='', flush=True) + print() else: print('Response:', response.output.content[0]['text']) diff --git a/python/README.md b/python/README.md index 9346de3d..24583ecf 100644 --- a/python/README.md +++ b/python/README.md @@ -159,14 +159,14 @@ async def main(): # Stream the content async for chunk in response.output: - async for chunk in response.output: - if isinstance(chunk, AgentStreamResponse): - print(chunk.text, end='', flush=True) - else: - print(f"Received unexpected chunk type: {type(chunk)}", file=sys.stderr) + if isinstance(chunk, AgentStreamResponse): + print(chunk.text, end='', flush=True) + else: + print(f"Received unexpected chunk type: {type(chunk)}", file=sys.stderr) + print() else: - # Handle non-streaming response (AgentProcessingResult) + # Handle non-streaming response (ConversationMessage) print("\n** RESPONSE ** \n") print(f"> Agent ID: {response.metadata.agent_id}") print(f"> Agent Name: {response.metadata.agent_name}")