-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini_test.py
More file actions
34 lines (30 loc) · 1.06 KB
/
gemini_test.py
File metadata and controls
34 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import sys
import google.generativeai as genai
GEMINI_API_KEY = "MY_API_KEY"
def main() -> int:
key = os.environ.get("GEMINI_API_KEY", GEMINI_API_KEY)
if not key or key == "MY_API_KEY":
print("Set GEMINI_API_KEY env var or replace GEMINI_API_KEY in gemini_test.py with your actual key.")
return 2
try:
genai.configure(api_key=key)
model = genai.GenerativeModel("gemini-1.5-flash")
prompt = "Respond with exactly: JAI Gemini test OK"
resp = model.generate_content(prompt)
text = getattr(resp, "text", None)
if not text and getattr(resp, "candidates", None):
try:
text = resp.candidates[0].content.parts[0].text
except Exception:
text = None
if not isinstance(text, str):
print("No text response received from Gemini.")
return 3
print(text.strip())
return 0
except Exception as e:
print(f"Gemini request failed: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())