import React, { useState, useEffect } from "react"; import axios from "axios"; const…
- User
import React, { useState, useEffect } from "react"; import axios from "axios";
const Chatbot = () => { const [messages, setMessages] = useState(() => { const savedMessages = localStorage.getItem("chatMessages"); return savedMessages ? JSON.parse(savedMessages) : []; }); const [userMessage, setUserMessage] = useState(""); const [loading, setLoading] = useState(false); // To handle loading state const [apiResponse, setApiResponse] = useState(null); // Declare apiResponse in the state
// Handle user message input const handleUserMessage = (e) => { setUserMessage(e.target.value); };
// Save messages to local storage whenever they change useEffect(() => { localStorage.setItem("chatMessages", JSON.stringify(messages)); }, [messages]);
// Send message to API and handle response const handleSendMessage = async () => { if (userMessage.trim()) { // Add the user message to the chat setMessages((prevMessages) => [ ...prevMessages, { text: userMessage, isUser: true }, ]); setUserMessage(""); setLoading(true); // Start loading animation
try { // Call Flask API to analyze query const response = await axios.post("http://127.0.0.1:5000/analyze_query", { query: userMessage, }); const botResponse = response.data; // Add the bot response to the chat setMessages((prevMessages) => [ ...prevMessages, { text: `Here's the information about: ${userMessage}`, isUser: false }, { text: botResponse.summary, isUser: false }, ]); // Store the full response in state to render references and additional data setApiResponse(botResponse); setLoading(false); // End loading animation } catch (error) { console.error("Error sending message:", error); setMessages((prevMessages) => [ ...prevMessages, { text: "Sorry, something went wrong. Please try again later.", isUser: false }, ]); setLoading(false); // End loading animation } }};
// Handle pressing Enter key const handleKeyDown = (e) => { if (e.key === "Enter") { handleSendMessage(); } };
// Function to extract website name from a URL const getWebsiteName = (url) => { try { const parsedUrl = new URL(url); return parsedUrl.hostname.replace("www.", ""); } catch (e) { return "Unknown Website"; } };
// Function to generate favicon URL const getFaviconUrl = (url) => { try { const parsedUrl = new URL(url); return
${parsedUrl.protocol}//${parsedUrl.hostname}/favicon.ico; } catch (e) { return ""; } };// Function to render dynamic content based on the format
**text**const renderContent = (content) => { if (!content) return null;const lines = content.split("\n"); return lines.map((line, index) => { if (line.match(/^\*\*[^*]+\*\*$/)) { return ( <h3 key={index} className="text-xl font-semibold mb-4 text-white"> {line.replace(/\*\*(.*)\*\*/g, "$1")} {/* Remove the ** around the text */} </h3> ); } if (line.match(/^\d+\.\s/)) { const numberPart = line.split(" ")[0]; // Extract the number (1.) const restOfLine = line.slice(line.indexOf(" ") + 1); const boldTextMatch = restOfLine.match(/\*\*(.*?)\*\*/); return ( <div key={index} className="mb-4"> <p className="text-gray-300"> {numberPart} <strong>{boldTextMatch ? boldTextMatch[1] : restOfLine}</strong> </p> {restOfLine.includes(":") && ( <p className="text-gray-300">{restOfLine.slice(restOfLine.indexOf(":") + 1).trim()}</p> )} </div> ); } if (line.match(/^\d+\.\s.*-\s/)) { return <p key={index} className="text-gray-300 mb-4">{line}</p>; } return <p key={index} className="text-gray-300 mb-4">{line}</p>; }); - Assistant
- User
share me full code
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.