# ask_llm_with_context.py import openai import os openai.api_base = "https://api.groq.com/openai/v1" openai.api_key = os.getenv("GROQ_API_KEY") def ask_llm_with_context(question, context, model="llama3-8b-8192"): prompt = f""" You are a legal teaching assistant supporting land surveyors in British Columbia. Your task is to answer questions **strictly** based on the following legal context. The users include licensed surveyors (BCLS) and Land Surveyors in Training (LSTs). Note: LSTs are not authorized to certify or sign legal survey documents and must work under BCLS supervision. These individuals are governed by: - Statutes - Survey and Plan Rules - Circular Letters - Practice Bulletins - Code of Ethics - ABCLS bylaws and manuals IMPORTANT INSTRUCTIONS: - Only cite section numbers, regulations, or acts that are explicitly visible in the provided context. - Do **NOT** make up legal citations, section numbers, or page references. - If the specific section is **not in the context**, respond with: "The context does not provide a specific section for this." - If unsure, recommend the appropriate document (e.g., "You may refer to the Survey and Plan Rules or Land Title Act"). Please organize your response in two sections: **Referenced Document(s)** List the name(s) of the Acts, circular letters, bulletins, or sections that are directly relevant to the question. **Explanation** Explain the answer clearly using the context. Focus on what the rule means, why it matters, and how it applies in practice. --- Legal Context: \"\"\"{context}\"\"\" Question: {question} Answer (cite only what's present in context): """ try: response = openai.ChatCompletion.create( model=model, messages=[ {"role": "system", "content": "You are a trusted legal assistant for BC land surveyors."}, {"role": "user", "content": prompt} ], temperature=0.3, max_tokens=600 ) disclaimer = ( "\n\n\n**Disclaimer:** This response is for informational and educational purposes only. " "It does not constitute legal advice and should not be used as a substitute for consulting the applicable legislation, " "Surveyor General’s office, or the Association of British Columbia Land Surveyors (ABCLS). " "Always refer to official sources for legal or professional decisions." ) return response['choices'][0]['message']['content'].strip() + disclaimer except Exception as e: return f"[!] LLM Error: {e}"