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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -8,10 +8,10 @@ load_dotenv()
8
  # Initialize the client
9
  client = OpenAI(
10
  base_url="https://api-inference.huggingface.co/v1",
11
- api_key=os.getenv('HUGGINGFACEHUB_API_TOKEN') # Ensure the environment variable is set correctly
12
  )
13
 
14
- # Model link
15
  model_link = "mistralai/Mistral-7B-Instruct-v0.2"
16
 
17
  def reset_conversation():
@@ -21,27 +21,27 @@ def reset_conversation():
21
  st.session_state.messages = []
22
  return None
23
 
 
 
 
 
 
24
  # Set the temperature value directly in the code
25
  temperature = 0.5
26
 
27
- # Add a button to clear conversation
28
- st.button('Reset Chat', on_click=reset_conversation) # Reset button
 
 
 
 
 
 
29
 
30
  # Initialize chat history
31
  if "messages" not in st.session_state:
32
  st.session_state.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:
47
  with st.chat_message(message["role"]):
@@ -49,34 +49,29 @@ for message in st.session_state.messages:
49
 
50
  # Accept user input
51
  if prompt := st.chat_input("Type your message here..."):
 
52
  # Display user message in chat message container
53
  with st.chat_message("user"):
54
  st.markdown(prompt)
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,
 
 
 
 
67
  temperature=temperature,
68
- max_tokens=150 # Adjust the token limit as needed
69
  )
70
-
71
- if 'choices' in response and response['choices']:
72
- content = response['choices'][0]['message']['content']
73
- st.markdown(content)
74
- st.session_state.messages.append({"role": "assistant", "content": content})
75
- else:
76
- st.markdown("No response received from the assistant.")
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
- st.session_state.messages.append({"role": "assistant", "content": "An error occurred. Please try again later."})
 
8
  # Initialize the client
9
  client = OpenAI(
10
  base_url="https://api-inference.huggingface.co/v1",
11
+ api_key=os.getenv('HUGGINGFACEHUB_API_TOKEN')
12
  )
13
 
14
+ # Model link for Mistral
15
  model_link = "mistralai/Mistral-7B-Instruct-v0.2"
16
 
17
  def reset_conversation():
 
21
  st.session_state.messages = []
22
  return None
23
 
24
+ # Define the available models
25
+ model_links = {
26
+ "Mistral-7B": "mistralai/Mistral-7B-Instruct-v0.2"
27
+ }
28
+
29
  # Set the temperature value directly in the code
30
  temperature = 0.5
31
 
32
+ # Add reset button to clear conversation
33
+ st.button('Reset Chat', on_click=reset_conversation)
34
+
35
+ # Define a system message for the Mistral model
36
+ system_message = {
37
+ "role": "system",
38
+ "content": "As a helpful and friendly assistant, provide concise and accurate responses to the user's queries."
39
+ }
40
 
41
  # Initialize chat history
42
  if "messages" not in st.session_state:
43
  st.session_state.messages = []
44
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Display chat messages from history on app rerun
46
  for message in st.session_state.messages:
47
  with st.chat_message(message["role"]):
 
49
 
50
  # Accept user input
51
  if prompt := st.chat_input("Type your message here..."):
52
+
53
  # Display user message in chat message container
54
  with st.chat_message("user"):
55
  st.markdown(prompt)
56
  # Add user message to chat history
57
  st.session_state.messages.append({"role": "user", "content": prompt})
58
 
 
 
 
59
  # Display assistant response in chat message container
60
  with st.chat_message("assistant"):
61
  try:
62
  response = client.chat.completions.create(
63
  model=model_link,
64
+ messages=[
65
+ system_message,
66
+ {"role": m["role"], "content": m["content"]}
67
+ for m in st.session_state.messages
68
+ ],
69
  temperature=temperature,
70
+ max_tokens=3000,
71
  )
72
+ st.markdown(response["choices"][0]["message"]["content"])
 
 
 
 
 
 
 
73
  except Exception as e:
74
+ st.write("An error occurred. Please try again later.")
75
+ st.write(e)
76
+
77
+ st.session_state.messages.append({"role": "assistant", "content": response["choices"][0]["message"]["content"]})