#Add loading and display in st.code of the history. Add and display a dataframe with the input and output pair in an editable grid and code listing: import streamlit as st import anthropic import os import base64 from streamlit.components.v1 import html # Set up the Anthropic client client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY")) # Initialize session state if "chat_history" not in st.session_state: st.session_state.chat_history = [] # Function to get file download link def get_download_link(file_contents, file_name): b64 = base64.b64encode(file_contents.encode()).decode() return f'Download {file_name}' # Streamlit app def main(): st.title("Claude 3.5 Sonnet API Demo") # User input user_input = st.text_input("Enter your message:", key="user_input") if user_input: # Call Claude API response = client.messages.create( model="claude-3-sonnet-20240229", max_tokens=1000, messages=[ {"role": "user", "content": user_input} ] ) # Append to chat history st.session_state.chat_history.append({"user": user_input, "claude": response.content[0].text}) # Clear user input st.session_state.user_input = "" # Display chat history for chat in st.session_state.chat_history: st.chat_message(chat["user"], is_user=True) st.chat_message(chat["claude"], avatar_style="thumbs", seed="Claude") # Save chat history to file chat_history_text = "\n".join([f"User: {chat['user']}\nClaude: {chat['claude']}" for chat in st.session_state.chat_history]) file_download_link = get_download_link(chat_history_text, "chat_history.txt") st.markdown(file_download_link, unsafe_allow_html=True) if __name__ == "__main__": main()