File size: 3,346 Bytes
685adc8
58e29c6
 
 
 
685adc8
 
 
 
 
 
 
 
 
 
5cde3d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from google import genai
from google.genai import types
import time
import logging
from typing import Optional
from dotenv import load_dotenv
import os
load_dotenv()

client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))

def get_response(prompt: str) -> str:
    response = client.models.generate_content(
        model="gemini-2.5-flash-preview-05-20", contents=prompt
    )
    return(response.text)

def generate_with_gemini(
    prompt: str,
    purpose: str,
    max_retries: int = 5,
    retry_delay: float = 1.0
) -> Optional[str]:
    """
    Generate text using Gemini API with fallback and retry logic.
    
    Args:
        prompt (str): The input prompt for text generation
        api_key (str): Your Gemini API key
        max_retries (int): Maximum number of retry attempts (default: 5)
        retry_delay (float): Initial delay between retries in seconds (default: 1.0)
    
    Returns:
        Optional[str]: Generated text response or None if all attempts fail
    """
    
    print(f"Purpose: {purpose}")
    # Model configurations with their respective max output tokens
    models_config = [
        {
            "name": "gemini-2.5-flash-preview-05-20",
            "max_output_tokens": 65536,
            "description": "Gemini 2.5 Flash Preview"
        },
        {
            "name": "gemini-2.0-flash",
            "max_output_tokens": 8192,
            "description": "Gemini 2.0 Flash"
        }
    ]
    
    # Try each model with retry logic
    for model_config in models_config:
        model_name = model_config["name"]
        max_tokens = model_config["max_output_tokens"]
        model_desc = model_config["description"]
        
        print(f"Attempting to use {model_desc} ({model_name})...")
        
        for attempt in range(max_retries):
            try:
                # Create generation config
                config = types.GenerateContentConfig(
                    max_output_tokens=max_tokens,
                    temperature=0.0  
                )
                
                # Make the API call
                response = client.models.generate_content(
                    model=model_name,
                    contents=[prompt],
                    config=config
                )
                
                # Check if response has content
                if response and response.text:
                    print(f"✅ Success with {model_desc} on attempt {attempt + 1}")
                    return response.text
                else:
                    print(f"⚠️  Empty response from {model_desc} on attempt {attempt + 1}")
                    
            except Exception as e:
                print(f"❌ Error with {model_desc} on attempt {attempt + 1}: {str(e)}")
                
                # If this is not the last attempt, wait before retrying
                if attempt < max_retries - 1:
                    wait_time = retry_delay * (2 ** attempt)  # Exponential backoff
                    print(f"⏳ Waiting {wait_time:.1f} seconds before retry...")
                    time.sleep(wait_time)
                else:
                    print(f"💥 All {max_retries} attempts failed for {model_desc}")
                    break
    
    print("❌ All models and retry attempts exhausted. Unable to generate response.")
    return None