Skip to content

Commit c2a4275

Browse files
committed
debugging streaming
1 parent 8a90003 commit c2a4275

File tree

2 files changed

+60
-59
lines changed

2 files changed

+60
-59
lines changed

mongodb-rag-docs/.docusaurus/client-manifest.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,9 @@
504504
"5354": {
505505
"js": [
506506
{
507-
"file": "assets/js/runtime~main.266052e3.js",
508-
"hash": "75a08fe72a9d2a9a",
509-
"publicPath": "/mongodb-rag/assets/js/runtime~main.266052e3.js"
507+
"file": "assets/js/runtime~main.d5dee0a9.js",
508+
"hash": "03a1c33c41e3b718",
509+
"publicPath": "/mongodb-rag/assets/js/runtime~main.d5dee0a9.js"
510510
}
511511
]
512512
},
@@ -621,9 +621,9 @@
621621
"7522": {
622622
"js": [
623623
{
624-
"file": "assets/js/e3e8e5bb.b2f6b0e6.js",
625-
"hash": "06409c248eb61e81",
626-
"publicPath": "/mongodb-rag/assets/js/e3e8e5bb.b2f6b0e6.js"
624+
"file": "assets/js/e3e8e5bb.7e7fac55.js",
625+
"hash": "4a21ad8447d1be8c",
626+
"publicPath": "/mongodb-rag/assets/js/e3e8e5bb.7e7fac55.js"
627627
}
628628
]
629629
},

mongodb-rag-docs/src/components/ChatbotInterface/index.js

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -224,74 +224,75 @@ export default function ChatbotInterface() {
224224
e.preventDefault();
225225
if (!input.trim()) return;
226226

227-
// Add user message
228227
const userMessage = { role: 'user', content: input };
229228
setMessages(msgs => [...msgs, userMessage]);
230229
setInput('');
231230
setIsLoading(true);
232-
setShowSampleQuestions(true);
233231

234232
try {
235-
// Try streaming first
236-
await handleStreamedChat(input, messages);
237-
} catch (error) {
238-
console.error('Streaming failed, falling back to regular chat:', error);
239-
240-
// Fallback to non-streaming implementation
241-
try {
242-
const response = await fetch('https://mongodb-rag-docs-backend.vercel.app/api/chat', {
243-
method: 'POST',
244-
headers: {
245-
'Content-Type': 'application/json',
246-
'Accept': 'application/json' // Request JSON response
247-
},
248-
body: JSON.stringify({
249-
query: input,
250-
sessionId: sessionId,
251-
history: !sessionId ? messages : undefined,
252-
stream: false
253-
})
254-
});
233+
const response = await fetch('https://mongodb-rag-docs-backend.vercel.app/api/chat', {
234+
method: 'POST',
235+
headers: { 'Content-Type': 'application/json' },
236+
body: JSON.stringify({
237+
query: input,
238+
sessionId: sessionId,
239+
history: !sessionId ? messages : undefined
240+
}),
241+
// Add request timeout
242+
signal: AbortSignal.timeout(45000) // 45 second timeout
243+
});
255244

256-
if (!response.ok) {
257-
throw new Error(`HTTP error! status: ${response.status}`);
258-
}
245+
if (!response.ok) {
246+
throw new Error(`HTTP error! status: ${response.status}`);
247+
}
259248

260-
const text = await response.text();
261-
const data = safeJsonParse(text);
262-
263-
if (!data) {
264-
throw new Error('Invalid response format');
265-
}
249+
const data = await response.json();
250+
251+
if (data.error) {
252+
throw new Error(data.error);
253+
}
266254

267-
if (data.sessionId && !sessionId) {
268-
setSessionId(data.sessionId);
255+
setMessages(msgs => [
256+
...msgs,
257+
{
258+
role: 'assistant',
259+
content: data.answer,
260+
sources: data.sources
269261
}
270-
271-
setMessages(msgs => [
272-
...msgs,
273-
{
274-
role: 'assistant',
275-
content: data.answer || 'Sorry, I received an invalid response.',
276-
sources: data.sources
277-
}
278-
]);
279-
} catch (error) {
280-
console.error('Chat error:', error);
281-
setMessages(msgs => [
282-
...msgs,
283-
{
284-
role: 'assistant',
285-
content: `Sorry, I encountered an error: ${error.message}`
286-
}
287-
]);
288-
}
262+
]);
263+
} catch (error) {
264+
handleError(error);
289265
} finally {
290266
setIsLoading(false);
291-
setCurrentStreamedMessage('');
292267
}
293268
};
269+
270+
const handleError = (error, fallback = true) => {
271+
console.error('Chat error:', error);
272+
273+
let errorMessage = 'Sorry, I encountered an error. Please try again later.';
274+
275+
if (error.response?.status === 504 || error.message?.includes('timed out')) {
276+
errorMessage = 'The search is taking longer than expected. You might try:' +
277+
'\n1. Rephrasing your question to be more specific' +
278+
'\n2. Breaking it into smaller parts' +
279+
'\n3. Trying again in a moment';
280+
}
281+
282+
setMessages(msgs => [
283+
...msgs,
284+
{
285+
role: 'assistant',
286+
content: errorMessage
287+
}
288+
]);
294289

290+
if (fallback && !error.message?.includes('timed out')) {
291+
// Optionally try fallback handling for non-timeout errors
292+
handleFallbackChat(input, messages);
293+
}
294+
};
295+
295296
const clearChat = () => {
296297
// Reset the chat
297298
setMessages([

0 commit comments

Comments
 (0)