Niansuh commited on
Commit
7b3b747
Β·
verified Β·
1 Parent(s): 8752f5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -116
app.py CHANGED
@@ -18,122 +18,146 @@ def icon(emoji: str):
18
 
19
  st.subheader("Groq Chat with LLaMA3 App", divider="rainbow", anchor=False)
20
 
21
- # Get the API keys from the environment variable
22
- api_keys = os.environ['GROQ_API_KEYS'].split(',')
23
-
24
- # Initialize the Groq client with the first API key
25
- client = None
26
- for api_key in api_keys:
27
- try:
28
- client = Groq(api_key=api_key)
29
- break
30
- except Exception as e:
31
- st.error(f"Failed to initialize client with API key {api_key}: {e}")
32
- continue
33
-
34
- if client is None:
35
- st.error("Failed to initialize client with any API key")
36
- st.stop()
37
-
38
- # Initialize chat history and selected model
39
- if "messages" not in st.session_state:
40
- st.session_state.messages = []
41
-
42
- if "selected_model" not in st.session_state:
43
- st.session_state.selected_model = None
44
-
45
- # Define model details
46
- models = {
47
- "llama3-70b-8192": {"name": "LLaMA3-70b", "tokens": 8192, "developer": "Meta"},
48
- "llama3-8b-8192": {"name": "LLaMA3-8b", "tokens": 8192, "developer": "Meta"},
49
- "llama2-70b-4096": {"name": "LLaMA2-70b-chat", "tokens": 4096, "developer": "Meta"},
50
- "gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 8192, "developer": "Google"},
51
- "mixtral-8x7b-32768": {
52
- "name": "Mixtral-8x7b-Instruct-v0.1",
53
- "tokens": 32768,
54
- "developer": "Mistral",
55
- },
56
- }
57
-
58
- # Layout for model selection and max_tokens slider
59
- col1, col2 = st.columns([1, 3]) # Adjust the ratio to make the first column smaller
60
-
61
- with col1:
62
- model_option = st.selectbox(
63
- "Choose a model:",
64
- options=list(models.keys()),
65
- format_func=lambda x: models[x]["name"],
66
- index=0, # Default to the first model in the list
67
- )
68
- max_tokens_range = models[model_option]["tokens"]
69
- max_tokens = st.slider(
70
- "Max Tokens:",
71
- min_value=512,
72
- max_value=max_tokens_range,
73
- value=min(32768, max_tokens_range),
74
- step=512,
75
- help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
76
- )
77
- system_message = {}
78
- if system_prompt := st.text_input("System Prompt"):
79
- system_message = {"role": "system", "content": system_prompt}
80
-
81
- # Detect model change and clear chat history if model has changed
82
- if st.session_state.selected_model != model_option:
83
- st.session_state.messages = []
84
- st.session_state.selected_model = model_option
85
-
86
- # Add a "Clear Chat" button
87
- if st.button("Clear Chat"):
88
- st.session_state.messages = []
89
-
90
- # Display chat messages from history on app rerun
91
- for message in st.session_state.messages:
92
- avatar = "πŸ”‹" if message["role"] == "assistant" else "πŸ§‘β€πŸ’»"
93
- with st.chat_message(message["role"], avatar=avatar):
94
- st.markdown(message["content"])
95
-
96
- def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
97
- """Yield chat response content from the Groq API response."""
98
- for chunk in chat_completion:
99
- if chunk.choices[0].delta.content:
100
- yield chunk.choices[0].delta.content
101
-
102
- if prompt := st.chat_input("Enter your prompt here..."):
103
- st.session_state.messages.append({"role": "user", "content": prompt})
104
-
105
- with st.chat_message("user", avatar="πŸ§‘β€πŸ’»"):
106
- st.markdown(prompt)
107
-
108
- messages=[
109
- {"role": m["role"], "content": m["content"]}
110
- for m in st.session_state.messages]
111
- if system_message:
112
- messages.insert(0,system_message)
113
- # Fetch response from Groq API
114
- try:
115
- chat_completion = client.chat.completions.create(
116
- model=model_option,
117
- messages=messages,
118
- max_tokens=max_tokens,
119
- stream=True,
120
  )
