135 lines
4.3 KiB
Python
135 lines
4.3 KiB
Python
#
|
||
# Copyright (c) 2024–2025, Daily
|
||
#
|
||
# SPDX-License-Identifier: BSD 2-Clause License
|
||
#
|
||
|
||
import asyncio
|
||
import os
|
||
import sys
|
||
|
||
import aiohttp
|
||
from dotenv import load_dotenv
|
||
from loguru import logger
|
||
from runner import configure
|
||
|
||
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
||
from pipecat.audio.vad.vad_analyzer import VADParams
|
||
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.cartesia import CartesiaTTSService
|
||
from pipecat.services.gemini_multimodal_live.gemini import (
|
||
GeminiMultimodalLiveLLMService,
|
||
GeminiMultimodalModalities,
|
||
InputParams,
|
||
)
|
||
from pipecat.transports.services.daily import DailyParams, DailyTransport
|
||
|
||
load_dotenv(override=True)
|
||
|
||
logger.remove(0)
|
||
logger.add(sys.stderr, level="DEBUG")
|
||
|
||
SYSTEM_INSTRUCTION = f"""
|
||
"You are Gemini Chatbot, a friendly, helpful robot.
|
||
|
||
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. Keep your responses brief. One or two sentences at most.
|
||
"""
|
||
|
||
|
||
async def main():
|
||
async with aiohttp.ClientSession() as session:
|
||
(room_url, token) = await configure(session)
|
||
|
||
transport = DailyTransport(
|
||
room_url,
|
||
token,
|
||
"Respond bot",
|
||
DailyParams(
|
||
audio_in_sample_rate=16000,
|
||
audio_out_sample_rate=24000,
|
||
audio_out_enabled=True,
|
||
vad_enabled=True,
|
||
vad_audio_passthrough=True,
|
||
# set stop_secs to something roughly similar to the internal setting
|
||
# of the Multimodal Live api, just to align events. This doesn't really
|
||
# matter because we can only use the Multimodal Live API's phrase
|
||
# endpointing, for now.
|
||
vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=0.5)),
|
||
),
|
||
)
|
||
|
||
llm = GeminiMultimodalLiveLLMService(
|
||
api_key=os.getenv("GOOGLE_API_KEY"),
|
||
transcribe_user_audio=True,
|
||
transcribe_model_audio=True,
|
||
system_instruction=SYSTEM_INSTRUCTION,
|
||
tools=[{"google_search": {}}, {"code_execution": {}}],
|
||
params=InputParams(modalities=GeminiMultimodalModalities.TEXT),
|
||
)
|
||
|
||
# Optionally, you can set the response modalities via a function
|
||
# llm.set_model_modalities(
|
||
# GeminiMultimodalModalities.TEXT
|
||
# )
|
||
|
||
tts = CartesiaTTSService(
|
||
api_key=os.getenv("CARTESIA_API_KEY"), voice_id="79a125e8-cd45-4c13-8a67-188112f4dd22"
|
||
)
|
||
|
||
messages = [
|
||
{
|
||
"role": "user",
|
||
"content": 'Start by saying "Hello, I\'m Gemini".',
|
||
},
|
||
]
|
||
|
||
# Set up conversation context and management
|
||
# The context_aggregator will automatically collect conversation context
|
||
context = OpenAILLMContext(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,
|
||
enable_metrics=True,
|
||
enable_usage_metrics=True,
|
||
),
|
||
)
|
||
|
||
@transport.event_handler("on_first_participant_joined")
|
||
async def on_first_participant_joined(transport, participant):
|
||
await transport.capture_participant_transcription(participant["id"])
|
||
await task.queue_frames([context_aggregator.user().get_context_frame()])
|
||
|
||
@transport.event_handler("on_participant_left")
|
||
async def on_participant_left(transport, participant, reason):
|
||
print(f"Participant left: {participant}")
|
||
await task.cancel()
|
||
|
||
runner = PipelineRunner()
|
||
|
||
await runner.run(task)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(main())
|