CoolFace
Apppublic

ETFogle/engineering-knowledge-bot

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
run_system.py41 linesDownload Raw Back to root
1import requests2from retrieval.retriever import Retriever3from config import HF_API_URL, HF_MODEL, LLM_API_KEY4 5SYSTEM_PROMPT = """You are an engineering knowledge assistant. Use ONLY the provided context. Cite sources. If the answer is not in the context, say you don’t know. Keep responses concise, technical, and grounded in the retrieved text.”"""6 7def call_llm(prompt):8    headers = {9        "Authorization": f"Bearer {LLM_API_KEY}",10        "Content-Type": "application/json",11    }12 13    payload = {14        "model": HF_MODEL,15        "messages": [16            {"role": "system", "content": SYSTEM_PROMPT},17            {"role": "user", "content": prompt}18        ],19        "max_tokens": 400,20        "temperature": 0.221    }22 23    r = requests.post(HF_API_URL, headers=headers, json=payload)24    r.raise_for_status()25    data = r.json()26 27    return data["choices"][0]["message"]["content"]28 29def build_prompt(query, contexts):30    ctx = "\n\n".join(31        f"[Source: {c['metadata']['source']}]\n{c['text']}"32        for c in contexts33    )34    return f"Context:\n{ctx}\n\nQuestion: {query}\nAnswer:"35 36def answer_query(query):37    retriever = Retriever()38    ctx = retriever.retrieve(query)39    prompt = build_prompt(query, ctx)40    answer = call_llm(prompt)41    return answer, ctx