File size: 1,301 Bytes
8e4d654
 
 
 
 
a4663b2
8e4d654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# llm_router.py

import os
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from langchain_groq import ChatGroq

router = APIRouter()

# Load context once at startup
_context_path = 'output.txt'
if os.path.exists(_context_path):
    with open(_context_path, 'r', encoding='utf-8') as f:
        data = f.read()
else:
    data = ""

def get_llm():
    """
    Returns the language model instance (LLM) using ChatGroq API.
    Reads the API key from the GROQ_API_KEY environment variable.
    """
    api_key = os.getenv("GROQ_API_KEY")
    if not api_key:
        raise RuntimeError("Environment variable GROQ_API_KEY is not set")
    return ChatGroq(
        model="llama-3.3-70b-versatile",
        temperature=0,
        max_tokens=1024,
        api_key=api_key
    )

llm = get_llm()

class QueryRequest(BaseModel):
    query: str

@router.post("/ask")
async def ask_query(request: QueryRequest):
    prompt = f"""You are EduLearnAI. A useful Assistant which helps people answer queries related to the Comsats University Islamabad Attock campus.
Using the provided context. If you don't know the answer, just say you don't know—don't hallucinate.
context:
{data}

question: {request.query}

answer:
"""
    ans = llm.invoke(prompt)
    return {"response": ans.content}