Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,7 @@ from typing import Generator
|
|
5 |
from groq import Groq
|
6 |
|
7 |
_ = load_dotenv(find_dotenv())
|
8 |
-
st.set_page_config(page_icon="π", layout="wide", page_title="
|
9 |
-
|
10 |
-
# Define multiple Groq API keys as environment variables
|
11 |
-
groq_api_keys = os.environ.get('GROQ_API_KEYS', '').split(',')
|
12 |
|
13 |
def icon(emoji: str):
|
14 |
"""Shows an emoji as a Notion-style page icon."""
|
@@ -17,42 +14,12 @@ def icon(emoji: str):
|
|
17 |
unsafe_allow_html=True,
|
18 |
)
|
19 |
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
st.subheader("TypeGPT.net", divider="rainbow", anchor=False)
|
24 |
-
|
25 |
-
# Select a Groq API key from the list
|
26 |
-
selected_api_key = st.selectbox("Choose a Groq API key:", options=groq_api_keys)
|
27 |
-
|
28 |
-
client = Groq(
|
29 |
-
api_key=selected_api_key,
|
30 |
-
)
|
31 |
-
|
32 |
-
# Initialize chat history and selected model
|
33 |
-
if "messages" not in st.session_state:
|
34 |
-
st.session_state.messages = []
|
35 |
-
|
36 |
-
if "selected_model" not in st.session_state:
|
37 |
-
st.session_state.selected_model = None
|
38 |
-
|
39 |
-
# Define model details
|
40 |
-
models = {
|
41 |
-
"llama3-70b-8192": {"name": "LLaMA3-70b", "tokens": 8192, "developer": "Meta"},
|
42 |
-
"llama3-8b-8192": {"name": "LLaMA3-8b", "tokens": 8192, "developer": "Meta"},
|
43 |
-
"gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 8192, "developer": "Google"},
|
44 |
-
"mixtral-8x7b-32768": {
|
45 |
-
"name": "Mixtral-8x7b-Instruct-v0.1",
|
46 |
-
"tokens": 32768,
|
47 |
-
"developer": "Mistral",
|
48 |
-
},
|
49 |
-
}
|
50 |
-
|
51 |
-
# Layout for model selection and max_tokens slider
|
52 |
-
col1, col2 = st.columns([1, 3]) # Adjust the ratio to make the first column smaller
|
53 |
-
|
54 |
|
55 |
-
with
|
56 |
model_option = st.selectbox(
|
57 |
"Choose a model:",
|
58 |
options=list(models.keys()),
|
@@ -72,6 +39,13 @@ with col1:
|
|
72 |
if system_prompt := st.text_input("System Prompt"):
|
73 |
system_message = {"role": "system", "content": system_prompt}
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
# Detect model change and clear chat history if model has changed
|
76 |
if st.session_state.selected_model != model_option:
|
77 |
st.session_state.messages = []
|
@@ -80,14 +54,13 @@ if st.session_state.selected_model != model_option:
|
|
80 |
# Add a "Clear Chat" button
|
81 |
if st.button("Clear Chat"):
|
82 |
st.session_state.messages = []
|
83 |
-
|
84 |
# Display chat messages from history on app rerun
|
85 |
for message in st.session_state.messages:
|
86 |
-
avatar = "
|
87 |
with st.chat_message(message["role"], avatar=avatar):
|
88 |
st.markdown(message["content"])
|
89 |
|
90 |
-
|
91 |
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
92 |
"""Yield chat response content from the Groq API response."""
|
93 |
for chunk in chat_completion:
|
@@ -97,7 +70,7 @@ def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
|
97 |
if prompt := st.chat_input("Enter your prompt here..."):
|
98 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
99 |
|
100 |
-
with st.chat_message("user", avatar="
|
101 |
st.markdown(prompt)
|
102 |
|
103 |
messages=[
|
@@ -115,7 +88,7 @@ if prompt := st.chat_input("Enter your prompt here..."):
|
|
115 |
)
|
116 |
|
117 |
# Use the generator function with st.write_stream
|
118 |
-
with st.chat_message("assistant", avatar="
|
119 |
chat_responses_generator = generate_chat_responses(chat_completion)
|
120 |
full_response = st.write_stream(chat_responses_generator)
|
121 |
except Exception as e:
|
@@ -131,4 +104,4 @@ if prompt := st.chat_input("Enter your prompt here..."):
|
|
131 |
combined_response = "\n".join(str(item) for item in full_response)
|
132 |
st.session_state.messages.append(
|
133 |
{"role": "assistant", "content": combined_response}
|
134 |
-
)
|
|
|
5 |
from groq import Groq
|
6 |
|
7 |
_ = load_dotenv(find_dotenv())
|
8 |
+
st.set_page_config(page_icon="π", layout="wide", page_title="Groq & LLaMA3 Chat Bot...")
|
|
|
|
|
|
|
9 |
|
10 |
def icon(emoji: str):
|
11 |
"""Shows an emoji as a Notion-style page icon."""
|
|
|
14 |
unsafe_allow_html=True,
|
15 |
)
|
16 |
|
17 |
+
st.subheader("Groq Chat with LLaMA3 App", divider="rainbow", anchor=False)
|
18 |
|
19 |
+
api_keys = os.environ['GROQ_API_KEYS'].split(',')
|
20 |
+
clients = [Groq(api_key=key) for key in api_keys]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
with st.sidebar:
|
23 |
model_option = st.selectbox(
|
24 |
"Choose a model:",
|
25 |
options=list(models.keys()),
|
|
|
39 |
if system_prompt := st.text_input("System Prompt"):
|
40 |
system_message = {"role": "system", "content": system_prompt}
|
41 |
|
42 |
+
# Initialize chat history and selected model
|
43 |
+
if "messages" not in st.session_state:
|
44 |
+
st.session_state.messages = []
|
45 |
+
|
46 |
+
if "selected_model" not in st.session_state:
|
47 |
+
st.session_state.selected_model = None
|
48 |
+
|
49 |
# Detect model change and clear chat history if model has changed
|
50 |
if st.session_state.selected_model != model_option:
|
51 |
st.session_state.messages = []
|
|
|
54 |
# Add a "Clear Chat" button
|
55 |
if st.button("Clear Chat"):
|
56 |
st.session_state.messages = []
|
57 |
+
|
58 |
# Display chat messages from history on app rerun
|
59 |
for message in st.session_state.messages:
|
60 |
+
avatar = "π" if message["role"] == "assistant" else "π§βπ»"
|
61 |
with st.chat_message(message["role"], avatar=avatar):
|
62 |
st.markdown(message["content"])
|
63 |
|
|
|
64 |
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
65 |
"""Yield chat response content from the Groq API response."""
|
66 |
for chunk in chat_completion:
|
|
|
70 |
if prompt := st.chat_input("Enter your prompt here..."):
|
71 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
72 |
|
73 |
+
with st.chat_message("user", avatar="π§βπ»"):
|
74 |
st.markdown(prompt)
|
75 |
|
76 |
messages=[
|
|
|
88 |
)
|
89 |
|
90 |
# Use the generator function with st.write_stream
|
91 |
+
with st.chat_message("assistant", avatar="π"):
|
92 |
chat_responses_generator = generate_chat_responses(chat_completion)
|
93 |
full_response = st.write_stream(chat_responses_generator)
|
94 |
except Exception as e:
|
|
|
104 |
combined_response = "\n".join(str(item) for item in full_response)
|
105 |
st.session_state.messages.append(
|
106 |
{"role": "assistant", "content": combined_response}
|
107 |
+
)
|