Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,73 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
import openai
|
| 4 |
-
from langchain.agents import initialize_agent, AgentType
|
| 5 |
-
from langchain.callbacks import StreamlitCallbackHandler
|
| 6 |
-
from langchain.chat_models import ChatOpenAI
|
| 7 |
-
from langchain.tools import DuckDuckGoSearchRun
|
| 8 |
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
if "messages" not in st.session_state:
|
| 15 |
-
st.session_state
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
if
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
search_agent = initialize_agent(
|
| 33 |
-
[search], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True
|
| 34 |
-
)
|
| 35 |
with st.chat_message("assistant"):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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))
|