import os import json import asyncio from dotenv import load_dotenv from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from pydantic import BaseModel from typing import List, Dict, Any, Optional # Modern SDK only from google import genai from google.genai import types # Load environment variables load_dotenv() API_KEY = os.getenv("GEMINI_API_KEY") if not API_KEY: key_file_path = os.getenv("GEMINI_API_KEY_FILE", "/app/gemini_api_key.txt") if os.path.exists(key_file_path): with open(key_file_path, 'r') as f: API_KEY = f.read().strip() if not API_KEY: raise ValueError("GEMINI_API_KEY environment variable or file not set") # Initialize the modern client client = genai.Client(api_key=API_KEY) MODEL_CANDIDATES = ['gemini-1.5-flash', 'gemini-1.5-pro'] # Directly set to a modern, fast model print(f"DEBUG: Initialized with MODEL_NAME={MODEL_NAME}") app = FastAPI() # Configure CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) def parse_json_response(response) -> Any: """Parses JSON response from the modern SDK robustly.""" try: text = response.text if not text: return {} # Return empty dict on empty response cleaned_text = text.strip() if cleaned_text.startswith("```"): lines = cleaned_text.splitlines() if lines[0].startswith("```"): lines = lines[1:] if lines[-1].startswith("```"): lines = lines[:-1] cleaned_text = "\n".join(lines).strip() result = json.loads(cleaned_text) if isinstance(result, list) and result: return result[0] return result except Exception as e: print(f"CRITICAL: Failed to parse JSON: {e}\nRaw text: {getattr(response, 'text', 'NO TEXT')}") return {} # Return empty dict to avoid frontend crash # --- Schemas (Native Python Dictionaries) --- evidence_schema = { "type": "object", "properties": { "url": {"type": "string"}, "snippet": {"type": "string"}, }, "required": ['url', 'snippet'] } product_schema = { "type": "object", "properties": { "name": {"type": "string"}, "purpose": {"type": "string"}, "evidence": {"type": "array", "items": evidence_schema}, }, "required": ['name', 'purpose', 'evidence'] } industry_schema = { "type": "object", "properties": { "name": {"type": "string"}, "evidence": {"type": "array", "items": evidence_schema}, }, "required": ['name', 'evidence'] } # --- Request Models --- class ProductDetailsRequest(BaseModel): name: str; url: str; language: str class FetchStep1DataRequest(BaseModel): start_url: str; language: str class ProductModel(BaseModel): name: str; purpose: str; evidence: List[Dict[str, str]] class TargetIndustryModel(BaseModel): name: str; evidence: List[Dict[str, str]] class FetchStep2DataRequest(BaseModel): products: List[ProductModel]; industries: List[TargetIndustryModel]; language: str class KeywordModel(BaseModel): term: str; rationale: str class FetchStep3DataRequest(BaseModel): keywords: List[KeywordModel]; market_scope: str; language: str class CompanyModel(BaseModel): name: str; start_url: str class CompetitorCandidateModel(BaseModel): name: str; url: str; confidence: float; why: str; evidence: List[Dict[str, str]] class FetchStep4DataRequest(BaseModel): company: CompanyModel; competitors: List[CompetitorCandidateModel]; language: str class AnalysisModel(BaseModel): competitor: Dict[str, str]; portfolio: List[Dict[str, str]]; target_industries: List[str] delivery_model: str; overlap_score: int; differentiators: List[str]; evidence: List[Dict[str, str]] class FetchStep5DataSilverBulletsRequest(BaseModel): company: CompanyModel; analyses: List[AnalysisModel]; language: str class SilverBulletModel(BaseModel): competitor_name: str; statement: str class FetchStep6DataConclusionRequest(BaseModel): company: CompanyModel; products: List[ProductModel]; industries: List[TargetIndustryModel] analyses: List[AnalysisModel]; silver_bullets: List[SilverBulletModel]; language: str class FetchStep7DataBattlecardsRequest(BaseModel): company: CompanyModel; analyses: List[AnalysisModel]; silver_bullets: List[SilverBulletModel]; language: str class ShortlistedCompetitorModel(BaseModel): name: str; url: str class FetchStep8DataReferenceAnalysisRequest(BaseModel): competitors: List[ShortlistedCompetitorModel]; language: str # --- API Helper --- async def call_gemini_json(prompt: str, schema: dict): """Calls Gemini with schema enforcement.""" last_err = None for model_name in MODEL_CANDIDATES: try: config_args = {"response_mime_type": "application/json"} if schema: config_args["response_schema"] = schema response = client.models.generate_content( model=model_name, contents=prompt, config=types.GenerateContentConfig(**config_args) ) return parse_json_response(response) except Exception as e: last_err = e print(f"DEBUG: Model {model_name} failed: {e}") if "404" in str(e) or "not supported" in str(e).lower(): continue break raise HTTPException(status_code=500, detail=f"Gemini API Error: {str(last_err)}") # --- Endpoints --- @app.post("/api/fetchProductDetails") async def fetch_product_details(request: ProductDetailsRequest): prompt = r"""Analysiere {url} und beschreibe den Zweck von "{name}" in 1-2 Sätzen. Antworte ausschließlich im JSON-Format.""" return await call_gemini_json(prompt.format(url=request.url, name=request.name), product_schema) @app.post("/api/fetchStep1Data") async def fetch_step1_data(request: FetchStep1DataRequest): prompt = r"""Analysiere die Webseite {url} und identifiziere die Hauptprodukte/Lösungen und deren Zielbranchen. Antworte ausschließlich im JSON-Format.""" schema = { "type": "object", "properties": { "products": {"type": "array", "items": product_schema}, "target_industries": {"type": "array", "items": industry_schema}, }, "required": ['products', 'target_industries'] } data = await call_gemini_json(prompt.format(url=request.start_url), schema) # Double check keys for frontend compatibility if 'products' not in data: data['products'] = [] if 'target_industries' not in data: data['target_industries'] = [] return data @app.post("/api/fetchStep2Data") async def fetch_step2_data(request: FetchStep2DataRequest): p_sum = ', '.join([p.name for p in request.products]) prompt = r"""Leite aus diesen Produkten 10-25 Keywords für die Wettbewerbsrecherche ab: {products}. Antworte im JSON-Format.""" schema = {"type": "object", "properties": {"keywords": {"type": "array", "items": {"type": "object", "properties": {"term": {"type": "string"}, "rationale": {"type": "string"}}, "required": ['term', 'rationale']}}}, "required": ['keywords']} return await call_gemini_json(prompt.format(products=p_sum), schema) @app.post("/api/fetchStep3Data") async def fetch_step3_data(request: FetchStep3DataRequest): k_sum = ', '.join([k.term for k in request.keywords]) prompt = r"""Finde Wettbewerber für Markt {scope} basierend auf: {keywords}. Antworte JSON.""" schema = {"type": "object", "properties": {"competitor_candidates": {"type": "array", "items": {"type": "object", "properties": {"name": {"type": "string"}, "url": {"type": "string"}, "confidence": {"type": "number"}, "why": {"type": "string"}, "evidence": {"type": "array", "items": evidence_schema}}, "required": ['name', 'url', 'confidence', 'why', 'evidence']}}}, "required": ['competitor_candidates']} return await call_gemini_json(prompt.format(scope=request.market_scope, keywords=k_sum), schema) @app.post("/api/fetchStep4Data") async def fetch_step4_data(request: FetchStep4DataRequest): c_sum = '\n'.join([f'- {c.name}: {c.url}' for c in request.competitors]) prompt = r"""Analysiere Portfolio & Positionierung für:\n{comps}\nVergleiche mit {me}. Antworte JSON.""" schema = {"type": "object", "properties": {"analyses": {"type": "array", "items": {"type": "object", "properties": {"competitor": {"type": "object", "properties": {"name": {"type": "string"}, "url": {"type": "string"}}}, "portfolio": {"type": "array", "items": {"type": "object", "properties": {"product": {"type": "string"}, "purpose": {"type": "string"}}}}, "target_industries": {"type": "array", "items": {"type": "string"}}, "delivery_model": {"type": "string"}, "overlap_score": {"type": "integer"}, "differentiators": {"type": "array", "items": {"type": "string"}}, "evidence": {"type": "array", "items": evidence_schema}}, "required": ['competitor', 'portfolio', 'target_industries', 'delivery_model', 'overlap_score', 'differentiators', 'evidence']}}}, "required": ['analyses']} return await call_gemini_json(prompt.format(comps=c_sum, me=request.company.name), schema) @app.post("/api/fetchStep5Data_SilverBullets") async def fetch_step5_data_silver_bullets(request: FetchStep5DataSilverBulletsRequest): c_sum = '\n'.join([f"- {a.competitor['name']}: {'; '.join(a.differentiators)}" for a in request.analyses]) prompt = r"""Erstelle prägnante Silver Bullets für {me} gegen diese Wettbewerber:\n{comps}\nAntworte JSON.""" schema = {"type": "object", "properties": {"silver_bullets": {"type": "array", "items": {"type": "object", "properties": {"competitor_name": {"type": "string"}, "statement": {"type": "string"}}, "required": ['competitor_name', 'statement']}}}, "required": ['silver_bullets']} return await call_gemini_json(prompt.format(me=request.company.name, comps=c_sum), schema) @app.post("/api/fetchStep6Data_Conclusion") async def fetch_step6_data_conclusion(request: FetchStep6DataConclusionRequest): prompt = r"""Erstelle ein abschließendes Fazit der Wettbewerbsanalyse für {me}. Antworte JSON.""" schema = {"type": "object", "properties": {"conclusion": {"type": "object", "properties": {"product_matrix": {"type": "array", "items": {"type": "object", "properties": {"product": {"type": "string"}, "availability": {"type": "array", "items": {"type": "object", "properties": {"competitor": {"type": "string"}, "has_offering": {"type": "boolean"}}, "required": ['competitor', 'has_offering']}}}, "required": ['product', 'availability']}}, "industry_matrix": {"type": "array", "items": {"type": "object", "properties": {"industry": {"type": "string"}, "availability": {"type": "array", "items": {"type": "object", "properties": {"competitor": {"type": "string"}, "has_offering": {"type": "boolean"}}, "required": ['competitor', 'has_offering']}}}, "required": ['industry', 'availability']}}, "overlap_scores": {"type": "array", "items": {"type": "object", "properties": {"competitor": {"type": "string"}, "score": {"type": "number"}}}}, "summary": {"type": "string"}, "opportunities": {"type": "string"}, "next_questions": {"type": "array", "items": {"type": "string"}}}, "required": ['product_matrix', 'industry_matrix', 'overlap_scores', 'summary', 'opportunities', 'next_questions']}}, "required": ['conclusion']} return await call_gemini_json(prompt.format(me=request.company.name), schema) @app.post("/api/fetchStep7Data_Battlecards") async def fetch_step7_data_battlecards(request: FetchStep7DataBattlecardsRequest): prompt = r"""Erstelle Sales Battlecards für {me} gegen seine Wettbewerber. Antworte JSON.""" schema = {"type": "object", "properties": {"battlecards": {"type": "array", "items": {"type": "object", "properties": {"competitor_name": {"type": "string"}, "competitor_profile": {"type": "object", "properties": {"focus": {"type": "string"}, "positioning": {"type": "string"}}, "required": ['focus', 'positioning']}, "strengths_vs_weaknesses": {"type": "array", "items": {"type": "string"}}, "landmine_questions": {"type": "array", "items": {"type": "string"}}, "silver_bullet": {"type": "string"}}, "required": ['competitor_name', 'competitor_profile', 'strengths_vs_weaknesses', 'landmine_questions', 'silver_bullet']}}}, "required": ['battlecards']} return await call_gemini_json(prompt.format(me=request.company.name), schema) @app.post("/api/fetchStep8Data_ReferenceAnalysis") async def fetch_step8_data_reference_analysis(request: FetchStep8DataReferenceAnalysisRequest): c_sum = '\n'.join([f'- {c.name}: {c.url}' for c in request.competitors]) prompt = r"""Finde offizielle Referenzkunden für diese Wettbewerber:\n{comps}\nAntworte JSON.""" schema = {"type": "object", "properties": {"reference_analysis": {"type": "array", "items": {"type": "object", "properties": {"competitor_name": {"type": "string"}, "references": {"type": "array", "items": {"type": "object", "properties": {"name": {"type": "string"}, "industry": {"type": "string"}, "testimonial_snippet": {"type": "string"}, "case_study_url": {"type": "string"}}, "required": ["name", "industry", "testimonial_snippet", "case_study_url"]}}}, "required": ["competitor_name", "references"]}}}, "required": ["reference_analysis"]} # IMPORTANT: The new SDK supports tools via a list in config, not directly as args to generate_content. response = client.models.generate_content( model=MODEL_NAME, contents=prompt, config=types.GenerateContentConfig( response_mime_type='application/json', tools=[types.Tool(google_search_retrieval={})] ) ) return parse_json_response(response) # Static Files dist_path = os.path.join(os.getcwd(), "dist") if os.path.exists(dist_path): print(f"DEBUG: Mounting static files from {dist_path}") app.mount("/", StaticFiles(directory=dist_path, html=True), name="static") @app.get("/api/health") async def health_check(): return {"status": "ok", "sdk": "modern-genai", "model": MODEL_NAME} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)