File size: 7,832 Bytes
6f050d2
2f22113
 
550f98e
ac1d1c0
 
6f050d2
b64c343
 
663780e
b64c343
663780e
b64c343
ac1d1c0
 
 
663780e
 
862323b
663780e
 
550f98e
 
663780e
862323b
2f22113
550f98e
663780e
550f98e
 
 
ac1d1c0
 
 
 
 
 
 
 
 
 
 
d6cad0d
 
ac1d1c0
 
663780e
ac1d1c0
 
 
 
 
 
 
550f98e
 
ac1d1c0
550f98e
 
 
b64c343
663780e
 
 
 
 
 
 
 
 
 
 
 
 
b64c343
550f98e
 
 
ac1d1c0
663780e
ac1d1c0
 
 
 
 
 
663780e
550f98e
663780e
862323b
663780e
550f98e
862323b
663780e
b64c343
663780e
550f98e
663780e
2f22113
663780e
7103644
ac1d1c0
 
 
663780e
ac1d1c0
663780e
ac1d1c0
 
 
 
 
 
663780e
 
ac1d1c0
 
663780e
550f98e
663780e
 
b64c343
550f98e
663780e
51d2e5a
550f98e
 
 
 
 
 
 
 
 
6f050d2
ac1d1c0
663780e
e480a63
 
ac1d1c0
e480a63
b64c343
663780e
 
 
 
b64c343
663780e
 
 
 
 
 
 
 
 
 
 
 
 
 
e480a63
 
663780e
 
 
 
ac1d1c0
 
 
 
 
 
 
 
 
663780e
 
ac1d1c0
 
 
 
 
 
 
 
 
 
 
 
663780e
 
7108e4b
550f98e
 
 
862323b
550f98e
 
 
a7739e0
550f98e
b64c343
550f98e
 
 
 
2f22113
663780e
550f98e
663780e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
550f98e
7108e4b
4a5989e
550f98e
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
import gradio as gr
import requests
import os
import json
from typing import List, Dict
from datetime import datetime

# Groq API Configuration
API_URL = "https://api.groq.com/openai/v1/chat/completions"
API_KEY = os.getenv("GROQ_API_KEY")

print(f"πŸ”‘ API Key Found: {'Yes' if API_KEY else 'No'}")

# In-memory chat history storage
chat_history: List[Dict[str, str]] = []

def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> str:
    """Groq API call with chat history and optional summarization"""
    
    if not API_KEY:
        return "❌ No API Key found"
    
    if not message.strip():
        return "❌ Empty message"
    
    try:
        headers = {
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json"
        }
        
        # Add current message to history with timestamp and topic
        chat_history.append({
            "role": "user",
            "content": message.strip(),
            "topic": topic,
            "timestamp": datetime.now().isoformat()
        })
        
        # Limit history to max_history messages to avoid token overflow
        recent_history = chat_history[-max_history:]
        
        # Prepare messages for API call, excluding timestamp and topic
        messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
        
        if summarize:
            # Create a prompt to summarize the conversation
            summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n"
            for msg in recent_history:
                if msg["topic"] == topic:
                    summary_prompt += f"{msg['role'].capitalize()}: {msg['content']}\n"
            summary_prompt += "\nProvide a concise summary of the key points discussed."
            messages.append({"role": "user", "content": summary_prompt})
        
        payload = {
            "model": "llama3-8b-8192",
            "messages": messages,
            "max_tokens": 512,
            "temperature": 0.7
        }
        
        print(f"πŸš€ Sending request to: {API_URL}")
        print(f"πŸ“ Payload: {json.dumps(payload, indent=2)}")
        
        response = requests.post(
            API_URL,
            headers=headers,
            json=payload,
            timeout=30
        )
        
        print(f"πŸ“Š Status Code: {response.status_code}")
        print(f"πŸ“‹ Response Headers: {dict(response.headers)}")
        print(f"πŸ“„ Response Text (first 300 chars): {response.text[:300]}")
        
        if response.status_code == 200:
            result = response.json()
            if "choices" in result and len(result["choices"]) > 0:
                response_content = result["choices"][0]["message"]["content"]
                # Add assistant's response to history
                chat_history.append({
                    "role": "assistant",
                    "content": response_content,
                    "topic": topic,
                    "timestamp": datetime.now().isoformat()
                })
                return response_content
            else:
                return f"❌ No choices in response: {json.dumps(result, indent=2)}"
        else:
            return f"❌ HTTP {response.status_code}: {response.text}"
            
    except requests.exceptions.Timeout:
        return "❌ Request timeout (30s)"
    except requests.exceptions.ConnectionError:
        return "❌ Connection error - cannot reach Groq API"
    except json.JSONDecodeError:
        return f"❌ Invalid JSON response: {response.text}"
    except Exception as e:
        return f"❌ Unexpected error: {str(e)}"

