Created
January 8, 2026 08:03
-
-
Save mohamed-ea/7083a3cfaa05caa533b77449d3f31678 to your computer and use it in GitHub Desktop.
Export the entire chat history in bulk from abacus.ai
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import json | |
| import re | |
| from datetime import datetime | |
| from abacusai import ApiClient | |
| # --- CONFIGURATION --- | |
| API_KEY = 'YOUR_API_KEY' | |
| DEPLOYMENT_ID = '766cb132c' # ID du déploiement "ChatLLM Deployment" depuis l'url | |
| OUTPUT_DIR = "Backup_Abacus_ChatLLM" | |
| # --------------------- | |
| client = ApiClient(api_key=API_KEY) | |
| def clean_filename(name): | |
| # Remplace les caractères interdits et limite la longueur | |
| s = re.sub(r'[\\/*?:"<>|]', "", name) | |
| return s[:100].strip() | |
| def export_all(): | |
| print(f"🚀 Démarrage de la sauvegarde complète...") | |
| print(f"📂 Dossier de destination : {OUTPUT_DIR}") | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| try: | |
| # 1. Lister toutes les conversations du déploiement principal | |
| print("📥 Récupération de la liste des conversations...") | |
| conversations = client.list_deployment_conversations(deployment_id=DEPLOYMENT_ID) | |
| total = len(conversations) | |
| print(f"✅ {total} conversations identifiées.") | |
| success_count = 0 | |
| for i, convo in enumerate(conversations): | |
| c_id = convo.deployment_conversation_id | |
| # Gestion du nom : parfois vide, on met un défaut | |
| raw_name = convo.name if convo.name and convo.name.strip() else f"Untitled_Chat_{c_id}" | |
| # Ajout de la date dans le nom de fichier pour le tri | |
| date_str = convo.last_event_created_at[:10] if convo.last_event_created_at else "UnknownDate" | |
| safe_name = clean_filename(raw_name) | |
| filename = f"{date_str} - {safe_name} ({c_id}).json" | |
| file_path = os.path.join(OUTPUT_DIR, filename) | |
| print(f"[{i+1}/{total}] 💾 {raw_name}...", end="", flush=True) | |
| try: | |
| # 2. Télécharger le contenu complet | |
| full_chat = client.get_deployment_conversation(deployment_conversation_id=c_id) | |
| # Conversion en dict | |
| data = full_chat.to_dict() if hasattr(full_chat, 'to_dict') else str(full_chat) | |
| with open(file_path, 'w', encoding='utf-8') as f: | |
| json.dump(data, f, indent=4, ensure_ascii=False) | |
| print(" OK ✅") | |
| success_count += 1 | |
| except Exception as e: | |
| print(f" ERREUR ❌ ({e})") | |
| print(f"\n🎉 Sauvegarde terminée ! {success_count}/{total} conversations exportées avec succès.") | |
| print(f"👉 Vos fichiers sont dans : {os.path.abspath(OUTPUT_DIR)}") | |
| except Exception as e: | |
| print(f"\n❌ Erreur critique lors de l'export : {e}") | |
| if __name__ == "__main__": | |
| export_all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment