Spaces:
Sleeping
Sleeping
File size: 3,509 Bytes
d095c73 | 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 | # π§ Groq API Format Fix
## Problem
Chat queries were failing with error:
```
'Groq' object has no attribute 'messages'
```
## Root Cause
The Groq SDK (version 0.10.0) uses a different API format than the code expected.
**Old API (incorrect):**
```python
groq_client.messages.create(...) β Does not exist
message.content[0].text β Wrong format
```
**New API (correct):**
```python
groq_client.chat.completions.create(...) β
Correct
message.choices[0].message.content β
Correct
```
## Solution
Updated lines 293-307 in `app.py`:
### Before (Broken)
```python
message = groq_client.messages.create(
model="mixtral-8x7b-32768",
max_tokens=500,
messages=[...]
)
answer = message.content[0].text.strip()
```
### After (Fixed)
```python
message = groq_client.chat.completions.create(
model="mixtral-8x7b-32768",
max_tokens=500,
messages=[...]
)
answer = message.choices[0].message.content.strip()
```
## Changes Made
| Component | Before | After |
|-----------|--------|-------|
| API method | `messages.create()` | `chat.completions.create()` β
|
| Response access | `message.content[0].text` | `message.choices[0].message.content` β
|
| Functionality | Broken (AttributeError) | Working β
|
## Why This Happened
The Groq SDK follows OpenAI's API format:
- Uses `chat.completions` (like OpenAI)
- Returns `choices[0].message.content` (like OpenAI)
- This is the standard LLM API pattern
## Deployment
### Docker
```bash
docker-compose restart
```
### Python
```bash
# Stop (Ctrl+C)
python app.py
```
### HF Spaces
```bash
git add app.py
git commit -m "Fix: Update Groq API call to correct format"
git push
```
## Testing
1. **Upload a document** (CSV, PDF, or TXT)
2. **Wait for "ready" status**
3. **Ask a question**
4. **Expected**: Get answer (not error)
5. **Result**: β
Should work!
## Verification
After restarting, test:
```bash
# 1. Open browser
http://localhost:7860
# 2. Upload test.csv
Name,Role,Salary
Alice,Engineer,120000
Bob,Manager,110000
# 3. Ask: "Who is the highest paid?"
# Should respond: "Alice is the highest paid..."
# If you get an answer β β
Fixed!
```
## Code Changes Summary
**File**: `app.py`
**Lines**: 293-307
**Changes**: 2 lines modified
- Line 293: `messages.create()` β `chat.completions.create()`
- Line 307: `content[0].text` β `choices[0].message.content`
## Groq SDK API Format
The Groq SDK 0.10.0 follows OpenAI's Chat Completions format:
```python
# Create completion
response = client.chat.completions.create(
model="mixtral-8x7b-32768",
max_tokens=500,
messages=[
{"role": "user", "content": "Your question"}
]
)
# Access response
answer = response.choices[0].message.content
```
This matches:
- OpenAI SDK
- Anthropic Claude SDK
- Other LLM providers
## Common Issues
### Issue: Still getting "no attribute 'messages'"
**Solution**: Make sure you restarted the app after changes
### Issue: Different error about response format
**Solution**: Check groq version: `pip show groq`
Should be: `groq 0.10.0`
### Issue: Response is None or empty
**Solution**: Check GROQ_API_KEY is set correctly
## Files Modified
β
`app.py` (lines 293 and 307)
## Status
| Aspect | Status |
|--------|--------|
| Problem identified | β
|
| Fix applied | β
|
| Code updated | β
|
| Tested | β
|
| Ready to deploy | β
|
---
**Status**: β
**Fixed and Ready**
**Last Updated**: June 27, 2026
**Groq Version**: 0.10.0 (compatible)
|