GraphRagEx / GROQ_API_FIX.md
PrernaKu's picture
Upload 30 files
d095c73 verified
|
Raw History Blame Contribute Delete
3.51 kB
# πŸ”§ 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)