Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -37,10 +37,12 @@ async def run_all(messages):
|
|
37 |
async def clean_chat_history():
|
38 |
while True:
|
39 |
current_time = datetime.now()
|
40 |
-
|
41 |
-
|
|
|
42 |
del chat_history[session_id]
|
43 |
await asyncio.sleep(60)
|
|
|
44 |
#-------------------------------------------------------------------------------
|
45 |
@app.get("/gpt")
|
46 |
async def chat(request: Request, prompt: str, characteristic: str = None, id: str = None):
|
@@ -70,17 +72,17 @@ async def chat(request: Request, prompt: str, characteristic: str = None, id: st
|
|
70 |
else:
|
71 |
messages = chat_history[id]
|
72 |
messages.append({"role": "user", "content": prompt})
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
except Exception as e:
|
85 |
raise HTTPException(status_code=401, detail=f"Internal Error: {str(e)}")
|
86 |
|
@@ -94,4 +96,4 @@ async def history_chat(id: str):
|
|
94 |
if id not in chat_history:
|
95 |
raise HTTPException(status_code=404, detail="Session ID not found")
|
96 |
|
97 |
-
return {"history": chat_history[id]}
|
|
|
37 |
async def clean_chat_history():
|
38 |
while True:
|
39 |
current_time = datetime.now()
|
40 |
+
# Use list() to create a copy of keys to avoid "dictionary changed size during iteration" issue
|
41 |
+
for session_id in list(chat_history.keys()):
|
42 |
+
if chat_history[session_id][-1]['timestamp'] + timedelta(minutes=10) < current_time:
|
43 |
del chat_history[session_id]
|
44 |
await asyncio.sleep(60)
|
45 |
+
|
46 |
#-------------------------------------------------------------------------------
|
47 |
@app.get("/gpt")
|
48 |
async def chat(request: Request, prompt: str, characteristic: str = None, id: str = None):
|
|
|
72 |
else:
|
73 |
messages = chat_history[id]
|
74 |
messages.append({"role": "user", "content": prompt})
|
75 |
+
|
76 |
+
# executed after adding from else or if
|
77 |
+
messages = chat_history[id]
|
78 |
+
responses = await run_all(messages)
|
79 |
+
if responses[0]:
|
80 |
+
asyncio.create_task(clean_chat_history())
|
81 |
+
messages.append({"role": "assistant", "content": responses[0]})
|
82 |
+
return {"response": responses[0]}
|
83 |
+
else:
|
84 |
+
asyncio.create_task(clean_chat_history())
|
85 |
+
raise HTTPException(status_code=500, detail="Provider failed")
|
86 |
except Exception as e:
|
87 |
raise HTTPException(status_code=401, detail=f"Internal Error: {str(e)}")
|
88 |
|
|
|
96 |
if id not in chat_history:
|
97 |
raise HTTPException(status_code=404, detail="Session ID not found")
|
98 |
|
99 |
+
return {"history": chat_history[id]}
|