Spaces:
Runtime error
Runtime error
| 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 a skilled insurance policy assistant. Based only on the provided context, answer each question clearly and briefly. | |
| π― GOAL: | |
| - Each answer should be precise, informative, and phrased naturally β around 1β2 full sentences. | |
| - Avoid long clauses or repeating policy language verbatim. | |
| - Focus on **clarity**, **brevity**, and **accuracy** β your answers will be judged for quality and relevance. | |
| π INSTRUCTIONS: | |
| 1. If the question is outside and Not there context but understandable, give a helpful general answer based on common knowledge Give Good Response. | |
| 2. Answers must be helpful and compact β **complete, but not wordy**. Avoid long multi-clause sentences. | |
| 3. Do NOT include section references (like βas per section 3.1.6β) or legal codes. | |
| 4. Avoid copying large phrases from the document. Rephrase naturally using simpler language where appropriate. | |
| 5. If it is completely irrelevant or cannot be answered even generally, respond with "Not Found". | |
| 6. Follow the tone and length of these examples: | |
| - "A grace period of thirty days is provided for premium payment after the due date." | |
| - "Yes, maternity expenses are covered after 24 months of continuous coverage, limited to two deliveries or terminations." | |
| - "Yes, the policy covers organ donor expenses for harvesting the organ if donated to the insured person." | |
| - "Not Found" | |
| π€ RETURN FORMAT: | |
| Respond in the exact JSON format below β no extra text or explanations. | |
| {{ | |
| "answers": [ | |
| "Answer to question 1", | |
| "Answer to question 2", | |
| ... | |
| ] | |
| }} | |
| π CONTEXT: | |
| {context} | |
| β QUESTIONS: | |
| {questions_text} | |
| Your task: Answer each question concisely and professionally. Use plain phrasing, stay within 1β2 clear sentences, and avoid unnecessary detail or repetition. | |
| """ | |
| model = genai.GenerativeModel('gemini-2.5-flash') | |
| 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)} |