-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gemini.py
More file actions
43 lines (35 loc) · 1.19 KB
/
Copy pathtest_gemini.py
File metadata and controls
43 lines (35 loc) · 1.19 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
35
36
37
38
39
40
41
42
43
import os
from dotenv import load_dotenv
import requests
# Load environment variables from .env file
load_dotenv()
# Get API key and model name from environment variables
api_key = os.getenv('GEMINI_API_KEY', '').strip('"\'')
model_name = os.getenv('GEMINI_MODEL_NAME', 'gemini-1.5-pro').strip('"\'')
if not api_key:
print("Error: GEMINI_API_KEY not found in .env file")
exit(1)
print(f"Testing with model: {model_name}")
# Prepare the API request
url = f'https://generativelanguage.googleapis.com/v1/models/{model_name}:generateContent?key={api_key}'
headers = {'Content-Type': 'application/json'}
data = {
'contents': [{
'role': 'user',
'parts': [{'text': 'Say hello'}]
}]
}
try:
# Make the API request
print("Sending request to Gemini API...")
response = requests.post(url, headers=headers, json=data)
# Print the response
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
print("Success! API is working correctly.")
print("Response:", response.json())
else:
print("Error response from API:")
print(response.text)
except Exception as e:
print(f"An error occurred: {str(e)}")