feat: update AI model to Gemini Flash and implement retry logic with error propagation
This commit is contained in:
@@ -51,49 +51,40 @@ def generate_bot_data():
|
||||
Berikan HANYA JSON output tanpa teks lain.
|
||||
"""
|
||||
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
model="meta-llama/llama-3-8b-instruct:free", # Free tier model for testing, can be changed
|
||||
messages=[
|
||||
{"role": "system", "content": "You output only valid JSON."},
|
||||
{"role": "user", "content": prompt}
|
||||
]
|
||||
)
|
||||
content = response.choices[0].message.content.strip()
|
||||
|
||||
# Strip markdown backticks if present
|
||||
if content.startswith("```"):
|
||||
content = content.split("```")[1]
|
||||
if content.startswith("json"):
|
||||
content = content[4:]
|
||||
content = content.split("```")[0].strip()
|
||||
max_retries = 5
|
||||
attempt = 0
|
||||
|
||||
while attempt < max_retries:
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
model="google/gemini-flash-preview", # Updated to Gemini Flash Preview
|
||||
messages=[
|
||||
{"role": "system", "content": "You output only valid JSON."},
|
||||
{"role": "user", "content": prompt}
|
||||
]
|
||||
)
|
||||
content = response.choices[0].message.content.strip()
|
||||
|
||||
import json
|
||||
return json.loads(content)
|
||||
except Exception as e:
|
||||
print(f"Error generating AI data: {e}")
|
||||
# Fallback dummy data if AI fails
|
||||
return {
|
||||
"akun1": {
|
||||
"nama_lengkap": "Budi Santoso",
|
||||
"email": "budi.santoso@gmail.com",
|
||||
"nama_bisnis": "Budi Creative Design",
|
||||
"deskripsi_bisnis": "Spesialis desain grafis dan branding logo",
|
||||
"lokasi": {
|
||||
"lat": -6.2088,
|
||||
"lng": 106.8456,
|
||||
"alamat": "Jl. Gajah Mada No. 12, Jakarta Pusat",
|
||||
"radius": 25
|
||||
},
|
||||
"identitas": {
|
||||
"tipe": "KTP",
|
||||
"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!"}
|
||||
}
|
||||
# Strip markdown backticks if present
|
||||
if content.startswith("```"):
|
||||
content = content.split("```")[1]
|
||||
if content.startswith("json"):
|
||||
content = content[4:]
|
||||
content = content.split("```")[0].strip()
|
||||
|
||||
import json
|
||||
return json.loads(content)
|
||||
|
||||
except Exception as e:
|
||||
attempt += 1
|
||||
print(f"Error generating AI data (Attempt {attempt}/{max_retries}): {e}")
|
||||
if attempt < max_retries:
|
||||
import time
|
||||
time.sleep(3) # Wait 3 seconds before retrying
|
||||
else:
|
||||
print("Max retries reached. AI generation failed.")
|
||||
raise e # Raise error instead of using static data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(generate_bot_data())
|
||||
|
||||
Reference in New Issue
Block a user