| import firebase_admin |
| from firebase_admin import credentials, db |
| import os |
| from dotenv import load_dotenv |
|
|
| |
| load_dotenv() |
|
|
| |
| cred = credentials.Certificate({ |
| "type": "service_account", |
| "project_id": os.getenv("FIREBASE_PROJECT_ID"), |
| "private_key_id": os.getenv("FIREBASE_PRIVATE_KEY_ID"), |
| "private_key": os.getenv("FIREBASE_PRIVATE_KEY").replace("\\n", "\n"), |
| "client_email": os.getenv("FIREBASE_CLIENT_EMAIL"), |
| "client_id": os.getenv("FIREBASE_CLIENT_ID"), |
| "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
| "token_uri": "https://oauth2.googleapis.com/token", |
| "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", |
| "client_x509_cert_url": os.getenv("FIREBASE_CLIENT_X509_CERT_URL"), |
| "universe_domain": "googleapis.com" |
| }) |
| firebase_admin.initialize_app(cred, { |
| 'databaseURL': os.getenv("FIREBASE_DATABASE_URL") |
| }) |
|
|
| def save_chat_message(user_input, krishna_response): |
| """ |
| Save a chat message to Firebase Realtime Database. |
| """ |
| ref = db.reference('chat_history') |
| ref.push({ |
| 'user': user_input, |
| 'krishna': krishna_response |
| }) |
|
|
| def get_chat_history(): |
| """ |
| Retrieve chat history from Firebase Realtime Database. |
| """ |
| ref = db.reference('chat_history') |
| history = ref.get() |
| if history: |
| return [(msg['user'], msg['krishna']) for msg in history.values()] |
| return [] |