Spaces:
Sleeping
Sleeping
File size: 2,550 Bytes
d2d5a16 84bfd81 d2d5a16 84bfd81 d2d5a16 84bfd81 a92ae62 d2d5a16 84bfd81 d2d5a16 84bfd81 d2d5a16 84bfd81 d2d5a16 84bfd81 d2d5a16 84bfd81 d2d5a16 84bfd81 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
"""
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.") |