-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgemini_client.py
More file actions
398 lines (335 loc) · 13.9 KB
/
gemini_client.py
File metadata and controls
398 lines (335 loc) · 13.9 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""
Gemini Client using the new google-genai SDK with Search Grounding.
v2.0: Added native Google Search grounding for real-time AI visibility testing.
This allows the Mentions Check to test actual live search results, not just training data.
"""
import os
import json
import logging
import httpx
from typing import List, Dict, Any, Optional
from google import genai
from google.genai import types
from dotenv import load_dotenv
logger = logging.getLogger(__name__)
class GeminiClient:
"""Gemini client using the new google-genai SDK."""
def __init__(self):
# Load environment variables
load_dotenv('.env.local')
# Get API key
self.api_key = os.getenv('GEMINI_API_KEY')
if not self.api_key:
raise ValueError("GEMINI_API_KEY not found in environment")
# Initialize client with new SDK
self.client = genai.Client(api_key=self.api_key)
# Serper dev API fallback
self.serper_api_key = os.getenv('SERPER_API_KEY')
logger.info(f"GeminiClient initialized with new google-genai SDK")
async def complete(
self,
messages: List[Dict[str, str]],
model: str,
tools: Optional[List[Dict[str, Any]]] = None,
tool_choice: Any = "auto",
**kwargs
) -> Any:
"""Simple completion using new Gemini SDK."""
try:
# Convert messages to prompt
prompt = self._convert_messages_to_prompt(messages)
# Generate content with new API
response = self.client.models.generate_content(
model="gemini-3-flash-preview",
contents=prompt
)
# Return OpenAI-compatible format
class MockChoice:
def __init__(self, content):
self.message = MockMessage(content)
class MockMessage:
def __init__(self, content):
self.content = content
class MockResponse:
def __init__(self, content):
self.choices = [MockChoice(content)]
return MockResponse(response.text)
except Exception as e:
logger.error(f"Gemini completion error: {e}")
raise
async def complete_with_tools(
self,
messages: List[Dict[str, Any]],
model: str,
tools: Optional[List[str]] = None,
max_iterations: int = 5,
**kwargs
) -> Dict[str, Any]:
"""Generate completion with tool integration."""
try:
# Convert messages to prompt
prompt = self._convert_messages_to_prompt(messages)
# Use search-enabled completion for web search queries
if self._needs_web_search(prompt):
response = await self._complete_with_search(prompt)
else:
response = self.client.models.generate_content(
model="gemini-3-flash-preview",
contents=prompt
)
# Return result in expected format
return {
"choices": [{
"message": {
"content": response.text,
"role": "assistant"
}
}],
"model": "gemini-3-flash-preview",
"usage": {
"total_tokens": len(response.text.split())
}
}
except Exception as e:
logger.error(f"Gemini tools completion error: {e}")
return {
"choices": [{
"message": {
"content": f"Error: {str(e)}",
"role": "assistant"
}
}],
"error": str(e)
}
async def query_with_structured_output(
self,
prompt: str,
system_prompt: str = "",
model: str = "gemini-3-flash-preview",
response_format: str = "json",
**kwargs
) -> Dict[str, Any]:
"""Generate structured output (JSON) from prompt."""
try:
# Combine system and user prompts
full_prompt = prompt
if system_prompt:
full_prompt = f"{system_prompt}\n\n{prompt}"
# Add JSON formatting instruction
if response_format == "json":
full_prompt += "\n\nReturn your response as valid JSON."
# Generate content with new API
response = self.client.models.generate_content(
model=model,
contents=full_prompt
)
return {
"success": True,
"response": response.text,
"model": model
}
except Exception as e:
logger.error(f"Gemini structured output error: {e}")
return {
"success": False,
"error": str(e),
"response": ""
}
async def _complete_with_search(self, prompt: str):
"""Complete prompt with web search (Serper fallback for now)."""
try:
# For now, use Serper search fallback
return await self._complete_with_serper_fallback(prompt)
except Exception as e:
logger.warning(f"Search failed: {e}, using regular Gemini")
response = self.client.models.generate_content(
model="gemini-3-flash-preview",
contents=prompt
)
return response
async def _complete_with_serper_fallback(self, prompt: str):
"""Fallback to Serper dev API for search."""
if not self.serper_api_key:
logger.warning("No Serper API key, using regular Gemini")
response = self.client.models.generate_content(
model="gemini-3-flash-preview",
contents=prompt
)
return response
try:
# Extract search terms from prompt
search_query = self._extract_search_terms(prompt)
# Search with Serper
search_results = await self._serper_search(search_query)
# Enhance prompt with search results
enhanced_prompt = f"{prompt}\n\nBased on these search results:\n{search_results}"
# Generate response with enhanced context
response = self.client.models.generate_content(
model="gemini-3-flash-preview",
contents=enhanced_prompt
)
return response
except Exception as e:
logger.warning(f"Serper fallback failed: {e}, using regular Gemini")
response = self.client.models.generate_content(
model="gemini-3-flash-preview",
contents=prompt
)
return response
async def _serper_search(self, query: str) -> str:
"""Search using Serper dev API."""
try:
async with httpx.AsyncClient() as client:
response = await client.post(
"https://google.serper.dev/search",
headers={
"X-API-KEY": self.serper_api_key,
"Content-Type": "application/json"
},
json={"q": query, "num": 5}
)
if response.status_code == 200:
data = response.json()
# Format search results
results = []
for item in data.get("organic", []):
results.append(f"- {item.get('title', '')}: {item.get('snippet', '')}")
return "\n".join(results)
else:
logger.error(f"Serper API error: {response.status_code}")
return ""
except Exception as e:
logger.error(f"Serper search error: {e}")
return ""
def _convert_messages_to_prompt(self, messages: List[Dict[str, str]]) -> str:
"""Convert OpenAI-style messages to single prompt."""
parts = []
for message in messages:
role = message.get("role", "user")
content = message.get("content", "")
if role == "system":
parts.append(f"System: {content}")
elif role == "user":
parts.append(f"User: {content}")
elif role == "assistant":
parts.append(f"Assistant: {content}")
return "\n\n".join(parts)
def _needs_web_search(self, prompt: str) -> bool:
"""Determine if prompt needs web search."""
search_indicators = [
"search the web", "find information", "latest", "current",
"best companies", "top companies", "alternatives to",
"information about", "details about", "companies that",
"tools for", "platforms for", "services for"
]
prompt_lower = prompt.lower()
return any(indicator in prompt_lower for indicator in search_indicators)
def _extract_search_terms(self, prompt: str) -> str:
"""Extract relevant search terms from prompt."""
# Simple extraction - look for quoted terms or key phrases
import re
# Look for quoted terms
quoted = re.findall(r'"([^"]*)"', prompt)
if quoted:
return quoted[0]
# Look for "information about X"
info_match = re.search(r'information about (.+?)[\.\?]', prompt, re.IGNORECASE)
if info_match:
return info_match.group(1).strip()
# Look for "best X" or "top X"
best_match = re.search(r'(?:best|top) (.+?) (?:for|in)', prompt, re.IGNORECASE)
if best_match:
return best_match.group(1).strip()
# Fallback: use first meaningful sentence
sentences = prompt.split('.')
if sentences:
return sentences[0][:100] # Limit length
return prompt[:100] # Fallback
async def query_with_search_grounding(self, query: str) -> Dict[str, Any]:
"""Query Gemini with real-time Google Search grounding.
This is the key method for accurate AEO visibility testing.
Instead of relying on training data, it uses live Google Search
to ground the response in real-time web results.
Returns:
Dict with response text, grounding sources, and metadata
"""
try:
# Configure Google Search grounding tool
grounding_tool = types.Tool(
google_search=types.GoogleSearch()
)
config = types.GenerateContentConfig(
tools=[grounding_tool]
)
# Generate content with search grounding
response = self.client.models.generate_content(
model="gemini-3-flash-preview",
contents=query,
config=config,
)
# Extract grounding metadata (sources, citations)
grounding_sources = []
grounding_chunks = []
if response.candidates and len(response.candidates) > 0:
candidate = response.candidates[0]
if hasattr(candidate, 'grounding_metadata') and candidate.grounding_metadata:
metadata = candidate.grounding_metadata
# Extract grounding chunks (actual sources)
if hasattr(metadata, 'grounding_chunks') and metadata.grounding_chunks:
for chunk in metadata.grounding_chunks:
if hasattr(chunk, 'web') and chunk.web:
grounding_sources.append({
'uri': chunk.web.uri if hasattr(chunk.web, 'uri') else '',
'title': chunk.web.title if hasattr(chunk.web, 'title') else ''
})
# Extract search queries used
if hasattr(metadata, 'search_entry_point') and metadata.search_entry_point:
grounding_chunks.append(str(metadata.search_entry_point))
return {
"success": True,
"response": response.text,
"model": "gemini-3-flash-preview",
"search_grounding": True,
"grounding_sources": grounding_sources,
"source_count": len(grounding_sources)
}
except Exception as e:
logger.error(f"Search grounding query error: {e}")
# Fallback to non-grounded query
return await self._fallback_query(query)
async def _fallback_query(self, query: str) -> Dict[str, Any]:
"""Fallback to standard query without search grounding."""
try:
response = self.client.models.generate_content(
model="gemini-3-flash-preview",
contents=query
)
return {
"success": True,
"response": response.text,
"model": "gemini-3-flash-preview",
"search_grounding": False,
"grounding_sources": [],
"source_count": 0
}
except Exception as e:
logger.error(f"Fallback query error: {e}")
return {
"success": False,
"error": str(e),
"response": "",
"search_grounding": False
}
async def query_mentions_with_search_grounding(self, query: str, company_name: str) -> Dict[str, Any]:
"""Query for company mentions with search grounding - main method for AEO mentions check.
DEPRECATED: Use query_with_search_grounding instead.
"""
return await self.query_with_search_grounding(query)
# Singleton instance
_gemini_client = None
def get_gemini_client() -> GeminiClient:
"""Get singleton Gemini client instance."""
global _gemini_client
if _gemini_client is None:
_gemini_client = GeminiClient()
return _gemini_client