97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
import asyncio
|
|
import os
|
|
import sys
|
|
|
|
import aiohttp
|
|
from dotenv import load_dotenv
|
|
from loguru import logger
|
|
from runner import configure
|
|
|
|
from pipecat.frames.frames import LLMMessagesFrame
|
|
from pipecat.pipeline.pipeline import Pipeline
|
|
from pipecat.pipeline.runner import PipelineRunner
|
|
from pipecat.pipeline.task import PipelineTask, PipelineParams
|
|
from pipecat.services.azure import AzureTTSService
|
|
from pipecat.services.openrouter import OpenRouterLLMService
|
|
from pipecat.services.openai import OpenAILLMContext
|
|
from pipecat.transports.services.daily import DailyParams, DailyTransport
|
|
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
|
|
|
load_dotenv(override=True)
|
|
|
|
logger.remove(0)
|
|
logger.add(sys.stderr, level="DEBUG")
|
|
|
|
|
|
async def main():
|
|
async with aiohttp.ClientSession() as session:
|
|
(room_url, token) = await configure(session)
|
|
|
|
transport = DailyTransport(
|
|
room_url,
|
|
token,
|
|
"Chatbot",
|
|
DailyParams(
|
|
audio_out_enabled=True,
|
|
transcription_enabled=True,
|
|
vad_enabled=True,
|
|
vad_analyzer=SileroVADAnalyzer(),
|
|
vad_audio_passthrough=True,
|
|
),
|
|
)
|
|
|
|
tts = AzureTTSService(
|
|
api_key=os.getenv("AZURE_API_KEY"),
|
|
region="eastus",
|
|
voice="en-US-JennyNeural",
|
|
params=AzureTTSService.InputParams(
|
|
language="en-US",
|
|
rate="1.1",
|
|
style="chat",
|
|
),
|
|
)
|
|
|
|
llm = OpenRouterLLMService(
|
|
api_key=os.getenv("OPENROUTER_API_KEY"), model="microsoft/phi-4"
|
|
)
|
|
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, helpful, and brief way.",
|
|
}
|
|
]
|
|
context = OpenAILLMContext(messages=messages)
|
|
context_aggregator = llm.create_context_aggregator(context)
|
|
|
|
pipeline = Pipeline(
|
|
[
|
|
transport.input(),
|
|
context_aggregator.user(),
|
|
llm,
|
|
tts,
|
|
transport.output(),
|
|
context_aggregator.assistant(),
|
|
]
|
|
)
|
|
|
|
task = PipelineTask(
|
|
pipeline,
|
|
PipelineParams(allow_interruptions=True),
|
|
)
|
|
|
|
@transport.event_handler("on_first_participant_joined")
|
|
async def on_first_participant_joined(transport, participant):
|
|
await transport.capture_participant_transcription(participant["id"])
|
|
messages.append(
|
|
{"role": "system", "content": "Please introduce yourself to the user."}
|
|
)
|
|
await task.queue_frames([LLMMessagesFrame(messages)])
|
|
|
|
runner = PipelineRunner()
|
|
|
|
await runner.run(task)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|