ogegadavis254 commited on
Commit
e424a1e
·
verified ·
1 Parent(s): efee10d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -17
app.py CHANGED
@@ -33,20 +33,14 @@ if "messages" not in st.session_state:
33
 
34
  st.title("Mistral-7B Chatbot")
35
 
36
- # Function to get streamed response
37
- def get_streamed_response(message, history):
38
- all_messages = []
39
- # Include system prompt
40
- all_messages.append({
41
- "role": "system",
42
- "content": "You are a knowledgeable and friendly assistant. Respond concisely and maintain a friendly tone."
43
- })
44
- # Add previous messages to history
45
- for msg in history:
46
- all_messages.append(msg)
47
- # Add the latest user message
48
- all_messages.append({"role": "user", "content": message})
49
- return all_messages
50
 
51
  # Display chat messages from history on app rerun
52
  for message in st.session_state.messages:
@@ -61,13 +55,12 @@ if prompt := st.chat_input("Type your message here..."):
61
  # Add user message to chat history
62
  st.session_state.messages.append({"role": "user", "content": prompt})
63
 
64
- # Prepare all messages for the conversation context
65
- history = st.session_state.messages
66
 
67
  # Display assistant response in chat message container
68
  with st.chat_message("assistant"):
69
  try:
70
- all_messages = get_streamed_response(prompt, history)
71
  response = client.chat.completions.create(
72
  model=model_link,
73
  messages=all_messages,
 
33
 
34
  st.title("Mistral-7B Chatbot")
35
 
36
+ # Function to prepare messages for the API
37
+ def get_messages_for_api():
38
+ messages = []
39
+ if len(st.session_state.messages) % 2 == 1: # If the last message is from the user
40
+ messages.append({"role": "assistant", "content": "Your assistant is ready."}) # Ensure the assistant starts or resumes
41
+ for message in st.session_state.messages:
42
+ messages.append(message)
43
+ return messages
 
 
 
 
 
 
44
 
45
  # Display chat messages from history on app rerun
46
  for message in st.session_state.messages:
 
55
  # Add user message to chat history
56
  st.session_state.messages.append({"role": "user", "content": prompt})
57
 
58
+ # Prepare messages for the conversation context
59
+ all_messages = get_messages_for_api()
60
 
61
  # Display assistant response in chat message container
62
  with st.chat_message("assistant"):
63
  try:
 
64
  response = client.chat.completions.create(
65
  model=model_link,
66
  messages=all_messages,