Update api.py
Browse files115 unmodified lines11611711811912012112278 unmodified lines201202203204205206207222 unmodified lines430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462107 unmodified lines570571572573574575576577578579580151 unmodified lines732733734735115 unmodified linesFreemium gate β 15 lifetime free requests per user.Returns usage metadata so the frontend can show a progress bar.Raises HTTP 402 when the free limit is exceeded.Required Supabase table:user_usage (user_id text PK, request_count int, is_premium bool,created_at timestamptz, last_seen timestamptz)78 unmodified linesdef get_user_context(user_id: str, req: ChatRequest) -> dict:"""Priority: request payload β Supabase user_profiles β sensible defaults.Required Supabase table:user_profiles (user_id text PK, location text, niche text,budget int, updated_at timestamptz)222 unmodified linesreturn f"""You are the Penafort Intelligence Engine β a senior business advisor \specialising in African markets, consumer behaviour, and operational strategy.[INTERNAL REASONING β NEVER REVEAL IN OUTPUT]Analytical lens: {fw['message']}Reasoning goal: {fw['goal']}Apply silently: {top_fw}{data_note}OUTPUT RULES β ENFORCED STRICTLY:1. FRAMEWORK INVISIBILITYNever name any framework, methodology, matrix, canvas, pillar, or analytical model \in your response. Reason through them silently. Your output must read as the natural \voice of a seasoned business advisor β not a consultant presenting a slide deck.2. MARKET GROUNDINGEvery insight must be grounded in {ctx['location']} and the {ctx['niche']}. \Reference local purchasing power, distribution channels, cultural dynamics, \and competitive realities specific to that market.3. DATA INTEGRITYQualify all numbers: use "estimated", "based on current data", "directionally", \or "approximately". Never state financial figures as guarantees or certainties.4. PRESCRIPTIVE CLOSEEnd every response with exactly one clear, actionable recommendation β \specific, local, and immediately executable. Not a list of options. One action.5. VOICESharp. Confident. No academic waffle. Bullet points only where they genuinely \aid clarity. Write like a strategy partner who has actually built businesses in Lagos."""107 unmodified lineslogger.info(f"Pillar={fw['pillar']} | Status={fw['status']}")# ββ 4. Vector memory (long-term context) βββββββββββββββββββββ# FIX: corrected model name (was 'models/models/gemini-embedding-2-preview')context = ""try:emb = genai.embed_content(model="models/text-embedding-004",content=req.message,task_type="retrieval_query",)["embedding"]151 unmodified lines"engine": "Penafort Strategy Engine v3.0","monitoring": "sentry" if SENTRY_DSN else "disabled","timestamp": datetime.now(timezone.utc).isoformat(),}115 unmodified lines11611711811912012178 unmodified lines200201202203204205222 unmodified lines428429430431432433434435436437438439440441442443444445446447448449450451452453107 unmodified lines561562563564565566567568569570571151 unmodified lines723724725726115 unmodified linesFreemium gate β 15 lifetime free requests per user.Returns usage metadata so the frontend can show a progress bar.Raises HTTP 402 when the free limit is exceeded.Required Supabase table:user_usage (user_id text PK, request_count int, is_premium bool,created_at timestamptz, last_seen timestamptz)78 unmodified linesdef get_user_context(user_id: str, req: ChatRequest) -> dict:"""Priority: request payload β Supabase user_profiles β sensible defaults.Required Supabase table:user_profiles (user_id text PK, location text, niche text,budget int, updated_at timestamptz)222 unmodified linesreturn f"""You are the Penafort Intelligence Engine β a senior business advisor \specialising in African markets, consumer behaviour, and operational strategy.[INTERNAL REASONING β NEVER REVEAL IN OUTPUT]Analytical lens: {fw['message']}Reasoning goal: {fw['goal']}Apply silently: {top_fw}{data_note}OUTPUT RULES β ENFORCED STRICTLY:1. FRAMEWORK INVISIBILITYNever name any framework, methodology, matrix, canvas, pillar, or analytical model \in your response. Reason through them silently. Your output must read as the natural \voice of a seasoned business advisor β not a consultant presenting a slide deck.2. MARKET GROUNDINGEvery insight must be grounded in {ctx['location']} and the {ctx['niche']}. \Reference local purchasing power, distribution channels, cultural dynamics, \and competitive realities specific to that market.3. DATA INTEGRITYQualify all numbers: use "estimated", "based on current data", "directionally", \or "approximately". Never state financial figures as guarantees or certainties.4. PRESCRIPTIVE CLOSEEnd every response with exactly one clear, actionable recommendation β \specific, local, and immediately executable. Not a list of options. One action.5. VOICESharp. Confident. No academic waffle. Bullet points only where they genuinely \aid clarity. Write like a strategy partner who has actually built businesses in Lagos."""107 unmodified lineslogger.info(f"Pillar={fw['pillar']} | Status={fw['status']}")# ββ 4. Vector memory (long-term context) βββββββββββββββββββββ# FIX: corrected model name (should not be changed from this 'models/models/gemini-embedding-2-preview')context = ""try:emb = genai.embed_content(model="models/gemini-embedding-2-preview",content=req.message,task_type="retrieval_query",)["embedding"]151 unmodified lines"engine": "Penafort Strategy Engine v3.0","monitoring": "sentry" if SENTRY_DSN else "disabled","timestamp": datetime.now(timezone.utc).isoformat(),}
api.py+3-12