Hi @neel,
I have implemented retries with exponential back-off to your code.. This should work with larger prompt sizes as well as long as you donβt exceed token limits. I hope this resolves your issue.
Happy Coding
import time
import json
def generate_search_queries(prompt: str) -> dict:
system_prompt = ()
retry_attempts = 5 # Number of retry attempts
backoff_factor = 2 # Exponential backoff factor
for attempt in range(retry_attempts):
try:
response = model.generate_content([system_prompt, prompt])
print(":brain: Gemini output:", response.text)
json_data = json.loads(response.text.strip())
return {
"domain": json_data.get("domain", "software vendors"),
"location": json_data.get("location", "California"),
"quantity": int(json_data.get("quantity", 3))
}
except Exception as e:
print(f"β οΈ Attempt {attempt + 1} failed: {e}")
if attempt < retry_attempts - 1:
# Exponential backoff logic: wait for a longer time after each failure
sleep_time = backoff_factor ** attempt # Exponentially increasing backoff time
print(f" Retrying in {sleep_time} seconds...")
time.sleep(sleep_time) # Retry after an exponentially increasing delay
else:
print("β All retry attempts failed.")
return {
"domain": "software vendors",
"location": "California",
"quantity": 3
}
def generate_next_prompt_variation(intent_prompt: str, used_prompts: list[str]) -> str:
retry_attempts = 5 # Number of retry attempts
backoff_factor = 2 # Exponential backoff factor
for attempt in range(retry_attempts):
try:
system_prompt = ()
response = model.generate_content(system_prompt)
prompt = response.text.strip()
prompt = prompt.strip("-β’1234567890. ").strip()
print("π§ New prompt from Gemini:", prompt)
return prompt
except Exception as e:
print(f"β Attempt {attempt + 1} failed: {e}")
if attempt < retry_attempts - 1:
# Exponential backoff logic: wait for a longer time after each failure
sleep_time = backoff_factor ** attempt # Exponentially increasing backoff time
print(f"β³ Retrying in {sleep_time} seconds...")
time.sleep(sleep_time) # Retry after an exponentially increasing delay
else:
return ""