I need to find a way to rejoin the API for an AI assistant interface I'm building…
- User
User: I need to find a way to rejoin the API for an AI assistant interface I'm building whenever a new chat is created on the frontend, as there is no API call to reset the chat. How can I do that?
Backend code: import logging import ast from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from gradio_client import Client
Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger()
app = FastAPI()
Configure CORS app.add_middleware( CORSMiddleware, allow_origins=[""], # Allows all origins allow_credentials=True, allow_methods=[""], # Allows all methods allow_headers=["*"], # Allows all headers )
Initialize the Gradio Client client = Client("yuntian-deng/o1mini")
class Message(BaseModel): text: str
def extract_latest_response(raw_response): try: # Remove the log tags and extract the content content = raw_response.replace('\nINFO:root:Raw response from Gradio Client: ', '').replace('\n', '')
# Parse the content as a Python tuple parsed_content = ast.literal_eval(content) # Extract the first element of the tuple (the list of messages) messages = parsed_content[0] # Get the last message in the list last_message = messages[-1] # The response is the second element of the last message tuple response = last_message[1] return responseexcept Exception as e: print(f"Error extracting response: {str(e)}") return None @app.post("/api/chat") async def chat(message: Message): try: logger.info(f"Received message: {message.text}")
# Send the entire conversation history to the Hugging Face Space response = client.predict(str(message.text), api_name="/predict") logger.info(f"Raw response from Gradio Client: {response}") # Extract the latest response extracted_response = extract_latest_response(str(response)) logger.info(f"Extracted response: {extracted_response}") return {"response": extracted_response}except Exception as e: logger.error(f"Error processing chat request: {str(e)}", exc_info=True) raise HTTPException(status_code=500, detail=str(e)) @app.post("/api/reset") async def reset_chat(): try: logger.info("Resetting chat...") # Call the reset endpoint of the Gradio client reset_response = client.predict(api_name="/reset_textbox") logger.info(f"Raw reset response from Gradio Client: {reset_response}")
logger.info(f"Reset response: {reset_response}") return {"status": "Chat has been reset.", "response": reset_response}except Exception as e: logger.error(f"Error resetting chat: {str(e)}", exc_info=True) raise HTTPException(status_code=500, detail="Failed to reset chat.") Copy To handle resetting the chat when a new chat is initiated from the frontend—especially when the AI model's API doesn't provide a direct reset endpoint—you can manage session states on your backend. Here's a comprehensive approach to achieve this:
-
Implement Session Management Assign a unique session ID to each chat session. This session ID will help track and manage individual conversations, allowing you to reset or maintain state as needed.
-
Store Conversation History Server-Side Maintain a conversation history for each session on the backend. This approach ensures that each chat session is isolated, and you have full control over the conversation flow.
-
Modify Backend Endpoints Update your API endpoints to handle session IDs and manage the conversation history accordingly.
-
Frontend Adjustments Ensure the frontend generates a new session ID when starting a new chat and includes this session ID with each request to the backend.
Detailed Implementation Steps a. Update Backend Code for Session Management Here's how you can modify your FastAPI backend to incorporate session management:
import logging import ast from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from gradio_client import Client import uuid from typing import Dict, List
Configure logging
logging.basicConfig(level=logging.INFO) logger = logging.getLogger()
app = FastAPI()
Configure CORS
app.add_middleware( CORSMiddleware, allow_origins=[""], # Adjust this in production for security allow_credentials=True, allow_methods=[""], allow_headers=["*"], )
Initialize the Gradio Client
Depending on whether the client maintains state, you might need one per session
For this example, we'll assume it's stateless or handles multiple sessions
client = Client("yuntian-deng/o1mini")
Data model for messages
class Message(BaseModel): session_id: str = None # Optional; if not provided, create a new session text: str
Define a type for session histories
ConversationHistory = Dict[str, List[str]]
In-memory store for conversation histories
For production, consider using a persistent store like Redis or a database
conversation_histories: ConversationHistory = {}
def extract_latest_response(raw_response): try: # Remove the log tags and extract the content content = raw_response.replace('\nINFO:root:Raw response from Gradio Client: ', '').replace('\n', '')
# Parse the content as a Python tuple parsed_content = ast.literal_eval(content) # Extract the first element of the tuple (the list of messages) messages = parsed_content[0] # Get the last message in the list last_message = messages[-1] # The response is the second element of the last message tuple response = last_message[1] return response except Exception as e: logger.error(f"Error extracting response: {str(e)}") return "Sorry, I encountered an error processing your request."@app.post("/api/chat") async def chat(message: Message): try: # If no session_id is provided, create a new one if not message.session_id: session_id = str(uuid.uuid4()) conversation_histories[session_id] = [] logger.info(f"Created new session: {session_id}") else: session_id = message.session_id if session_id not in conversation_histories: # Handle invalid session_id raise HTTPException(status_code=400, detail="Invalid session_id.")
# Append the user's message to the conversation history conversation_histories[session_id].append(f"User: {message.text}") logger.info(f"Session {session_id} | User: {message.text}") # Prepare the prompt by joining the history prompt = "\n".join(conversation_histories[session_id]) # Send the prompt to the AI model response = client.predict(prompt, api_name="/predict") logger.info(f"Session {session_id} | Raw response: {response}") # Extract the latest response extracted_response = extract_latest_response(str(response)) logger.info(f"Session {session_id} | AI: {extracted_response}") # Append the AI's response to the conversation history conversation_histories[session_id].append(f"AI: {extracted_response}") return {"session_id": session_id, "response": extracted_response} except HTTPException as he: raise he except Exception as e: logger.error(f"Error processing chat request: {str(e)}", exc_info=True) raise HTTPException(status_code=500, detail="Internal Server Error.")@app.post("/api/reset") async def reset_chat(): try: # Generate a new session_id session_id = str(uuid.uuid4()) conversation_histories[session_id] = [] logger.info(f"Reset chat. New session created: {session_id}") return {"session_id": session_id, "status": "Chat has been reset."} except Exception as e: logger.error(f"Error resetting chat: {str(e)}", exc_info=True) raise HTTPException(status_code=500, detail="Failed to reset chat.") b. Explanation of the Backend Modifications Session ID Handling:
Creation: If the frontend does not provide a session_id, the backend generates a new UUID and initializes a new conversation history. Validation: If a session_id is provided, the backend checks its validity. If it's invalid, an error is returned. Conversation History Management:
The backend maintains an in-memory dictionary conversation_histories to store the conversation history for each session. Appending Messages: Each user message and AI response is appended to the respective session's history. Prompt Preparation:
The conversation history is joined into a single prompt string, which is then sent to the AI model. This approach ensures that the AI has context for the conversation. Reset Functionality:
The /api/reset endpoint generates a new session_id and initializes an empty conversation history, effectively resetting the chat. Error Handling:
Proper error handling ensures that invalid sessions are caught and that unexpected errors return a 500 status with appropriate logging. c. Frontend Adjustments Ensure that your frontend handles session_id appropriately:
Starting a New Chat:
When the user initiates a new chat, call the /api/reset endpoint to obtain a new session_id. Store this session_id (e.g., in the application state or context). Sending Messages:
Include the session_id with each message sent to the /api/chat endpoint. Example payload: { "session_id": "your-session-id", "text": "Hello, how are you?" } Handling Responses:
Use the returned session_id from the reset or initial chat response for subsequent messages. This ensures continuity within the specific chat session. d. Considerations for Production Persistence: The current implementation uses an in-memory dictionary to store conversation histories. For production, consider using a persistent storage solution like Redis, PostgreSQL, or another database to handle sessions, especially if you expect the server to restart or scale horizontally.
Security:
CORS: Restrict allow_origins to specific domains instead of allowing all origins. Session Validation: Implement additional checks to secure session_ids and prevent unauthorized access. Scalability: If you anticipate high traffic, ensure that session management is optimized and that the storage solution can handle concurrent access efficiently.
Client Handling: Depending on whether the Gradio client maintains state across multiple sessions, you might need to instantiate separate clients per session or find alternative ways to ensure stateless interactions.
- Alternative Approach: Stateless Interaction If managing session states seems cumbersome or if the AI model effectively handles stateless interactions, you can simplify by sending each user message without maintaining history. However, this might limit the AI's ability to provide contextually relevant responses.
Conclusion By implementing session management and handling conversation histories on your backend, you gain full control over chat sessions. This approach circumvents the need for a direct reset API from the AI model and ensures that each chat session remains isolated and manageable.
Example Frontend Workflow:
Start New Chat:
Call /api/reset to obtain a new session_id. Store the session_id for the session. Send Message:
Include the session_id with each message sent to /api/chat. Reset Chat:
When the user opts to start over, call /api/reset again to obtain a fresh session_id. This structured approach ensures that each chat session is cleanly separated and can be reset as needed without relying on the AI model's internal mechanisms.
Copy Here is my frontend code for my chat interface and the sidebar where new chats are created:
// src/components/ChatInterface.js import React, { useState, useContext, useRef, useEffect } from 'react'; import { ChatContext } from '../context/ChatContext'; import Message from './Message'; import CodeModal from './CodeModal'; function ChatInterface() { const { chats, currentChatId, addMessage, updateChat, } = useContext(ChatContext); const currentChat = chats.find((chat) => chat.id === currentChatId); const [message, setMessage] = useState(''); const [codeContent, setCodeContent] = useState(''); const [isCodeModalOpen, setIsCodeModalOpen] = useState(false); const [isSystemPromptOpen, setIsSystemPromptOpen] = useState(false); const [isTyping, setIsTyping] = useState(false); const messagesEndRef = useRef(null);
useEffect(() => { scrollToBottom(); }, [currentChat?.messages, isTyping]);
const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); };
const handleSend = async () => { if (!message.trim() || !currentChatId) return;
const userMessage = { sender: 'user', text: message }; addMessage(currentChatId, userMessage); setMessage(''); scrollToBottom();
// Set typing indicator setIsTyping(true);
// Send the message to the backend try { const response = await fetch('http://localhost:8000/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ text: formatMessageWithSystemPrompt(message, currentChat.systemPrompt) }), }); const data = await response.json(); const responseText = data.response;
// Check if the response contains code blocks const codeMatch = responseText.match(/
(?:\w+\n)?([\s\S]*?)/); if (codeMatch) { //setCodeContent(codeMatch[1]); //setIsCodeModalOpen(true); }const botMessage = { sender: 'bot', text: responseText }; addMessage(currentChatId, botMessage); scrollToBottom(); } catch (error) { console.error('Error:', error); const errorMessage = { sender: 'bot', text: 'Sorry, something went wrong. Please try again.' }; addMessage(currentChatId, errorMessage); scrollToBottom(); } finally { // Remove typing indicator setIsTyping(false); } };
const formatMessageWithSystemPrompt = (message, systemPrompt) => { if (systemPrompt.trim() === '') { return message; } return {message}; };
const handleKeyDown = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } };
const handleSystemPromptChange = (e) => { const newPrompt = e.target.value; updateChat(currentChatId, { systemPrompt: newPrompt }); };
const toggleSystemPrompt = () => { setIsSystemPromptOpen(!isSystemPromptOpen); };
if (!currentChat) { return No active chat. Please start a new chat.; }
return ( {currentChat.messages.map((msg, index) => ( ))} {isTyping && ( Echo is typing... )} {isSystemPromptOpen ? 'Hide System Prompt' : 'Show System Prompt'} {isSystemPromptOpen && ( )} <textarea className="flex-1 p-2 border rounded-lg resize-none dark:bg-zinc-800 dark:text-gray-200" rows="2" placeholder="Type your message..." value={message} onChange={(e) => setMessage(e.target.value)} onKeyDown={handleKeyDown} /> Send {isCodeModalOpen && ( )} ); }
export default ChatInterface;
import React, { useContext, useState } from 'react'; import { ChatContext } from '../context/ChatContext'; import { PlusIcon, TrashIcon } from '@heroicons/react/solid'; import { ThemeContext } from '../context/ThemeContext'; function Sidebar() { const { chats, currentChatId, setCurrentChatId, addChat, deleteChat, } = useContext(ChatContext);
const { theme, toggleTheme } = useContext(ThemeContext);
const [isResetting, setIsResetting] = useState(false); const [error, setError] = useState(null);
const handleSelectChat = (id) => { setCurrentChatId(id); };
const handleNewChat = async () => { setIsResetting(true); setError(null);
try { const response = await fetch('http://localhost:8000/api/reset', { method: 'POST', headers: { 'Content-Type': 'application/json', }, });
if (!response.ok) { throw new Error('Failed to reset chat.'); }
const data = await response.json(); console.log(data.status); // Optional: Handle the response as needed
addChat('New Chat'); setCurrentChatId(null); // Optionally set to the new chat's ID if returned } catch (err) { console.error('Error resetting chat:', err); setError('Unable to reset the chat. Please try again.'); } finally { setIsResetting(false); } };
const handleDeleteChat = (e, id) => { e.stopPropagation(); // Prevent triggering the chat selection deleteChat(id); };
const handleGoHome = () => { setCurrentChatId(null); };
return ( {/* Chatbot Name "Echo" at the top */} Echo
{error && ( {error} )}
{/* Dark Mode Toggle at the bottom */}
function DarkModeToggle({ theme, toggleTheme }) { return ( Dark Mode <button onClick={toggleTheme} className={relative inline-flex items-center h-6 rounded-full w-11 focus:outline-none transition-colors duration-200 { theme === 'dark' ? 'bg-blue-600' : 'bg-gray-200 dark:bg-gray-700' }} > <span className="sr-only">Toggle Dark Mode</span> <span className={{ theme === 'dark' ? 'translate-x-6' : 'translate-x-1' } inline-block w-4 h-4 transform bg-white rounded-full transition-transform duration-200} /> ); }
export default Sidebar;
-
- Assistant
To effectively reset the chat whenever a new chat is created on the frontend and manage multiple chat sessions, you should implement session management on your backend and adjust your frontend accordingly. This ensures that each chat session is isolated, allowing users to have multiple independent conversations with the AI assistant.
Below is a comprehensive guide to achieving this:
1. Backend Enhancements: Implement Session Management
a. Update Backend Code for Session Management
Here's how you can modify your FastAPI backend to handle session management using unique session IDs:
# backend/main.py import logging import ast from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from gradio_client import Client import uuid from typing import Dict, List # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger() app = FastAPI() # Configure CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], # Update with specific origins in production allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Initialize the Gradio Client client = Client("yuntian-deng/o1mini") # Data model for messages class Message(BaseModel): session_id: str = None # Optional; if not provided, create a new session text: str # Define a type for session histories ConversationHistory = Dict[str, List[str]] # In-memory store for conversation histories # Consider using persistent storage (e.g., Redis, database) for production conversation_histories: ConversationHistory = {} def extract_latest_response(raw_response: str) -> str: try: # Remove the log tags and extract the content content = raw_response.replace('<log>\nINFO:root:Raw response from Gradio Client: ', '').replace('\n</log>', '') # Parse the content as a Python tuple parsed_content = ast.literal_eval(content) # Extract the first element of the tuple (the list of messages) messages = parsed_content[0] # Get the last message in the list last_message = messages[-1] # The response is the second element of the last message tuple response = last_message[1] return response except Exception as e: logger.error(f"Error extracting response: {str(e)}") return "Sorry, I encountered an error processing your request." @app.post("/api/chat") async def chat(message: Message): try: # If no session_id is provided, create a new one if not message.session_id: session_id = str(uuid.uuid4()) conversation_histories[session_id] = [] logger.info(f"Created new session: {session_id}") else: session_id = message.session_id if session_id not in conversation_histories: # Handle invalid session_id raise HTTPException(status_code=400, detail="Invalid session_id.") # Append the user's message to the conversation history conversation_histories[session_id].append(f"User: {message.text}") logger.info(f"Session {session_id} | User: {message.text}") # Prepare the prompt by joining the history prompt = "\n".join(conversation_histories[session_id]) # Send the prompt to the AI model response = client.predict(prompt, api_name="/predict") logger.info(f"Session {session_id} | Raw response: {response}") # Extract the latest response extracted_response = extract_latest_response(str(response)) logger.info(f"Session {session_id} | AI: {extracted_response}") # Append the AI's response to the conversation history conversation_histories[session_id].append(f"AI: {extracted_response}") return {"session_id": session_id, "response": extracted_response} except HTTPException as he: raise he except Exception as e: logger.error(f"Error processing chat request: {str(e)}", exc_info=True) raise HTTPException(status_code=500, detail="Internal Server Error.") @app.post("/api/reset") async def reset_chat(): try: # Generate a new session_id session_id = str(uuid.uuid4()) conversation_histories[session_id] = [] logger.info(f"Reset chat. New session created: {session_id}") return {"session_id": session_id, "status": "Chat has been reset."} except Exception as e: logger.error(f"Error resetting chat: {str(e)}", exc_info=True) raise HTTPException(status_code=500, detail="Failed to reset chat.")b. Explanation of Backend Modifications
-
Session ID Handling:
- Creation: If the incoming request does not include a
session_id, the backend generates a new UUID and initializes a new conversation history. - Validation: If a
session_idis provided, the backend verifies its validity. An error is returned if thesession_idis invalid.
- Creation: If the incoming request does not include a
-
Conversation History Management:
- Storage: An in-memory dictionary
conversation_historiesstores conversation histories for each session. For scalability and persistence, consider using Redis or a database. - Appending Messages: Both user messages and AI responses are appended to the respective session's history.
- Storage: An in-memory dictionary
-
Prompt Preparation:
- The conversation history is concatenated into a single prompt string, providing context for the AI model.
-
Reset Functionality:
- The
/api/resetendpoint generates a newsession_idand initializes an empty conversation history, effectively resetting the chat.
- The
-
Error Handling:
- Comprehensive error handling ensures that invalid sessions are addressed, and unexpected errors are logged and returned appropriately.
2. Frontend Adjustments: Integrate Session Management
a. Update Context to Handle
session_idFirst, ensure your
ChatContextmanagessession_idfor each chat. This way, each chat can maintain its own session state.// src/context/ChatContext.js import React, { createContext, useState } from 'react'; import { v4 as uuidv4 } from 'uuid'; export const ChatContext = createContext(); export const ChatProvider = ({ children }) => { const [chats, setChats] = useState([]); const [currentChatId, setCurrentChatId] = useState(null); const addChat = (name) => { const newChat = { id: uuidv4(), name: name || `Chat ${chats.length + 1}`, messages: [], session_id: null, // Initialize with no session_id systemPrompt: '', }; setChats([...chats, newChat]); setCurrentChatId(newChat.id); }; const deleteChat = (id) => { setChats(chats.filter(chat => chat.id !== id)); if (currentChatId === id && chats.length > 1) { setCurrentChatId(chats[0].id); } else if (chats.length === 1) { setCurrentChatId(null); } }; const addMessage = (chatId, message) => { setChats(prevChats => prevChats.map(chat => chat.id === chatId ? { ...chat, messages: [...chat.messages, message] } : chat ) ); }; const updateChat = (chatId, updatedFields) => { setChats(prevChats => prevChats.map(chat => chat.id === chatId ? { ...chat, ...updatedFields } : chat ) ); }; return ( <ChatContext.Provider value={{ chats, currentChatId, setCurrentChatId, addChat, deleteChat, addMessage, updateChat, }}> {children} </ChatContext.Provider> ); };b. Modify the Sidebar to Handle New Chat Sessions
Update the
Sidebarcomponent to handle the creation of new chats by resetting the chat session on the backend and associating the returnedsession_idwith the new chat.// src/components/Sidebar.js import React, { useContext, useState } from 'react'; import { ChatContext } from '../context/ChatContext'; import { PlusIcon, TrashIcon } from '@heroicons/react/solid'; import { ThemeContext } from '../context/ThemeContext'; function Sidebar() { const { chats, currentChatId, setCurrentChatId, addChat, deleteChat, updateChat } = useContext(ChatContext); const { theme, toggleTheme } = useContext(ThemeContext); const [isResetting, setIsResetting] = useState(false); const [error, setError] = useState(null); const handleSelectChat = (id) => { setCurrentChatId(id); }; const handleNewChat = async () => { setIsResetting(true); setError(null); try { const response = await fetch('http://localhost:8000/api/reset', { method: 'POST', headers: { 'Content-Type': 'application/json', }, }); if (!response.ok) { throw new Error('Failed to reset chat.'); } const data = await response.json(); const { session_id, status } = data; console.log(status); // Optional: Handle the response as needed // Add a new chat with the received session_id const newChatName = `Chat ${chats.length + 1}`; const newChat = { id: uuidv4(), name: newChatName, messages: [], session_id: session_id, systemPrompt: '', }; addChat(newChatName); // Update the newly added chat with session_id const latestChat = chats[chats.length - 1]; updateChat(latestChat.id, { session_id: session_id }); // Set the new chat as the current chat setCurrentChatId(latestChat.id); } catch (err) { console.error('Error resetting chat:', err); setError('Unable to reset the chat. Please try again.'); } finally { setIsResetting(false); } }; const handleDeleteChat = (e, id) => { e.stopPropagation(); // Prevent triggering the chat selection deleteChat(id); }; const handleGoHome = () => { setCurrentChatId(null); }; return ( <div className="w-64 bg-gray-100 dark:bg-zinc-900 border-r dark:border-gray-700 p-4 flex flex-col"> {/* Chatbot Name "Echo" at the top */} <div className="mb-6 text-2xl font-bold text-gray-800 dark:text-gray-200 cursor-pointer" onClick={handleGoHome}> Echo </div> <div className="flex justify-between items-center mb-4"> <h2 className="text-xl font-bold text-gray-800 dark:text-gray-200">Past Chats</h2> <button onClick={handleNewChat} className={`p-2 bg-blue-500 text-white rounded hover:bg-blue-600 flex items-center justify-center ${ isResetting ? 'opacity-50 cursor-not-allowed' : '' }`} title="Start New Chat" disabled={isResetting} > {isResetting ? 'Resetting...' : <PlusIcon className="h-5 w-5" />} </button> </div> {error && ( <div className="mb-4 text-red-500 text-sm"> {error} </div> )} <ul className="flex-1 overflow-y-auto"> {chats.length === 0 && ( <li className="text-gray-500 dark:text-gray-400">No past chats.</li> )} {chats.map((chat) => ( <li key={chat.id} className={`mb-2 cursor-pointer p-2 rounded flex justify-between items-center ${ chat.id === currentChatId ? 'bg-blue-100 dark:bg-blue-900' : 'hover:bg-gray-100 dark:hover:bg-gray-700' }`} onClick={() => handleSelectChat(chat.id)} > <span className='text-black dark:text-white'>{chat.name}</span> <button onClick={(e) => handleDeleteChat(e, chat.id)} className="text-red-500 hover:text-red-700" title="Delete Chat" > <TrashIcon className="h-5 w-5" /> </button> </li> ))} </ul> {/* Dark Mode Toggle at the bottom */} <div className="mt-4"> <DarkModeToggle theme={theme} toggleTheme={toggleTheme} /> </div> </div> ); } function DarkModeToggle({ theme, toggleTheme }) { return ( <div className="flex items-center justify-center"> <span className="text-gray-800 dark:text-gray-200 mr-2">Dark Mode</span> <button onClick={toggleTheme} className={`relative inline-flex items-center h-6 rounded-full w-11 focus:outline-none transition-colors duration-200 ${ theme === 'dark' ? 'bg-blue-600' : 'bg-gray-200 dark:bg-gray-700' }`} > <span className="sr-only">Toggle Dark Mode</span> <span className={`${ theme === 'dark' ? 'translate-x-6' : 'translate-x-1' } inline-block w-4 h-4 transform bg-white rounded-full transition-transform duration-200`} /> </button> </div> ); } export default Sidebar;Key Changes in
Sidebar:-
Handling New Chat Creation:
- When a user clicks the "New Chat" button, the frontend calls the
/api/resetendpoint to obtain a newsession_id. - The returned
session_idis associated with the newly created chat.
- When a user clicks the "New Chat" button, the frontend calls the
-
Updating Chat Context:
- The newly created chat is updated with the received
session_idto ensure that all subsequent messages in this chat are linked to the correct session.
- The newly created chat is updated with the received
-
Error Handling:
- Proper error handling is implemented to inform users if resetting the chat fails.
c. Update ChatInterface to Include
session_idin MessagesModify the
ChatInterfacecomponent to include thesession_idwhen sending messages to the backend.// src/components/ChatInterface.js import React, { useState, useContext, useRef, useEffect } from 'react'; import { ChatContext } from '../context/ChatContext'; import Message from './Message'; import CodeModal from './CodeModal'; function ChatInterface() { const { chats, currentChatId, addMessage, updateChat } = useContext(ChatContext); const currentChat = chats.find((chat) => chat.id === currentChatId); const [message, setMessage] = useState(''); const [codeContent, setCodeContent] = useState(''); const [isCodeModalOpen, setIsCodeModalOpen] = useState(false); const [isSystemPromptOpen, setIsSystemPromptOpen] = useState(false); const [isTyping, setIsTyping] = useState(false); const messagesEndRef = useRef(null); useEffect(() => { scrollToBottom(); }, [currentChat?.messages, isTyping]); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; const handleSend = async () => { if (!message.trim() || !currentChatId) return; const userMessage = { sender: 'user', text: message }; addMessage(currentChatId, userMessage); setMessage(''); scrollToBottom(); // Set typing indicator setIsTyping(true); // Send the message to the backend try { const response = await fetch('http://localhost:8000/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: currentChat.session_id, // Include session_id text: formatMessageWithSystemPrompt(message, currentChat.systemPrompt), }), }); if (!response.ok) { throw new Error('Failed to fetch response from the server.'); } const data = await response.json(); const responseText = data.response; const sessionId = data.session_id; // Update the chat's session_id if it's a new one if (sessionId && sessionId !== currentChat.session_id) { updateChat(currentChatId, { session_id: sessionId }); } // Check if the response contains code blocks const codeMatch = responseText.match(/```(?:\w+\n)?([\s\S]*?)```/); if (codeMatch) { setCodeContent(codeMatch[1]); setIsCodeModalOpen(true); } const botMessage = { sender: 'bot', text: responseText }; addMessage(currentChatId, botMessage); scrollToBottom(); } catch (error) { console.error('Error:', error); const errorMessage = { sender: 'bot', text: 'Sorry, something went wrong. Please try again.' }; addMessage(currentChatId, errorMessage); scrollToBottom(); } finally { // Remove typing indicator setIsTyping(false); } }; const formatMessageWithSystemPrompt = (message, systemPrompt) => { if (systemPrompt.trim() === '') { return message; } return `${systemPrompt}\n\n${message}`; }; const handleKeyDown = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }; const handleSystemPromptChange = (e) => { const newPrompt = e.target.value; updateChat(currentChatId, { systemPrompt: newPrompt }); }; const toggleSystemPrompt = () => { setIsSystemPromptOpen(!isSystemPromptOpen); }; if (!currentChat) { return <div className="flex-1 p-4">No active chat. Please start a new chat.</div>; } return ( <div className="flex flex-col h-screen"> <div className="flex-1 p-32 pt-8 pb-8 overflow-y-auto bg-[#eeeeee] dark:bg-[#161618]"> {currentChat.messages.map((msg, index) => ( <Message key={index} sender={msg.sender} text={msg.text} /> ))} {isTyping && ( <div className="mb-4 flex justify-start"> <div className="max-w-xs md:max-w-md lg:max-w-lg p-3 rounded-lg bg-gray-200 dark:bg-gray-800 text-gray-800 dark:text-gray-200 flex items-center"> <div className="flex space-x-1 mr-2"> <div className="w-2 h-2 bg-gray-600 rounded-full animate-bounce-custom animation-delay-0"></div> <div className="w-2 h-2 bg-gray-600 rounded-full animate-bounce-custom animation-delay-200"></div> <div className="w-2 h-2 bg-gray-600 rounded-full animate-bounce-custom animation-delay-400"></div> </div> <span className="text-sm">Echo is typing...</span> </div> </div> )} <div ref={messagesEndRef} /> </div> <div id="message_area" className="p-4 border-t dark:border-gray-700 bg-white dark:bg-[#161618]"> <button onClick={toggleSystemPrompt} className="mb-2 text-sm text-blue-500 hover:underline focus:outline-none"> {isSystemPromptOpen ? 'Hide System Prompt' : 'Show System Prompt'} </button> {isSystemPromptOpen && ( <textarea className="w-full p-2 border rounded-lg resize-none dark:bg-gray-800 dark:text-gray-200 mb-2" rows="3" placeholder="Enter system prompt..." value={currentChat.systemPrompt} onChange={handleSystemPromptChange} /> )} <div className="flex"> <textarea className="flex-1 p-2 border rounded-lg resize-none dark:bg-zinc-800 dark:text-gray-200" rows="2" placeholder="Type your message..." value={message} onChange={(e) => setMessage(e.target.value)} onKeyDown={handleKeyDown} /> <button className="ml-2 bg-green-500 text-white px-4 py-2 rounded-lg hover:bg-green-600" onClick={handleSend} > Send </button> </div> </div> {isCodeModalOpen && <CodeModal code={codeContent} setIsCodeModalOpen={setIsCodeModalOpen} />} </div> ); } export default ChatInterface;Key Changes in
ChatInterface:-
Including
session_idin API Requests:- When sending a message, the
session_idassociated with the current chat is included in the request payload.
- When sending a message, the
-
Handling
session_idin Responses:- If the backend returns a new
session_id, it's updated in the chat context to maintain consistency.
- If the backend returns a new
-
Code Modal Activation:
- Uncommented the code modal functionality to display code blocks if present in the AI's response.
c. Ensure Unique Session IDs for Each Chat
Make sure that each chat in your context has its own unique
session_id. This is handled in the updatedaddChatfunction and when resetting the chat to associate the newsession_id.d. Install UUID Dependency
Ensure you have the
uuidlibrary installed to generate unique identifiers for chats.npm install uuidOr, if using yarn:
yarn add uuide. Final Considerations
-
State Persistence:
- Currently, conversation histories are stored in-memory on the backend. For production environments, this should be replaced with persistent storage solutions like Redis, PostgreSQL, or other databases to ensure data persistence across server restarts and scalability.
-
Security Enhancements:
- CORS: Restrict
allow_originsto trusted domains to enhance security. - Session Validation: Implement additional validation and possibly authentication to secure
session_ids and prevent unauthorized access.
- CORS: Restrict
-
Scalability:
- If your application scales horizontally (multiple backend instances), ensure that the session storage is centralized (e.g., using Redis) to maintain consistency across instances.
-
Graceful Error Handling:
- Enhance error messages and user feedback mechanisms to provide a better user experience in case of failures.
3. Summary Workflow
-
Starting a New Chat:
- Frontend: User clicks the "New Chat" button in the sidebar.
- Action: The frontend calls the
/api/resetendpoint. - Backend: Generates a new
session_idand initializes an empty conversation history. - Frontend: Receives the
session_idand associates it with the newly created chat in the context.
-
Sending Messages:
- Frontend: User types a message and sends it.
- Action: The message, along with the associated
session_id, is sent to the/api/chatendpoint. - Backend: Validates the
session_id, appends the message to the conversation history, generates a response, and updates the conversation history with the AI's reply. - Frontend: Receives the AI's response and displays it in the chat interface.
-
Resetting a Chat:
- Frontend: User initiates a chat reset (e.g., starting a new chat).
- Action: Similar to starting a new chat, calls the
/api/resetendpoint to obtain a newsession_id. - Backend: Generates a new
session_idand clears the previous conversation history. - Frontend: Associates the new
session_idwith the new chat.
By implementing the above modifications, your AI assistant interface will effectively handle multiple chat sessions, ensuring that each conversation is isolated and can be reset independently. This enhances the user experience by providing clear session management and preventing cross-talk between different chat sessions.
-