You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is an example if you already have a backend application taking care of generating the chatbot's responses and you want to integrate it with this `Chatbot` component:
223
+
Here is a complete example of how to set up a local running demo with backend integration for the `Chatbot` component:
224
224
225
-
First, we will set a new state `gettingResponse` that will indicate us if we are currently fetching a response from the backend:
225
+
==== Step 1: Configure Vite Proxy
226
226
227
-
[source, tsx]
227
+
First, update your `vite.config.ts` to add a proxy configuration for your backend API:
This configuration will proxy any requests to `/ask` to your backend server running on `http://localhost:8001`.
231
249
232
-
Then, we will define a new function `fetchResponseFromAPI` that will be responsible for fetching the chatbot's response from the backend based on the user's message:
250
+
==== Step 2: Update handleSubmit Function
251
+
252
+
Replace the existing `handleSubmit` function in your `Chatbot` component with this implementation that calls your backend API:
233
253
234
254
[source, tsx]
235
255
----
236
-
const fetchResponseFromAPI = async () => {
237
-
setGettingResponse(true);
238
-
const requestBody = {
239
-
message: inputMessage,
240
-
sessionId: currentSession?.id, // Include session context
src: data.sources || [], // Include source references
257
-
};
258
-
} catch (error) {
259
-
console.error("API call failed:", error);
260
-
return {
261
-
response: "Sorry, something went wrong.",
262
-
src: [],
263
-
};
264
-
} finally {
265
-
setGettingResponse(false);
266
-
}
267
-
};
300
+
setIsLoading(false);
301
+
simulateTypingEffect(chatbotReply);
302
+
303
+
} catch (error) {
304
+
console.error("API call failed:", error);
305
+
306
+
// Fallback response in case of error
307
+
const errorReply = {
308
+
response: 'Sorry, I encountered an error while processing your request. Please try again.',
309
+
src: [],
310
+
};
311
+
312
+
setIsLoading(false);
313
+
simulateTypingEffect(errorReply);
314
+
}
315
+
};
268
316
----
269
317
270
-
WARNING: Ideally you will want to consider using a framework to manage the states, caching and hooks like `tanstack/react-query` for example as well as adding an authentication and authorization to your backend API calls
This replaces the mock data with actual Neo4j query results for source visualization.
341
+
342
+
==== Backend API Requirements
343
+
344
+
Your backend API endpoint (`/ask`) should accept a POST request with this structure:
345
+
346
+
[source, json]
347
+
----
348
+
{
349
+
"question": "User's question text",
350
+
"session_id": "optional-session-identifier"
351
+
}
352
+
----
353
+
354
+
And return a response in this format:
355
+
356
+
[source, json]
357
+
----
358
+
{
359
+
"response": "Chatbot's response text",
360
+
"src": ["source1", "source2", "source3"]
361
+
}
362
+
----
363
+
364
+
WARNING: For production use, consider implementing proper authentication and authorization for your backend API calls, as well as using state management libraries like `@tanstack/react-query` for better caching and error handling.
365
+
281
366
=== Session-Aware Backend Integration
282
367
283
368
For more advanced use cases, you can send session context to your backend to maintain conversation history and provide more contextual responses:
0 commit comments