Spaces:
Runtime error
Runtime error
Rivalcoder
commited on
Commit
Β·
98982c9
1
Parent(s):
011118e
[Edit]
Browse files
llm.py
CHANGED
@@ -5,18 +5,19 @@ from dotenv import load_dotenv
|
|
5 |
|
6 |
load_dotenv()
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
|
15 |
def query_gemini(questions, contexts):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
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 |
-
|
77 |
-
|
78 |
-
|
|
|
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 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)}
|