-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini_enhancer.py
More file actions
171 lines (135 loc) · 5.58 KB
/
gemini_enhancer.py
File metadata and controls
171 lines (135 loc) · 5.58 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
"""Gemini AI enhancement layer for Vectara search results."""
import os
import logging
from typing import List, Dict, Any, Optional
from google import genai
from google.genai import types
logger = logging.getLogger(__name__)
class SearchSource:
"""Model for a search source/result."""
def __init__(self, file_path: str, file_name: str, file_type: str,
repo: str, owner: str, source_url: str,
relevance_score: float, snippet: str):
self.file_path = file_path
self.file_name = file_name
self.file_type = file_type
self.repo = repo
self.owner = owner
self.source_url = source_url
self.relevance_score = relevance_score
self.snippet = snippet
def init_gemini_client() -> genai.Client:
"""
Initialize Gemini client with Vertex AI credentials.
Returns:
Configured Gemini client
Raises:
ValueError: If credentials are not properly configured
"""
credentials_path = os.getenv("VERTEX_AI_CREDENTIALS_PATH")
if credentials_path and not os.getenv("GOOGLE_APPLICATION_CREDENTIALS"):
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.abspath(credentials_path)
project_id = os.getenv("VERTEX_AI_PROJECT_ID")
location = os.getenv("VERTEX_AI_LOCATION", "us-central1")
if not project_id:
raise ValueError("Missing VERTEX_AI_PROJECT_ID in environment/.env")
logger.info(f"✅ Initializing Gemini client with project: {project_id}, location: {location}")
return genai.Client(vertexai=True, project=project_id, location=location)
def format_sources_for_gemini(sources: List[Any]) -> str:
"""
Format Vectara sources into readable text for Gemini.
Args:
sources: List of SearchSource objects from Vectara
Returns:
Formatted string with source information
"""
if not sources:
return "No sources available."
formatted = []
# Limit to top 5 sources to avoid token limits
for idx, source in enumerate(sources[:5], 1):
# Handle both dict and object formats
if isinstance(source, dict):
file_path = source.get('file_path', 'Unknown')
relevance = source.get('relevance_score', 0.0)
snippet = source.get('snippet', 'No snippet available')
repo = source.get('repo', 'Unknown')
file_type = source.get('file_type', 'Unknown')
else:
file_path = getattr(source, 'file_path', 'Unknown')
relevance = getattr(source, 'relevance_score', 0.0)
snippet = getattr(source, 'snippet', 'No snippet available')
repo = getattr(source, 'repo', 'Unknown')
file_type = getattr(source, 'file_type', 'Unknown')
formatted.append(f"""
Source {idx}:
File: {file_path}
Repository: {repo}
Type: {file_type}
Relevance Score: {relevance:.2f}
Code Snippet:
```
{snippet}
```
---""")
return "\n".join(formatted)
async def enhance_with_gemini(
user_query: str,
vectara_summary: Optional[str],
vectara_sources: List[Any]
) -> str:
"""
Enhance Vectara results using Gemini 2.5 Flash.
Args:
user_query: Original user question
vectara_summary: RAG-generated summary from Vectara
vectara_sources: List of relevant code files
Returns:
Enhanced AI response (5-6 lines + optional code samples)
Raises:
Exception: If Gemini API call fails
"""
try:
logger.info(f"🤖 Enhancing search results with Gemini for query: '{user_query}'")
# Initialize Gemini client
client = init_gemini_client()
# Format sources for the prompt
sources_text = format_sources_for_gemini(vectara_sources)
# Build comprehensive prompt for Gemini
prompt = f"""You are an AI code assistant analyzing search results from a GitHub codebase.
**User Question:** {user_query}
**Search Results Summary from RAG System:**
{vectara_summary or 'No summary provided'}
**Relevant Code Files and Snippets:**
{sources_text}
**Your Task:**
1. Carefully analyze the user's question and the search results
2. Provide a clear, concise answer in **5-6 lines of text maximum**
3. If the question asks about code implementation or specific files, include relevant code samples with file names
4. Focus on what the user actually wants to know - be direct and helpful
5. If the search results don't fully answer the question, acknowledge this and provide the best answer you can
**Response Format:**
- Start with a brief explanation (5-6 lines)
- If applicable, include code samples with file names in markdown format
- Be conversational but professional
**Important:** Keep your response concise and to the point. Don't repeat the question or add unnecessary verbosity."""
# Call Gemini API
logger.info("📡 Calling Gemini 2.5 Flash API...")
model = client.models.generate_content(
model="gemini-2.5-flash",
contents=prompt,
config=types.GenerateContentConfig(
temperature=0.7,
top_p=0.95,
max_output_tokens=1024,
)
)
# Extract response text
enhanced_response = model.text
logger.info(f"✅ Gemini enhancement completed: {len(enhanced_response)} characters")
return enhanced_response
except Exception as e:
logger.error(f"❌ Gemini enhancement failed: {str(e)}")
# Fallback to original summary if Gemini fails
logger.warning("⚠️ Falling back to original Vectara summary")
return vectara_summary or "Unable to generate enhanced response. Please try again."