File size: 1,792 Bytes
f9766bc
 
 
 
 
38aa8e7
f9766bc
5d04006
f9766bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
tutor.py
AI Tutor chat powered by Groq. Maintains conversation history per session.
"""
from groq import Groq
import os

GROQ_API_KEY = os.environ.get("GROQ_API_KEY")

SYSTEM_PROMPT = """You are LearnCraft Tutor — a friendly, encouraging, and highly knowledgeable AI tutor.
Your role is to help students understand topics deeply, answer their questions clearly, and guide them
step-by-step when they are confused.

Guidelines:
- Be concise but thorough. Use examples and analogies generously.
- If a student seems confused, break things down into smaller steps.
- Celebrate correct answers and effort warmly.
- Never just give answers to homework — guide the student to figure it out.
- Format responses with clear structure (use short paragraphs, numbered steps where helpful).
- Keep responses under 200 words unless a complex explanation is needed.
- Always end with a follow-up question or encouragement to keep the student engaged.
"""


def get_tutor_reply(messages: list, topic: str = "") -> str:
    """
    Send conversation history to Groq and return the tutor's reply.
    messages: list of {"role": "user"/"assistant", "content": str}
    """
    system = SYSTEM_PROMPT
    if topic:
        system += f"\n\nThe student is currently studying: {topic}. Focus your answers around this topic when relevant."

    try:
        client = Groq(api_key=GROQ_API_KEY)
        response = client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=[{"role": "system", "content": system}] + messages,
            max_tokens=400,
            temperature=0.7,
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        return f"Sorry, I couldn't connect right now. Please try again! (Error: {str(e)})"