Spaces:
Sleeping
Sleeping
|
Download GROQ_API_FIX.md from PrernaKu/GraphRagEx: direct link, hf CLI and curl.
- Browser
- Download file 3.51 kB
-
https://huggingface.co/spaces/PrernaKu/GraphRagEx/resolve/main/GROQ_API_FIX.md
- Command line
-
hf download hf://spaces/PrernaKu/GraphRagEx/GROQ_API_FIX.md
-
curl -L -o GROQ_API_FIX.md https://huggingface.co/spaces/PrernaKu/GraphRagEx/resolve/main/GROQ_API_FIX.md
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):
groq_client.messages.create(...) β Does not exist
message.content[0].text β Wrong format
New API (correct):
groq_client.chat.completions.create(...) β
Correct
message.choices[0].message.content β
Correct
Solution
Updated lines 293-307 in app.py:
Before (Broken)
message = groq_client.messages.create(
model="mixtral-8x7b-32768",
max_tokens=500,
messages=[...]
)
answer = message.content[0].text.strip()
After (Fixed)
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
docker-compose restart
Python
# Stop (Ctrl+C)
python app.py
HF Spaces
git add app.py
git commit -m "Fix: Update Groq API call to correct format"
git push
Testing
- Upload a document (CSV, PDF, or TXT)
- Wait for "ready" status
- Ask a question
- Expected: Get answer (not error)
- Result: β Should work!
Verification
After restarting, test:
# 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:
# 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)