# gpt_helpers.py # Updated to fallback to OPENAI_API_KEY if OPENAI_API_KEY_VERDICTAI not set, to avoid runtime error. # Minor changes: Refined system prompt for consistency with app (handle diverse doc types, high quote density, Bluebook, conditional IRAC). # Added jurisdiction-specific handling and anti-hallucination emphasis. No major overhaul needed—still used for initial drafts before Grok polish. import os import logging from openai import OpenAI logger = logging.getLogger(__name__) OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") openai_client = OpenAI(api_key=OPENAI_API_KEY) OPENAI_API_KEY_VERDICTAI = os.environ.get("OPENAI_API_KEY_VERDICTAI") if not OPENAI_API_KEY_VERDICTAI: logger.warning("OPENAI_API_KEY_VERDICTAI not set; falling back to OPENAI_API_KEY for fine-tuned model.") OPENAI_API_KEY_VERDICTAI = OPENAI_API_KEY # Fallback def ask_gpt41_mini(prompt, jurisdiction): try: # Use separate client for fine-tuned model verdictai_client = OpenAI(api_key=OPENAI_API_KEY_VERDICTAI) system_content = ( f"You are a legal assistant drafting documents for {jurisdiction} jurisdiction. Act as a junior associate: generate initial drafts for various legal documents (e.g., orders, memos, motions, pleadings, complaints, answers, notices, client letters) based on user instructions like terms, recipients, formats. " "If the task requires research, note potential sources but do not hallucinate—rely on provided context. Always quote directly from retrieved case law with high density. Use full case names and Bluebook citations (e.g., 'Smith v. Jones, 123 S.W.3d 456, 460 (Ky. 2005)'). " "Include facts from those cases when applying them. Discuss the facts of the cases, quote relevant holdings, and explain the issues clearly. " "Ground any statutes mentioned with direct quotes from reliable sources. " "Use IRAC structure only if the task involves legal analysis (e.g., briefs, memos); for summaries or pure drafting, organize appropriately without forcing IRAC. Do not paraphrase available holdings; ensure no unsubstantiated content." ) response = verdictai_client.chat.completions.create( model="ft:gpt-4.1-mini-2025-04-14:w-jeffrey-scott-psc:verdictaitrain2:BzIkzaDy", messages=[ {"role": "system", "content": system_content}, {"role": "user", "content": prompt} ], temperature=0.3, max_tokens=8192 ) content = response.choices[0].message.content logger.info(f"GPT-4.1-mini response length: {len(content)}") # ADDED: Log response length for debugging emptiness return content except Exception as e: logger.error(f"GPT-4.1-mini error: {type(e).__name__}: {str(e)}") # UPDATED: More detailed error logging return f"[GPT-4.1-mini Error: {type(e).__name__}] {str(e)}"