gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Here is code for a streamlit application. Implement a clear history button. [code]
- User
Here is code for a streamlit application. Implement a clear history button.
import streamlit as st from gradio_client import Client import os import json # Set page configuration st.set_page_config( page_title="LLM Chat Application", layout="wide", initial_sidebar_state="auto" ) # Constants HISTORY_FILE = 'history.json' # Changed to a JSON file to store conversation history as list of dicts MODEL_CLIENT_NAME = 'yuntian-deng/ChatGPT4' # Replace with your actual model identifier CONFIG_FILE = 'config.json' # Config file to store top_p and temperature # Function to load history from the file def load_history(): if os.path.exists(HISTORY_FILE): with open(HISTORY_FILE, 'r', encoding='utf-8') as f: return json.load(f) return [] # Function to save history to the file def save_history(history): with open(HISTORY_FILE, 'w', encoding='utf-8') as f: json.dump(history, f) # Function to load config from the file def load_config(): if os.path.exists(CONFIG_FILE): with open(CONFIG_FILE, 'r', encoding='utf-8') as f: return json.load(f) else: return {"top_p": 1.0, "temperature": 1.0} # Function to save config to the file def save_config(config): with open(CONFIG_FILE, 'w', encoding='utf-8') as f: json.dump(config, f) # Initialize session state for history if 'history' not in st.session_state: st.session_state.history = load_history() # Initialize session state for config if 'config' not in st.session_state: st.session_state.config = load_config() for i, msg in enumerate(st.session_state.history): cols = st.columns((1, 1)) with cols[0]: st.text_area(f"User {i+1}", value=msg["user"], height=68, key=f"user_text_{i}") with cols[1]: st.text_area(f"Assistant {i+1}", value=msg["llm"], height=68, key=f"llm_text_{i}") def open_history_page(): st.session_state['display_history'] = True # Create a form for new input with st.form("chat_form", clear_on_submit=True): user_input = st.text_area("Your Message", key='user_input', placeholder="Type your message here...") # Generate Response button generate = st.form_submit_button("Send Message") # Sliders for Temperature and Top-P sampling col1, col2 = st.columns(2) with col1: temperature = st.slider( "Temperature", min_value=0.0, max_value=5.0, value=st.session_state.config.get('temperature', 1.0), step=0.1, help="Controls the randomness of the model's output." ) with col2: top_p = st.slider( "Top-P Sampling", min_value=0.0, max_value=1.0, value=st.session_state.config.get('top_p', 1.0), step=0.05, help="Nucleus sampling." ) col3, col4 = st.columns(2) with col3: user_prefix = st.text_area('User Prefix', key='user_prefix', placeholder="User...", value="User: ", height=68) with col4: llm_prefix = st.text_area('LLM Prefix', key='llm_prefix', placeholder="Assistant...", value="Assistant: ", height=68) # Update the config in session_state and save to file when sliders change if temperature != st.session_state.config.get('temperature', 1.0) or top_p != st.session_state.config.get('top_p', 1.0): st.session_state.config['temperature'] = temperature st.session_state.config['top_p'] = top_p save_config(st.session_state.config) # Handle form submission if generate: if user_input.strip(): # Append user's message to the history st.session_state.history.append({"user": user_input.strip(), "llm": ""}) save_history(st.session_state.history) try: # Initialize the client client = Client(MODEL_CLIENT_NAME) # Prepare the prompt prompt = "\n".join([f"{user_prefix}{msg['user']}\n{llm_prefix}{msg['llm']}" for msg in st.session_state.history]) # Call the model to get a response result = client.predict( inputs=prompt, top_p=st.session_state.config['top_p'], temperature=st.session_state.config['temperature'], chat_counter=0, chatbot=[], api_name='/predict' ) # Adjust based on actual API response structure # Assuming 'result' is a list of lists where result[0][0][1] is the response output = result[0][0][1].strip() # Append model response to history st.session_state.history[-1]["llm"] = output save_history(st.session_state.history) # Re-render the page to show updated history st.rerun() except Exception as e: st.error(f"Error communicating with the model: {e}") else: st.warning("Please enter a message.") # Button to open history page if st.button("Open History Page"): open_history_page() # Display the conversation history on a new page if st.session_state.get('display_history', False): st.markdown("# Conversation History") st.markdown(st.session_state.history) - Assistant