File size: 7,460 Bytes
dd89029
90802da
 
 
dd89029
90802da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9dde93
 
 
90802da
 
 
 
 
e9dde93
90802da
 
 
 
 
e9dde93
 
90802da
 
 
 
e9dde93
90802da
 
 
e9dde93
 
 
 
 
90802da
 
 
 
 
e9dde93
 
 
90802da
e9dde93
 
90802da
 
 
 
e9dde93
90802da
 
 
 
 
 
 
 
 
e9dde93
90802da
 
 
 
 
 
 
e9dde93
 
90802da
 
 
 
 
e9dde93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90802da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9dde93
 
90802da
 
 
 
 
 
 
 
 
 
e9dde93
90802da
 
 
 
 
 
 
 
 
 
 
 
e9dde93
90802da
 
 
 
e9dde93
 
 
 
 
 
 
 
 
 
 
 
90802da
 
 
 
e9dde93
 
 
90802da
e9dde93
 
90802da
 
e9dde93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import streamlit as st
import os
from groq import Groq
import httpx

# Disable proxy environment variables completely
for var in ['http_proxy', 'https_proxy', 'HTTP_PROXY', 'HTTPS_PROXY']:
    if var in os.environ:
        del os.environ[var]

# Page configuration
st.set_page_config(
    page_title="Groq AI Chat",
    page_icon="πŸ€–",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS
st.markdown("""
    <style>
    .main {
        padding: 2rem;
    }
    .stChatMessage {
        padding: 1rem;
        border-radius: 0.5rem;
        margin-bottom: 1rem;
    }
    .css-1dp5vir {
        padding: 2rem;
    }
    </style>
""", unsafe_allow_html=True)

# Title and description
st.title("πŸ€– Groq AI Chat Assistant")
st.markdown("⚑ Powered by Groq's lightning-fast LLM API - Get instant AI responses!")

# Sidebar configuration
with st.sidebar:
    st.header("βš™οΈ Configuration")
    
    st.markdown("---")
    
    # API Key input
    api_key = st.text_input(
        "Enter your Groq API Key",
        type="password",
        help="Get your free API key from https://console.groq.com"
    )
    
    if not api_key:
        st.warning("πŸ”‘ Please enter your Groq API key to proceed.")
    else:
        st.success("βœ… API Key loaded")
    
    st.markdown("---")
    
    # Model selection
    model_option = st.selectbox(
        "Select Model",
        [
            "llama-3.3-70b-versatile",
            "llama-3.1-8b-instant",
            "gemma2-9b-it"
        ],
        help="Choose the AI model to use",
        index=0
    )
    
    # Temperature slider
    temperature = st.slider(
        "Temperature (Creativity)",
        min_value=0.0,
        max_value=2.0,
        value=0.7,
        step=0.1,
        help="Higher = more creative, Lower = more focused"
    )
    
    # Max tokens slider
    max_tokens = st.slider(
        "Max Tokens (Response Length)",
        min_value=100,
        max_value=2048,
        value=1024,
        step=100,
        help="Maximum length of response"
    )
    
    st.markdown("---")
    
    # Clear chat button
    if st.button("πŸ—‘οΈ Clear Chat History", use_container_width=True):
        st.session_state.messages = []
        st.rerun()
    
    st.markdown("---")
    
    # Info section
    st.markdown("**πŸ“‹ Model Information**")
    
    model_info = {
        "llama-3.3-70b-versatile": "Most powerful model - Best for complex tasks",
        "llama-3.1-8b-instant": "Fastest responses - Good for quick questions",
        "gemma2-9b-it": "Balanced performance - Good all-rounder"
    }
    
    st.info(f"**{model_option}**\n\n{model_info[model_option]}")
    
    st.markdown("---")
    
    st.markdown("**πŸ”— Resources**")
    st.markdown("""
    - [Get Groq API Key](https://console.groq.com)
    - [Groq Documentation](https://console.groq.com/docs)
    - [API Status](https://status.groq.com)
    """)

# Initialize session state for chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Chat input and processing
if api_key:
    if prompt := st.chat_input("Ask me anything..."):
        # Add user message to history
        st.session_state.messages.append({"role": "user", "content": prompt})
        
        # Display user message
        with st.chat_message("user"):
            st.markdown(prompt)
        
        # Get AI response
        try:
            # Create HTTP client without proxy
            http_client = httpx.Client(
                timeout=30.0,
                limits=httpx.Limits(max_connections=5, max_keepalive_connections=2)
            )
            
            # Initialize Groq client with custom HTTP client
            client = Groq(
                api_key=api_key,
                http_client=http_client
            )
            
            with st.chat_message("assistant"):
                message_placeholder = st.empty()
                
                # Call Groq API
                response = client.chat.completions.create(
                    model=model_option,
                    messages=st.session_state.messages,
                    temperature=temperature,
                    max_tokens=max_tokens,
                    top_p=1
                )
                
                assistant_message = response.choices[0].message.content
                message_placeholder.markdown(assistant_message)
                
                # Add assistant response to history
                st.session_state.messages.append({
                    "role": "assistant",
                    "content": assistant_message
                })
            
            # Close HTTP client
            http_client.close()
        
        except Exception as e:
            error_str = str(e).lower()
            full_error = str(e)
            
            if "proxies" in error_str or "proxy" in error_str:
                st.error("❌ Proxy Error - Trying alternative connection method...")
                st.info("Refresh the page and try again. If problem persists, restart the Space.")
            elif "authentication" in error_str or "401" in error_str or "invalid" in error_str or "api_error" in error_str:
                st.error("❌ Invalid API Key")
                st.warning("Please check your Groq API key:")
                st.info("""
                1. Go to https://console.groq.com
                2. Click API Keys in sidebar
                3. Copy your key (should start with 'gsk_')
                4. Paste it above - NO extra spaces!
                5. Make sure your account has credits
                """)
                with st.expander("Show full error"):
                    st.code(full_error)
            elif "rate_limit" in error_str or "429" in error_str:
                st.error("❌ Rate limit reached - Please wait a moment and try again")
            elif "connection" in error_str:
                st.error("❌ Connection Error - Please check your internet connection")
            elif "decommissioned" in error_str or "model" in error_str:
                st.error("❌ Model Error - This model is no longer available")
                st.info("Try selecting a different model from the sidebar")
            else:
                st.error(f"❌ Error: {full_error}")
                st.info("Make sure you have an active Groq account with available credits")

else:
    st.info("πŸ‘ˆ Please enter your Groq API key in the sidebar to start chatting.")
    
    st.markdown("---")
    
    st.markdown("## πŸš€ Getting Started")
    st.markdown("""
    1. **Get API Key**: Visit [console.groq.com](https://console.groq.com) and sign up (free)
    2. **Create Key**: Go to API Keys section and create a new API key
    3. **Paste Key**: Paste your key in the sidebar on the left
    4. **Start Chatting**: Type your message and press Enter!
    
    ### Features
    - ⚑ Lightning-fast responses
    - 🎯 Multiple AI models to choose from
    - πŸŽ›οΈ Adjustable temperature and response length
    - πŸ’Ύ Chat history management
    - 🌐 Works on desktop and mobile
    """)

# Footer
st.markdown("---")
st.markdown("""
<div style="text-align: center; color: gray; font-size: 12px;">
    Made with Streamlit & Groq API | Β© 2025
</div>
""", unsafe_allow_html=True)