i have an issue in multi multimodal agents configu...
# ask-ai
q
i have an issue in multi multimodal agents configuration . here is my error: bit of fruit jam or simply on their own. Croissants can also be filled with almond paste, chocolate, or ham and cheese for a savory twist. They make a delightful breakfast or snack!", "interrupted": false} 2025-02-25 115315,488 - DEBUG livekit.plugins.openai.realtime - conversation item created {"type": "conversation.item.created", "event_id": "event_B4mqpG84jN0r32Wjo0JnP", "previous_item_id": "item_B4mq7hBN4eDYssVcd5vag", "item": {"id": "item_B4mqm71CyMQSZUMHRZ3ge", "object": "realtime.item", "type": "message", "status": "completed", "role": "user", "content": [{"type": "input_audio", "transcript": null}]}} 2025-02-25 115316,429 - DEBUG livekit.agents - committed user speech {"user_transcript": "Pouvez-vous me transformer en agent de la m\u00e9t\u00e9o ?\n", "interrupted": false} 2025-02-25 115316,878 - DEBUG livekit.plugins.openai.realtime - conversation item created {"type": "conversation.item.created", "event_id": "event_B4mqrAi3OjRoLRbTNhCb3", "previous_item_id": "item_B4mqm71CyMQSZUMHRZ3ge", "item": {"id": "item_B4mqpFeSN4qGI6TrqbGvS", "object": "realtime.item", "type": "message", "status": "in_progress", "role": "assistant", "content": []}} 2025-02-25 115321,098 - DEBUG livekit.agents - committed agent speech {"agent_transcript": "I'll connect you to the Weather Assistant for your weather-related questions. Just a moment!", "interrupted": false} 2025-02-25 115328,877 - DEBUG livekit.plugins.openai.realtime - conversation item created {"type": "conversation.item.created", "event_id": "event_B4mr3zoYycrwiB50ssYRo", "previous_item_id": "item_B4mqpFeSN4qGI6TrqbGvS", "item": {"id": "item_B4mr2rPdYRZNkHrYPelMg", "object": "realtime.item", "type": "message", "status": "completed", "role": "user", "content": [{"type": "input_audio", "transcript": null}]}} 2025-02-25 115328,877 - DEBUG livekit.agents - committed user speech {"user_transcript": "", "interrupted": false} 2025-02-25 115330,688 - DEBUG livekit.plugins.openai.realtime - conversation item created {"type": "conversation.item.created", "event_id": "event_B4mr5e3K7tZNtbYL8MvYs", "previous_item_id": "item_B4mr2rPdYRZNkHrYPelMg", "item": {"id": "item_B4mr3hKxNb4IVZqh9jSKc", "object": "realtime.item", "type": "message", "status": "in_progress", "role": "assistant", "content": []}} 2025-02-25 115333,364 - DEBUG livekit.agents - committed agent speech {"agent_transcript": "Thank you for your patience! How can I assist you further?", "interrupted": false} 2025-02-25 115336,600 - DEBUG livekit.plugins.openai.realtime - conversation item created {"type": "conversation.item.created", "event_id": "event_B4mrAL3l2fIPEXWCmmGRH", "previous_item_id": "item_B4mr3hKxNb4IVZqh9jSKc", "item": {"id": "item_B4mr9sFFCvtewDXlt8XHM", "object": "realtime.item", "type": "message", "status": "completed", "role": "user", "content": [{"type": "input_audio", "transcript": null}]}} 2025-02-25 115336,846 - DEBUG livekit.agents - committed user speech {"user_transcript": "Who are you?\n", "interrupted": false} 2025-02-25 115337,390 - DEBUG livekit.plugins.openai.realtime - conversation item created {"type": "conversation.item.created", "event_id": "event_B4mrBBiJy5tc5dhG457f1", "previous_item_id": "item_B4mr9sFFCvtewDXlt8XHM", "item": {"id": "item_B4mrAuuIaZ6VNrPnvBeD5", "object": "realtime.item", "type": "message", "status": "in_progress", "role": "assistant", "content": []}} 2025-02-25 115342,785 - DEBUG livekit.agents - committed agent speech {"agent_transcript": "If you have any more questions or need assistance with something else, feel free to ask! I'm here to help.", "interrupted": false} 2025-02-25 115345,319 - ERROR livekit.plugins.openai.realtime - Error in _recv_task Traceback (most recent call last): File "C:\Users\Usuario\my-app\multimodal-multi-agent\venv\Lib\site-packages\livekit\agents\utils\log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\my-app\multimodal-multi-agent\venv\Lib\site-packages\livekit\plugins\openai\realtime\realtime_model.py", line 1189, in _recv_task raise Exception("OpenAI S2S connection closed unexpectedly") Exception: OpenAI S2S connection closed unexpectedly 2025-02-25 115345,326 - ERROR livekit.plugins.openai.realtime - Error in _main_task Traceback (most recent call last): File "C:\Users\Usuario\my-app\multimodal-multi-agent\venv\Lib\site-packages\livekit\agents\utils\log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\my-app\multimodal-multi-agent\venv\Lib\site-packages\livekit\plugins\openai\realtime\realtime_model.py", line 1266, in _main_task await asyncio.gather(*tasks) File "C:\Users\Usuario\my-app\multimodal-multi-agent\venv\Lib\site-packages\livekit\agents\utils\log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Usuario\my-app\multimodal-multi-agent\venv\Lib\site-packages\livekit\plugins\openai\realtime\realtime_model.py", line 1189, in _recv_task raise Exception("OpenAI S2S connection closed unexpectedly") Exception: OpenAI S2S connection closed unexpectedly
g
L'erreur "OpenAI S2S connection closed unexpectedly" indique que la connexion avec l'API OpenAI Realtime a été interrompue de manière inattendue. Cela peut être dû à plusieurs raisons, telles qu'une instabilité du réseau, une configuration incorrecte, ou des limitations du service OpenAI. Pour résoudre ce problème, vous pouvez essayer les étapes suivantes : 1. Vérifiez la connexion réseau : Assurez-vous que votre connexion Internet est stable et qu'il n'y a pas de coupures ou de latences importantes. 2. Revoyez la configuration : Assurez-vous que toutes les configurations nécessaires pour l'API OpenAI Realtime sont correctement définies, y compris les clés API et les paramètres de session. 3. Mettez à jour les dépendances : Assurez-vous que vous utilisez les dernières versions des bibliothèques LiveKit et OpenAI, car des mises à jour peuvent contenir des correctifs pour des problèmes connus. 4. Logs et débogage : Consultez les logs pour voir s'il y a des messages d'erreur supplémentaires qui pourraient fournir plus de contexte sur la cause de la déconnexion. 5. Réessayer la connexion : Implémentez une logique de reconnexion automatique pour gérer les déconnexions inattendues. Si le problème persiste, il pourrait être utile de contacter le support technique de LiveKit ou OpenAI pour obtenir une assistance plus approfondie. Sources: - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1732022090.671149 - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1729733852.224729 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
q
@millions-hair-24956 here is my file if that helps: import asyncio import logging from dotenv import load_dotenv from livekit import rtc from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, cli, llm from livekit.agents.multimodal import MultimodalAgent from livekit.plugins import openai load_dotenv() logger = logging.getLogger("home-agent") logger.setLevel(logging.INFO) class HomeAssistantFnc(llm.FunctionContext): def __init__(self, ctx, weather_event, bakery_event): super().__init__() self.ctx = ctx self.weather_event = weather_event self.bakery_event = bakery_event @llm.ai_callable(description="Dispatch the weather agent if not active.") async def dispatch_weather_agent(self) -> str: if not self.weather_event.is_set(): logger.info("Dispatching weather agent via function call...") await dispatch_weather_agent(self.ctx.room.name) self.weather_event.set() return "Weather agent dispatched" else: logger.info("Weather agent already active, skipping dispatch.") return "Weather agent already active" @llm.ai_callable(description="Dispatch the bakery agent if not active.") async def dispatch_bakery_agent(self) -> str: if not self.bakery_event.is_set(): logger.info("Dispatching bakery agent via function call...") await dispatch_bakery_agent(self.ctx.room.name) self.bakery_event.set() return "Bakery agent dispatched" else: logger.info("Bakery agent already active, skipping dispatch.") return "Bakery agent already active" def prewarm(proc): logger.info("Prewarming: No VAD needed for multimodal agent.") async def entrypoint(ctx: JobContext): logger.info("Connecting to room %s...", ctx.room.name) await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY) logger.info("Connected to room: %s", ctx.room.name) participant = await ctx.wait_for_participant() logger.info("Participant '%s' joined. Starting home agent...", participant.identity) # Create events to track the active state of secondary agents. weather_event = asyncio.Event() bakery_event = asyncio.Event() fnc_ctx = HomeAssistantFnc(ctx, weather_event, bakery_event) # Initial chat context. initial_ctx = llm.ChatContext().append( role="system", text=( "You are the main assistant. Answer queries normally. " "Dispatch the weather assistant for weather queries, and the bakery assistant for croissant or bakery advice." ) ) # Create a realtime model for multimodal interactions using the preview model. model = openai.realtime.RealtimeModel( model="gpt-4o-mini-realtime-preview-2024-12-17", instructions=( "You are the main assistant. Answer queries normally. " "Dispatch the weather assistant for weather queries, and the bakery assistant for croissant or bakery advice." ), voice="alloy", temperature=0.8, modalities=["audio", "text"], turn_detection=openai.realtime.ServerVadOptions( threshold=0.6, prefix_padding_ms=200, silence_duration_ms=500 ) ) agent = MultimodalAgent(model=model, chat_ctx=initial_ctx) agent.start(ctx.room, participant) logger.info("Home agent started.") # Helper function to send a message. async def say_message(message: str): logger.info("Home Agent speaking: '%s'", message) # Obtain a copy of the current chat context. chat_ctx = agent.chat_ctx_copy() chat_ctx.append(role="assistant", text=message) await agent.set_chat_ctx(chat_ctx) # Trigger the realtime response. agent.generate_reply() # --- Handle secondary agent handbacks --- async def handle_agent_done(agent_name: str, event: asyncio.Event): logger.info(f"{agent_name.capitalize()} agent done signal received. Handling transfer back to main assistant.") from livekit import api lkapi = api.LiveKitAPI() try: dispatches = await lkapi.agent_dispatch.list_dispatch(room_name=ctx.room.name) logger.debug("Current dispatches: %s", dispatches) for d in dispatches: if d.agent_name == agent_name: await lkapi.agent_dispatch.delete_dispatch(dispatch_id=d.id, room_name=ctx.room.name) logger.info(f"Deleted {agent_name} agent dispatch: {d.id}") break except Exception as e: logger.error(f"Failed to delete {agent_name} agent dispatch: {e}") finally: await lkapi.aclose() # Reset conversation context. new_ctx = llm.ChatContext().append( role="system", text="You are the main assistant. Answer queries normally." ) await agent.set_chat_ctx(new_ctx) await asyncio.sleep(2) # Allow overlapping speech to finish. await say_message("Ok, I got it.") event.clear() async def handle_data_received(data: rtc.DataPacket): try: if data.data == b"weather_agent_joined": logger.info("Weather agent joined; no subscription toggling needed for multimodal agent.") elif data.data == b"weather_agent_done": await handle_agent_done("weather", weather_event) elif data.data == b"bakery_agent_joined": logger.info("Bakery agent joined; no subscription toggling needed for multimodal agent.") elif data.data == b"bakery_agent_done": await handle_agent_done("bakery", bakery_event) else: logger.debug("Data received: %s", data.data) except Exception as e: logger.error("Error handling data packet: %s", e) @ctx.room.on("data_received") def on_data_received(data: rtc.DataPacket): asyncio.create_task(handle_data_received(data)) async def determine_intent(message: str) -> str: logger.info("Determining intent via NLP for message: '%s'", message) prompt = ( "Classify the following user message into one of these categories: weather, bakery, or general. " "Return only one word as the intent. Message: \"" + message + "\"" ) nlp_ctx = llm.ChatContext().append(role="system", text=prompt) response = await openai.LLM().chat(chat_ctx=nlp_ctx) intent = response.strip().lower() if intent not in {"weather", "bakery", "general"}: intent = "general" logger.info("Determined intent via NLP: '%s'", intent) return intent async def handle_incoming_message(message: str): logger.info("Handling incoming message: '%s'", message) intent = await determine_intent(message) if intent == "weather": logger.info("Intent 'weather' detected. Initiating dispatch to weather agent.") asyncio.create_task(dispatch_weather_agent(ctx.room.name)) await say_message("Ok, I got it.") elif intent == "bakery": logger.info("Intent 'bakery' detected. Initiating dispatch to bakery agent.") asyncio.create_task(dispatch_bakery_agent(ctx.room.name)) await say_message("Ok, I got it.") else: logger.info("Processing message as general query.") # Generate reply using the multimodal agent. agent.generate_reply() @ctx.room.on("message_received") def on_message_received(msg: rtc.ChatMessage): if msg.message: logger.info("Chat message received: '%s'", msg.message) asyncio.create_task(handle_incoming_message(msg.message)) await say_message("Hey, how can I help you today?") logger.info("Initial greeting sent by home agent.") async def dispatch_weather_agent(room_name: str): from livekit import api logger.info("Dispatching weather agent to room '%s'...", room_name) lkapi = api.LiveKitAPI() dispatch_request = api.CreateAgentDispatchRequest( agent_name="weather_agent", room=room_name, metadata="Weather inquiry dispatched by home agent", ) logger.debug("Weather dispatch request: %s", dispatch_request) try: dispatch = await lkapi.agent_dispatch.create_dispatch(dispatch_request) logger.info("Weather agent dispatched successfully: %s", dispatch) except Exception as e: logger.error("Failed to dispatch weather agent: %s", e) finally: await lkapi.aclose() async def dispatch_bakery_agent(room_name: str): from livekit import api logger.info("Dispatching bakery agent to room '%s'...", room_name) lkapi = api.LiveKitAPI() dispatch_request = api.CreateAgentDispatchRequest( agent_name="bakery_agent", room=room_name, metadata="Bakery inquiry dispatched by home agent", ) logger.debug("Bakery dispatch request: %s", dispatch_request) try: dispatch = await lkapi.agent_dispatch.create_dispatch(dispatch_request) logger.info("Bakery agent dispatched successfully: %s", dispatch) except Exception as e: logger.error("Failed to dispatch bakery agent: %s", e) finally: await lkapi.aclose() if name == "__main__": cli.run_app( WorkerOptions( entrypoint_fnc=entrypoint, prewarm_fnc=prewarm, ), )
g
L'erreur "OpenAI S2S connection closed unexpectedly" que vous rencontrez est souvent liée à des problèmes de connexion réseau ou de configuration avec l'API OpenAI Realtime. Voici quelques suggestions pour résoudre ce problème : 1. Vérifiez la connexion réseau : Assurez-vous que votre connexion Internet est stable et qu'il n'y a pas de coupures ou de latences importantes. 2. Revoyez la configuration : Assurez-vous que toutes les configurations nécessaires pour l'API OpenAI Realtime sont correctement définies, y compris les clés API et les paramètres de session. 3. Mettez à jour les dépendances : Assurez-vous que vous utilisez les dernières versions des bibliothèques LiveKit et OpenAI, car des mises à jour peuvent contenir des correctifs pour des problèmes connus. 4. Réessayer la connexion : Implémentez une logique de reconnexion automatique pour gérer les déconnexions inattendues. 5. Logs et débogage : Consultez les logs pour voir s'il y a des messages d'erreur supplémentaires qui pourraient fournir plus de contexte sur la cause de la déconnexion. Si le problème persiste, il pourrait être utile de contacter le support technique de LiveKit ou OpenAI pour obtenir une assistance plus approfondie. Sources: - https://docs.livekit.io/python/livekit/agents/multimodal/multimodal_agent.html#livekit.agents.multimodal.multimodal_agent.MultimodalAgent - https://docs.livekit.io/python/livekit/agents/multimodal/index.html#livekit.agents.multimodal.MultimodalAgent 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.