ogegadavis254 commited on
Commit
7d011fb
1 Parent(s): c211211

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -27,21 +27,10 @@ temperature = 0.5
27
  # Add reset button to clear conversation
28
  st.button('Reset Chat', on_click=reset_conversation)
29
 
30
- # Define a system message for the Mistral model
31
- system_message = {
32
- "role": "system",
33
- "content": "As a helpful and friendly assistant, provide concise and accurate responses to the user's queries."
34
- }
35
-
36
  # Initialize chat history
37
  if "messages" not in st.session_state:
38
  st.session_state.messages = []
39
 
40
- # Display chat messages from history on app rerun
41
- for message in st.session_state.messages:
42
- with st.chat_message(message["role"]):
43
- st.markdown(message["content"])
44
-
45
  # Accept user input
46
  if prompt := st.chat_input("Type your message here..."):
47
 
@@ -54,18 +43,30 @@ if prompt := st.chat_input("Type your message here..."):
54
  # Display assistant response in chat message container
55
  with st.chat_message("assistant"):
56
  try:
 
 
 
 
 
57
  response = client.chat.completions.create(
58
  model=model_link,
59
- messages=[
60
- system_message, # System message
61
- *[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages] # User and assistant messages
62
- ],
63
  temperature=temperature,
64
- max_tokens=3000,
65
  )
66
- st.markdown(response["choices"][0]["message"]["content"])
 
 
 
 
67
  except Exception as e:
68
  st.write("An error occurred. Please try again later.")
69
  st.write(e)
70
 
71
- st.session_state.messages.append({"role": "assistant", "content": response["choices"][0]["message"]["content"]})
 
 
 
 
 
 
 
27
  # Add reset button to clear conversation
28
  st.button('Reset Chat', on_click=reset_conversation)
29
 
 
 
 
 
 
 
30
  # Initialize chat history
31
  if "messages" not in st.session_state:
32
  st.session_state.messages = []
33
 
 
 
 
 
 
34
  # Accept user input
35
  if prompt := st.chat_input("Type your message here..."):
36
 
 
43
  # Display assistant response in chat message container
44
  with st.chat_message("assistant"):
45
  try:
46
+ # Prepare the messages for the API call
47
+ messages_for_api = [
48
+ {"role": "system", "content": "As a helpful and friendly assistant, provide concise and accurate responses to the user's queries."}
49
+ ] + st.session_state.messages # Add system message and user messages to the list
50
+
51
  response = client.chat.completions.create(
52
  model=model_link,
53
+ messages=messages_for_api,
 
 
 
54
  temperature=temperature,
55
+ max_tokens=150 # Adjust the max tokens according to your needs
56
  )
57
+
58
+ # Get the response content
59
+ assistant_response = response["choices"][0]["message"]["content"]
60
+ st.markdown(assistant_response)
61
+
62
  except Exception as e:
63
  st.write("An error occurred. Please try again later.")
64
  st.write(e)
65
 
66
+ # Append the assistant's response to the chat history
67
+ st.session_state.messages.append({"role": "assistant", "content": assistant_response})
68
+
69
+ # Display chat messages from history on app rerun
70
+ for message in st.session_state.messages:
71
+ with st.chat_message(message["role"]):
72
+ st.markdown(message["content"])