Day 14 · 1 Hour
LLM with Groq API
Add an AI doubt assistant to your Django app — Groq runs LLaMA 3 at 800 tokens/sec, free.
🔥 Real-World Hook (0–5 min)
Groq runs LLaMA 3 at 800 tokens per second — that's faster than reading. For free.
Every startup building AI features in India right now is using Groq for speed and cost. Today you add an AI feature to your Django app in 12 lines of Python — a student doubt assistant that answers programming questions instantly.
🌐 LLM = Function Call that Returns Text (5–25 min)
💡 Demystified
An LLM API is just a POST request. You send: {"model": "llama3", "messages": [{"role": "user", "content": "Explain recursion"}]}. You get back: {"content": "Recursion is a function calling itself..."}. That's it. No magic — just HTTP.
💻 Live Coding — AI Doubt Assistant (25–45 min)
# pip install groq from groq import Groq client = Groq(api_key="your-groq-api-key") def ask_ai(question): response = client.chat.completions.create( model="llama-3.3-70b-versatile", messages=[ {"role": "system", "content": "You are a Python tutor helping beginners. Be concise and use code examples."}, {"role": "user", "content": question} ] ) return response.choices[0].message.content # Test it answer = ask_ai("What is the difference between a list and a tuple in Python?") print(answer)
# Django view: POST /api/ask/ → returns AI answer from rest_framework.views import APIView from rest_framework.response import Response from groq import Groq client = Groq(api_key=settings.GROQ_API_KEY) class AskAIView(APIView): def post(self, request): question = request.data.get("question", "") if not question: return Response({"error": "question required"}, status=400) response = client.chat.completions.create( model="llama-3.3-70b-versatile", messages=[ {"role": "system", "content": "You are a Python tutor. Be concise."}, {"role": "user", "content": question} ] ) answer = response.choices[0].message.content return Response({"answer": answer})
🎯 You Code It (45–55 min)
- Create a free Groq account at console.groq.com and get an API key
- Install:
pip install groq - Test the
ask_ai()function with 3 Python questions from your notebook - Add
AskAIViewto your Django project, wire upPOST /api/ask/ - Build a simple HTML chat interface: text input + submit → fetch
/api/ask/→ show response - Bonus: Add conversation history — store previous Q&A in a list and pass all messages to the API
- Show: Working chat interface answering Python questions using Groq
📄 Day 14 Cheat Sheet — Groq LLM API
Groq Setup
| pip install groq | Install |
| from groq import Groq | Import |
| Groq(api_key="...") | Create client |
| store key in .env | Never hardcode |
| console.groq.com | Free API key |
Chat Completion
| client.chat.completions.create() | Main call |
| model="llama-3.3-70b-versatile" | Fastest free model |
| messages=[{role, content}] | Conversation array |
| role: "system" | Set AI persona |
| role: "user" | User message |
| role: "assistant" | AI's prior reply |
Response
| response.choices[0] | First choice |
| .message.content | The text reply |
| response.usage.total_tokens | Tokens used |
| max_tokens=500 | Limit response length |
| temperature=0.7 | Creativity (0=deterministic) |
Prompt Engineering
| system prompt | Sets AI's role and style |
| "Be concise" | Shorter answers |
| "Use code examples" | More code in response |
| "Explain to a beginner" | Simpler language |
| conversation history | Pass all prior messages |