From e1def8574705b57c42057b9f48b31b1b7f7fbf9a Mon Sep 17 00:00:00 2001 From: Filipi Fuchter Date: Fri, 28 Nov 2025 14:37:53 -0300 Subject: [PATCH] Sending the answer in chunks --- .../agents/pipecat/pipecat-agent.py | 29 +++++++++++++++++-- examples/aws-agentcore/server/server.py | 1 - 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/examples/aws-agentcore/agents/pipecat/pipecat-agent.py b/examples/aws-agentcore/agents/pipecat/pipecat-agent.py index e92df69b2..feee79f67 100644 --- a/examples/aws-agentcore/agents/pipecat/pipecat-agent.py +++ b/examples/aws-agentcore/agents/pipecat/pipecat-agent.py @@ -4,6 +4,8 @@ # SPDX-License-Identifier: BSD 2-Clause License # +import base64 +import json import os from bedrock_agentcore import BedrockAgentCoreApp @@ -24,10 +26,10 @@ from pipecat.services.openai.llm import OpenAILLMService from pipecat.transports.base_transport import BaseTransport, TransportParams from pipecat.transports.daily.transport import DailyParams from pipecat.transports.smallwebrtc.connection import SmallWebRTCConnection -from pipecat.transports.smallwebrtc.transport import SmallWebRTCTransport from pipecat.transports.smallwebrtc.request_handler import ( SmallWebRTCRequest, ) +from pipecat.transports.smallwebrtc.transport import SmallWebRTCTransport app = BedrockAgentCoreApp() @@ -57,8 +59,29 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): yield {"status": "initializing bot!"} # Returning the answer if isinstance(transport, SmallWebRTCTransport): - yield {"status": "Will return smallwebrtc answer."} - yield {"answer": transport._client._webrtc_connection.get_answer()} + yield {"status": "ANSWER:START"} + answer_dict = transport._client._webrtc_connection.get_answer() + answer_json_str = json.dumps(answer_dict) + answer_bytes = answer_json_str.encode("utf-8") + encoded_answer = base64.b64encode(answer_bytes).decode("ascii") + + # Break encoded_answer into multiple messages + chunk_size = 1000 # Adjust this size based on your needs + total_chunks = ( + len(encoded_answer) + chunk_size - 1 + ) // chunk_size # Calculate total chunks + + for i in range(0, len(encoded_answer), chunk_size): + chunk = encoded_answer[i : i + chunk_size] + chunk_index = i // chunk_size + yield { + "answer_chunk": chunk, + "chunk_index": chunk_index, + "total_chunks": total_chunks, + "is_last_chunk": chunk_index == total_chunks - 1, + } + + yield {"status": "ANSWER:END"} stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY")) diff --git a/examples/aws-agentcore/server/server.py b/examples/aws-agentcore/server/server.py index bf8e31cc4..b94c5fdba 100644 --- a/examples/aws-agentcore/server/server.py +++ b/examples/aws-agentcore/server/server.py @@ -57,7 +57,6 @@ async def offer(request: Request): response = bedrock.invoke_agent_runtime( agentRuntimeArn=AGENT_RUNTIME_ARN, contentType="application/json", - accept="application/json", payload=json.dumps(data), # TODO: create a custom randon id runtimeSessionId="user-123456-conversation-111115555",