File size: 1,208 Bytes
4a3f117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()

client = OpenAI(
    api_key=os.getenv("GROQ_API_KEY"),
    base_url="https://api.groq.com/openai/v1",
)

MODEL = "llama-3.1-8b-instant"

SYSTEM_PROMPT = (
    "You are a programming tutor specializing in Data Structures, Algorithms, "
    "and Web Development. Answer questions clearly and concisely."
)


def ask_baseline(question: str) -> dict:
    start = time.time()

    response = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": question},
        ],
        max_tokens=300,
        temperature=0.3,
    )

    elapsed = round(time.time() - start, 2)
    answer = response.choices[0].message.content.strip()

    return {
        "system": "Baseline",
        "question": question,
        "answer": answer,
        "response_time": elapsed,
    }


if __name__ == "__main__":
    test_q = "What is binary search?"
    print(f"Question: {test_q}\n")
    result = ask_baseline(test_q)
    print(f"Answer:\n{result['answer']}")
    print(f"\nResponse time: {result['response_time']}s")