From 5d8c184d99f95fe0761036bbb691fc9ac03e0b6f Mon Sep 17 00:00:00 2001 From: padillamt Date: Fri, 18 Jul 2025 16:30:03 -0700 Subject: [PATCH] inworld: commit of original text file and changes that copy openai's with Inworld TTS as only change --- .../07aa-interruptible-inworld-http.py | 16 ++- .../07aa-interruptible-inworld-http_copy.py | 117 ++++++++++++++++++ 2 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 examples/foundational/07aa-interruptible-inworld-http_copy.py diff --git a/examples/foundational/07aa-interruptible-inworld-http.py b/examples/foundational/07aa-interruptible-inworld-http.py index 2b8b1612d..03622a2d4 100644 --- a/examples/foundational/07aa-interruptible-inworld-http.py +++ b/examples/foundational/07aa-interruptible-inworld-http.py @@ -16,16 +16,15 @@ from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.runner import PipelineRunner from pipecat.pipeline.task import PipelineParams, PipelineTask from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext -from pipecat.services.deepgram.stt import DeepgramSTTService -from pipecat.services.openai.llm import OpenAILLMService from pipecat.services.inworld.tts import InworldHttpTTSService +from pipecat.services.openai.llm import OpenAILLMService +from pipecat.services.openai.stt import OpenAISTTService from pipecat.transports.base_transport import BaseTransport, TransportParams from pipecat.transports.network.fastapi_websocket import FastAPIWebsocketParams from pipecat.transports.services.daily import DailyParams load_dotenv(override=True) - # We store functions so objects (e.g. SileroVADAnalyzer) don't get # instantiated. The function will be called when the desired transport gets # selected. @@ -53,7 +52,11 @@ async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_si # Create an HTTP session async with aiohttp.ClientSession() as session: - stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY")) + stt = OpenAISTTService( + api_key=os.getenv("OPENAI_API_KEY"), + model="gpt-4o-transcribe", + prompt="Expect words related to dogs, such as breed names.", + ) tts = InworldHttpTTSService( api_key=os.getenv("INWORLD_API_KEY", ""), @@ -67,7 +70,7 @@ async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_si 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 converted to audio so don't include special characters in your answers. Respond to what the user said in a creative and helpful way.", + "content": "You are very knowledgable about dogs. Your output will be converted to audio so don't include special characters in your answers. Respond to what the user said in a creative and helpful way.", }, ] @@ -77,7 +80,7 @@ async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_si pipeline = Pipeline( [ transport.input(), # Transport user input - stt, + stt, # STT context_aggregator.user(), # User responses llm, # LLM tts, # TTS @@ -89,6 +92,7 @@ async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_si task = PipelineTask( pipeline, params=PipelineParams( + audio_out_sample_rate=24000, enable_metrics=True, enable_usage_metrics=True, ), diff --git a/examples/foundational/07aa-interruptible-inworld-http_copy.py b/examples/foundational/07aa-interruptible-inworld-http_copy.py new file mode 100644 index 000000000..0121865ab --- /dev/null +++ b/examples/foundational/07aa-interruptible-inworld-http_copy.py @@ -0,0 +1,117 @@ +# +# Copyright (c) 2024–2025, Daily +# +# SPDX-License-Identifier: BSD 2-Clause License +# + +import argparse +import os + +import aiohttp +from dotenv import load_dotenv +from loguru import logger + +from pipecat.audio.vad.silero import SileroVADAnalyzer +from pipecat.pipeline.pipeline import Pipeline +from pipecat.pipeline.runner import PipelineRunner +from pipecat.pipeline.task import PipelineParams, PipelineTask +from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext +from pipecat.services.deepgram.stt import DeepgramSTTService +from pipecat.services.inworld.tts import InworldHttpTTSService +from pipecat.services.openai.llm import OpenAILLMService +from pipecat.transports.base_transport import BaseTransport, TransportParams +from pipecat.transports.network.fastapi_websocket import FastAPIWebsocketParams +from pipecat.transports.services.daily import DailyParams + +load_dotenv(override=True) + + +# 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(), + ), + "twilio": lambda: FastAPIWebsocketParams( + audio_in_enabled=True, + audio_out_enabled=True, + vad_analyzer=SileroVADAnalyzer(), + ), + "webrtc": lambda: TransportParams( + audio_in_enabled=True, + audio_out_enabled=True, + vad_analyzer=SileroVADAnalyzer(), + ), +} + + +async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_sigint: bool): + logger.info(f"Starting bot") + + # Create an HTTP session + async with aiohttp.ClientSession() as session: + stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY")) + + tts = InworldHttpTTSService( + api_key=os.getenv("INWORLD_API_KEY", ""), + voice_id="Ashley", + model="inworld-tts-1", + aiohttp_session=session, + ) + + llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY")) + + 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 converted to audio so don't include special characters in your answers. Respond to what the user said in a creative and helpful way.", + }, + ] + + context = OpenAILLMContext(messages) + context_aggregator = llm.create_context_aggregator(context) + + pipeline = Pipeline( + [ + transport.input(), # Transport user input + stt, + context_aggregator.user(), # User responses + llm, # LLM + tts, # TTS + transport.output(), # Transport bot output + context_aggregator.assistant(), # Assistant spoken responses + ] + ) + + task = PipelineTask( + pipeline, + params=PipelineParams( + enable_metrics=True, + enable_usage_metrics=True, + ), + ) + + @transport.event_handler("on_client_connected") + async def on_client_connected(transport, client): + logger.info(f"Client connected") + # Kick off the conversation. + messages.append({"role": "system", "content": "Please introduce yourself to the user."}) + await task.queue_frames([context_aggregator.user().get_context_frame()]) + + @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=handle_sigint) + + await runner.run(task) + + +if __name__ == "__main__": + from pipecat.examples.run import main + + main(run_example, transport_params=transport_params)