Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import find_dotenv, load_dotenv
|
3 |
+
import streamlit as st
|
4 |
+
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="Groq & LLaMA3 Chat Bot...")
|
9 |
+
|
10 |
+
|
11 |
+
def icon(emoji: str):
|
12 |
+
"""Shows an emoji as a Notion-style page icon."""
|
13 |
+
st.write(
|
14 |
+
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
|
15 |
+
unsafe_allow_html=True,
|
16 |
+
)
|
17 |
+
|
18 |
+
|
19 |
+
# icon("β‘οΈ")
|
20 |
+
|
21 |
+
st.subheader("Groq Chat with LLaMA3 App", divider="rainbow", anchor=False)
|
22 |
+
|
23 |
+
client = Groq(
|
24 |
+
api_key=os.environ['GROQ_API_KEY'],
|
25 |
+
)
|
26 |
+
|
27 |
+
# Initialize chat history and selected model
|
28 |
+
if "messages" not in st.session_state:
|
29 |
+
st.session_state.messages = []
|
30 |
+
|
31 |
+
if "selected_model" not in st.session_state:
|
32 |
+
st.session_state.selected_model = None
|
33 |
+
|
34 |
+
# Define model details
|
35 |
+
models = {
|
36 |
+
"llama3-70b-8192": {"name": "LLaMA3-70b", "tokens": 8192, "developer": "Meta"},
|
37 |
+
"llama3-8b-8192": {"name": "LLaMA3-8b", "tokens": 8192, "developer": "Meta"},
|
38 |
+
"llama2-70b-4096": {"name": "LLaMA2-70b-chat", "tokens": 4096, "developer": "Meta"},
|
39 |
+
"gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 8192, "developer": "Google"},
|
40 |
+
"mixtral-8x7b-32768": {
|
41 |
+
"name": "Mixtral-8x7b-Instruct-v0.1",
|
42 |
+
"tokens": 32768,
|
43 |
+
"developer": "Mistral",
|
44 |
+
},
|
45 |
+
}
|
46 |
+
|
47 |
+
# Layout for model selection and max_tokens slider
|
48 |
+
col1, col2 = st.columns([1, 3]) # Adjust the ratio to make the first column smaller
|
49 |
+
|
50 |
+
|
51 |
+
with col1:
|
52 |
+
model_option = st.selectbox(
|
53 |
+
"Choose a model:",
|
54 |
+
options=list(models.keys()),
|
55 |
+
format_func=lambda x: models[x]["name"],
|
56 |
+
index=0, # Default to the first model in the list
|
57 |
+
)
|
58 |
+
max_tokens_range = models[model_option]["tokens"]
|
59 |
+
max_tokens = st.slider(
|
60 |
+
"Max Tokens:",
|
61 |
+
min_value=512,
|
62 |
+
max_value=max_tokens_range,
|
63 |
+
value=min(32768, max_tokens_range),
|
64 |
+
step=512,
|
65 |
+
help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
|
66 |
+
)
|
67 |
+
system_message = {}
|
68 |
+
if system_prompt := st.text_input("System Prompt"):
|
69 |
+
system_message = {"role": "system", "content": system_prompt}
|
70 |
+
|
71 |
+
# Detect model change and clear chat history if model has changed
|
72 |
+
if st.session_state.selected_model != model_option:
|
73 |
+
st.session_state.messages = []
|
74 |
+
st.session_state.selected_model = model_option
|
75 |
+
|
76 |
+
# Add a "Clear Chat" button
|
77 |
+
if st.button("Clear Chat"):
|
78 |
+
st.session_state.messages = []
|
79 |
+
|
80 |
+
# Display chat messages from history on app rerun
|
81 |
+
for message in st.session_state.messages:
|
82 |
+
avatar = "π" if message["role"] == "assistant" else "π§βπ»"
|
83 |
+
with st.chat_message(message["role"], avatar=avatar):
|
84 |
+
st.markdown(message["content"])
|
85 |
+
|
86 |
+
|
87 |
+
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
88 |
+
"""Yield chat response content from the Groq API response."""
|
89 |
+
for chunk in chat_completion:
|
90 |
+
if chunk.choices[0].delta.content:
|
91 |
+
yield chunk.choices[0].delta.content
|
92 |
+
|
93 |
+
if prompt := st.chat_input("Enter your prompt here..."):
|
94 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
95 |
+
|
96 |
+
with st.chat_message("user", avatar="π§βπ»"):
|
97 |
+
st.markdown(prompt)
|
98 |
+
|
99 |
+
messages=[
|
100 |
+
{"role": m["role"], "content": m["content"]}
|
101 |
+
for m in st.session_state.messages]
|
102 |
+
if system_message:
|
103 |
+
messages.insert(0,system_message)
|
104 |
+
# Fetch response from Groq API
|
105 |
+
try:
|
106 |
+
chat_completion = client.chat.completions.create(
|
107 |
+
model=model_option,
|
108 |
+
messages=messages,
|
109 |
+
max_tokens=max_tokens,
|
110 |
+
stream=True,
|
111 |
+
)
|
112 |
+
|
113 |
+
# Use the generator function with st.write_stream
|
114 |
+
with st.chat_message("assistant", avatar="π"):
|
115 |
+
chat_responses_generator = generate_chat_responses(chat_completion)
|
116 |
+
full_response = st.write_stream(chat_responses_generator)
|
117 |
+
except Exception as e:
|
118 |
+
st.error(e, icon="β")
|
119 |
+
|
120 |
+
# Append the full response to session_state.messages
|
121 |
+
if isinstance(full_response, str):
|
122 |
+
st.session_state.messages.append(
|
123 |
+
{"role": "assistant", "content": full_response}
|
124 |
+
)
|
125 |
+
else:
|
126 |
+
# Handle the case where full_response is not a string
|
127 |
+
combined_response = "\n".join(str(item) for item in full_response)
|
128 |
+
st.session_state.messages.append(
|
129 |
+
{"role": "assistant", "content": combined_response}
|
130 |
+
)
|