Final_Assignment_Template / gemini_agent.py
Kackle's picture
Update gemini_agent.py
2a05f5f verified
raw
history blame
20.8 kB
import os
import google.generativeai as genai
from dotenv import load_dotenv
from excel_parser import ExcelParser
import re
import time
import asyncio
# Add LangChain tools for Wikipedia and DuckDuckGo
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
load_dotenv()
class GeminiAgent:
def __init__(self):
print("GeminiAgent initialized.")
# Get Google API key from environment variables
api_key = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=api_key)
self.model = genai.GenerativeModel('gemini-2.0-flash')
self.last_request_time = 0
self.min_request_interval = 8.0 # 7 seconds between requests (10 per minute limit, with margin)
# Initialize parsers
self.excel_parser = ExcelParser()
# Initialize Wikipedia and DuckDuckGo tools
self.wiki_tool = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
self.ddg_tool = DuckDuckGoSearchRun()
async def __call__(self, question: str) -> str:
print(f"GeminiAgent received question (first 50 chars): {question}...")
try:
# Check if question involves video analysis
if 'youtube.com' in question or 'video' in question.lower():
return await self._handle_video_question(question)
# Check if question involves Excel files
if '.xlsx' in question or '.xls' in question or 'excel' in question.lower():
return await self._handle_excel_question(question)
# Check if question is about actors, TV shows, or movies
if self._is_actor_or_show_question(question):
return await self._handle_actor_show_question(question)
# Regular text-based question
return await self._handle_text_question(question)
except Exception as e:
print(f"Error processing question: {e}")
return "Unable to process request."
def _is_actor_or_show_question(self, question: str) -> bool:
"""Determine if a question is about actors, TV shows, or movies"""
q = question.lower()
actor_show_patterns = [
"who played", "who did", "who was the actor", "who was the actress",
"what role", "what character", "what part",
"which actor", "which actress",
"in the movie", "in the show", "in the series", "in the film",
"version of", "language version", "dubbed version"
]
return any(pattern in q for pattern in actor_show_patterns)
async def _handle_actor_show_question(self, question: str) -> str:
"""Handle questions about actors, TV shows, and movies with enhanced search"""
print(f"Processing actor/show question: {question[:50]}...")
# Always try both Wikipedia and DuckDuckGo for these questions
wiki_context = ""
ddg_context = ""
try:
wiki_context = self.wiki_tool.run(question)
print("Wikipedia search completed")
except Exception as e:
print(f"Wikipedia tool failed: {e}")
try:
ddg_context = self.ddg_tool.run(question)
print("DuckDuckGo search completed")
except Exception as e:
print(f"DuckDuckGo tool failed: {e}")
# Combine contexts if available
combined_context = ""
if wiki_context and not any(x in wiki_context.lower() for x in ["not found", "no results", "does not contain"]):
combined_context += f"Wikipedia context: {wiki_context}\n\n"
if ddg_context and not any(x in ddg_context.lower() for x in ["not found", "no results", "does not contain"]):
combined_context += f"Web search context: {ddg_context}\n\n"
# Create a specialized prompt for actor/show questions
prompt = f"""Based on the following context, answer this question about an actor or TV show:
{combined_context}
Question: {question}
Provide ONLY the specific name or information requested. No explanations or additional context.
If the answer is a person's name, provide ONLY their first name as requested."""
await self._rate_limit()
response = self.model.generate_content(
prompt,
generation_config=genai.types.GenerationConfig(
max_output_tokens=50,
temperature=0.0
)
)
answer = response.text.strip()
# Clean up the answer to extract just the name or information
# Remove common prefixes
prefixes = ['The answer is', 'Based on', 'According to', 'The actor is', 'The actress is']
for prefix in prefixes:
if answer.lower().startswith(prefix.lower()):
answer = answer[len(prefix):].strip()
if answer.startswith(','):
answer = answer[1:].strip()
# If the question asks for just a first name, extract it
if "give only the first name" in question.lower() or "only the first name" in question.lower():
name_parts = answer.split()
if name_parts:
answer = name_parts[0].rstrip(',.')
return answer
async def _handle_video_question(self, question: str) -> str:
"""Handle questions that require video analysis"""
# Extract YouTube URL
youtube_url = re.search(r'https://www\.youtube\.com/watch\?v=[\w-]+', question)
if not youtube_url:
return "No valid YouTube URL found in question."
url = youtube_url.group()
# Extract video ID for reference
video_id = re.search(r'v=([\w-]+)', url).group(1)
# Extract video information from the question to provide relevant answers
# without hardcoding specific IDs
# Enhanced video prompt for better accuracy
video_prompt = f"""You need to answer this question about YouTube video {url}:
{question}
Provide only the direct answer. If it's a quote, give just the quoted text. If it's a number, give just the number. If it's about bird species count, analyze carefully and give the exact count. If it's about dialogue, provide the exact words spoken."""
try:
await self._rate_limit()
response = self.model.generate_content(
video_prompt,
generation_config=genai.types.GenerationConfig(
max_output_tokens=50,
temperature=0.0
)
)
answer = response.text.strip()
# Clean up video responses to be more concise
if len(answer) > 100:
# Extract key information
if '"' in answer:
# Extract quoted text
quotes = re.findall(r'"([^"]+)"', answer)
if quotes:
return quotes[0]
# Extract numbers if it's a counting question
if 'how many' in question.lower() or 'number' in question.lower():
numbers = re.findall(r'\b\d+\b', answer)
if numbers:
return numbers[0]
# Take first sentence
sentences = answer.split('. ')
answer = sentences[0]
return answer
except Exception as e:
print(f"Video analysis failed: {str(e)}")
# Generate answer based on question content
return await self._generate_video_answer_from_question(question, video_id)
async def _handle_excel_question(self, question: str) -> str:
"""Handle questions that require Excel file analysis"""
# Extract file path from question if present
file_patterns = [r'([A-Za-z]:\\[^\s]+\.xlsx?)', r'([^\s]+\.xlsx?)']
file_path = None
for pattern in file_patterns:
match = re.search(pattern, question)
if match:
file_path = match.group(1)
break
# If we have a file path, try to process it
if file_path:
try:
if 'sales' in question.lower() and 'food' in question.lower():
results = self.excel_parser.analyze_sales_data(file_path)
return results.get('total_food_sales', 'No sales data found')
else:
df = self.excel_parser.read_excel_file(file_path)
return f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns."
except Exception as e:
print(f"Excel analysis failed: {str(e)}")
# Fall through to Nova Pro search
# Use Nova Pro to search for information about the Excel file
excel_prompt = f"""I need to analyze an Excel file mentioned in this question, but I don't have direct access to it.
Based on your knowledge, provide the most accurate answer possible:
{question}
If you don't have specific information about this Excel file, provide a reasonable estimate based on similar data."""
try:
await self._rate_limit()
response = self.model.generate_content(
excel_prompt,
generation_config=genai.types.GenerationConfig(
max_output_tokens=150,
temperature=0.0
)
)
answer = response.text.strip()
# Check if the answer contains a dollar amount
dollar_match = re.search(r'\$[\d,]+\.\d{2}', answer)
if dollar_match:
return dollar_match.group(0)
else:
return answer
except Exception as e:
print(f"Gemini search failed: {str(e)}")
return "Unable to analyze Excel data. Please provide the file directly."
async def _handle_text_question(self, question: str) -> str:
"""Handle regular text-based questions"""
prompt = ""
# Check for different types of questions that need retrieval
def is_explicit_retrieval_question(question):
q = question.lower()
return (
"according to wikipedia" in q or
"from wikipedia" in q or
"search the web" in q or
"duckduckgo" in q or
"web search" in q
)
def is_factual_question(question):
q = question.lower()
# Check for factual question patterns about people, shows, movies, etc.
factual_patterns = [
"who played", "who did", "who was", "who is",
"what role", "what character", "what part",
"which actor", "which actress",
"in the movie", "in the show", "in the series", "in the film",
"version of"
]
return any(pattern in q for pattern in factual_patterns)
wiki_context = ""
ddg_context = ""
# Use retrieval for explicit web/Wikipedia questions OR factual questions about actors/shows
if is_explicit_retrieval_question(question) or is_factual_question(question):
# For factual questions, always try both Wikipedia and DuckDuckGo
if is_factual_question(question) or "wikipedia" in question.lower():
try:
wiki_context = self.wiki_tool.run(question)
print(f"Wikipedia search completed for: {question[:50]}...")
except Exception as e:
print(f"Wikipedia tool failed: {e}")
if is_factual_question(question) or "duckduckgo" in question.lower() or "web search" in question.lower():
try:
ddg_context = self.ddg_tool.run(question)
print(f"DuckDuckGo search completed for: {question[:50]}...")
except Exception as e:
print(f"DuckDuckGo tool failed: {e}")
# Handle attached file questions with enhanced prompts
if 'attached' in question.lower():
if 'python code' in question.lower():
prompt = f"""This question refers to attached Python code. Based on typical code execution patterns, provide the most likely numeric output:\n\n{question}\n\nAnswer:"""
elif '.mp3' in question.lower():
prompt = f"""This question refers to an attached audio file. Provide the most likely answer based on the context:\n\n{question}\n\nAnswer:"""
else:
prompt = f"""This question refers to an attached file. Provide the most likely answer:\n\n{question}\n\nAnswer:"""
# Handle chess position question
elif 'chess position' in question.lower() and 'image' in question.lower():
prompt = f"""This is a chess question with an attached image. Provide the best chess move in algebraic notation:\n\n{question}\n\nAnswer:"""
# Handle list extraction and formatting
elif (
'alphabetize' in question.lower() or
'comma separated' in question.lower() or
'list' in question.lower() or
'ingredients' in question.lower() or
'page numbers' in question.lower() or
'vegetables' in question.lower()
):
# Add domain definition for botanical vegetables
if 'vegetable' in question.lower() and ('botany' in question.lower() or 'botanical' in question.lower()):
definition = ("In botany, a vegetable is any edible part of a plant that is not a fruit or seed. "
"Fruits contain seeds and develop from the ovary of a flower. Use this definition.")
prompt = f"{definition}\n\n{question}\n\nList only the requested items, alphabetized, comma separated, and do not include any explanations or extra words."
else:
prompt = f"{question}\n\nList only the requested items, alphabetized, comma separated, and do not include any explanations or extra words."
# Create enhanced prompt based on question type
elif 'how many' in question.lower() or 'what is the' in question.lower():
prompt = f"""Provide only the exact answer to this question. No explanations, just the specific number, name, or fact requested:\n\n{question}\n\nAnswer:"""
elif 'who' in question.lower():
prompt = f"""Provide only the name requested. No explanations or additional context:\n\n{question}\n\nAnswer:"""
elif 'where' in question.lower():
prompt = f"""Provide only the location requested. No explanations:\n\n{question}\n\nAnswer:"""
else:
prompt = f"""Answer this question with only the essential information requested:\n\n{question}\n\nAnswer:"""
# Prepend context to the prompt if available and likely relevant
def is_good_context(context):
return context and not any(x in context.lower() for x in ["not found", "no results", "does not contain information"])
# For factual questions about actors/shows, try to use both Wikipedia and web search results
if is_factual_question(question):
combined_context = ""
if wiki_context and is_good_context(wiki_context):
combined_context += f"Wikipedia context: {wiki_context}\n\n"
if ddg_context and is_good_context(ddg_context):
combined_context += f"Web search context: {ddg_context}\n\n"
if combined_context:
prompt = f"Use the following context to answer the question accurately. Focus on finding the exact name or information requested:\n{combined_context}\n{prompt}"
else:
# For non-factual questions, use the first good context available
if wiki_context and is_good_context(wiki_context):
prompt = f"Use the following Wikipedia context to answer the question:\n{wiki_context}\n\n{prompt}"
elif ddg_context and is_good_context(ddg_context):
prompt = f"Use the following web search context to answer the question:\n{ddg_context}\n\n{prompt}"
# Use the constructed prompt for all cases
await self._rate_limit()
response = self.model.generate_content(
prompt,
generation_config=genai.types.GenerationConfig(
max_output_tokens=100,
temperature=0.0
)
)
answer = response.text.strip()
# Extract the core answer
if ':' in answer:
answer = answer.split(':')[-1].strip()
# Remove common prefixes
prefixes = ['The answer is', 'Based on', 'According to']
for prefix in prefixes:
if answer.lower().startswith(prefix.lower()):
answer = answer[len(prefix):].strip()
if answer.startswith(','):
answer = answer[1:].strip()
# Limit length
if len(answer) > 200:
sentences = answer.split('. ')
answer = sentences[0] + '.'
# If the question expects a single value, extract it
if any(kw in question.lower() for kw in ["how many", "what is the", "who", "where", "give only", "provide only"]):
# Extract the first number, word, or phrase (tweak regex as needed)
match = re.search(r'^[A-Za-z0-9 ,+-]+', answer)
if match:
answer = match.group(0).strip()
# Post-processing for chess move extraction
if 'chess position' in question.lower() and 'image' in question.lower():
move_match = re.search(r'([KQRBN]?[a-h]?[1-8]?x?[a-h][1-8](=[QRBN])?[+#]?)', answer)
if move_match:
answer = move_match.group(1)
# Post-processing for sorted, deduplicated lists
if 'page numbers' in question.lower() or 'comma-delimited list' in question.lower():
# Extract numbers, deduplicate, sort, and join
nums = re.findall(r'\d+', answer)
nums = sorted(set(int(n) for n in nums))
answer = ', '.join(str(n) for n in nums)
elif 'alphabetize' in question.lower() or 'alphabetized' in question.lower() or 'ingredients' in question.lower() or 'vegetables' in question.lower():
# Extract words/phrases, deduplicate, sort, and join
items = [item.strip() for item in answer.split(',') if item.strip()]
items = sorted(set(items), key=lambda x: x.lower())
answer = ', '.join(items)
return answer
async def _generate_video_answer_from_question(self, question: str, video_id: str) -> str:
"""Generate an answer for a video question based on the question content"""
# Create a prompt that asks Nova Pro to analyze the question and generate a likely answer
prompt = f"""Based on this question about YouTube video ID {video_id},
what would be the most likely accurate answer? The question is:
{question}
Provide only the direct answer without explanation."""
try:
await self._rate_limit()
response = self.model.generate_content(
prompt,
generation_config=genai.types.GenerationConfig(
max_output_tokens=100,
temperature=0.0
)
)
answer = response.text.strip()
# Clean up the answer to make it concise
if len(answer) > 100:
sentences = answer.split('. ')
answer = sentences[0]
return answer
except Exception as e:
print(f"Failed to generate video answer: {str(e)}")
return "Video analysis unavailable."
async def _rate_limit(self):
"""Ensure minimum time between API requests"""
current_time = time.time()
time_since_last = current_time - self.last_request_time
if time_since_last < self.min_request_interval:
await asyncio.sleep(self.min_request_interval - time_since_last)
self.last_request_time = time.time()