def clear_history():
    """Clear the chat history"""
    chat_history.clear()
    return "βœ… Chat history cleared"

def view_history(topic: str = None):
    """View the chat history, optionally filtered by topic"""
    if not chat_history:
        return "❌ No chat history available"
    
    output = "πŸ“œ Chat History:\n\n"
    for msg in chat_history:
        if topic is None or msg["topic"] == topic:
            output += f"[{msg['timestamp']}] {msg['role'].capitalize()} ({msg['topic']}): {msg['content']}\n"
    return output

def manual_curl_example():
    """Generate curl command for manual testing"""
    if not API_KEY:
        return "❌ No API key to generate curl"
    
    curl_cmd = f'''curl -X POST "{API_URL}" \\
  -H "Authorization: Bearer {API_KEY}" \\
  -H "Content-Type: application/json" \\
  -d '{{
    "model": "llama3-8b-8192",
    "messages": [
      {{"role": "user", "content": "Hello, test message"}}
    ],
    "max_tokens": 100
  }}'
'''
    return curl_cmd

# Create Gradio interface
with gr.Blocks(title="Groq Debug with Memory") as demo:
    gr.Markdown("# πŸ” Groq API Debug Tool with Memory")
    
    with gr.Tab("Chat with Memory"):
        gr.Markdown("Chat with Groq, maintain history, and summarize by topic")
        
        chat_input = gr.Textbox(
            label="Message",
            placeholder="Enter your message",
            value="Hello, how can you help me today?"
        )
        topic_input = gr.Textbox(
            label="Topic",
            placeholder="Enter topic (e.g., 'coding', 'general')",
            value="general"
        )
        summarize_checkbox = gr.Checkbox(
            label="Summarize conversation for this topic",
            value=False
        )
        chat_button = gr.Button("Send Message", variant="primary")
        chat_output = gr.Textbox(
            label="Response",
            lines=10,
            max_lines=20
        )
        
        chat_button.click(
            groq_with_memory,
            inputs=[chat_input, topic_input, summarize_checkbox],
            outputs=[chat_output]
        )
    
    with gr.Tab("View History"):
        gr.Markdown("View or clear chat history")
        
        history_topic = gr.Textbox(
            label="Filter by Topic (optional)",
            placeholder="Enter topic to filter (leave blank for all)"
        )
        history_button = gr.Button("View History")
        clear_button = gr.Button("Clear History", variant="secondary")
        history_output = gr.Textbox(
            label="Chat History",
            lines=10,
            max_lines=20
        )
        
        history_button.click(
            view_history,
            inputs=[history_topic],
            outputs=[history_output]
        )
        clear_button.click(
            clear_history,
            outputs=[history_output]
        )
    
    with gr.Tab("Manual Test"):
        gr.Markdown("Copy this curl command and run it in terminal")
        
        curl_button = gr.Button("Generate Curl Command")
        curl_output = gr.Code(
            label="Curl Command",
            language="shell"
        )
        
        curl_button.click(
            manual_curl_example,
            outputs=[curl_output]
        )
    
    with gr.Tab("API Info"):
        gr.Markdown(f"""
        **Debug Information:**
        - API URL: `{API_URL}`
        - API Key Status: {'βœ… Found' if API_KEY else '❌ Missing'}
        - API Key Preview: `{API_KEY[:10] + '...' if API_KEY else 'None'}`
        
        **Common Issues:**
        1. **Wrong URL** - Make sure using correct Groq endpoint
        2. **Invalid Model** - Check if model name is correct
        3. **Malformed Request** - Check JSON structure
        4. **Rate Limiting** - Wait between requests
        5. **Network Issues** - Check HF Spaces connectivity
        
        **Expected Models:**
        - `llama3-8b-8192`
        - `llama3-70b-8192`
        - `mixtral-8x7b-32768`
        - `gemma-7b-it`
        """)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)