Refactoring how we are structuring the code.
This commit is contained in:
196
examples/aws-agentcore/agents/network-test/network-test.py
Normal file
196
examples/aws-agentcore/agents/network-test/network-test.py
Normal file
@@ -0,0 +1,196 @@
|
||||
import asyncio
|
||||
import os
|
||||
import socket
|
||||
|
||||
import aioice
|
||||
from bedrock_agentcore import BedrockAgentCoreApp
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv(override=True)
|
||||
|
||||
app = BedrockAgentCoreApp()
|
||||
|
||||
|
||||
def test_udp():
|
||||
"""Test UDP connectivity using STUN server"""
|
||||
stun_server = ("stun.l.google.com", 19302)
|
||||
msg = b"\x00\x01\x00\x00" + b"\x21\x12\xa4\x42" + b"\x00" * 12
|
||||
|
||||
sock = None
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.settimeout(3)
|
||||
|
||||
print("Testing UDP connectivity to STUN server...")
|
||||
sock.sendto(msg, stun_server)
|
||||
|
||||
_, _ = sock.recvfrom(1024)
|
||||
print("STUN response received")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print("STUN test failed:", e)
|
||||
return False
|
||||
|
||||
finally:
|
||||
if sock:
|
||||
sock.close()
|
||||
|
||||
|
||||
async def _async_turn_test(turn_server, turn_port, username, password, turn_transport, turn_ssl):
|
||||
"""Internal async TURN test using aioice."""
|
||||
print(f"Testing TURN server: {turn_server}:{turn_port}:{turn_transport}")
|
||||
|
||||
connection = aioice.Connection(
|
||||
ice_controlling=True,
|
||||
turn_server=(turn_server, turn_port),
|
||||
turn_username=username,
|
||||
turn_password=password,
|
||||
turn_ssl=turn_ssl,
|
||||
turn_transport=turn_transport,
|
||||
)
|
||||
try:
|
||||
print(f"Gathering ICE candidates via TURN {turn_server}:{turn_port} ...")
|
||||
await connection.gather_candidates()
|
||||
candidates = connection.local_candidates
|
||||
|
||||
relay_candidates = [c for c in candidates if c.type == "relay"]
|
||||
|
||||
if relay_candidates:
|
||||
print("TURN relay candidate acquired:", relay_candidates[0])
|
||||
return True
|
||||
|
||||
print("No TURN relay candidates received — allocation failed.")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"TURN test failed: {e}")
|
||||
return False
|
||||
|
||||
finally:
|
||||
await connection.close()
|
||||
|
||||
|
||||
def test_turn_with_auth(server, port, username, password, transport, turn_ssl=False):
|
||||
"""Sync wrapper for aioice TURN test."""
|
||||
return asyncio.run(_async_turn_test(server, port, username, password, transport, turn_ssl))
|
||||
|
||||
|
||||
def comprehensive_network_test():
|
||||
"""Run comprehensive network connectivity tests."""
|
||||
results = {}
|
||||
|
||||
# Test basic UDP connectivity
|
||||
results["udp_stun"] = test_udp()
|
||||
|
||||
turn_username = os.getenv("TURN_USERNAME")
|
||||
turn_credential = os.getenv("TURN_CREDENTIAL")
|
||||
|
||||
# TURN test list
|
||||
turn_servers = [
|
||||
(
|
||||
"turn.cloudflare.com", # cleaned
|
||||
3478,
|
||||
turn_username,
|
||||
turn_credential,
|
||||
"udp",
|
||||
False,
|
||||
),
|
||||
(
|
||||
"turn.cloudflare.com", # cleaned
|
||||
5349,
|
||||
turn_username,
|
||||
turn_credential,
|
||||
"tcp",
|
||||
True,
|
||||
),
|
||||
(
|
||||
"turn.cloudflare.com", # cleaned
|
||||
443,
|
||||
turn_username,
|
||||
turn_credential,
|
||||
"tcp",
|
||||
True,
|
||||
),
|
||||
(
|
||||
"turn.cloudflare.com", # cleaned
|
||||
80,
|
||||
turn_username,
|
||||
turn_credential,
|
||||
"tcp",
|
||||
False,
|
||||
),
|
||||
(
|
||||
"turn.cloudflare.com", # cleaned
|
||||
3478,
|
||||
turn_username,
|
||||
turn_credential,
|
||||
"tcp",
|
||||
False,
|
||||
),
|
||||
]
|
||||
|
||||
results["turn_tests"] = []
|
||||
|
||||
for host, port, username, password, transport, tls in turn_servers:
|
||||
success = test_turn_with_auth(host, port, username, password, transport, tls)
|
||||
results["turn_tests"].append({"server": f"{host}:{port}", "success": success})
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def test_tcp_connectivity(host, port):
|
||||
"""Test TCP connectivity to a host."""
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(3)
|
||||
result = sock.connect_ex((host, port))
|
||||
sock.close()
|
||||
|
||||
if result == 0:
|
||||
print(f"TCP connection to {host}:{port} successful")
|
||||
return True
|
||||
|
||||
print(f"TCP connection to {host}:{port} failed")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"TCP test failed: {e}")
|
||||
return False
|
||||
|
||||
|
||||
@app.entrypoint
|
||||
def my_agent(payload):
|
||||
network_results = comprehensive_network_test()
|
||||
|
||||
udp_ok = network_results.get("udp_stun", False)
|
||||
turn_ok = any(t["success"] for t in network_results["turn_tests"])
|
||||
tcp_ok = network_results.get("tcp_test", False)
|
||||
|
||||
connectivity_status = []
|
||||
if udp_ok:
|
||||
connectivity_status.append("UDP/STUN")
|
||||
if turn_ok:
|
||||
connectivity_status.append("TURN")
|
||||
if tcp_ok:
|
||||
connectivity_status.append("TCP")
|
||||
|
||||
return {
|
||||
"result": f"Hello {payload.get('name', 'World')}!",
|
||||
"network_test_results": network_results,
|
||||
"connectivity_status": ", ".join(connectivity_status)
|
||||
if connectivity_status
|
||||
else "No connectivity",
|
||||
"webrtc_feasible": udp_ok or turn_ok,
|
||||
"turn_available": turn_ok,
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if os.getenv("PIPECAT_LOCAL_DEV") == "1":
|
||||
# Running locally
|
||||
results = comprehensive_network_test()
|
||||
print(results)
|
||||
else:
|
||||
# Running on AgentCore Runtime
|
||||
app.run()
|
||||
@@ -0,0 +1,3 @@
|
||||
aioice
|
||||
aiohttp
|
||||
bedrock-agentcore
|
||||
243
examples/aws-agentcore/agents/pipecat-daily/pipecat-daily.py
Normal file
243
examples/aws-agentcore/agents/pipecat-daily/pipecat-daily.py
Normal file
@@ -0,0 +1,243 @@
|
||||
#
|
||||
# Copyright (c) 2024–2025, Daily
|
||||
#
|
||||
# SPDX-License-Identifier: BSD 2-Clause License
|
||||
#
|
||||
|
||||
import os
|
||||
|
||||
import aiohttp
|
||||
from bedrock_agentcore import BedrockAgentCoreApp
|
||||
from dotenv import load_dotenv
|
||||
from loguru import logger
|
||||
from pipecat.adapters.schemas.function_schema import FunctionSchema
|
||||
from pipecat.adapters.schemas.tools_schema import ToolsSchema
|
||||
from pipecat.audio.turn.smart_turn.base_smart_turn import SmartTurnParams
|
||||
from pipecat.audio.turn.smart_turn.local_smart_turn_v3 import LocalSmartTurnAnalyzerV3
|
||||
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
||||
from pipecat.audio.vad.vad_analyzer import VADParams
|
||||
from pipecat.frames.frames import LLMRunFrame, TTSSpeakFrame
|
||||
from pipecat.pipeline.pipeline import Pipeline
|
||||
from pipecat.pipeline.runner import PipelineRunner
|
||||
from pipecat.pipeline.task import PipelineParams, PipelineTask
|
||||
from pipecat.processors.aggregators.llm_context import LLMContext
|
||||
from pipecat.processors.aggregators.llm_response_universal import LLMContextAggregatorPair
|
||||
from pipecat.runner.types import DailyRunnerArguments, RunnerArguments
|
||||
from pipecat.runner.utils import create_transport
|
||||
from pipecat.services.cartesia.tts import CartesiaTTSService
|
||||
from pipecat.services.deepgram.stt import DeepgramSTTService
|
||||
from pipecat.services.llm_service import FunctionCallParams
|
||||
from pipecat.services.openai.llm import OpenAILLMService
|
||||
from pipecat.transports.base_transport import BaseTransport, TransportParams
|
||||
from pipecat.transports.daily.transport import DailyLogLevel, DailyParams, DailyTransport
|
||||
|
||||
app = BedrockAgentCoreApp()
|
||||
|
||||
load_dotenv(override=True)
|
||||
|
||||
|
||||
async def get_public_ip():
|
||||
"""Retrieve public IP from AWS metadata service or external service."""
|
||||
try:
|
||||
# Fallback to external service
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(
|
||||
"https://api.ipify.org", timeout=aiohttp.ClientTimeout(total=5)
|
||||
) as response:
|
||||
if response.status == 200:
|
||||
return await response.text()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
async def fetch_weather_from_api(params: FunctionCallParams):
|
||||
await params.result_callback({"conditions": "nice", "temperature": "75"})
|
||||
|
||||
|
||||
async def fetch_restaurant_recommendation(params: FunctionCallParams):
|
||||
await params.result_callback({"name": "The Golden Dragon"})
|
||||
|
||||
|
||||
# We store functions so objects (e.g. SileroVADAnalyzer) don't get
|
||||
# instantiated. The function will be called when the desired transport gets
|
||||
# selected.
|
||||
transport_params = {
|
||||
"daily": lambda: DailyParams(
|
||||
audio_in_enabled=True,
|
||||
audio_out_enabled=True,
|
||||
vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)),
|
||||
turn_analyzer=LocalSmartTurnAnalyzerV3(params=SmartTurnParams()),
|
||||
),
|
||||
"webrtc": lambda: TransportParams(
|
||||
audio_in_enabled=True,
|
||||
audio_out_enabled=True,
|
||||
vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.2)),
|
||||
turn_analyzer=LocalSmartTurnAnalyzerV3(params=SmartTurnParams()),
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
|
||||
logger.info(f"Starting bot")
|
||||
|
||||
public_ip = await get_public_ip()
|
||||
if public_ip:
|
||||
logger.info(f"Public IP address: {public_ip}")
|
||||
else:
|
||||
logger.warning("Could not retrieve public IP address")
|
||||
|
||||
yield {"status": "initializing", "ip": public_ip}
|
||||
|
||||
stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY"))
|
||||
|
||||
tts = CartesiaTTSService(
|
||||
api_key=os.getenv("CARTESIA_API_KEY"),
|
||||
voice_id="71a7ad14-091c-4e8e-a314-022ece01c121", # British Reading Lady
|
||||
)
|
||||
|
||||
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"))
|
||||
|
||||
# You can also register a function_name of None to get all functions
|
||||
# sent to the same callback with an additional function_name parameter.
|
||||
llm.register_function("get_current_weather", fetch_weather_from_api)
|
||||
llm.register_function("get_restaurant_recommendation", fetch_restaurant_recommendation)
|
||||
|
||||
@llm.event_handler("on_function_calls_started")
|
||||
async def on_function_calls_started(service, function_calls):
|
||||
await tts.queue_frame(TTSSpeakFrame("Let me check on that."))
|
||||
|
||||
weather_function = FunctionSchema(
|
||||
name="get_current_weather",
|
||||
description="Get the current weather",
|
||||
properties={
|
||||
"location": {
|
||||
"type": "string",
|
||||
"description": "The city and state, e.g. San Francisco, CA",
|
||||
},
|
||||
"format": {
|
||||
"type": "string",
|
||||
"enum": ["celsius", "fahrenheit"],
|
||||
"description": "The temperature unit to use. Infer this from the user's location.",
|
||||
},
|
||||
},
|
||||
required=["location", "format"],
|
||||
)
|
||||
restaurant_function = FunctionSchema(
|
||||
name="get_restaurant_recommendation",
|
||||
description="Get a restaurant recommendation",
|
||||
properties={
|
||||
"location": {
|
||||
"type": "string",
|
||||
"description": "The city and state, e.g. San Francisco, CA",
|
||||
},
|
||||
},
|
||||
required=["location"],
|
||||
)
|
||||
tools = ToolsSchema(standard_tools=[weather_function, restaurant_function])
|
||||
|
||||
messages = [
|
||||
{
|
||||
"role": "system",
|
||||
"content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Your output will be spoken aloud, so avoid special characters that can't easily be spoken, such as emojis or bullet points. Respond to what the user said in a creative and helpful way.",
|
||||
},
|
||||
]
|
||||
|
||||
context = LLMContext(messages, tools)
|
||||
context_aggregator = LLMContextAggregatorPair(context)
|
||||
|
||||
pipeline = Pipeline(
|
||||
[
|
||||
transport.input(),
|
||||
stt,
|
||||
context_aggregator.user(),
|
||||
llm,
|
||||
tts,
|
||||
transport.output(),
|
||||
context_aggregator.assistant(),
|
||||
]
|
||||
)
|
||||
|
||||
task = PipelineTask(
|
||||
pipeline,
|
||||
params=PipelineParams(
|
||||
enable_metrics=True,
|
||||
enable_usage_metrics=True,
|
||||
),
|
||||
idle_timeout_secs=runner_args.pipeline_idle_timeout_secs,
|
||||
)
|
||||
|
||||
@transport.event_handler("on_client_connected")
|
||||
async def on_client_connected(transport, client):
|
||||
logger.info(f"Client connected")
|
||||
# Kick off the conversation.
|
||||
await task.queue_frames([LLMRunFrame()])
|
||||
|
||||
@transport.event_handler("on_client_disconnected")
|
||||
async def on_client_disconnected(transport, client):
|
||||
logger.info(f"Client disconnected")
|
||||
await task.cancel()
|
||||
|
||||
runner = PipelineRunner(handle_sigint=runner_args.handle_sigint)
|
||||
|
||||
task_id = app.add_async_task("voice_agent")
|
||||
|
||||
await runner.run(task)
|
||||
|
||||
app.complete_async_task(task_id)
|
||||
|
||||
yield {"status": "completed"}
|
||||
|
||||
|
||||
async def bot(runner_args: RunnerArguments):
|
||||
"""Bot entry point for running locally and on Pipecat Cloud."""
|
||||
transport = await create_transport(runner_args, transport_params)
|
||||
async for result in run_bot(transport, runner_args):
|
||||
pass # Consume the stream
|
||||
|
||||
|
||||
@app.entrypoint
|
||||
async def agentcore_bot(payload, context):
|
||||
"""Bot entry point for running on Amazon Bedrock AgentCore Runtime."""
|
||||
room_url = payload.get("roomUrl")
|
||||
transport = await create_transport(
|
||||
DailyRunnerArguments(room_url=room_url),
|
||||
transport_params,
|
||||
)
|
||||
if isinstance(transport, DailyTransport):
|
||||
transport.set_log_level(DailyLogLevel.Info)
|
||||
turn_username = os.getenv("TURN_USERNAME")
|
||||
turn_credential = os.getenv("TURN_CREDENTIAL")
|
||||
transport._client._client.set_ice_config(
|
||||
{
|
||||
"placement": "replace",
|
||||
"iceServers": [
|
||||
{
|
||||
"urls": [
|
||||
"turn:turn.cloudflare.com:80?transport=tcp",
|
||||
"turns:turn.cloudflare.com:443?transport=tcp",
|
||||
],
|
||||
"username": turn_username,
|
||||
"credential": turn_credential,
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
async for result in run_bot(transport, RunnerArguments()):
|
||||
yield result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# NOTE: ideally we shouldn't have to branch for local dev vs AgentCore, but
|
||||
# local AgentCore container-based dev doesn't seem to be working, or at
|
||||
# least not for this project.
|
||||
if os.getenv("PIPECAT_LOCAL_DEV") == "1":
|
||||
# Running locally
|
||||
from pipecat.runner.run import main
|
||||
|
||||
main()
|
||||
else:
|
||||
# Running on AgentCore Runtime
|
||||
app.run()
|
||||
@@ -0,0 +1,3 @@
|
||||
aiohttp
|
||||
bedrock-agentcore
|
||||
pipecat-ai[webrtc,daily,silero,deepgram,openai,cartesia,local-smart-turn-v3,runner]
|
||||
Reference in New Issue
Block a user