-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_gemini_api.py
More file actions
155 lines (126 loc) · 4.98 KB
/
test_gemini_api.py
File metadata and controls
155 lines (126 loc) · 4.98 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
"""
Test Google Gemini API for sentiment analysis
"""
# Configure logging
import logging
import sys
import time
import google.generativeai as genai
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
# Gemini API configuration
GEMINI_API_KEY = "AIzaSyBna2MpDFxJPXMNBjLudxhO_8RDIv00z3s" # API key provided
GEMINI_MODEL = "gemini-1.5-pro" # Using the most capable model
def test_gemini_api():
"""Test the Gemini API for sentiment analysis."""
logger.info("Testing Gemini API for sentiment analysis")
# Configure Gemini API
genai.configure(api_key=GEMINI_API_KEY)
# Sample crypto-related texts with different sentiments
test_texts = [
(
"Bitcoin just broke through the $85,000 level and is showing strong "
"bullish momentum. The market looks extremely positive."
),
(
"Ethereum is falling rapidly, down 15% this week. Technical "
"indicators suggest more pain ahead for holders."
),
(
"XRP trading sideways at $2.15 with low volume. The market appears "
"indecisive at the moment."
),
]
# Expected sentiments (for validation)
expected = ["bullish", "bearish", "neutral"]
# Test each sample
results = []
for i, text in enumerate(test_texts):
try:
# Get model
model = genai.GenerativeModel(GEMINI_MODEL)
# Create prompt
system_prompt = (
"Classify the sentiment of the following cryptocurrency-related "
"text as strictly one of: bullish, bearish, or neutral. Respond "
"with only the single word classification."
)
# Configure generation parameters
generation_config = {
"temperature": 0.1, # Low temperature for classification
"max_output_tokens": 10, # Limit response length
"top_p": 0.95,
"top_k": 40,
}
# Create chat session with system prompt
chat = model.start_chat(
history=[
{"role": "user", "parts": [system_prompt]},
{
"role": "model",
"parts": [
"I'll classify the sentiment as bullish, bearish, or "
"neutral with just a single word."
],
},
]
)
# Get sentiment classification
response = chat.send_message(text, generation_config=generation_config)
# Extract the sentiment (should be just the word)
raw_sentiment = response.text.strip().lower()
sentiment = raw_sentiment
# Check if it matches expected formatting
if sentiment in ["bullish", "bearish", "neutral"]:
formatted_sentiment = sentiment
elif "bullish" in sentiment:
formatted_sentiment = "bullish"
elif "bearish" in sentiment:
formatted_sentiment = "bearish"
elif "neutral" in sentiment:
formatted_sentiment = "neutral"
else:
formatted_sentiment = "unknown"
# Fix F541: Changed from f-string without placeholders to regular string
logger.info("Text: '{}'...".format(text[:50]))
logger.info(f"Raw response: '{response.text}'")
logger.info(f"Formatted sentiment: '{formatted_sentiment}'")
logger.info(f"Expected sentiment: '{expected[i]}'")
logger.info(f"Match: {formatted_sentiment == expected[i]}")
logger.info("-" * 50)
results.append(
{
"text": text[:50] + "...",
"raw_response": response.text,
"formatted_sentiment": formatted_sentiment,
"expected": expected[i],
"match": formatted_sentiment == expected[i],
}
)
# Add delay to avoid rate limits
time.sleep(1)
except Exception as e:
logger.error(f"Error testing Gemini API: {e}")
return False
# Calculate accuracy
correct = sum(1 for r in results if r["match"])
accuracy = correct / len(results) if results else 0
logger.info("Gemini API Test Summary:")
logger.info(f"Correctly classified: {correct}/{len(results)}")
logger.info(f"Accuracy: {accuracy:.2%}")
return accuracy >= 0.66 # At least 2/3 correct to pass
def main():
"""Run the test script."""
logger.info("Starting Gemini API test")
success = test_gemini_api()
if success:
logger.info("✅ Gemini API test passed successfully!")
return 0
else:
logger.error("❌ Gemini API test failed!")
return 1
if __name__ == "__main__":
sys.exit(main())