File size: 2,230 Bytes
5271f22
474d6f8
 
c575d92
474d6f8
01a6781
5271f22
474d6f8
 
bc4a33a
474d6f8
bc4a33a
474d6f8
c575d92
bc4a33a
474d6f8
 
 
c575d92
 
 
474d6f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01a6781
474d6f8
5271f22
01a6781
5271f22
474d6f8
 
bc4a33a
 
 
 
 
474d6f8
bc4a33a
 
474d6f8
bc4a33a
 
474d6f8
 
 
 
 
 
bc4a33a
474d6f8
bc4a33a
474d6f8
c575d92
474d6f8
 
 
 
c575d92
 
474d6f8
bc4a33a
01a6781
5271f22
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
import gradio as gr
import requests
import os
from dotenv import load_dotenv
# Load .env (for local testing)
load_dotenv()

# Get API Key
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
    raise ValueError("Missing GROQ_API_KEY")

# Groq API Config
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
MODEL = "llama3-8b-8192"

headers = {
    "Authorization": f"Bearer {GROQ_API_KEY.strip()}",
    "Content-Type": "application/json"
}

# Core Chat Function with updated system prompt
def echo(message, history):
    messages = [
        {
            "role": "system",
            "content": (
                "You are an AI tutor specializing in large language models (LLMs). "
                "Always give short, clear, beginner-friendly answers. "
                "When a user refers to 'RAG', always interpret it as 'Retrieval-Augmented Generation' in the context of LLMs, "
                "not as the traffic-light system (Red, Amber, Green). "
                "Avoid jargon, and break complex ideas into bite-sized explanations. "
                "If the user asks a follow-up question, respond with full awareness of the prior conversation."
            )
        }
    ]

    # Add chat history
    for user_msg, bot_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": bot_msg})
    
    # Append current message
    messages.append({"role": "user", "content": message})

    try:
        response = requests.post(
            GROQ_API_URL,
            headers=headers,
            json={
                "model": MODEL,
                "messages": messages
            }
        )

        if response.status_code == 200:
            reply = response.json()["choices"][0]["message"]["content"]
            return reply
        else:
            return f"Error {response.status_code}: {response.text}"
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio Chat UI
demo = gr.ChatInterface(
    fn=echo,
    title="LLM Mentor",
    examples=["What is an LLM?", "What is RAG in LLMs?", "Explain NLP simply"],
    theme="default"
)

# For Hugging Face compatibility
demo.launch(ssr_mode=False)