121
-
122
- # Use the generator function with st.write_stream
123
- with st.chat_message("assistant", avatar="πŸ”‹"):
124
- chat_responses_generator = generate_chat_responses(chat_completion)
125
- full_response = st.write_stream(chat_responses_generator)
126
- except Exception as e:
127
- st.error(e, icon="❌")
128
-
129
- # Append the full response to session_state.messages
130
- if isinstance(full_response, str):
131
- st.session_state.messages.append(
132
- {"role": "assistant", "content": full_response}
133
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  else:
135
- # Handle the case where full_response is not a string
136
- combined_response = "\n".join(str(item) for item in full_response)
137
- st.session_state.messages.append(
138
- {"role": "assistant", "content": combined_response}
139
- )
 
18
 
19
  st.subheader("Groq Chat with LLaMA3 App", divider="rainbow", anchor=False)
20
 
21
+ # Create a settings page
22
+ def settings_page():
23
+ st.title("Settings")
24
+ api_keys = st.text_input("Enter your API keys (comma-separated):")
25
+ if st.button("Save"):
26
+ os.environ['GROQ_API_KEYS'] = api_keys
27
+ st.success("API keys saved successfully!")
28
+
29
+ # Create a main page
30
+ def main_page():
31
+ # Get the API keys from the environment variable
32
+ api_keys = os.environ.get('GROQ_API_KEYS')
33
+ if api_keys is None:
34
+ st.error("Please set your API keys in the settings page.")
35
+ return
36
+
37
+ # Initialize the Groq client with the first API key
38
+ client = None
39
+ for api_key in api_keys.split(','):
40
+ try:
41
+ client = Groq(api_key=api_key)
42
+ break
43
+ except Exception as e:
44
+ st.error(f"Failed to initialize client with API key {api_key}: {e}")
45
+ continue
46
+
47
+ if client is None:
48
+ st.error("Failed to initialize client with any API key")
49
+ st.stop()
50
+
51
+ # Initialize chat history and selected model
52
+ if "messages" not in st.session_state:
53
+ st.session_state.messages = []
54
+
55
+ if "selected_model" not in st.session_state:
56
+ st.session_state.selected_model = None
57
+
58
+ # Define model details
59
+ models = {
60
+ "llama3-70b-8192": {"name": "LLaMA3-70b", "tokens": 8192, "developer": "Meta"},
61
+ "llama3-8b-8192": {"name": "LLaMA3-8b", "tokens": 8192, "developer": "Meta"},
62
+ "llama2-70b-4096": {"name": "LLaMA2-70b-chat", "tokens": 4096, "developer": "Meta"},
63
+ "gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 8192, "developer": "Google"},
64
+ "mixtral-8x7b-32768": {
65
+ "name": "Mixtral-8x7b-Instruct-v0.1",
66
+ "tokens": 32768,
67
+ "developer": "Mistral",
68
+ },
69
+ }
70
+
71
+ # Layout for model selection and max_tokens slider
72
+ col1, col2 = st.columns([1, 3]) # Adjust the ratio to make the first column smaller
73
+
74
+ with col1:
75
+ model_option = st.selectbox(
76
+ "Choose a model:",
77
+ options=list(models.keys()),
78
+ format_func=lambda x: models[x]["name"],
79
+ index=0, # Default to the first model in the list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  )
81
+ max_tokens_range = models[model_option]["tokens"]
82
+ max_tokens = st.slider(
83
+ "Max Tokens:",
84
+ min_value=512,
85
+ max_value=max_tokens_range,
86
+ value=min(32768, max_tokens_range),
87
+ step=512,
88
+ help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
 
 
 
 
89
  )
90
+ system_message = {}
91
+ if system_prompt := st.text_input("System Prompt"):
92
+ system_message = {"role": "system", "content": system_prompt}
93
+
94
+ # Detect model change and clear chat history if model has changed
95
+ if st.session_state.selected_model != model_option:
96
+ st.session_state.messages = []
97
+ st.session_state.selected_model = model_option
98
+
99
+ # Add a "Clear Chat" button
100
+ if st.button("Clear Chat"):
101
+ st.session_state.messages = []
102
+
103
+ # Display chat messages from history on app rerun
104
+ for message in st.session_state.messages:
105
+ avatar = "πŸ”‹" if message["role"] == "assistant" else "πŸ§‘β€πŸ’»"
106
+ with st.chat_message(message["role"], avatar=avatar):
107
+ st.markdown(message["content"])
108
+
109
+ def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
110
+ """Yield chat response content from the Groq API response."""
111
+ for chunk in chat_completion:
112
+ if chunk.choices[0].delta.content:
113
+ yield chunk.choices[0].delta.content
114
+
115
+ if prompt := st.chat_input("Enter your prompt here..."):
116
+ st.session_state.messages.append({"role": "user", "content": prompt})
117
+
118
+ with st.chat_message("user", avatar="πŸ§‘β€πŸ’»"):
119
+ st.markdown(prompt)
120
+
121
+ messages=[
122
+ {"role": m["role"], "content": m["content"]}
123
+ for m in st.session_state.messages]
124
+ if system_message:
125
+ messages.insert(0,system_message)
126
+ # Fetch response from Groq API
127
+ try:
128
+ chat_completion = client.chat.completions.create(
129
+ model=model_option,
130
+ messages=messages,
131
+ max_tokens=max_tokens,
132
+ stream=True,
133
+ )
134
+
135
+ # Use the generator function with st.write_stream
136
+ with st.chat_message("assistant", avatar="πŸ”‹"):
137
+ chat_responses_generator = generate_chat_responses(chat_completion)
138
+ full_response = st.write_stream(chat_responses_generator)
139
+ except Exception as e:
140
+ st.error(e, icon="❌")
141
+
142
+ # Append the full response to session_state.messages
143
+ if isinstance(full_response, str):
144
+ st.session_state.messages.append(
145
+ {"role": "assistant", "content": full_response}
146
+ )
147
+ else:
148
+ # Handle the case where full_response is not a string
149
+ combined_response = "\n".join(str(item) for item in full_response)
150
+ st.session_state.messages.append(
151
+ {"role": "assistant", "content": combined_response}
152
+ )
153
+
154
+ # Create a sidebar with a settings button
155
+ with st.sidebar:
156
+ if st.button("Settings"):
157
+ settings_page()
158
  else:
159
+ main_page()
160
+
161
+ # If the user is not in the settings page, show the main page
162
+ if not st.sidebar.button("Settings"):
163
+ main_page()