Skip to content

Commit d29176c

Browse files
committed
Refactor chat stream endpoint and update HTML for query parameter handling
- Changed `/chat/stream` endpoint from POST to GET and updated user message retrieval to support query parameters. - Improved error handling for missing user messages by checking session history. - Updated `chat.html` to encode user messages for URL query parameters and streamline EventSource creation. - Enhanced user experience by ensuring proper message handling and session updates during streaming.
1 parent 24351aa commit d29176c

2 files changed

Lines changed: 14 additions & 26 deletions

File tree

src/deepseek_wrapper/templates/chat.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@
5454
if (btn) btn.disabled = true;
5555
if (sendingStatus) sendingStatus.style.display = 'block';
5656

57-
const formData = new FormData();
58-
formData.append('user_message', input.value);
59-
60-
const userMessage = input.value;
57+
const userMessageValue = input.value; // Capture value before clearing
6158
input.value = '';
6259
autoResizeTextarea(input); // Reset height after clearing
6360

64-
const eventSource = new EventSource(`/chat/stream?_=${Date.now()}`, {
61+
// Encode user_message for URL query parameter
62+
const encodedUserMessage = encodeURIComponent(userMessageValue);
63+
64+
// Create and handle the EventSource for streaming
65+
const eventSource = new EventSource(`/chat/stream?user_message=${encodedUserMessage}&_=${Date.now()}`, {
6566
withCredentials: true
6667
});
6768

src/deepseek_wrapper/web.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,61 +115,48 @@ async def upload_file(file: UploadFile = File(...), request: Request = None):
115115
request.session["history"] = history
116116
return JSONResponse({"filename": file.filename, "content_type": file.content_type})
117117

118-
@app.post("/chat/stream")
119-
async def chat_stream(request: Request):
118+
@app.get("/chat/stream")
119+
async def chat_stream(request: Request, user_message: str = None):
120120
"""Stream chat responses using Server-Sent Events."""
121121
try:
122-
# Get form data from request
123-
form_data = await request.form()
124-
user_message = form_data.get("user_message")
125-
126122
if not user_message:
127-
return JSONResponse({"error": "No message provided"}, status_code=400)
123+
history_check = request.session.get("history", [])
124+
if history_check and history_check[-1]["role"] == "user":
125+
user_message = history_check[-1]["content"]
126+
else:
127+
return JSONResponse({"error": "No message provided in query parameters"}, status_code=400)
128128

129-
# Get history from session
130129
history = request.session.get("history", [])
131130

132-
# Add user message to history
133131
user_msg = {"role": "user", "content": user_message, "timestamp": now_str()}
134132
history.append(user_msg)
135133

136-
# Update session with user message
137134
request.session["history"] = history
138135

139-
# Create an event stream generator
140136
async def event_generator():
141-
# Send user message confirmation first
142137
yield f"data: {json.dumps({'type': 'user_msg_received', 'message': user_msg})}\n\n"
143-
await asyncio.sleep(0.1) # Small delay to allow frontend rendering
138+
await asyncio.sleep(0.1)
144139

145-
# Start assistant message
146140
assistant_msg = {"role": "assistant", "content": "", "timestamp": now_str()}
147141
yield f"data: {json.dumps({'type': 'assistant_msg_start', 'message': assistant_msg})}\n\n"
148142

149143
try:
150-
# Start streaming response
151144
collected_content = []
152145
async for content_chunk in client.async_chat_completion_stream(history):
153-
# Add chunk to the collected content
154146
collected_content.append(content_chunk)
155147
assistant_msg["content"] = ''.join(collected_content)
156148

157-
# Send the chunk to the client
158149
yield f"data: {json.dumps({'type': 'content_chunk', 'chunk': content_chunk})}\n\n"
159150

160-
# Stream is complete - update the history in the session
161151
complete_msg = {"role": "assistant", "content": ''.join(collected_content), "timestamp": now_str()}
162152
history.append(complete_msg)
163153
request.session["history"] = history
164154

165-
# Send completion message
166155
yield f"data: {json.dumps({'type': 'complete', 'message': complete_msg})}\n\n"
167156
except Exception as e:
168-
# Handle errors during streaming
169157
error_msg = str(e)
170158
yield f"data: {json.dumps({'type': 'error', 'error': error_msg})}\n\n"
171159

172-
# Add error message to history
173160
error_response = f"Error: {error_msg}"
174161
history.append({"role": "assistant", "content": error_response, "timestamp": now_str()})
175162
request.session["history"] = history

0 commit comments

Comments
 (0)