From 1201f0cf19e6c8140ef37c032b099c852ad7394d Mon Sep 17 00:00:00 2001 From: Tvrtko Sternak Date: Wed, 18 Dec 2024 13:58:52 +0100 Subject: [PATCH] Replace prints with logging --- autogen/agentchat/realtime_agent/client.py | 9 ++++--- .../realtime_agent/function_observer.py | 7 ++++-- .../realtime_agent/twilio_observer.py | 24 ++++++++++--------- notebook/agentchat_realtime_swarm.ipynb | 3 ++- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/autogen/agentchat/realtime_agent/client.py b/autogen/agentchat/realtime_agent/client.py index 5b371b1a92..86c65a3452 100644 --- a/autogen/agentchat/realtime_agent/client.py +++ b/autogen/agentchat/realtime_agent/client.py @@ -7,6 +7,7 @@ # import asyncio import json +import logging from abc import ABC, abstractmethod from typing import Any, Optional @@ -18,6 +19,8 @@ from .function_observer import FunctionObserver +logger = logging.getLogger(__name__) + class Client(ABC): def __init__(self, agent, audio_adapter, function_observer: FunctionObserver): @@ -83,9 +86,9 @@ async def initialize_session(self): # todo override in specific clients async def session_update(self, session_options): update = {"type": "session.update", "session": session_options} - print("Sending session update:", json.dumps(update), flush=True) + logger.info("Sending session update:", json.dumps(update)) await self._openai_ws.send(json.dumps(update)) - print("Sending session update finished", flush=True) + logger.info("Sending session update finished") async def _read_from_client(self): try: @@ -93,7 +96,7 @@ async def _read_from_client(self): response = json.loads(openai_message) await self.notify_observers(response) except Exception as e: - print(f"Error in _read_from_client: {e}") + logger.warning(f"Error in _read_from_client: {e}") async def run(self): async with websockets.connect( diff --git a/autogen/agentchat/realtime_agent/function_observer.py b/autogen/agentchat/realtime_agent/function_observer.py index b4855d63ab..777fbf993e 100644 --- a/autogen/agentchat/realtime_agent/function_observer.py +++ b/autogen/agentchat/realtime_agent/function_observer.py @@ -7,12 +7,15 @@ import asyncio import json +import logging from asyncer import asyncify from pydantic import BaseModel from .realtime_observer import RealtimeObserver +logger = logging.getLogger(__name__) + class FunctionObserver(RealtimeObserver): def __init__(self, agent): @@ -21,7 +24,7 @@ def __init__(self, agent): async def update(self, response): if response.get("type") == "response.function_call_arguments.done": - print(f"Received event: {response['type']}", response) + logger.info(f"Received event: {response['type']}", response) await self.call_function( call_id=response["call_id"], name=response["name"], kwargs=json.loads(response["arguments"]) ) @@ -34,13 +37,13 @@ async def call_function(self, call_id, name, kwargs): result = await func(**kwargs) except Exception: result = "Function call failed" + logger.warning(f"Function call failed: {name}") if isinstance(result, BaseModel): result = result.model_dump_json() elif not isinstance(result, str): result = json.dumps(result) - print(f"Function call result: {result}") await self._client.function_result(call_id, result) async def run(self): diff --git a/autogen/agentchat/realtime_agent/twilio_observer.py b/autogen/agentchat/realtime_agent/twilio_observer.py index da950f27d6..6364fa99be 100644 --- a/autogen/agentchat/realtime_agent/twilio_observer.py +++ b/autogen/agentchat/realtime_agent/twilio_observer.py @@ -7,6 +7,7 @@ import base64 import json +import logging from fastapi import WebSocketDisconnect @@ -24,9 +25,11 @@ ] SHOW_TIMING_MATH = False +logger = logging.getLogger(__name__) + class TwilioAudioAdapter(RealtimeObserver): - def __init__(self, websocket, log_events=False): + def __init__(self, websocket): super().__init__() self.websocket = websocket @@ -36,12 +39,11 @@ def __init__(self, websocket, log_events=False): self.last_assistant_item = None self.mark_queue = [] self.response_start_timestamp_twilio = None - self.log_events = log_events async def update(self, response): """Receive events from the OpenAI Realtime API, send audio back to Twilio.""" - if response["type"] in LOG_EVENT_TYPES and self.log_events: - print(f"Received event: {response['type']}", response) + if response["type"] in LOG_EVENT_TYPES: + logger.info(f"Received event: {response['type']}", response) if response.get("type") == "response.audio.delta" and "delta" in response: audio_payload = base64.b64encode(base64.b64decode(response["delta"])).decode("utf-8") @@ -51,7 +53,7 @@ async def update(self, response): if self.response_start_timestamp_twilio is None: self.response_start_timestamp_twilio = self.latest_media_timestamp if SHOW_TIMING_MATH: - print(f"Setting start timestamp for new response: {self.response_start_timestamp_twilio}ms") + logger.info(f"Setting start timestamp for new response: {self.response_start_timestamp_twilio}ms") # Update last_assistant_item safely if response.get("item_id"): @@ -61,24 +63,24 @@ async def update(self, response): # Trigger an interruption. Your use case might work better using `input_audio_buffer.speech_stopped`, or combining the two. if response.get("type") == "input_audio_buffer.speech_started": - print("Speech started detected.") + logger.info("Speech started detected.") if self.last_assistant_item: - print(f"Interrupting response with id: {self.last_assistant_item}") + logger.info(f"Interrupting response with id: {self.last_assistant_item}") await self.handle_speech_started_event() async def handle_speech_started_event(self): """Handle interruption when the caller's speech starts.""" - print("Handling speech started event.") + logger.info("Handling speech started event.") if self.mark_queue and self.response_start_timestamp_twilio is not None: elapsed_time = self.latest_media_timestamp - self.response_start_timestamp_twilio if SHOW_TIMING_MATH: - print( + logger.info( f"Calculating elapsed time for truncation: {self.latest_media_timestamp} - {self.response_start_timestamp_twilio} = {elapsed_time}ms" ) if self.last_assistant_item: if SHOW_TIMING_MATH: - print(f"Truncating item with ID: {self.last_assistant_item}, Truncated at: {elapsed_time}ms") + logger.info(f"Truncating item with ID: {self.last_assistant_item}, Truncated at: {elapsed_time}ms") truncate_event = { "type": "conversation.item.truncate", @@ -112,7 +114,7 @@ async def run(self): await openai_ws.send(json.dumps(audio_append)) elif data["event"] == "start": self.stream_sid = data["start"]["streamSid"] - print(f"Incoming stream has started {self.stream_sid}") + logger.info(f"Incoming stream has started {self.stream_sid}") self.response_start_timestamp_twilio = None self.latest_media_timestamp = 0 self.last_assistant_item = None diff --git a/notebook/agentchat_realtime_swarm.ipynb b/notebook/agentchat_realtime_swarm.ipynb index 3697b185e2..d36996c1ab 100644 --- a/notebook/agentchat_realtime_swarm.ipynb +++ b/notebook/agentchat_realtime_swarm.ipynb @@ -384,6 +384,7 @@ "outputs": [], "source": [ "# import asyncio\n", + "import logging\n", "from datetime import datetime\n", "from time import time\n", "\n", @@ -423,7 +424,7 @@ " \"\"\"Handle WebSocket connections between Twilio and OpenAI.\"\"\"\n", " await websocket.accept()\n", "\n", - " audio_adapter = TwilioAudioAdapter(websocket, log_events=False)\n", + " audio_adapter = TwilioAudioAdapter(websocket)\n", " realtime_agent = RealtimeAgent(\n", " name=\"Customer_service_Bot\",\n", " llm_config=realtime_llm_config,\n",