Spaces:
Runtime error
Runtime error
File size: 4,027 Bytes
d4f6849 3f2f6d9 d4f6849 3f2f6d9 d4f6849 33fe7cc 00662bb 33fe7cc d4f6849 ad024aa 269b3c2 00662bb d4f6849 33fe7cc d4f6849 33fe7cc d4f6849 3f2f6d9 d4f6849 3f2f6d9 d4f6849 3f2f6d9 d4f6849 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import json
from src.utils import OpenAIClient
from src.config import logger
from ._bot_actions_service import BotActionService
class WebRTCService:
def __init__(self):
self.openai_client = OpenAIClient
self.bot_action_service = BotActionService()
async def __aenter__(self):
return self
async def __aexit__(self, *args):
pass
async def handle_voice_conversion(
self, websocket, conversation_id, user_id, conversation_history, redis_client
):
while True:
data = await websocket.receive_text()
message = json.loads(data)
# user_status = await redis_client.get(f"session:{conversation_id}")
# if not user_status:
# logger.error("User session has ended")
# raise Exception("A max 25 minute session has ended")
openai_server_events = {
"session_created": message["type"] == "session.created",
"function_call": (
message["type"] == "response.function_call_arguments.done"
),
"input_transcription": (
message["type"]
== "conversation.item.input_audio_transcription.completed"
),
"output_transcription": (
message["type"] == "response.done"
and message["response"]["status"] == "completed"
and message["response"]["output"][0]["type"] == "message"
),
}
if openai_server_events["session_created"]:
create_conversation_event = {
"type": "conversation.item.create",
"previous_item_id": None,
"item": {
"type": "message",
"role": "user",
"content": [
{
"type": "input_text",
"text": f"Conversation History: {conversation_history}\n\n",
}
],
},
}
await websocket.send_text(json.dumps(create_conversation_event))
await websocket.send_text(json.dumps({"type": "response.create"}))
if openai_server_events["function_call"]:
response = await self.bot_action_service.handle_ai_function_call(
data=message, user_id=user_id
)
event_response = {
"type": "conversation.item.create",
"previous_item_id": None,
"item": {
"type": "message",
"role": "user",
"content": [
{
"type": "input_text",
"text": response,
}
],
},
}
await websocket.send_text(json.dumps(event_response))
event_response_2 = {"type": "response.create"}
await websocket.send_text(json.dumps(event_response_2))
elif openai_server_events["input_transcription"]:
user_message = message["transcript"]
await self.bot_action_service.handle_user_message(
message_content=user_message, conversation_id=conversation_id
)
elif openai_server_events["output_transcription"]:
ai_response = message["response"]["output"][0]["content"][0][
"transcript"
]
await self.bot_action_service.handle_ai_message(
message_content=ai_response, conversation_id=conversation_id
)
elif message["type"] == "error":
logger.error(f"Error: {message['error']}")
|