MM58-crypto commited on
Commit
60db93f
·
1 Parent(s): 5643ebb

wooo another commit

Browse files
Files changed (1) hide show
  1. main.py +7 -7
main.py CHANGED
@@ -163,9 +163,9 @@ async def user_session_management(request: Request, call_next):
163
  session = None
164
 
165
  # this if statement is not running OR session_id is never saved and found on mongodb
166
- if session:
167
  retrieved_session = collection.find_one({"_id": session_id})
168
- print("FOUND SESSION:", session is not None)
169
  if retrieved_session:
170
  collection.update_one(
171
  {"_id": session_id},
@@ -175,7 +175,7 @@ async def user_session_management(request: Request, call_next):
175
  #else:
176
  # session_id = None
177
 
178
- if session is None:
179
  try:
180
  session_id = str(uuid.uuid4())
181
  print("CREATING NEW SESSION:", session_id)
@@ -186,12 +186,12 @@ async def user_session_management(request: Request, call_next):
186
  "messages": []
187
  }
188
  usr_doc = collection.insert_one(session)
189
- #new_session = True
190
 
191
  except Exception as e:
192
  print("An error occured when trying to generate a session id / create new session: ", e)
193
 
194
- request.state.session = session
195
  # forward request to actual endpoint and get the response
196
  response = await call_next(request)
197
 
@@ -233,14 +233,14 @@ async def chat_stream(req: ChatRequest, request: Request):
233
  data: <token>\n\n — text to append
234
  data: [DONE]\n\n — end of stream
235
  """
236
- session = request.state.session
237
 
238
  async def token_generator():
239
  # Run the blocking pipeline call in a thread pool so FastAPI
240
  # stays non-blocking for other concurrent requests
241
  loop = asyncio.get_event_loop()
242
  full_reply = await loop.run_in_executor(
243
- None, run_query, req.message, req.intent, session["_id"]
244
  )
245
  # via mongodb api, insert the msgs and replies into the docs
246
  # then in haystack_pipeline.py retrieve the docs and inject them into the prompt
 
163
  session = None
164
 
165
  # this if statement is not running OR session_id is never saved and found on mongodb
166
+ if session_id is not None:
167
  retrieved_session = collection.find_one({"_id": session_id})
168
+ print("FOUND SESSION:", session_id is not None)
169
  if retrieved_session:
170
  collection.update_one(
171
  {"_id": session_id},
 
175
  #else:
176
  # session_id = None
177
 
178
+ if session_id is None:
179
  try:
180
  session_id = str(uuid.uuid4())
181
  print("CREATING NEW SESSION:", session_id)
 
186
  "messages": []
187
  }
188
  usr_doc = collection.insert_one(session)
189
+
190
 
191
  except Exception as e:
192
  print("An error occured when trying to generate a session id / create new session: ", e)
193
 
194
+ request.state.session_id = session_id
195
  # forward request to actual endpoint and get the response
196
  response = await call_next(request)
197
 
 
233
  data: <token>\n\n — text to append
234
  data: [DONE]\n\n — end of stream
235
  """
236
+ session_id = request.state.session_id
237
 
238
  async def token_generator():
239
  # Run the blocking pipeline call in a thread pool so FastAPI
240
  # stays non-blocking for other concurrent requests
241
  loop = asyncio.get_event_loop()
242
  full_reply = await loop.run_in_executor(
243
+ None, run_query, req.message, req.intent, session_id
244
  )
245
  # via mongodb api, insert the msgs and replies into the docs
246
  # then in haystack_pipeline.py retrieve the docs and inject them into the prompt