You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
3.3 KiB
76 lines
3.3 KiB
from urllib.parse import quote_plus
|
|
import os
|
|
from sqlalchemy import create_engine, text
|
|
from sqlalchemy.exc import OperationalError
|
|
import time
|
|
|
|
db_user = os.getenv("DB_USER")
|
|
db_pass = os.getenv("DB_PASSWORD")
|
|
db_host = os.getenv("DB_HOST")
|
|
db_port = os.getenv("DB_PORT")
|
|
db_name = os.getenv("DB_NAME")
|
|
db_pass = quote_plus(db_pass)
|
|
db_user = quote_plus(db_user)
|
|
db_url = f"postgresql+psycopg://{db_user}:{db_pass}@{db_host}:{db_port}/{db_name}"
|
|
# 🟢 IMPROVED ENGINE
|
|
engine = create_engine(
|
|
db_url,
|
|
pool_pre_ping=True, # 👈 This checks if the connection is alive before using it
|
|
pool_recycle=3600, # 👈 Closes and reopens connections older than 1 hour
|
|
pool_size=10, # 👈 Allows up to 10 simultaneous connections
|
|
max_overflow=20 # 👈 Allows extra connections during high traffic
|
|
)
|
|
|
|
|
|
def get_active_agent_config(retries=3, delay=1):
|
|
"""
|
|
Fetches active prompts with automatic retry logic for database "hiccups".
|
|
"""
|
|
attempt = 0
|
|
while attempt < retries:
|
|
try:
|
|
with engine.connect() as conn:
|
|
query = text("""
|
|
SELECT content
|
|
FROM agent_agentprompt
|
|
WHERE settings_id = 1 AND is_active = true
|
|
ORDER BY id ASC
|
|
""")
|
|
result = conn.execute(query).fetchall()
|
|
prompt_list = [row.content for row in result]
|
|
|
|
return {"system_prompts": prompt_list}
|
|
|
|
except OperationalError as e:
|
|
attempt += 1
|
|
print(f"⚠️ DB Connection error (Attempt {attempt}/{retries}): {e}")
|
|
if attempt < retries:
|
|
time.sleep(delay) # Wait a second before trying again
|
|
else:
|
|
print("❌ DB Retry limit reached.")
|
|
return None
|
|
except Exception as e:
|
|
print(f"❌ Unexpected Error: {e}")
|
|
return None
|
|
|
|
def default_system_prompt():
|
|
return [
|
|
"You are a strict Islamic Knowledge Assistant.",
|
|
"Your Goal: Answer the user's question using the provided 'Context from the database'.",
|
|
|
|
"STRICT BEHAVIORAL RULE: You must maintain the highest standard of Adab (Etiquette).",
|
|
"If the user is disrespectful, vulgar, uses profanity, or mocks Islam:",
|
|
"1. Do NOT engage with the toxicity.",
|
|
"2. Do NOT lecture them.",
|
|
"3. Refuse to answer immediately by saying: 'I cannot answer this due to violations of Adab.'",
|
|
|
|
# --- CRITICAL FIXES ---
|
|
"If the Context is in a different language than the User's Question, you MUST translate the relevant information into the User's language.",
|
|
"Do NOT worry if the context source language (e.g., Russian/Arabic) does not match the user's language (e.g., English). Translate the meaning accurately.",
|
|
# ----------------------
|
|
|
|
"If the answer is explicitly found in the context (even in another language), answer directly.",
|
|
"If the answer is NOT found in the context, strictly reply: 'Information not available in the knowledge base.'",
|
|
"Maintain a respectful, scholarly tone.",
|
|
"Do not explain your reasoning process in the final output.",
|
|
]
|