View a markdown version of this page

串流代理程式回應 - Amazon Bedrock AgentCore

串流代理程式回應

下列 Strands Agents 範例顯示 AgentCore 執行期代理程式如何將回應串流回用戶端。

from strands import Agent from bedrock_agentcore import BedrockAgentCoreApp app = BedrockAgentCoreApp() agent = Agent() @app.entrypoint async def agent_invocation(payload): """Handler for agent invocation""" user_message = payload.get( "prompt", "No prompt found in input, please guide customer to create a json payload with prompt key" ) stream = agent.stream_async(user_message) async for event in stream: print(event) yield (event) if __name__ == "__main__": app.run()