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