├── requirements.txt ├── README.md └── mistral_chat.py /requirements.txt: -------------------------------------------------------------------------------- 1 | mistralai==0.0.8 2 | streamlit==1.29.0 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Quick Start Guide 2 | 3 | 1. Clone the repository: 4 | ```bash 5 | git clone https://github.com/vindiw/mistral-streamlit-chat.git 6 | ``` 7 | 2. Navigate to the cloned directory: 8 | ```bash 9 | cd mistral-streamlit-chat 10 | ``` 11 | 3. Set your Mistral API key as an environment variable. You can do this in the terminal with the following command or by adding it to a `.env` file in the project root directory. 12 | ```bash 13 | export MISTRAL_API_KEY=your_api_key 14 | ``` 15 | 4. Run the application: 16 | ```bash 17 | streamlit run mistral_chat.py 18 | ``` 19 | 20 | Please note that you need a Mistral API key to use this application. You can sign up for one at the [Mistral Console](https://console.mistral.ai). 21 | 22 | ## Resources 23 | 24 | - This app was built following Streamlit's [tutorial on building conversational apps](https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps). 25 | - For more information on using Mistral's client-side library, refer to the [Mistral documentation](https://docs.mistral.ai/platform/client). 26 | 27 | ## Contact 28 | 29 | Feel free to contact me on [Twitter](https://twitter.com/vindiww) if you have any questions or feedback. 30 | -------------------------------------------------------------------------------- /mistral_chat.py: -------------------------------------------------------------------------------- 1 | from mistralai.client import MistralClient 2 | from mistralai.models.chat_completion import ChatMessage 3 | import streamlit as st 4 | import os 5 | 6 | st.title("Mistral Chat") 7 | 8 | # Function to reset the state 9 | def reset_state(): 10 | for key in st.session_state: 11 | del st.session_state[key] 12 | 13 | # Get the API key from the environment variables or the user 14 | api_key = os.getenv("MISTRAL_API_KEY") 15 | if not api_key: 16 | if "api_key" not in st.session_state: 17 | st.session_state["api_key"] = st.text_input("Enter your API key", type="password") 18 | api_key = st.session_state["api_key"] 19 | else: 20 | if expected_password := os.getenv("PASSWORD"): 21 | password = st.text_input("What's the secret password?", type="password") 22 | # Check if the entered key matches the expected password 23 | if password != expected_password: 24 | api_key = '' 25 | st.error("Unauthorized access.") 26 | reset_state() # This line will reset the script 27 | else: 28 | api_key = os.getenv("MISTRAL_API_KEY") 29 | 30 | client = MistralClient(api_key=api_key) 31 | 32 | # Initialize the model in session state if it's not already set 33 | if "mistral_model" not in st.session_state: 34 | st.session_state["mistral_model"] = 'mistral-tiny' 35 | 36 | # Always display the dropdown 37 | model_options = ('mistral-tiny', 'mistral-small', 'mistral-medium') 38 | st.session_state["mistral_model"] = st.selectbox('Select a model', model_options, index=model_options.index(st.session_state["mistral_model"]), key="model_select") 39 | 40 | # Add system prompt input 41 | if "system_prompt" not in st.session_state: 42 | st.session_state["system_prompt"] = '' 43 | st.text_input('System Prompt', value=st.session_state["system_prompt"], key="system_prompt") 44 | 45 | if "messages" not in st.session_state: 46 | st.session_state.messages = [] 47 | 48 | # Add system prompt as a ChatMessage if it doesn't exist 49 | if st.session_state["system_prompt"] and not any(message.role == "system" for message in st.session_state.messages): 50 | st.session_state.messages.insert(0, ChatMessage(role="system", content=st.session_state["system_prompt"])) 51 | 52 | for message in st.session_state.messages: 53 | if message.role != "system": # Skip system messages for UI 54 | with st.chat_message(message.role): # Use dot notation here 55 | st.markdown(message.content) # And here 56 | 57 | if prompt := st.chat_input("What is up?"): 58 | new_message = ChatMessage(role="user", content=prompt) 59 | st.session_state.messages.append(new_message) 60 | with st.chat_message("user"): 61 | st.markdown(prompt) 62 | 63 | with st.chat_message("assistant"): 64 | message_placeholder = st.empty() 65 | full_response = "" 66 | for response in client.chat_stream( 67 | model=st.session_state["mistral_model"], 68 | messages=st.session_state.messages, # Pass the entire messages list 69 | ): 70 | full_response += (response.choices[0].delta.content or "") 71 | message_placeholder.markdown(full_response + "▌") 72 | message_placeholder.markdown(full_response) 73 | st.session_state.messages.append(ChatMessage(role="assistant", content=full_response)) 74 | --------------------------------------------------------------------------------