Spaces:
Runtime error
Runtime error
Commit
·
33fe7cc
1
Parent(s):
4eac4ee
Add conversation history handling to WebRTC and WebSocket services
Browse files
src/services/_conversation_service.py
CHANGED
|
@@ -35,6 +35,26 @@ class ConversationService:
|
|
| 35 |
return False
|
| 36 |
return True
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
async def create_conversation(self, user_id, modality):
|
| 39 |
user_object = await self.user_repository.get_by_id(user_id)
|
| 40 |
conversation_object = await self.conversation_repository.insert_one(
|
|
@@ -71,15 +91,25 @@ class ConversationService:
|
|
| 71 |
await websocket.close(code=1008, reason="Unsupported modality")
|
| 72 |
raise Exception("Unsupported modality")
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
if modality == "voice":
|
| 75 |
async with self.web_rtc_service() as rtc_service:
|
| 76 |
await rtc_service.handle_voice_conversion(
|
| 77 |
-
websocket=websocket,
|
|
|
|
|
|
|
|
|
|
| 78 |
)
|
| 79 |
else:
|
| 80 |
async with self.websocket_service() as ws_service:
|
| 81 |
await ws_service.handle_text_conversation_with_openai_websockets(
|
| 82 |
-
client_websocket=websocket,
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
async def create_conversation_summary(self, user_id, conversation_id):
|
|
@@ -89,21 +119,23 @@ class ConversationService:
|
|
| 89 |
conversation_object: Conversation = (
|
| 90 |
await self.conversation_repository.get_by_id(conversation_id)
|
| 91 |
)
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
conversation_object
|
| 95 |
-
)
|
| 96 |
)
|
| 97 |
|
| 98 |
-
conversation_history
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
async with self.openai_client() as client:
|
| 103 |
conversation_summary = await client.text_generation(
|
| 104 |
query=f"Generate Conversation Summary\n\n {conversation_history}"
|
| 105 |
)
|
| 106 |
-
|
| 107 |
conversation_object.summary = conversation_summary
|
| 108 |
updated_converation: Conversation = await self.conversation_repository.update(
|
| 109 |
conversation_object.id, conversation_object
|
|
|
|
| 35 |
return False
|
| 36 |
return True
|
| 37 |
|
| 38 |
+
async def get_conversation_history(self, conversation_id, find_all=False, limit=20):
|
| 39 |
+
conversation_object: Conversation = (
|
| 40 |
+
await self.conversation_repository.get_by_id(conversation_id)
|
| 41 |
+
)
|
| 42 |
+
messages: list[Message] = (
|
| 43 |
+
await self.message_repository.get_messages_by_conversation(
|
| 44 |
+
conversation_object
|
| 45 |
+
)
|
| 46 |
+
)
|
| 47 |
+
if len(messages) == 0:
|
| 48 |
+
return {"conversation_history": ""}
|
| 49 |
+
|
| 50 |
+
if not find_all:
|
| 51 |
+
messages = messages[-limit:]
|
| 52 |
+
|
| 53 |
+
conversation_history = "\n".join(
|
| 54 |
+
[f"{message.role}: {message.content}" for message in messages]
|
| 55 |
+
)
|
| 56 |
+
return {"conversation_history": conversation_history}
|
| 57 |
+
|
| 58 |
async def create_conversation(self, user_id, modality):
|
| 59 |
user_object = await self.user_repository.get_by_id(user_id)
|
| 60 |
conversation_object = await self.conversation_repository.insert_one(
|
|
|
|
| 91 |
await websocket.close(code=1008, reason="Unsupported modality")
|
| 92 |
raise Exception("Unsupported modality")
|
| 93 |
|
| 94 |
+
conversation_history = await self.get_conversation_history(
|
| 95 |
+
conversation_id, find_all=False, limit=20
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
if modality == "voice":
|
| 99 |
async with self.web_rtc_service() as rtc_service:
|
| 100 |
await rtc_service.handle_voice_conversion(
|
| 101 |
+
websocket=websocket,
|
| 102 |
+
conversation_id=conversation_id,
|
| 103 |
+
conversation_history=conversation_history,
|
| 104 |
+
user_id=user_id,
|
| 105 |
)
|
| 106 |
else:
|
| 107 |
async with self.websocket_service() as ws_service:
|
| 108 |
await ws_service.handle_text_conversation_with_openai_websockets(
|
| 109 |
+
client_websocket=websocket,
|
| 110 |
+
conversation_id=conversation_id,
|
| 111 |
+
conversation_history=conversation_history,
|
| 112 |
+
user_id=user_id,
|
| 113 |
)
|
| 114 |
|
| 115 |
async def create_conversation_summary(self, user_id, conversation_id):
|
|
|
|
| 119 |
conversation_object: Conversation = (
|
| 120 |
await self.conversation_repository.get_by_id(conversation_id)
|
| 121 |
)
|
| 122 |
+
conversation_history = await self.get_conversation_history(
|
| 123 |
+
conversation_id, find_all=True
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
+
if conversation_history["conversation_history"] == "":
|
| 127 |
+
conversation_object.summary = ""
|
| 128 |
+
updated_converation: Conversation = (
|
| 129 |
+
await self.conversation_repository.update(
|
| 130 |
+
conversation_object.id, conversation_object
|
| 131 |
+
)
|
| 132 |
+
)
|
| 133 |
+
return {"conversation_summary": updated_converation.summary}
|
| 134 |
|
| 135 |
async with self.openai_client() as client:
|
| 136 |
conversation_summary = await client.text_generation(
|
| 137 |
query=f"Generate Conversation Summary\n\n {conversation_history}"
|
| 138 |
)
|
|
|
|
| 139 |
conversation_object.summary = conversation_summary
|
| 140 |
updated_converation: Conversation = await self.conversation_repository.update(
|
| 141 |
conversation_object.id, conversation_object
|
src/services/_web_rtc_service.py
CHANGED
|
@@ -17,12 +17,15 @@ class WebRTCService:
|
|
| 17 |
async def __aexit__(self, *args):
|
| 18 |
pass
|
| 19 |
|
| 20 |
-
async def handle_voice_conversion(
|
|
|
|
|
|
|
| 21 |
while True:
|
| 22 |
data = await websocket.receive_text()
|
| 23 |
message = json.loads(data)
|
| 24 |
|
| 25 |
openai_server_events = {
|
|
|
|
| 26 |
"function_call": (
|
| 27 |
message["type"] == "response.function_call_arguments.done"
|
| 28 |
),
|
|
@@ -37,6 +40,24 @@ class WebRTCService:
|
|
| 37 |
),
|
| 38 |
}
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if openai_server_events["function_call"]:
|
| 41 |
response = await self.bot_action_service.handle_ai_function_call(
|
| 42 |
data=message, user_id=user_id
|
|
|
|
| 17 |
async def __aexit__(self, *args):
|
| 18 |
pass
|
| 19 |
|
| 20 |
+
async def handle_voice_conversion(
|
| 21 |
+
self, websocket, conversation_id, user_id, conversation_history
|
| 22 |
+
):
|
| 23 |
while True:
|
| 24 |
data = await websocket.receive_text()
|
| 25 |
message = json.loads(data)
|
| 26 |
|
| 27 |
openai_server_events = {
|
| 28 |
+
"session_created": message["type"] == "session.created",
|
| 29 |
"function_call": (
|
| 30 |
message["type"] == "response.function_call_arguments.done"
|
| 31 |
),
|
|
|
|
| 40 |
),
|
| 41 |
}
|
| 42 |
|
| 43 |
+
if openai_server_events["session_created"]:
|
| 44 |
+
create_conversation_event = {
|
| 45 |
+
"type": "conversation.item.create",
|
| 46 |
+
"previous_item_id": None,
|
| 47 |
+
"item": {
|
| 48 |
+
"type": "message",
|
| 49 |
+
"role": "user",
|
| 50 |
+
"content": [
|
| 51 |
+
{
|
| 52 |
+
"type": "input_text",
|
| 53 |
+
"text": f"Conversation History: {conversation_history}\n\n",
|
| 54 |
+
}
|
| 55 |
+
],
|
| 56 |
+
},
|
| 57 |
+
}
|
| 58 |
+
await websocket.send_text(json.dumps(create_conversation_event))
|
| 59 |
+
await websocket.send_text(json.dumps({"type": "response.create"}))
|
| 60 |
+
|
| 61 |
if openai_server_events["function_call"]:
|
| 62 |
response = await self.bot_action_service.handle_ai_function_call(
|
| 63 |
data=message, user_id=user_id
|
src/services/_websocket_service.py
CHANGED
|
@@ -21,7 +21,7 @@ class WebSocketService:
|
|
| 21 |
pass
|
| 22 |
|
| 23 |
async def handle_text_conversation_with_openai_websockets(
|
| 24 |
-
self, client_websocket, conversation_id, user_id
|
| 25 |
):
|
| 26 |
openai_websocket = None
|
| 27 |
async with websockets.connect(
|
|
@@ -112,11 +112,18 @@ class WebSocketService:
|
|
| 112 |
|
| 113 |
receiver_task = asyncio.create_task(openai_receiver())
|
| 114 |
|
|
|
|
| 115 |
while True:
|
| 116 |
user_message = await client_websocket.receive_text()
|
| 117 |
user_message = json.loads(user_message)["user_message"]
|
|
|
|
| 118 |
await self.bot_action_service.handle_user_message(
|
| 119 |
message_content=user_message,
|
| 120 |
conversation_id=conversation_id,
|
| 121 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
await openai_sender(user_message)
|
|
|
|
| 21 |
pass
|
| 22 |
|
| 23 |
async def handle_text_conversation_with_openai_websockets(
|
| 24 |
+
self, client_websocket, conversation_id, user_id, conversation_history
|
| 25 |
):
|
| 26 |
openai_websocket = None
|
| 27 |
async with websockets.connect(
|
|
|
|
| 112 |
|
| 113 |
receiver_task = asyncio.create_task(openai_receiver())
|
| 114 |
|
| 115 |
+
conversation_history_sent = False
|
| 116 |
while True:
|
| 117 |
user_message = await client_websocket.receive_text()
|
| 118 |
user_message = json.loads(user_message)["user_message"]
|
| 119 |
+
|
| 120 |
await self.bot_action_service.handle_user_message(
|
| 121 |
message_content=user_message,
|
| 122 |
conversation_id=conversation_id,
|
| 123 |
)
|
| 124 |
+
|
| 125 |
+
if not conversation_history_sent:
|
| 126 |
+
user_message = f"{conversation_history}\n{user_message}"
|
| 127 |
+
conversation_history_sent = True
|
| 128 |
+
|
| 129 |
await openai_sender(user_message)
|