Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|