Spaces:
Paused
Paused
Daniel Marques
commited on
Commit
β’
0e21fbd
1
Parent(s):
5d66516
feat: add broadcast
Browse files- .dockerignore +3 -1
- prompt_template_utils.py +0 -92
- websocket/__init__.py +0 -0
- redisPubSubManger.py β websocket/socketManager.py +73 -0
.dockerignore
CHANGED
@@ -2,8 +2,10 @@
|
|
2 |
!*.py
|
3 |
!*.txt
|
4 |
!*.html
|
|
|
5 |
!requirements.txt
|
6 |
!SOURCE_DOCUMENTS
|
7 |
!static
|
8 |
-
|
|
|
9 |
|
|
|
2 |
!*.py
|
3 |
!*.txt
|
4 |
!*.html
|
5 |
+
!*.sh
|
6 |
!requirements.txt
|
7 |
!SOURCE_DOCUMENTS
|
8 |
!static
|
9 |
+
!websocket
|
10 |
+
|
11 |
|
prompt_template_utils.py
DELETED
@@ -1,92 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
This file implements prompt template for llama based models.
|
3 |
-
Modify the prompt template based on the model you select.
|
4 |
-
This seems to have significant impact on the output of the LLM.
|
5 |
-
"""
|
6 |
-
|
7 |
-
from langchain.memory import ConversationBufferMemory
|
8 |
-
from langchain.prompts import PromptTemplate
|
9 |
-
|
10 |
-
# this is specific to Llama-2.
|
11 |
-
|
12 |
-
# system_prompt = """You are a helpful assistant, you will use the context and documents provided in the training to answer users questions.
|
13 |
-
# Read the context provided before answering questions and think step by step. If you can't answer a user's question based on the
|
14 |
-
# context provided, inform the user. Don't use any other information to answer the user."""
|
15 |
-
|
16 |
-
# system_prompt = """You are a helpful assistant, and you will use the context and documents provided in the training to answer users' questions. Please read the context provided carefully before responding to questions and follow a step-by-step thought process. If you cannot answer a user's question based on the provided context, please inform the user. Do not use any other information to answer the user. Provide a detailed response based on the content of locally trained documents."""
|
17 |
-
|
18 |
-
system_prompt = """It's a useful assistant who will use the context and documents provided in the training to answer users' questions.
|
19 |
-
Read the context provided before answering the questions and think step by step. If you can't answer, just say "I don't know" and don't try to put together an answer to respond to the user."""
|
20 |
-
|
21 |
-
def get_prompt_template(system_prompt=system_prompt, promptTemplate_type=None, history=False):
|
22 |
-
if promptTemplate_type == "llama":
|
23 |
-
B_INST, E_INST = "[INST]", "[/INST]"
|
24 |
-
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
25 |
-
SYSTEM_PROMPT = B_SYS + system_prompt + E_SYS
|
26 |
-
if history:
|
27 |
-
instruction = """
|
28 |
-
Context: {history} \n {context}
|
29 |
-
User: {question}"""
|
30 |
-
|
31 |
-
prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
|
32 |
-
prompt = PromptTemplate(input_variables=["history", "context", "question"], template=prompt_template)
|
33 |
-
else:
|
34 |
-
instruction = """
|
35 |
-
Context: {context}
|
36 |
-
User: {question}"""
|
37 |
-
|
38 |
-
prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
|
39 |
-
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
|
40 |
-
elif promptTemplate_type == "mistral":
|
41 |
-
B_INST, E_INST = "<s>[INST] ", " [/INST]"
|
42 |
-
if history:
|
43 |
-
prompt_template = (
|
44 |
-
B_INST
|
45 |
-
+ system_prompt
|
46 |
-
+ """
|
47 |
-
|
48 |
-
Context: {history} \n {context}
|
49 |
-
User: {question}"""
|
50 |
-
+ E_INST
|
51 |
-
)
|
52 |
-
prompt = PromptTemplate(input_variables=["history", "context", "question"], template=prompt_template)
|
53 |
-
else:
|
54 |
-
prompt_template = (
|
55 |
-
B_INST
|
56 |
-
+ system_prompt
|
57 |
-
+ """
|
58 |
-
|
59 |
-
Context: {context}
|
60 |
-
User: {question}"""
|
61 |
-
+ E_INST
|
62 |
-
)
|
63 |
-
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
|
64 |
-
else:
|
65 |
-
# change this based on the model you have selected.
|
66 |
-
if history:
|
67 |
-
prompt_template = (
|
68 |
-
system_prompt
|
69 |
-
+ """
|
70 |
-
|
71 |
-
Context: {history} \n {context}
|
72 |
-
User: {question}
|
73 |
-
Answer:"""
|
74 |
-
)
|
75 |
-
prompt = PromptTemplate(input_variables=["history", "context", "question"], template=prompt_template)
|
76 |
-
else:
|
77 |
-
prompt_template = (
|
78 |
-
system_prompt
|
79 |
-
+ """
|
80 |
-
|
81 |
-
Context: {context}
|
82 |
-
User: {question}
|
83 |
-
Answer:"""
|
84 |
-
)
|
85 |
-
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
|
86 |
-
|
87 |
-
memory = ConversationBufferMemory(input_key="question", memory_key="history")
|
88 |
-
|
89 |
-
return (
|
90 |
-
prompt,
|
91 |
-
memory,
|
92 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
websocket/__init__.py
ADDED
File without changes
|
redisPubSubManger.py β websocket/socketManager.py
RENAMED
@@ -67,3 +67,76 @@ class RedisPubSubManager:
|
|
67 |
room_id (str): Channel or room ID to unsubscribe from.
|
68 |
"""
|
69 |
await self.pubsub.unsubscribe(room_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
room_id (str): Channel or room ID to unsubscribe from.
|
68 |
"""
|
69 |
await self.pubsub.unsubscribe(room_id)
|
70 |
+
|
71 |
+
|
72 |
+
class WebSocketManager:
|
73 |
+
|
74 |
+
def __init__(self):
|
75 |
+
"""
|
76 |
+
Initializes the WebSocketManager.
|
77 |
+
|
78 |
+
Attributes:
|
79 |
+
rooms (dict): A dictionary to store WebSocket connections in different rooms.
|
80 |
+
pubsub_client (RedisPubSubManager): An instance of the RedisPubSubManager class for pub-sub functionality.
|
81 |
+
"""
|
82 |
+
self.rooms: dict = {}
|
83 |
+
self.pubsub_client = RedisPubSubManager()
|
84 |
+
|
85 |
+
async def add_user_to_room(self, room_id: str, websocket: WebSocket) -> None:
|
86 |
+
"""
|
87 |
+
Adds a user's WebSocket connection to a room.
|
88 |
+
|
89 |
+
Args:
|
90 |
+
room_id (str): Room ID or channel name.
|
91 |
+
websocket (WebSocket): WebSocket connection object.
|
92 |
+
"""
|
93 |
+
await websocket.accept()
|
94 |
+
|
95 |
+
if room_id in self.rooms:
|
96 |
+
self.rooms[room_id].append(websocket)
|
97 |
+
else:
|
98 |
+
self.rooms[room_id] = [websocket]
|
99 |
+
|
100 |
+
await self.pubsub_client.connect()
|
101 |
+
pubsub_subscriber = await self.pubsub_client.subscribe(room_id)
|
102 |
+
asyncio.create_task(self._pubsub_data_reader(pubsub_subscriber))
|
103 |
+
|
104 |
+
async def broadcast_to_room(self, room_id: str, message: str) -> None:
|
105 |
+
"""
|
106 |
+
Broadcasts a message to all connected WebSockets in a room.
|
107 |
+
|
108 |
+
Args:
|
109 |
+
room_id (str): Room ID or channel name.
|
110 |
+
message (str): Message to be broadcasted.
|
111 |
+
"""
|
112 |
+
await self.pubsub_client._publish(room_id, message)
|
113 |
+
|
114 |
+
async def remove_user_from_room(self, room_id: str, websocket: WebSocket) -> None:
|
115 |
+
"""
|
116 |
+
Removes a user's WebSocket connection from a room.
|
117 |
+
|
118 |
+
Args:
|
119 |
+
room_id (str): Room ID or channel name.
|
120 |
+
websocket (WebSocket): WebSocket connection object.
|
121 |
+
"""
|
122 |
+
self.rooms[room_id].remove(websocket)
|
123 |
+
|
124 |
+
if len(self.rooms[room_id]) == 0:
|
125 |
+
del self.rooms[room_id]
|
126 |
+
await self.pubsub_client.unsubscribe(room_id)
|
127 |
+
|
128 |
+
async def _pubsub_data_reader(self, pubsub_subscriber):
|
129 |
+
"""
|
130 |
+
Reads and broadcasts messages received from Redis PubSub.
|
131 |
+
|
132 |
+
Args:
|
133 |
+
pubsub_subscriber (aioredis.ChannelSubscribe): PubSub object for the subscribed channel.
|
134 |
+
"""
|
135 |
+
while True:
|
136 |
+
message = await pubsub_subscriber.get_message(ignore_subscribe_messages=True)
|
137 |
+
if message is not None:
|
138 |
+
room_id = message['channel'].decode('utf-8')
|
139 |
+
all_sockets = self.rooms[room_id]
|
140 |
+
for socket in all_sockets:
|
141 |
+
data = message['data'].decode('utf-8')
|
142 |
+
await socket.send_text(data)
|