Skip to content

Commit

Permalink
Replace prints with logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sternakt committed Dec 18, 2024
1 parent e8fc5a2 commit 1201f0c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
9 changes: 6 additions & 3 deletions autogen/agentchat/realtime_agent/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# import asyncio
import json
import logging
from abc import ABC, abstractmethod
from typing import Any, Optional

Expand All @@ -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):
Expand Down Expand Up @@ -83,17 +86,17 @@ 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:
async for openai_message in self._openai_ws:
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(
Expand Down
7 changes: 5 additions & 2 deletions autogen/agentchat/realtime_agent/function_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"])
)
Expand All @@ -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):
Expand Down
24 changes: 13 additions & 11 deletions autogen/agentchat/realtime_agent/twilio_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import base64
import json
import logging

from fastapi import WebSocketDisconnect

Expand All @@ -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

Expand All @@ -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")
Expand All @@ -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"):
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion notebook/agentchat_realtime_swarm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@
"outputs": [],
"source": [
"# import asyncio\n",
"import logging\n",
"from datetime import datetime\n",
"from time import time\n",
"\n",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 1201f0c

Please sign in to comment.