check this opeanai realtime code is fine or its a ...
# ask-ai
b
check this opeanai realtime code is fine or its a mess import logging import os from dotenv import load_dotenv from livekit.agents import ( Agent, AgentSession, JobContext, JobProcess, RoomInputOptions, RoomOutputOptions, RunContext, WorkerOptions, cli, metrics, MetricsCollectedEvent, # Added this import ) from livekit.agents.llm import function_tool from livekit.plugins import openai from livekit.plugins.turn_detector import TurnDetection logger = logging.getLogger("agent") # Load environment variables from .env file load_dotenv(".env") # Debug: Print environment variables to confirm loading print("OPENAI_API_KEY:", os.getenv("OPENAI_API_KEY")) if not os.getenv("OPENAI_API_KEY"): raise ValueError("OPENAI_API_KEY is not set in the .env file or environment variables.") class Assistant(Agent): def __init__(self) -> None: super().__init__( instructions="""You are a helpful voice AI assistant. You eagerly assist users with their questions by providing information from your extensive knowledge. Your responses are concise, to the point, and without any complex formatting or punctuation. You are curious, friendly, and have a sense of humor.""", ) @function_tool async def lookup_weather(self, context: RunContext, location: str): """Use this tool to look up current weather information in the given location. If the location is not supported by the weather service, the tool will indicate this. You must tell the user the location's weather is unavailable. Args: location: The location to look up weather information for (e.g. city name) """ logger.info(f"Looking up weather for {location}") return "sunny with a temperature of 70 degrees." def prewarm(proc: JobProcess): proc.userdata["vad"] = silero.VAD.load() async def entrypoint(ctx: JobContext): ctx.log_context_fields = { "room": ctx.room.name, } session = AgentSession( llm=openai.LLM(model='gpt-4o-realtime-preview', api_key=os.getenv("OPENAI_API_KEY")), turn_detection=TurnDetection(type="semantic_vad", eagerness="auto", create_response=True, interrupt_response=True, ), ) usage_collector = metrics.UsageCollector() @session.on("metrics_collected") def _on_metrics_collected(ev: MetricsCollectedEvent): metrics.log_metrics(ev.metrics) usage_collector.collect(ev.metrics) async def log_usage(): summary = usage_collector.get_summary() logger.info(f"Usage: {summary}") ctx.add_shutdown_callback(log_usage) await session.start( agent=Assistant(), room=ctx.room, room_input_options=RoomInputOptions( noise_cancellation=noise_cancellation.BVC(), ), room_output_options=RoomOutputOptions(transcription_enabled=True), ) await ctx.connect() if name == "__main__": cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm))
t
Thanks for reaching out, Reuban. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!