Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,3 @@
|
|
1 |
-
""" Simple Chatbot
|
2 |
-
@author: Nigel Gebodh
|
3 |
-
@email: [email protected]
|
4 |
-
"""
|
5 |
-
|
6 |
import streamlit as st
|
7 |
from openai import OpenAI
|
8 |
import os
|
@@ -23,7 +18,6 @@ def reset_conversation():
|
|
23 |
'''
|
24 |
Resets Conversation
|
25 |
'''
|
26 |
-
st.session_state.conversation = []
|
27 |
st.session_state.messages = []
|
28 |
return None
|
29 |
|
@@ -38,7 +32,19 @@ if "messages" not in st.session_state:
|
|
38 |
st.session_state.messages = []
|
39 |
|
40 |
st.title("Mistral-7B Chatbot")
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Display chat messages from history on app rerun
|
44 |
for message in st.session_state.messages:
|
@@ -46,7 +52,7 @@ for message in st.session_state.messages:
|
|
46 |
st.markdown(message["content"])
|
47 |
|
48 |
# Accept user input
|
49 |
-
if prompt := st.chat_input("Type your message here
|
50 |
|
51 |
# Display user message in chat message container
|
52 |
with st.chat_message("user"):
|
@@ -54,26 +60,25 @@ if prompt := st.chat_input("Type your message here...."):
|
|
54 |
# Add user message to chat history
|
55 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
56 |
|
|
|
|
|
|
|
57 |
# Display assistant response in chat message container
|
58 |
with st.chat_message("assistant"):
|
59 |
try:
|
60 |
-
|
61 |
model=model_link,
|
62 |
-
messages=
|
63 |
-
{"role": m["role"], "content": m["content"]}
|
64 |
-
for m in st.session_state.messages
|
65 |
-
],
|
66 |
temperature=temperature,
|
67 |
-
|
68 |
-
max_tokens=3000,
|
69 |
)
|
70 |
|
71 |
-
|
72 |
|
73 |
except Exception as e:
|
74 |
-
|
75 |
-
st.
|
76 |
-
st.
|
77 |
-
st.write(e)
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from openai import OpenAI
|
3 |
import os
|
|
|
18 |
'''
|
19 |
Resets Conversation
|
20 |
'''
|
|
|
21 |
st.session_state.messages = []
|
22 |
return None
|
23 |
|
|
|
32 |
st.session_state.messages = []
|
33 |
|
34 |
st.title("Mistral-7B Chatbot")
|
35 |
+
|
36 |
+
# Function to get streamed response
|
37 |
+
def get_streamed_response(message, history):
|
38 |
+
all_message = [{
|
39 |
+
"role": "system",
|
40 |
+
"content": "From now on, you are an AI assistant knowledgeable in general topics. You can respond with relevant information and provide concise, friendly replies. Always maintain a helpful and neutral tone. Ensure to be concise to keep the conversation flowing smoothly."
|
41 |
+
}]
|
42 |
+
|
43 |
+
for human, assistant in history:
|
44 |
+
all_message.append({"role": "user", "content": human})
|
45 |
+
all_message.append({"role": "assistant", "content": assistant})
|
46 |
+
|
47 |
+
return all_message
|
48 |
|
49 |
# Display chat messages from history on app rerun
|
50 |
for message in st.session_state.messages:
|
|
|
52 |
st.markdown(message["content"])
|
53 |
|
54 |
# Accept user input
|
55 |
+
if prompt := st.chat_input("Type your message here..."):
|
56 |
|
57 |
# Display user message in chat message container
|
58 |
with st.chat_message("user"):
|
|
|
60 |
# Add user message to chat history
|
61 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
62 |
|
63 |
+
# Prepare all messages for the conversation context
|
64 |
+
history = [(msg["content"], next((m["content"] for m in st.session_state.messages if m["role"] == "assistant"), "")) for msg in st.session_state.messages]
|
65 |
+
|
66 |
# Display assistant response in chat message container
|
67 |
with st.chat_message("assistant"):
|
68 |
try:
|
69 |
+
response = client.chat.completions.create(
|
70 |
model=model_link,
|
71 |
+
messages=get_streamed_response(prompt, history),
|
|
|
|
|
|
|
72 |
temperature=temperature,
|
73 |
+
max_tokens=150 # Adjust the token limit as needed
|
|
|
74 |
)
|
75 |
|
76 |
+
st.markdown(response['choices'][0]['message']['content'])
|
77 |
|
78 |
except Exception as e:
|
79 |
+
st.markdown("An error occurred. Please try again later.")
|
80 |
+
st.markdown("Error details:")
|
81 |
+
st.markdown(str(e))
|
|
|
82 |
|
83 |
+
# Add assistant's response to chat history
|
84 |
+
st.session_state.messages.append({"role": "assistant", "content": response['choices'][0]['message']['content']})
|