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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -72
app.py CHANGED
@@ -1,73 +1,30 @@
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))
 
 
 
 
1
  import os
2
+ import streamlit as st
3
+ import openai
4
+
5
+ # Load your OpenAI API key from an environment variable
6
+ openai.api_key = os.getenv("OPENAI_API_KEY")
7
+
8
+ def get_openai_response(user_input):
9
+ """
10
+ Sends user input to OpenAI's Chat API and returns the model's response.
11
+ """
12
+ try:
13
+ response = openai.ChatCompletion.create(
14
+ model="gpt-3.5-turbo", # Use the model suited for chat applications
15
+ messages=[
16
+ {"role": "system", "content": "You are a helpful assistant."},
17
+ {"role": "user", "content": user_input},
18
+ ]
19
+ )
20
+ # Extract the text from the last response in the chat
21
+ return response.choices[0].message['content'].strip() if response.choices else "No response from the model."
22
+ except Exception as e:
23
+ return f"An error occurred: {str(e)}"
24
+
25
+ # Streamlit app layout
26
+ st.title("Your Advanced Streamlit Chatbot")
27
+ user_input = st.text_input("What would you like to ask?")
28
+ if st.button("Submit"):
29
+ chatbot_response = get_openai_response(user_input) if user_input else "Please enter a question or message to get a response."
30
+ st.write(f"Chatbot: {chatbot_response}")