mitulagr2 commited on
Commit
d04ea2c
1 Parent(s): 1e8af32

stream response

Browse files
Files changed (2) hide show
  1. app/main.py +8 -23
  2. app/rag.py +2 -7
app/main.py CHANGED
@@ -45,38 +45,23 @@ manager = ConnectionManager()
45
 
46
  @app.websocket("/ws/{client_id}")
47
  async def websocket_endpoint(websocket: WebSocket, client_id: int):
48
- print("CONNECTING")
49
  await manager.connect(websocket)
50
- print("1")
51
  now = datetime.now()
52
  current_time = now.strftime("%H:%M")
53
- print("2")
54
  try:
55
  while True:
56
- print("3")
57
- data = await websocket.receive_text()
58
- # print("4")
59
- # await manager.send_personal_message(f"You wrote: {data}", websocket)
60
- print("5")
61
- message = {"time":current_time,"clientId":client_id,"message":data}
62
- print("6")
63
- await manager.broadcast(json.dumps(message))
64
- print("7")
65
-
66
  except WebSocketDisconnect:
67
- print("DISCONNECTING")
68
  manager.disconnect(websocket)
69
- print("1")
70
  message = {"time":current_time,"clientId":client_id,"message":"Offline"}
71
- print("2")
72
  await manager.broadcast(json.dumps(message))
73
- print("3")
74
-
75
- @app.get("/query")
76
- async def process_input(text: str):
77
- if text and len(text.strip()) > 0:
78
- text = text.strip()
79
- return StreamingResponse(session_assistant.ask(text), media_type='text/event-stream')
80
 
81
 
82
  @app.post("/upload")
 
45
 
46
  @app.websocket("/ws/{client_id}")
47
  async def websocket_endpoint(websocket: WebSocket, client_id: int):
 
48
  await manager.connect(websocket)
 
49
  now = datetime.now()
50
  current_time = now.strftime("%H:%M")
 
51
  try:
52
  while True:
53
+ data = await websocket.receive_text().strip()
54
+ if len(data) > 0:
55
+ generator = session_assistant.ask(data)
56
+ for text in generator
57
+ message = {"time":current_time,"clientId":client_id,"message":text}
58
+ # await manager.broadcast(json.dumps(message))
59
+ await manager.send_personal_message(json.dumps(message), websocket)
 
 
 
60
  except WebSocketDisconnect:
 
61
  manager.disconnect(websocket)
 
62
  message = {"time":current_time,"clientId":client_id,"message":"Offline"}
 
63
  await manager.broadcast(json.dumps(message))
64
+ # return StreamingResponse(, media_type='text/event-stream')
 
 
 
 
 
 
65
 
66
 
67
  @app.post("/upload")
app/rag.py CHANGED
@@ -102,15 +102,10 @@ class ChatPDF:
102
  similarity_top_k=6,
103
  )
104
 
105
- async def ask(self, query: str):
106
  logger.info("retrieving the response to the query")
107
  streaming_response = self.query_engine.query(query)
108
- print(streaming_response)
109
- print("PRINTING STREAM")
110
- generator = streaming_response.response_gen
111
- for text in generator:
112
- print(text)
113
- yield text
114
 
115
  def clear(self):
116
  pass
 
102
  similarity_top_k=6,
103
  )
104
 
105
+ def ask(self, query: str):
106
  logger.info("retrieving the response to the query")
107
  streaming_response = self.query_engine.query(query)
108
+ return streaming_response.response_gen
 
 
 
 
 
109
 
110
  def clear(self):
111
  pass