import os from typing import List from pydantic import BaseModel, Field, ValidationError import google.generativeai as genai from dotenv import load_dotenv from utils import extract_json load_dotenv() genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) model = genai.GenerativeModel("gemini-1.5-flash") class ReportData(BaseModel): short_summary: str markdown_report: str follow_up_questions: List[str] key_points: List[str] = [] citations: List[str] = [] INSTRUCTIONS = """ You are a senior researcher tasked with writing a cohesive report for a research query. Output ONLY JSON with the following fields: - short_summary: 2–3 sentence summary - markdown_report: At least 1000 words - follow_up_questions: list of follow-up research questions - key_points: list of key bullet points - citations: list of URLs """ def generate_report(query: str, search_results: list[str]) -> ReportData: prompt = f"{INSTRUCTIONS}\n\nQuery: {query}\n\nSearch Results:\n{search_results}" response = model.generate_content(prompt) raw_text = response.text.strip() print("Gemini raw report:\n", raw_text) try: clean_json = extract_json(raw_text) return ReportData.parse_raw(clean_json) except ValidationError as e: print("❌ Failed to parse Gemini output:", e) raise