Promotingai commited on
Commit
a1bc6ed
·
verified ·
1 Parent(s): a526b78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -32
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
- openai_api_key = os.getenv("OPENAI_API_KEY")
10
 
11
- st.title("🔎 PromptingAI.io - Chat with search")
 
 
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  if "messages" not in st.session_state:
15
- st.session_state["messages"] = [
16
- {"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
17
- ]
18
-
19
- for msg in st.session_state.messages:
20
- st.chat_message(msg["role"]).write(msg["content"])
21
-
22
- if prompt := st.chat_input(placeholder="What is PromptingAI.io"):
23
- st.session_state.messages.append({"role": "user", "content": prompt})
24
- st.chat_message("user").write(prompt)
25
-
26
- if not openai_api_key:
27
- st.info("Please add your OpenAI API key to continue.")
28
- st.stop()
29
-
30
- llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key, streaming=True)
31
- search = DuckDuckGoSearchRun(name="Search")
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
- st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
37
- response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
38
- st.session_state.messages.append({"role": "assistant", "content": response})
39
- st.write(response)
 
 
 
 
 
 
 
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))