Rivalcoder commited on
Commit
98982c9
Β·
1 Parent(s): 011118e
Files changed (1) hide show
  1. llm.py +39 -20
llm.py CHANGED
@@ -5,18 +5,19 @@ from dotenv import load_dotenv
5
 
6
  load_dotenv()
7
 
8
- api_key = os.getenv("GOOGLE_API_KEY")
9
- if not api_key:
10
- raise ValueError("GOOGLE_API_KEY environment variable is not set. Please add it to your .env file")
 
11
 
12
- print(f"Google API Key loaded: {api_key[:10]}..." if api_key else "No API key found")
13
- genai.configure(api_key=api_key)
14
 
15
  def query_gemini(questions, contexts):
16
- try:
17
- context = "\n\n".join(contexts)
18
- questions_text = "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions)])
19
- prompt = f"""
20
  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.
21
 
22
  🧠 FORMAT & TONE GUIDELINES:
@@ -73,19 +74,37 @@ Respond with only the following JSON β€” no explanations, no comments, no markdo
73
 
74
  Your task: For each question, provide a complete, professional, and clearly written answer in 2–3 sentences using a formal but readable tone.
75
  """
76
- model = genai.GenerativeModel('gemini-2.5-flash-lite')
77
- response = model.generate_content(prompt)
78
- response_text = response.text.strip()
 
79
  try:
 
 
 
 
 
 
80
  if response_text.startswith("```json"):
81
  response_text = response_text.replace("```json", "").replace("```", "").strip()
82
  elif response_text.startswith("```"):
83
  response_text = response_text.replace("```", "").strip()
84
- parsed_response = json.loads(response_text)
85
- return parsed_response
86
- except json.JSONDecodeError:
87
- print(f"Failed to parse JSON response: {response_text}")
88
- return {"answers": ["Error parsing response"] * len(questions)}
89
- except Exception as e:
90
- print(f"Error in query_gemini: {str(e)}")
91
- return {"answers": [f"Error generating response: {str(e)}"] * len(questions)}
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  load_dotenv()
7
 
8
+ # Support multiple Gemini keys (comma-separated or single key)
9
+ api_keys = os.getenv("GOOGLE_API_KEYS") or os.getenv("GOOGLE_API_KEY")
10
+ if not api_keys:
11
+ raise ValueError("No Gemini API keys found in GOOGLE_API_KEYS or GOOGLE_API_KEY environment variable.")
12
 
13
+ api_keys = [k.strip() for k in api_keys.split(",") if k.strip()]
14
+ print(f"Loaded {len(api_keys)} Gemini API key(s)")
15
 
16
  def query_gemini(questions, contexts):
17
+ context = "\n\n".join(contexts)
18
+ questions_text = "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions)])
19
+
20
+ prompt = f"""
21
  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.
22
 
23
  🧠 FORMAT & TONE GUIDELINES:
 
74
 
75
  Your task: For each question, provide a complete, professional, and clearly written answer in 2–3 sentences using a formal but readable tone.
76
  """
77
+
78
+ last_exception = None
79
+
80
+ for key_idx, key in enumerate(api_keys):
81
  try:
82
+ genai.configure(api_key=key)
83
+ model = genai.GenerativeModel("gemini-2.5-flash-lite")
84
+ response = model.generate_content(prompt)
85
+ response_text = response.text.strip()
86
+
87
+ # Clean JSON from wrapped code blocks
88
  if response_text.startswith("```json"):
89
  response_text = response_text.replace("```json", "").replace("```", "").strip()
90
  elif response_text.startswith("```"):
91
  response_text = response_text.replace("```", "").strip()
92
+
93
+ parsed = json.loads(response_text)
94
+ if "answers" in parsed and isinstance(parsed["answers"], list):
95
+ return parsed
96
+ else:
97
+ raise ValueError("Invalid response format received from Gemini.")
98
+
99
+ except Exception as e:
100
+ msg = str(e).lower()
101
+ if "429" in msg or "quota" in msg or "rate limit" in msg or "exceeded" in msg:
102
+ print(f"Gemini key {key[:8]}... quota/rate limited. Trying next key ({key_idx+1}/{len(api_keys)}).")
103
+ last_exception = e
104
+ continue
105
+ else:
106
+ print(f"Error using Gemini key {key[:8]}...: {e}")
107
+ return {"answers": [f"Error generating response: {str(e)}"] * len(questions)}
108
+
109
+ print(f"All Gemini API keys failed. Last error: {last_exception}")
110
+ return {"answers": [f"Error generating response: {str(last_exception)}"] * len(questions)}