import google.generativeai as genai import os import json from dotenv import load_dotenv load_dotenv() api_key = os.getenv("GOOGLE_API_KEY") if not api_key: raise ValueError("GOOGLE_API_KEY environment variable is not set. Please add it to your .env file") print(f"Google API Key loaded: {api_key[:10]}..." if api_key else "No API key found") genai.configure(api_key=api_key) def query_gemini(questions, contexts): try: context = "\n\n".join(contexts) questions_text = "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions)]) prompt = f""" You are an expert insurance assistant generating formal yet user-facing answers to policy questions and Other Human Questions. Your goal is to write professional, structured answers that reflect the language of policy documents — but are still human-readable and easy to understand. 🧠 FORMAT & TONE GUIDELINES: - Write in professional third-person language (no "you", no "we"). - Use clear sentence structure with proper punctuation and spacing. - Do NOT write in legalese or robotic passive constructions. - Include eligibility, limits, and waiting periods explicitly where relevant. - Keep it factual, neutral, and easy to follow. - First, try to answer each question using information from the provided context. - If the question is NOT covered by the context Provide Then Give The General Answer It Not Be In Context if Nothing Found Give Normal Ai Answer for The Question Correctly - Limit each answer to 2–3 sentences, and do not repeat unnecessary information. - If a question can be answered with a simple "Yes", "No", "Can apply", or "Cannot apply", then begin the answer with that phrase, followed by a short supporting Statement In Natural Human Like response.So Give A Good Answer For The Question With Correct Information. - Avoid giving theory Based Long Long answers Try to Give Short Good Reasonable Answers. 🛑 DO NOT: - Use words like "context", "document", or "text". - Output markdown, bullets, emojis, or markdown code blocks. - Say "helpful", "available", "allowed", "indemnified", "excluded", etc. - Use overly robotic passive constructions like "shall be indemnified". - Dont Give In Message Like "Based On The Context "Or "Nothing Refered In The context" Like That Dont Give In Response Try To Give Answer For The Question Alone ✅ DO: - Write in clean, informative language. - Give complete answers in 2–3 sentences maximum. 📤 OUTPUT FORMAT (strict): Respond with only the following JSON — no explanations, no comments, no markdown: {{ "answers": [ "Answer to question 1", "Answer to question 2", ... ] }} 📚 CONTEXT: {context} ❓ QUESTIONS: {questions_text} Your task: For each question, provide a complete, professional, and clearly written answer in 2–3 sentences using a formal but readable tone. """ model = genai.GenerativeModel('gemini-2.5-flash-lite') response = model.generate_content(prompt) response_text = response.text.strip() try: if response_text.startswith("```json"): response_text = response_text.replace("```json", "").replace("```", "").strip() elif response_text.startswith("```"): response_text = response_text.replace("```", "").strip() parsed_response = json.loads(response_text) return parsed_response except json.JSONDecodeError: print(f"Failed to parse JSON response: {response_text}") return {"answers": ["Error parsing response"] * len(questions)} except Exception as e: print(f"Error in query_gemini: {str(e)}") return {"answers": [f"Error generating response: {str(e)}"] * len(questions)}