Spaces:
Sleeping
Sleeping
| """ | |
| Vertex AI RAG Agent | |
| A package for interacting with Google Cloud Vertex AI RAG capabilities. | |
| """ | |
| import os | |
| import json | |
| import vertexai | |
| # ============================================================================== | |
| # BAGIAN 1: LOGIKA AUTENTIKASI (Jalankan ini dulu!) | |
| # Kode ini mempersiapkan kredensial sebelum Vertex AI diinisialisasi. | |
| # ============================================================================== | |
| GCP_SA_KEY_SECRET_NAME = "GCP_SA_KEY" # Pastikan nama ini sesuai dengan di HF Settings | |
| gcp_sa_key_json_str = os.getenv(GCP_SA_KEY_SECRET_NAME) | |
| if gcp_sa_key_json_str: | |
| try: | |
| credentials_path = "/tmp/gcp_credentials.json" | |
| with open(credentials_path, "w") as f: | |
| f.write(gcp_sa_key_json_str) | |
| # Atur env var yang akan dibaca oleh library Google Cloud secara otomatis | |
| os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path | |
| print("β Google Cloud credentials prepared successfully.") | |
| except Exception as e: | |
| print(f"β Error preparing Google Cloud credentials: {e}") | |
| else: | |
| print(f"β οΈ Secret '{GCP_SA_KEY_SECRET_NAME}' not found. Vertex AI initialization might fail.") | |
| # ============================================================================== | |
| # BAGIAN 2: SEKARANG INISIALISASI VERTEX AI (setelah autentikasi siap) | |
| # Kode ini adalah kode yang sudah Anda miliki sebelumnya. | |
| # ============================================================================== | |
| # Dapatkan konfigurasi dari environment variables | |
| PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT') | |
| LOCATION = os.getenv('GOOGLE_CLOUD_LOCATION') | |
| # Lakukan inisialisasi Vertex AI | |
| try: | |
| if PROJECT_ID and LOCATION: | |
| print(f"Initializing Vertex AI with project={PROJECT_ID}, location={LOCATION}") | |
| vertexai.init(project=PROJECT_ID, location=LOCATION) | |
| print("β Vertex AI initialization successful") | |
| else: | |
| print( | |
| f"β οΈ Missing Vertex AI configuration. PROJECT_ID={PROJECT_ID}, LOCATION={LOCATION}. " | |
| f"Tools requiring Vertex AI may not work properly." | |
| ) | |
| except Exception as e: | |
| print(f"β Failed to initialize Vertex AI: {str(e)}") | |
| print("Please check your Google Cloud credentials and project settings.") | |
| # ============================================================================== | |
| # BAGIAN 3: IMPOR SISA APLIKASI ANDA | |
| # ============================================================================== | |
| # Import agent setelah inisialisasi selesai | |
| from . import agent | |
| print("π¦ RAG Agent package loaded.") |