Spaces:
Running

axeronis commited on
Commit
76d56a5
·
1 Parent(s): f1f3684

fix: increase max tokens for AI funnel

Browse files
Files changed (1) hide show
  1. backend/app/api/playbook.py +37 -8
backend/app/api/playbook.py CHANGED
@@ -569,7 +569,6 @@ async def generate_funnel_with_ai(
569
  import json
570
  from app.models.knowledge import KnowledgeBase
571
  from app.models.product import Product
572
- from app.services.llm_client import get_llm_client
573
 
574
  # Get tenant
575
  result = await session.execute(
@@ -649,19 +648,49 @@ async def generate_funnel_with_ai(
649
 
650
  Верни ТОЛЬКО JSON, без markdown и пояснений."""
651
 
652
- # Call LLM via existing client
653
  try:
654
- llm = get_llm_client()
655
- raw_text = await llm.generate_text(prompt)
656
- raw_text = raw_text.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
657
 
658
  # Clean up JSON if wrapped in markdown
659
  if raw_text.startswith("```"):
660
- raw_text = raw_text.split("```")[1]
661
- if raw_text.startswith("json"):
662
- raw_text = raw_text[4:]
 
 
663
  raw_text = raw_text.strip()
664
 
 
 
 
 
 
 
665
  # Parse generated stages
666
  generated_stages = json.loads(raw_text)
667
 
 
569
  import json
570
  from app.models.knowledge import KnowledgeBase
571
  from app.models.product import Product
 
572
 
573
  # Get tenant
574
  result = await session.execute(
 
648
 
649
  Верни ТОЛЬКО JSON, без markdown и пояснений."""
650
 
651
+ # Call LLM via existing client with higher token limit
652
  try:
653
+ import re
654
+ import asyncio
655
+ from google import genai
656
+ from google.genai import types
657
+ from app.core.config import settings
658
+
659
+ # Use direct API call with higher token limit for long response
660
+ client = genai.Client(api_key=settings.GEMINI_API_KEY)
661
+
662
+ config = types.GenerateContentConfig(
663
+ temperature=0.7,
664
+ max_output_tokens=4000, # Higher limit for JSON array
665
+ )
666
+
667
+ loop = asyncio.get_event_loop()
668
+ response = await loop.run_in_executor(
669
+ None,
670
+ lambda: client.models.generate_content(
671
+ model="gemini-2.0-flash",
672
+ contents=prompt,
673
+ config=config
674
+ )
675
+ )
676
+
677
+ raw_text = response.text.strip()
678
 
679
  # Clean up JSON if wrapped in markdown
680
  if raw_text.startswith("```"):
681
+ parts = raw_text.split("```")
682
+ if len(parts) >= 2:
683
+ raw_text = parts[1]
684
+ if raw_text.startswith("json"):
685
+ raw_text = raw_text[4:]
686
  raw_text = raw_text.strip()
687
 
688
+ # Try to extract JSON array with regex as fallback
689
+ if not raw_text.startswith("["):
690
+ match = re.search(r'\[[\s\S]*\]', raw_text)
691
+ if match:
692
+ raw_text = match.group(0)
693
+
694
  # Parse generated stages
695
  generated_stages = json.loads(raw_text)
696