feat: update AI model to Gemini Flash and implement retry logic with error propagation

This commit is contained in:
2026-05-13 17:31:03 +07:00
parent cdda3287b0
commit e1296e3c48

View File

@@ -51,49 +51,40 @@ def generate_bot_data():
Berikan HANYA JSON output tanpa teks lain. Berikan HANYA JSON output tanpa teks lain.
""" """
try: max_retries = 5
response = client.chat.completions.create( attempt = 0
model="meta-llama/llama-3-8b-instruct:free", # Free tier model for testing, can be changed
messages=[ while attempt < max_retries:
{"role": "system", "content": "You output only valid JSON."}, try:
{"role": "user", "content": prompt} response = client.chat.completions.create(
] model="google/gemini-flash-preview", # Updated to Gemini Flash Preview
) messages=[
content = response.choices[0].message.content.strip() {"role": "system", "content": "You output only valid JSON."},
{"role": "user", "content": prompt}
# Strip markdown backticks if present ]
if content.startswith("```"): )
content = content.split("```")[1] content = response.choices[0].message.content.strip()
if content.startswith("json"):
content = content[4:]
content = content.split("```")[0].strip()
import json # Strip markdown backticks if present
return json.loads(content) if content.startswith("```"):
except Exception as e: content = content.split("```")[1]
print(f"Error generating AI data: {e}") if content.startswith("json"):
# Fallback dummy data if AI fails content = content[4:]
return { content = content.split("```")[0].strip()
"akun1": {
"nama_lengkap": "Budi Santoso", import json
"email": "budi.santoso@gmail.com", return json.loads(content)
"nama_bisnis": "Budi Creative Design",
"deskripsi_bisnis": "Spesialis desain grafis dan branding logo", except Exception as e:
"lokasi": { attempt += 1
"lat": -6.2088, print(f"Error generating AI data (Attempt {attempt}/{max_retries}): {e}")
"lng": 106.8456, if attempt < max_retries:
"alamat": "Jl. Gajah Mada No. 12, Jakarta Pusat", import time
"radius": 25 time.sleep(3) # Wait 3 seconds before retrying
}, else:
"identitas": { print("Max retries reached. AI generation failed.")
"tipe": "KTP", raise e # Raise error instead of using static data
"nomor": "3171234567890001"
}
},
"akun2": {"nama_lengkap": "Andi Pratama", "email": "andipratama12@yahoo.com"},
"layanan": {"judul": "Jasa Desain Banner", "deskripsi": "Desain banner cepat dan profesional", "harga": 50000},
"review": {"rating": 5, "komentar": "Pekerjaannya cepat dan hasilnya bagus sekali!"}
}
if __name__ == "__main__": if __name__ == "__main__":
print(generate_bot_data()) print(generate_bot_data())