More robust handling of conversation item retrieval errors in OpenAIRealtimeBetaLLMService
This commit is contained in:
@@ -122,6 +122,7 @@ class RealtimeError(BaseModel):
|
|||||||
code: Optional[str] = ""
|
code: Optional[str] = ""
|
||||||
message: str
|
message: str
|
||||||
param: Optional[str] = None
|
param: Optional[str] = None
|
||||||
|
event_id: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -23,8 +23,6 @@ except ModuleNotFoundError as e:
|
|||||||
)
|
)
|
||||||
raise Exception(f"Missing module: {e}")
|
raise Exception(f"Missing module: {e}")
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
from pipecat.frames.frames import (
|
from pipecat.frames.frames import (
|
||||||
BotStoppedSpeakingFrame,
|
BotStoppedSpeakingFrame,
|
||||||
CancelFrame,
|
CancelFrame,
|
||||||
@@ -130,14 +128,20 @@ class OpenAIRealtimeBetaLLMService(LLMService):
|
|||||||
|
|
||||||
async def retrieve_conversation_item(self, item_id: str):
|
async def retrieve_conversation_item(self, item_id: str):
|
||||||
future = self.get_event_loop().create_future()
|
future = self.get_event_loop().create_future()
|
||||||
retrieval_in_progress = False
|
retrieval_in_flight = False
|
||||||
if not self._retrieve_conversation_item_futures.get(item_id):
|
if not self._retrieve_conversation_item_futures.get(item_id):
|
||||||
self._retrieve_conversation_item_futures[item_id] = []
|
self._retrieve_conversation_item_futures[item_id] = []
|
||||||
else:
|
else:
|
||||||
retrieval_in_progress = True
|
retrieval_in_flight = True
|
||||||
self._retrieve_conversation_item_futures[item_id].append(future)
|
self._retrieve_conversation_item_futures[item_id].append(future)
|
||||||
if not retrieval_in_progress:
|
if not retrieval_in_flight:
|
||||||
await self.send_client_event(events.ConversationItemRetrieveEvent(item_id=item_id))
|
await self.send_client_event(
|
||||||
|
# Set event_id to "rci_{item_id}" so that we can identiy an
|
||||||
|
# error later if the retrieval fails. We don't need a UUID
|
||||||
|
# suffix to the event_id because we're ensuring only one
|
||||||
|
# in-flight retrieval per item_id
|
||||||
|
events.ConversationItemRetrieveEvent(item_id=item_id, event_id=f"rci_{item_id}")
|
||||||
|
)
|
||||||
return await future
|
return await future
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -543,11 +547,8 @@ class OpenAIRealtimeBetaLLMService(LLMService):
|
|||||||
Otherwise:
|
Otherwise:
|
||||||
- return false
|
- return false
|
||||||
"""
|
"""
|
||||||
match = re.match(
|
if evt.error.code == "item_retrieve_invalid_item_id":
|
||||||
r"^Error retrieving item: the item with id '(.*)' does not exist\.$", evt.error.message
|
item_id = evt.error.event_id.split("_", 1)[1] # event_id is of the form "rci_{item_id}"
|
||||||
)
|
|
||||||
if match:
|
|
||||||
item_id = match.group(1)
|
|
||||||
futures = self._retrieve_conversation_item_futures.pop(item_id, None)
|
futures = self._retrieve_conversation_item_futures.pop(item_id, None)
|
||||||
if futures:
|
if futures:
|
||||||
for future in futures:
|
for future in futures:
|
||||||
|
|||||||
Reference in New Issue
Block a user